diff --git a/Cargo.lock b/Cargo.lock index 5a5a8ae..0485ad9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1970,6 +1970,7 @@ dependencies = [ "gpui-native-icons", "gpui-native-theme", "gpui-native-utils", + "image", "log", "unicode-segmentation", ] diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 55cc5d5..c5c0e1e 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -12,6 +12,7 @@ gpui-native-utils = { workspace = true } log = "0.4" unicode-segmentation = "1.12" flume = { version = "0.12", optional = true } +image = { version = "0.25.1", default-features = false, optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] cpal = { version = "0.18", optional = true } @@ -54,6 +55,7 @@ full = [ "alert", "toast", "speech", + "grain", ] button = ["spinner"] checkbox = [] @@ -82,8 +84,9 @@ tooltip = [] menu = ["scroll_area"] dropdown_menu = ["menu", "button"] select = ["menu"] -dialog = ["button", "scroll_area"] +dialog = ["button", "scroll_area", "grain"] empty_state = [] +grain = ["dep:image"] alert = [] toast = [] speech = ["spinner", "tooltip", "dep:flume", "dep:cpal"] diff --git a/crates/core/src/dialog.rs b/crates/core/src/dialog.rs index 1ad6cd3..5f0019e 100644 --- a/crates/core/src/dialog.rs +++ b/crates/core/src/dialog.rs @@ -12,6 +12,7 @@ use gpui_native_theme::{ActiveTheme as _, TextRole, Typeset as _}; use crate::button::{Button, ButtonVariant}; use crate::common::{DIALOG_LAYER, Sizable, v_flex}; +use crate::grain::grain; use crate::scroll_area::ScrollArea; /// Requests the dialog's close: `false` closes. @@ -26,6 +27,7 @@ pub struct Dialog { id: ElementId, open: bool, alert: bool, + grain: bool, title: Option, a11y_label: Option, on_change: Option, @@ -45,6 +47,7 @@ impl Dialog { id: id.into(), open: false, alert: false, + grain: false, title: None, a11y_label: None, on_change: None, @@ -67,6 +70,12 @@ impl Dialog { self } + /// Textures the scrim with a film-grain overlay. + pub fn grain(mut self, grain: bool) -> Self { + self.grain = grain; + self + } + /// Requests close through `on_change` on escape, scrim clicks, and the close button. pub fn on_change(mut self, handler: F) -> Self where @@ -280,6 +289,7 @@ impl RenderOnce for Dialog { .inset_0() .bg(scrim_color) .occlude() + .when(self.grain, |scrim| scrim.child(grain())) .when(!alert, |scrim| { scrim.on_mouse_down(MouseButton::Left, { let on_change = on_change.clone(); diff --git a/crates/core/src/grain.rs b/crates/core/src/grain.rs new file mode 100644 index 0000000..e330a3b --- /dev/null +++ b/crates/core/src/grain.rs @@ -0,0 +1,90 @@ +//! The grain: a film-noise overlay for material surfaces. + +use std::sync::{Arc, OnceLock}; + +use gpui::{ + App, Bounds, Corners, IntoElement, RenderImage, RenderOnce, Styled as _, Window, canvas, point, + px, size, +}; +use gpui_native_theme::GRAIN_ALPHA; +use gpui_native_utils::ResultLogErr as _; +use image::{Frame, ImageBuffer, Rgba}; + +/// The side of one noise tile, in device pixels. +const TILE_PX: u32 = 256; + +/// A film-grain overlay: tiled monochrome noise at [`GRAIN_ALPHA`] strength. +#[derive(IntoElement)] +#[must_use] +pub struct Grain; + +/// Creates a grain overlay. +pub fn grain() -> Grain { + Grain +} + +impl RenderOnce for Grain { + fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement { + let texture = texture(); + canvas( + |_, _, _| (), + move |bounds, (), window, _| { + let tile = px(TILE_PX as f32); + let columns = (bounds.size.width / tile).ceil() as u32; + let rows = (bounds.size.height / tile).ceil() as u32; + + for row in 0..rows { + for column in 0..columns { + let x = bounds.origin.x + column as f32 * tile; + let y = bounds.origin.y + row as f32 * tile; + let tile_bounds = Bounds { + origin: point(x, y), + size: size( + tile.min(bounds.origin.x + bounds.size.width - x), + tile.min(bounds.origin.y + bounds.size.height - y), + ), + }; + window + .paint_image( + tile_bounds, + tile_bounds, + Corners::default(), + texture.clone(), + 0, + false, + ) + .log_err(); + } + } + }, + ) + .absolute() + .inset_0() + } +} + +/// The shared noise tile, every tile reuses its one atlas entry. +fn texture() -> Arc { + static TEXTURE: OnceLock> = OnceLock::new(); + + TEXTURE + .get_or_init(|| { + // An LCG keeps the grain deterministic across launches. + let mut state = 0x9E37_79B9_u32; + let mut noise = move || { + state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + (state >> 24) as u8 + }; + // The grays are noise samples, not palette colors. + let alpha = (GRAIN_ALPHA * 255.0) as u8; + Arc::new(RenderImage::new([Frame::new(ImageBuffer::from_fn( + TILE_PX, + TILE_PX, + |_, _| { + let gray = noise(); + Rgba([gray, gray, gray, alpha]) + }, + ))])) + }) + .clone() +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 10d54e8..65076aa 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -54,6 +54,9 @@ pub mod dialog; #[cfg(feature = "empty_state")] pub mod empty_state; +#[cfg(feature = "grain")] +pub mod grain; + #[cfg(feature = "menu")] pub mod menu; diff --git a/crates/data/Cargo.toml b/crates/data/Cargo.toml index 78f413d..022ec21 100644 --- a/crates/data/Cargo.toml +++ b/crates/data/Cargo.toml @@ -21,6 +21,7 @@ list = [] sidebar = [ "dep:gpui-native-core", "gpui-native-core/scroll_area", + "gpui-native-core/grain", ] tabs = [ "dep:gpui-native-core", diff --git a/crates/data/src/sidebar.rs b/crates/data/src/sidebar.rs index 2f97e56..43a88e3 100644 --- a/crates/data/src/sidebar.rs +++ b/crates/data/src/sidebar.rs @@ -11,6 +11,7 @@ use gpui::{ ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _, Window, div, }; +use gpui_native_core::grain::grain as grain_overlay; use gpui_native_core::scroll_area::ScrollArea; use gpui_native_theme::{ActiveTheme as _, TextRole, Typeset as _}; use gpui_native_utils::{nav_key, paint_selectable_row, selectable_label_color}; @@ -118,6 +119,7 @@ pub struct Sidebar { id: ElementId, selected: Option, transparent: bool, + grain: bool, a11y_label: Option, on_change: Option, entries: Option, @@ -137,6 +139,7 @@ impl Sidebar { id: id.into(), selected: None, transparent: false, + grain: false, a11y_label: None, on_change: None, entries: None, @@ -164,6 +167,13 @@ impl Sidebar { self.transparent } + /// Textures the material with a film-grain overlay; it renders only while + /// the sidebar paints one. + pub fn grain(mut self, grain: bool) -> Self { + self.grain = grain; + self + } + /// Reports the next selected row, from clicks and keys alike. pub fn on_change(mut self, handler: F) -> Self where @@ -234,6 +244,7 @@ impl RenderOnce for Sidebar { id, selected, transparent: material, + grain, a11y_label, on_change, entries, @@ -381,6 +392,7 @@ impl RenderOnce for Sidebar { .child(body) .when_some(footer, |sidebar, footer| sidebar.child(footer)) .when_some(overlay_strip, |sidebar, strip| sidebar.child(strip)) + .when(material && grain, |sidebar| sidebar.child(grain_overlay())) } } diff --git a/crates/theme/src/lib.rs b/crates/theme/src/lib.rs index b8b61a6..4345242 100644 --- a/crates/theme/src/lib.rs +++ b/crates/theme/src/lib.rs @@ -105,11 +105,13 @@ pub const PIXEL_AVATAR_LIGHT_CAP: f32 = 0.95; pub const PIXEL_AVATAR_DARK_FACTOR: f32 = 0.45; pub const PIXEL_AVATAR_DARK_FLOOR: f32 = 0.18; /// The default window material transparency, as a percent (0 opaque, 100 the lightest fill). -pub const DEFAULT_TRANSPARENCY: u8 = 94; +pub const DEFAULT_TRANSPARENCY: u8 = 80; /// The light-mode floor of the material fill. pub const MIN_MATERIAL_FILL_LIGHT: f32 = 0.45; /// The dark-mode floor of the material fill. pub const MIN_MATERIAL_FILL_DARK: f32 = 0.15; +/// The opacity of the grain overlay's noise. +pub const GRAIN_ALPHA: f32 = 0.08; /// Light or dark, resolved from the system appearance. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Deserialize)] diff --git a/examples/gallery/src/main.rs b/examples/gallery/src/main.rs index 6d6028b..7c3681c 100644 --- a/examples/gallery/src/main.rs +++ b/examples/gallery/src/main.rs @@ -226,6 +226,8 @@ struct Gallery { alert_open: bool, /// Whether the app sidebar paints the window's native material. sidebar_transparent: bool, + /// Whether a film-grain overlay textures the window. + grain: bool, } impl Gallery { @@ -258,6 +260,7 @@ impl Gallery { let left_sidebar = Sidebar::new("gallery-sidebar") .selected(selected) .transparent(self.sidebar_transparent) + .grain(self.grain) .accessibility_label("Component catalog") .entries(|_, _| { CATALOG @@ -300,6 +303,7 @@ impl Gallery { let gallery_change = gallery.clone(); Dialog::new("gallery-dialog") .open(open) + .grain(true) .title("Delete file?") .on_change(move |open, _, cx| { gallery_change @@ -351,6 +355,7 @@ impl Gallery { Dialog::new("gallery-alert") .open(open) .alert(true) + .grain(true) .title("Sign out of the workspace?") .on_change(move |open, _, cx| { gallery_change @@ -595,6 +600,7 @@ impl Render for WelcomePanel { let layout_right = self.layout_state.clone(); let dock_state = self.dock_state.clone(); let gallery = self.gallery.clone(); + let grain_gallery = self.gallery.clone(); ScrollArea::new("welcome-demo") .size_full() @@ -667,6 +673,20 @@ impl Render for WelcomePanel { .log_err(); }), ) + .child( + Button::new("welcome-toggle-grain") + .label("Toggle grain") + .variant(ButtonVariant::Secondary) + .small() + .on_click(move |_, _, cx| { + grain_gallery + .update(cx, |gallery, cx| { + gallery.grain = !gallery.grain; + cx.notify(); + }) + .log_err(); + }), + ) .child( Button::new("welcome-split") .label("Split active right") @@ -4470,6 +4490,7 @@ fn main() { dialog_open: false, alert_open: false, sidebar_transparent: true, + grain: true, }); let palette = cx.new(|cx| {