core: add film-grain overlay for materials

This commit is contained in:
2026-11-14 14:13:02 +07:00
parent db41b19e4a
commit 19154527f2
9 changed files with 145 additions and 2 deletions
+4 -1
View File
@@ -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"]
+10
View File
@@ -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<SharedString>,
a11y_label: Option<SharedString>,
on_change: Option<ChangeHandler>,
@@ -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<F>(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();
+90
View File
@@ -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()
}
+3
View File
@@ -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;