xgpui-shader 0.5.0

Shader and paint extension for xgpui with gradients, brushes, and portable WGSL shaders
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//! Explicit extension containers. Custom materials do not enter native Style/Fill.
use crate::{Paint, WindowPaintExt};
use gpui::*;
use refineable::Refineable;
use std::{cell::Cell, rc::Rc, sync::Arc};

enum SurfacePaint {
    Paint(Paint),
    #[cfg(feature = "portable-shaders")]
    Brush(crate::Brush),
}
impl SurfacePaint {
    fn visible(&self) -> bool {
        match self {
            Self::Paint(Paint::Native(color)) => !color.is_transparent(),
            _ => true,
        }
    }
}

/// A normal layout container with extension-owned background/border materials.
/// Explicit materials override native background/border colors on this container.
/// Material changes are supplied by rebuilding it; native hover refinements do not
/// carry custom materials. Descendant interaction and parent clip/opacity are native.
pub struct Painted {
    root: Div,
    children: Vec<AnyElement>,
    background: Option<SurfacePaint>,
    border: Option<SurfacePaint>,
    #[cfg(feature = "portable-shaders")]
    shadows: Vec<crate::instanced::ShadowLayer>,
    #[cfg(feature = "portable-shaders")]
    animated_border: Option<crate::instanced::AnimatedBorderLayer>,
    #[cfg(feature = "portable-shaders")]
    text_brush: Option<Arc<dyn GlyphBrush>>,
    #[cfg(feature = "portable-shaders")]
    backdrop: Option<crate::BackdropBlur>,
    #[cfg(feature = "portable-shaders")]
    layer_effect: Option<crate::LayerEffect>,
}
/// Create an opt-in extension paint container.
pub fn painted() -> Painted {
    Painted {
        root: div(),
        children: Vec::new(),
        background: None,
        border: None,
        #[cfg(feature = "portable-shaders")]
        shadows: Vec::new(),
        #[cfg(feature = "portable-shaders")]
        animated_border: None,
        #[cfg(feature = "portable-shaders")]
        text_brush: None,
        #[cfg(feature = "portable-shaders")]
        backdrop: None,
        #[cfg(feature = "portable-shaders")]
        layer_effect: None,
    }
}
/// Compatibility name for the original shader element.
pub type Shader = Painted;
/// Place a reusable producer in an extension-owned styled container.
pub fn shader(content: Arc<dyn CustomContent>) -> Shader {
    painted().bg_paint(content)
}
impl Painted {
    /// Isolate this bounded surface and its children, then apply a group effect.
    /// Overflow is clipped; deferred overlays paint outside the capture.
    #[cfg(feature = "portable-shaders")]
    pub fn layer_effect(mut self, effect: crate::LayerEffect) -> Self {
        self.layer_effect = Some(effect);
        self
    }

    /// Explicit bounded backdrop pass, before this surface and its children.
    #[cfg(feature = "portable-shaders")]
    pub fn backdrop_blur(mut self, effect: crate::BackdropBlur) -> Self {
        self.backdrop = Some(effect);
        self
    }
    /// Add an immutable programmable shadow layer. Requires uniform corner radii.
    /// Outsets paint before the background; insets before children. Append layers
    /// in back-to-front order within each group. Base geometry/opacity are used;
    /// rebuild this container to update shadow styles.
    #[cfg(feature = "portable-shaders")]
    pub fn shadow_paint(mut self, layer: crate::instanced::ShadowLayer) -> Self {
        self.shadows.push(layer);
        self
    }
    /// Paint an animated ring over the background, before children. Requires uniform
    /// base corner radii. Supply a new phase and request frames from the host view;
    /// a constant phase produces a static/reduced-motion border.
    #[cfg(feature = "portable-shaders")]
    pub fn animated_border(mut self, layer: crate::instanced::AnimatedBorderLayer) -> Self {
        self.animated_border = Some(layer);
        self
    }
    /// Color descendant monochrome glyphs with a continuous field over this
    /// container's full bounds. Emoji retain their native color; nested text
    /// brush containers override this scope. Uses grayscale atlas coverage.
    #[cfg(feature = "portable-shaders")]
    pub fn text_brush(mut self, brush: crate::Brush) -> Self {
        self.text_brush = Some(brush.text_brush().expect("validated text brush"));
        self
    }
    /// Main-pass brush over the full background. Overrides earlier bg_paint/bg_brush.
    #[cfg(feature = "portable-shaders")]
    pub fn bg_brush(mut self, brush: crate::Brush) -> Self {
        self.background = Some(SurfacePaint::Brush(brush));
        self
    }
    /// Main-pass solid-style border brush. Uses layout border widths; dashed
    /// borders require border_paint. Overrides earlier border_paint/border_brush.
    #[cfg(feature = "portable-shaders")]
    pub fn border_brush(mut self, brush: crate::Brush) -> Self {
        self.border = Some(SurfacePaint::Brush(brush));
        self
    }
    pub fn bg_paint(mut self, paint: impl Into<Paint>) -> Self {
        self.background = Some(SurfacePaint::Paint(paint.into()));
        self
    }
    pub fn border_paint(mut self, paint: impl Into<Paint>) -> Self {
        self.border = Some(SurfacePaint::Paint(paint.into()));
        self
    }
}
impl Styled for Painted {
    fn style(&mut self) -> &mut StyleRefinement {
        self.root.style()
    }
}
impl ParentElement for Painted {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

struct Geometry {
    bounds: Cell<Bounds<Pixels>>,
    clip_style: Style,
}
impl Geometry {
    fn mask(&self, window: &Window) -> Option<ContentMask<Pixels>> {
        self.clip_style
            .overflow_mask(self.bounds.get(), window.rem_size())
    }
}
/// Retained element implementation for `Painted`; its fields are private.
pub struct PaintedElement {
    root: Div,
    geometry: Rc<Geometry>,
    #[cfg(feature = "portable-shaders")]
    shadows: Vec<crate::instanced::ShadowLayer>,
    #[cfg(feature = "portable-shaders")]
    text_brush: Option<Arc<dyn GlyphBrush>>,
    #[cfg(feature = "portable-shaders")]
    backdrop: Option<crate::BackdropBlur>,
    #[cfg(feature = "portable-shaders")]
    layer_effect: Option<crate::LayerEffect>,
}
impl IntoElement for PaintedElement {
    type Element = Self;
    fn into_element(self) -> Self {
        self
    }
}
impl IntoElement for Painted {
    type Element = PaintedElement;
    fn into_element(mut self) -> PaintedElement {
        let mut style = Style::default();
        style.refine(self.root.style());
        #[cfg(feature = "portable-shaders")]
        if !self.shadows.is_empty() || self.animated_border.is_some() {
            let r = &style.corner_radii;
            assert!(
                r.top_left == r.top_right
                    && r.top_left == r.bottom_left
                    && r.top_left == r.bottom_right,
                "shader borders and shadows require uniform corner radii; use rounded(...) on the container"
            );
        }
        let custom_background = self
            .background
            .as_ref()
            .is_some_and(|paint| !matches!(paint, SurfacePaint::Paint(Paint::Native(_))));
        let overlay_border = self.border.is_some() || custom_background;
        let border = self.border.or_else(|| {
            style
                .border_color
                .map(|color| SurfacePaint::Paint(Paint::from(color)))
        });
        #[cfg(feature = "portable-shaders")]
        if matches!(border, Some(SurfacePaint::Brush(_))) {
            assert!(
                style.border_style == BorderStyle::Solid,
                "main-pass brushes require solid borders; use border_paint for dashed borders"
            );
        }
        let mut clip_style = style.clone();
        if overlay_border {
            let visible = border.as_ref().is_some_and(SurfacePaint::visible);
            clip_style.border_color = Some(if visible {
                white()
            } else {
                transparent_black()
            });
            // Draw the border after children, outside the content inset mask. Native
            // border widths still participate in Taffy layout; no placeholder pixels.
            self.root.style().border_color = Some(transparent_black());
        }
        let geometry = Rc::new(Geometry {
            bounds: Cell::new(Bounds::default()),
            clip_style,
        });
        match self.background {
            Some(SurfacePaint::Paint(Paint::Native(color))) => {
                self.root.style().background = Some(Fill::Color(color))
            }
            Some(background) => {
                self.root.style().background = None;
                let inset: Vec<_> = style
                    .box_shadow
                    .iter()
                    .filter(|s| s.inset)
                    .cloned()
                    .collect();
                self.root.style().box_shadow = Some(
                    style
                        .box_shadow
                        .iter()
                        .filter(|s| !s.inset)
                        .cloned()
                        .collect(),
                );
                let geometry = geometry.clone();
                let radii = style.corner_radii;
                self.root = self.root.child(
                    canvas(
                        |_, _, _| (),
                        move |_, (), window, _| {
                            let bounds = geometry.bounds.get();
                            let radii = radii
                                .to_pixels(window.rem_size())
                                .clamp_radii_for_quad_size(bounds.size);
                            match background {
                                SurfacePaint::Paint(Paint::Custom(content)) => window.paint_custom(
                                    CustomGeometry::RoundedRect {
                                        bounds,
                                        corner_radii: radii,
                                    },
                                    content,
                                ),
                                #[cfg(feature = "portable-shaders")]
                                SurfacePaint::Brush(brush) => brush
                                    .paint_background(window, bounds, radii)
                                    .expect("validated background brush"),
                                SurfacePaint::Paint(Paint::Native(_)) => unreachable!(),
                            }
                            window.paint_inset_shadows(bounds, radii, &inset);
                        },
                    )
                    .absolute()
                    .size_0(),
                );
            }
            None => {}
        }
        #[cfg(feature = "portable-shaders")]
        {
            let inset: Vec<_> = self
                .shadows
                .iter()
                .filter(|s| s.is_inset())
                .cloned()
                .collect();
            if !inset.is_empty() {
                let geometry = geometry.clone();
                let radii = style.corner_radii;
                self.root = self.root.child(
                    canvas(
                        |_, _, _| (),
                        move |_, (), window, _| {
                            let bounds = geometry.bounds.get();
                            let radius = radii
                                .to_pixels(window.rem_size())
                                .clamp_radii_for_quad_size(bounds.size)
                                .top_left;
                            // Root Div has applied its own opacity; inherited opacity comes from GPUI.
                            for layer in &inset {
                                layer
                                    .paint(window, bounds, radius, 1.)
                                    .expect("validated inset shadow layer");
                            }
                        },
                    )
                    .absolute()
                    .size_0(),
                );
            }
        }
        #[cfg(feature = "portable-shaders")]
        if let Some(border) = self.animated_border {
            let geometry = geometry.clone();
            let radii = style.corner_radii;
            self.root = self.root.child(
                canvas(
                    |_, _, _| (),
                    move |_, (), window, _| {
                        let bounds = geometry.bounds.get();
                        let radius = radii
                            .to_pixels(window.rem_size())
                            .clamp_radii_for_quad_size(bounds.size)
                            .top_left;
                        border
                            .paint(window, bounds, radius)
                            .expect("validated animated border");
                    },
                )
                .absolute()
                .size_0(),
            );
        }
        for child in self.children {
            self.root = self.root.child(ClippedChild {
                child,
                geometry: geometry.clone(),
            });
        }
        if overlay_border && let Some(border) = border {
            let geometry = geometry.clone();
            let radii = style.corner_radii;
            let widths = style.border_widths;
            let border_style = style.border_style;
            self.root = self.root.child(
                canvas(
                    |_, _, _| (),
                    move |_, (), window, _| {
                        let bounds = geometry.bounds.get();
                        let quad = quad(
                            bounds,
                            radii.to_pixels(window.rem_size()),
                            transparent_black(),
                            widths.to_pixels(window.rem_size()),
                            transparent_black(),
                            border_style,
                        );
                        match border {
                            SurfacePaint::Paint(paint) => {
                                window.paint_quad_with_paints(quad, transparent_black(), paint)
                            }
                            #[cfg(feature = "portable-shaders")]
                            SurfacePaint::Brush(brush) => brush
                                .paint_border(window, &quad)
                                .expect("validated border brush"),
                        }
                    },
                )
                .absolute()
                .size_0(),
            );
        }
        PaintedElement {
            root: self.root,
            geometry,
            #[cfg(feature = "portable-shaders")]
            shadows: self.shadows,
            #[cfg(feature = "portable-shaders")]
            text_brush: self.text_brush,
            #[cfg(feature = "portable-shaders")]
            backdrop: self.backdrop,
            #[cfg(feature = "portable-shaders")]
            layer_effect: self.layer_effect,
        }
    }
}
impl Element for PaintedElement {
    type RequestLayoutState = <Div as Element>::RequestLayoutState;
    type PrepaintState = <Div as Element>::PrepaintState;
    fn id(&self) -> Option<ElementId> {
        Element::id(&self.root)
    }
    fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
        self.root.source_location()
    }
    fn request_layout(
        &mut self,
        id: Option<&GlobalElementId>,
        inspector: Option<&InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (LayoutId, Self::RequestLayoutState) {
        Element::request_layout(&mut self.root, id, inspector, window, cx)
    }
    fn prepaint(
        &mut self,
        id: Option<&GlobalElementId>,
        inspector: Option<&InspectorElementId>,
        bounds: Bounds<Pixels>,
        state: &mut Self::RequestLayoutState,
        window: &mut Window,
        cx: &mut App,
    ) -> Self::PrepaintState {
        #[cfg(feature = "portable-shaders")]
        let captured = self.layer_effect.is_some();
        let mut prepaint = |window: &mut Window| {
            self.geometry.bounds.set(bounds);
            #[cfg(feature = "portable-shaders")]
            if let Some(brush) = self.text_brush.clone() {
                return window.with_glyph_brush(brush, bounds, |window| {
                    Element::prepaint(&mut self.root, id, inspector, bounds, state, window, cx)
                });
            }
            Element::prepaint(&mut self.root, id, inspector, bounds, state, window, cx)
        };
        #[cfg(feature = "portable-shaders")]
        if captured {
            return window.with_layer_effect_scope(bounds, prepaint);
        }
        prepaint(window)
    }

    fn paint(
        &mut self,
        id: Option<&GlobalElementId>,
        inspector: Option<&InspectorElementId>,
        bounds: Bounds<Pixels>,
        layout: &mut Self::RequestLayoutState,
        prepaint: &mut Self::PrepaintState,
        window: &mut Window,
        cx: &mut App,
    ) {
        #[cfg(feature = "portable-shaders")]
        let effect = self.layer_effect.clone().filter(|_| {
            self.geometry.clip_style.visibility != Visibility::Hidden
                && self.geometry.clip_style.display != Display::None
                && !bounds.is_empty()
        });
        let mut paint = |window: &mut Window| {
            #[cfg(feature = "portable-shaders")]
            {
                let style = &self.geometry.clip_style;
                if style.visibility != Visibility::Hidden
                    && style.display != Display::None
                    && !bounds.is_empty()
                {
                    let radius = style
                        .corner_radii
                        .to_pixels(window.rem_size())
                        .clamp_radii_for_quad_size(bounds.size)
                        .top_left;
                    // Before Div paints, only ancestor opacity/masks are active. Fold this
                    // container's base opacity into the shadow once, without changing GPUI.
                    let opacity = style.opacity.unwrap_or(1.).clamp(0., 1.);
                    if let Some(effect) = &self.backdrop {
                        effect.paint(
                            window,
                            bounds,
                            style
                                .corner_radii
                                .to_pixels(window.rem_size())
                                .clamp_radii_for_quad_size(bounds.size),
                            opacity,
                        );
                    }
                    for layer in self.shadows.iter().filter(|s| !s.is_inset()) {
                        layer
                            .paint(window, bounds, radius, opacity)
                            .expect("validated outset shadow layer");
                    }
                }
            }
            #[cfg(feature = "portable-shaders")]
            if let Some(brush) = self.text_brush.clone() {
                window.with_glyph_brush(brush, bounds, |window| {
                    Element::paint(
                        &mut self.root,
                        id,
                        inspector,
                        bounds,
                        layout,
                        prepaint,
                        window,
                        cx,
                    )
                });
                return;
            }
            Element::paint(
                &mut self.root,
                id,
                inspector,
                bounds,
                layout,
                prepaint,
                window,
                cx,
            );
        };
        #[cfg(feature = "portable-shaders")]
        if let Some(effect) = effect {
            return effect.paint(window, bounds, paint);
        }
        paint(window)
    }
}
// Delegate layout directly, so wrapping does not insert an extra Taffy node or
// change the parent's flex/grid child sizing. Apply the same mask in both phases.
struct ClippedChild {
    child: AnyElement,
    geometry: Rc<Geometry>,
}
impl IntoElement for ClippedChild {
    type Element = Self;
    fn into_element(self) -> Self {
        self
    }
}
impl Element for ClippedChild {
    type RequestLayoutState = ();
    type PrepaintState = ();
    fn id(&self) -> Option<ElementId> {
        None
    }
    fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
        None
    }
    fn request_layout(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (LayoutId, ()) {
        (self.child.request_layout(window, cx), ())
    }
    fn prepaint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        _: Bounds<Pixels>,
        _: &mut (),
        window: &mut Window,
        cx: &mut App,
    ) {
        window.with_content_mask(self.geometry.mask(window), |window| {
            self.child.prepaint(window, cx)
        });
    }
    fn paint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        _: Bounds<Pixels>,
        _: &mut (),
        _: &mut (),
        window: &mut Window,
        cx: &mut App,
    ) {
        window.with_content_mask(self.geometry.mask(window), |window| {
            self.child.paint(window, cx)
        });
    }
}

#[cfg(test)]
#[path = "surface_shadow_tests.rs"]
mod tests;

#[cfg(all(test, feature = "portable-shaders"))]
mod brush_setter_tests {
    use super::*;
    #[::core::prelude::v1::test]
    fn explicit_material_setters_use_the_last_value() {
        let brush = crate::Brush::solid([1.; 4]).unwrap();
        let surface = painted()
            .bg_brush(brush.clone())
            .bg_paint(white())
            .border_brush(brush.clone())
            .border_paint(white());
        assert!(matches!(
            surface.background,
            Some(SurfacePaint::Paint(Paint::Native(_)))
        ));
        assert!(matches!(
            surface.border,
            Some(SurfacePaint::Paint(Paint::Native(_)))
        ));
        let surface = surface.bg_brush(brush.clone()).border_brush(brush);
        assert!(matches!(surface.background, Some(SurfacePaint::Brush(_))));
        assert!(matches!(surface.border, Some(SurfacePaint::Brush(_))));
    }
}