core: add film-grain overlay for materials
This commit is contained in:
Generated
+1
@@ -1970,6 +1970,7 @@ dependencies = [
|
|||||||
"gpui-native-icons",
|
"gpui-native-icons",
|
||||||
"gpui-native-theme",
|
"gpui-native-theme",
|
||||||
"gpui-native-utils",
|
"gpui-native-utils",
|
||||||
|
"image",
|
||||||
"log",
|
"log",
|
||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ gpui-native-utils = { workspace = true }
|
|||||||
log = "0.4"
|
log = "0.4"
|
||||||
unicode-segmentation = "1.12"
|
unicode-segmentation = "1.12"
|
||||||
flume = { version = "0.12", optional = true }
|
flume = { version = "0.12", optional = true }
|
||||||
|
image = { version = "0.25.1", default-features = false, optional = true }
|
||||||
|
|
||||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
cpal = { version = "0.18", optional = true }
|
cpal = { version = "0.18", optional = true }
|
||||||
@@ -54,6 +55,7 @@ full = [
|
|||||||
"alert",
|
"alert",
|
||||||
"toast",
|
"toast",
|
||||||
"speech",
|
"speech",
|
||||||
|
"grain",
|
||||||
]
|
]
|
||||||
button = ["spinner"]
|
button = ["spinner"]
|
||||||
checkbox = []
|
checkbox = []
|
||||||
@@ -82,8 +84,9 @@ tooltip = []
|
|||||||
menu = ["scroll_area"]
|
menu = ["scroll_area"]
|
||||||
dropdown_menu = ["menu", "button"]
|
dropdown_menu = ["menu", "button"]
|
||||||
select = ["menu"]
|
select = ["menu"]
|
||||||
dialog = ["button", "scroll_area"]
|
dialog = ["button", "scroll_area", "grain"]
|
||||||
empty_state = []
|
empty_state = []
|
||||||
|
grain = ["dep:image"]
|
||||||
alert = []
|
alert = []
|
||||||
toast = []
|
toast = []
|
||||||
speech = ["spinner", "tooltip", "dep:flume", "dep:cpal"]
|
speech = ["spinner", "tooltip", "dep:flume", "dep:cpal"]
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ use gpui_native_theme::{ActiveTheme as _, TextRole, Typeset as _};
|
|||||||
|
|
||||||
use crate::button::{Button, ButtonVariant};
|
use crate::button::{Button, ButtonVariant};
|
||||||
use crate::common::{DIALOG_LAYER, Sizable, v_flex};
|
use crate::common::{DIALOG_LAYER, Sizable, v_flex};
|
||||||
|
use crate::grain::grain;
|
||||||
use crate::scroll_area::ScrollArea;
|
use crate::scroll_area::ScrollArea;
|
||||||
|
|
||||||
/// Requests the dialog's close: `false` closes.
|
/// Requests the dialog's close: `false` closes.
|
||||||
@@ -26,6 +27,7 @@ pub struct Dialog {
|
|||||||
id: ElementId,
|
id: ElementId,
|
||||||
open: bool,
|
open: bool,
|
||||||
alert: bool,
|
alert: bool,
|
||||||
|
grain: bool,
|
||||||
title: Option<SharedString>,
|
title: Option<SharedString>,
|
||||||
a11y_label: Option<SharedString>,
|
a11y_label: Option<SharedString>,
|
||||||
on_change: Option<ChangeHandler>,
|
on_change: Option<ChangeHandler>,
|
||||||
@@ -45,6 +47,7 @@ impl Dialog {
|
|||||||
id: id.into(),
|
id: id.into(),
|
||||||
open: false,
|
open: false,
|
||||||
alert: false,
|
alert: false,
|
||||||
|
grain: false,
|
||||||
title: None,
|
title: None,
|
||||||
a11y_label: None,
|
a11y_label: None,
|
||||||
on_change: None,
|
on_change: None,
|
||||||
@@ -67,6 +70,12 @@ impl Dialog {
|
|||||||
self
|
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.
|
/// Requests close through `on_change` on escape, scrim clicks, and the close button.
|
||||||
pub fn on_change<F>(mut self, handler: F) -> Self
|
pub fn on_change<F>(mut self, handler: F) -> Self
|
||||||
where
|
where
|
||||||
@@ -280,6 +289,7 @@ impl RenderOnce for Dialog {
|
|||||||
.inset_0()
|
.inset_0()
|
||||||
.bg(scrim_color)
|
.bg(scrim_color)
|
||||||
.occlude()
|
.occlude()
|
||||||
|
.when(self.grain, |scrim| scrim.child(grain()))
|
||||||
.when(!alert, |scrim| {
|
.when(!alert, |scrim| {
|
||||||
scrim.on_mouse_down(MouseButton::Left, {
|
scrim.on_mouse_down(MouseButton::Left, {
|
||||||
let on_change = on_change.clone();
|
let on_change = on_change.clone();
|
||||||
|
|||||||
@@ -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<RenderImage> {
|
||||||
|
static TEXTURE: OnceLock<Arc<RenderImage>> = 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()
|
||||||
|
}
|
||||||
@@ -54,6 +54,9 @@ pub mod dialog;
|
|||||||
#[cfg(feature = "empty_state")]
|
#[cfg(feature = "empty_state")]
|
||||||
pub mod empty_state;
|
pub mod empty_state;
|
||||||
|
|
||||||
|
#[cfg(feature = "grain")]
|
||||||
|
pub mod grain;
|
||||||
|
|
||||||
#[cfg(feature = "menu")]
|
#[cfg(feature = "menu")]
|
||||||
pub mod menu;
|
pub mod menu;
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ list = []
|
|||||||
sidebar = [
|
sidebar = [
|
||||||
"dep:gpui-native-core",
|
"dep:gpui-native-core",
|
||||||
"gpui-native-core/scroll_area",
|
"gpui-native-core/scroll_area",
|
||||||
|
"gpui-native-core/grain",
|
||||||
]
|
]
|
||||||
tabs = [
|
tabs = [
|
||||||
"dep:gpui-native-core",
|
"dep:gpui-native-core",
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use gpui::{
|
|||||||
ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _,
|
ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _,
|
||||||
Styled as _, Window, div,
|
Styled as _, Window, div,
|
||||||
};
|
};
|
||||||
|
use gpui_native_core::grain::grain as grain_overlay;
|
||||||
use gpui_native_core::scroll_area::ScrollArea;
|
use gpui_native_core::scroll_area::ScrollArea;
|
||||||
use gpui_native_theme::{ActiveTheme as _, TextRole, Typeset as _};
|
use gpui_native_theme::{ActiveTheme as _, TextRole, Typeset as _};
|
||||||
use gpui_native_utils::{nav_key, paint_selectable_row, selectable_label_color};
|
use gpui_native_utils::{nav_key, paint_selectable_row, selectable_label_color};
|
||||||
@@ -118,6 +119,7 @@ pub struct Sidebar {
|
|||||||
id: ElementId,
|
id: ElementId,
|
||||||
selected: Option<usize>,
|
selected: Option<usize>,
|
||||||
transparent: bool,
|
transparent: bool,
|
||||||
|
grain: bool,
|
||||||
a11y_label: Option<SharedString>,
|
a11y_label: Option<SharedString>,
|
||||||
on_change: Option<ChangeHandler>,
|
on_change: Option<ChangeHandler>,
|
||||||
entries: Option<EntriesFn>,
|
entries: Option<EntriesFn>,
|
||||||
@@ -137,6 +139,7 @@ impl Sidebar {
|
|||||||
id: id.into(),
|
id: id.into(),
|
||||||
selected: None,
|
selected: None,
|
||||||
transparent: false,
|
transparent: false,
|
||||||
|
grain: false,
|
||||||
a11y_label: None,
|
a11y_label: None,
|
||||||
on_change: None,
|
on_change: None,
|
||||||
entries: None,
|
entries: None,
|
||||||
@@ -164,6 +167,13 @@ impl Sidebar {
|
|||||||
self.transparent
|
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.
|
/// Reports the next selected row, from clicks and keys alike.
|
||||||
pub fn on_change<F>(mut self, handler: F) -> Self
|
pub fn on_change<F>(mut self, handler: F) -> Self
|
||||||
where
|
where
|
||||||
@@ -234,6 +244,7 @@ impl RenderOnce for Sidebar {
|
|||||||
id,
|
id,
|
||||||
selected,
|
selected,
|
||||||
transparent: material,
|
transparent: material,
|
||||||
|
grain,
|
||||||
a11y_label,
|
a11y_label,
|
||||||
on_change,
|
on_change,
|
||||||
entries,
|
entries,
|
||||||
@@ -381,6 +392,7 @@ impl RenderOnce for Sidebar {
|
|||||||
.child(body)
|
.child(body)
|
||||||
.when_some(footer, |sidebar, footer| sidebar.child(footer))
|
.when_some(footer, |sidebar, footer| sidebar.child(footer))
|
||||||
.when_some(overlay_strip, |sidebar, strip| sidebar.child(strip))
|
.when_some(overlay_strip, |sidebar, strip| sidebar.child(strip))
|
||||||
|
.when(material && grain, |sidebar| sidebar.child(grain_overlay()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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_FACTOR: f32 = 0.45;
|
||||||
pub const PIXEL_AVATAR_DARK_FLOOR: f32 = 0.18;
|
pub const PIXEL_AVATAR_DARK_FLOOR: f32 = 0.18;
|
||||||
/// The default window material transparency, as a percent (0 opaque, 100 the lightest fill).
|
/// 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.
|
/// The light-mode floor of the material fill.
|
||||||
pub const MIN_MATERIAL_FILL_LIGHT: f32 = 0.45;
|
pub const MIN_MATERIAL_FILL_LIGHT: f32 = 0.45;
|
||||||
/// The dark-mode floor of the material fill.
|
/// The dark-mode floor of the material fill.
|
||||||
pub const MIN_MATERIAL_FILL_DARK: f32 = 0.15;
|
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.
|
/// Light or dark, resolved from the system appearance.
|
||||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Deserialize)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Deserialize)]
|
||||||
|
|||||||
@@ -226,6 +226,8 @@ struct Gallery {
|
|||||||
alert_open: bool,
|
alert_open: bool,
|
||||||
/// Whether the app sidebar paints the window's native material.
|
/// Whether the app sidebar paints the window's native material.
|
||||||
sidebar_transparent: bool,
|
sidebar_transparent: bool,
|
||||||
|
/// Whether a film-grain overlay textures the window.
|
||||||
|
grain: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Gallery {
|
impl Gallery {
|
||||||
@@ -258,6 +260,7 @@ impl Gallery {
|
|||||||
let left_sidebar = Sidebar::new("gallery-sidebar")
|
let left_sidebar = Sidebar::new("gallery-sidebar")
|
||||||
.selected(selected)
|
.selected(selected)
|
||||||
.transparent(self.sidebar_transparent)
|
.transparent(self.sidebar_transparent)
|
||||||
|
.grain(self.grain)
|
||||||
.accessibility_label("Component catalog")
|
.accessibility_label("Component catalog")
|
||||||
.entries(|_, _| {
|
.entries(|_, _| {
|
||||||
CATALOG
|
CATALOG
|
||||||
@@ -300,6 +303,7 @@ impl Gallery {
|
|||||||
let gallery_change = gallery.clone();
|
let gallery_change = gallery.clone();
|
||||||
Dialog::new("gallery-dialog")
|
Dialog::new("gallery-dialog")
|
||||||
.open(open)
|
.open(open)
|
||||||
|
.grain(true)
|
||||||
.title("Delete file?")
|
.title("Delete file?")
|
||||||
.on_change(move |open, _, cx| {
|
.on_change(move |open, _, cx| {
|
||||||
gallery_change
|
gallery_change
|
||||||
@@ -351,6 +355,7 @@ impl Gallery {
|
|||||||
Dialog::new("gallery-alert")
|
Dialog::new("gallery-alert")
|
||||||
.open(open)
|
.open(open)
|
||||||
.alert(true)
|
.alert(true)
|
||||||
|
.grain(true)
|
||||||
.title("Sign out of the workspace?")
|
.title("Sign out of the workspace?")
|
||||||
.on_change(move |open, _, cx| {
|
.on_change(move |open, _, cx| {
|
||||||
gallery_change
|
gallery_change
|
||||||
@@ -595,6 +600,7 @@ impl Render for WelcomePanel {
|
|||||||
let layout_right = self.layout_state.clone();
|
let layout_right = self.layout_state.clone();
|
||||||
let dock_state = self.dock_state.clone();
|
let dock_state = self.dock_state.clone();
|
||||||
let gallery = self.gallery.clone();
|
let gallery = self.gallery.clone();
|
||||||
|
let grain_gallery = self.gallery.clone();
|
||||||
|
|
||||||
ScrollArea::new("welcome-demo")
|
ScrollArea::new("welcome-demo")
|
||||||
.size_full()
|
.size_full()
|
||||||
@@ -667,6 +673,20 @@ impl Render for WelcomePanel {
|
|||||||
.log_err();
|
.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(
|
.child(
|
||||||
Button::new("welcome-split")
|
Button::new("welcome-split")
|
||||||
.label("Split active right")
|
.label("Split active right")
|
||||||
@@ -4470,6 +4490,7 @@ fn main() {
|
|||||||
dialog_open: false,
|
dialog_open: false,
|
||||||
alert_open: false,
|
alert_open: false,
|
||||||
sidebar_transparent: true,
|
sidebar_transparent: true,
|
||||||
|
grain: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
let palette = cx.new(|cx| {
|
let palette = cx.new(|cx| {
|
||||||
|
|||||||
Reference in New Issue
Block a user