gpui-material-components 0.3.0

Material 3 Expressive desktop components for GPUI
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
use std::rc::Rc;

use gpui::{
    AnyElement, App, Edges, ElementId, Entity, Hsla, IntoElement, Pixels, SharedString, Size,
    Window, px, radians,
};
use gpui_material_theme::{ColorRole, CornerShape, Elevation};

use crate::{Icon, IconButton, IconName, ScaffoldInsets};

use super::bottom_sheet::{BottomSheetState, BottomSheetThresholds};
use super::dialog::{
    DialogCloseReason, DialogProperties, DialogState, ModalClosed, ModalDismissRequest,
    ModalSession,
};

mod gesture;
mod render;

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum SheetEdge {
    #[default]
    Bottom,
    Start,
    End,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum SheetMode {
    #[default]
    Standard,
    Modal,
}

/// Placement of a side sheet relative to its window edge or layout slot.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum SheetPlacement {
    #[default]
    Docked,
    Detached,
}

/// Logical detached margins in pixels. The default is 16 px on each side.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SheetMargins {
    pub top: Pixels,
    pub bottom: Pixels,
    pub start: Pixels,
    pub end: Pixels,
}

impl Default for SheetMargins {
    fn default() -> Self {
        Self {
            top: px(16.),
            bottom: px(16.),
            start: px(16.),
            end: px(16.),
        }
    }
}

impl SheetMargins {
    fn fit(self, viewport: Size<Pixels>, rtl: bool) -> Edges<Pixels> {
        let (start, end) =
            fit_margins(self.start, self.end, (viewport.width - px(48.)).max(px(0.)));
        let (top, bottom) = fit_margins(
            self.top,
            self.bottom,
            (viewport.height - px(48.)).max(px(0.)),
        );
        let (left, right) = if rtl { (end, start) } else { (start, end) };
        Edges {
            top,
            bottom,
            left,
            right,
        }
    }
}

fn fit_margins(a: Pixels, b: Pixels, available: Pixels) -> (Pixels, Pixels) {
    let sanitize = |margin: Pixels| {
        if f32::from(margin).is_finite() {
            margin.clamp(px(0.), available)
        } else {
            px(0.)
        }
    };
    let (a, b) = (sanitize(a), sanitize(b));
    if a + b > available {
        let factor = available / (a + b);
        (a * factor, b * factor)
    } else {
        (a, b)
    }
}

/// Optional local colors for the sheet and its adornments.
#[derive(Clone, Copy, Debug, Default)]
pub struct SheetColors {
    pub container: Option<Hsla>,
    pub content: Option<Hsla>,
    pub header: Option<Hsla>,
    pub divider: Option<Hsla>,
    pub scrim: Option<Hsla>,
}

/// Activation for a close control supplied by the caller.
#[derive(Clone)]
pub struct SheetClose(CloseAction);

#[derive(Clone)]
enum CloseAction {
    Modal(ModalSession),
    Standard(Option<ModalDismissRequest>, Option<ModalClosed>),
}

impl SheetClose {
    /// Requests dismissal from the sheet's Close action.
    pub fn activate(&self, window: &mut Window, cx: &mut App) {
        self.request(window, cx);
    }

    pub(super) fn request(&self, window: &mut Window, cx: &mut App) -> bool {
        let reason = DialogCloseReason::Cancel;
        match &self.0 {
            CloseAction::Modal(session) => session.dismiss(reason, window, cx),
            CloseAction::Standard(request, Some(closed))
                if request
                    .as_ref()
                    .is_none_or(|request| request(reason, window, cx)) =>
            {
                closed(reason, window, cx);
                true
            }
            CloseAction::Standard(_, _) => false,
        }
    }
}

type CloseSlot = Box<dyn FnOnce(SheetClose) -> AnyElement>;

type Change = Rc<dyn Fn(bool, &mut Window, &mut App)>;

enum SheetBinding {
    Immediate(Entity<DialogState>),
    Retained(Entity<BottomSheetState>),
}

/// A docked or modal Material sheet. Standard sheets take part in parent layout.
/// The caller owns the bottom sheet's expanded value and handles changes.
#[derive(IntoElement)]
pub struct Sheet {
    id: ElementId,
    state: SheetBinding,
    title: SharedString,
    content: AnyElement,
    edge: SheetEdge,
    mode: SheetMode,
    extent: Pixels,
    expansion: Option<(bool, Change)>,
    placement: SheetPlacement,
    margins: SheetMargins,
    header: Option<AnyElement>,
    back: Option<AnyElement>,
    close_content: Option<CloseSlot>,
    close_label: SharedString,
    show_close: bool,
    footer: Option<AnyElement>,
    footer_divider: bool,
    edge_divider: bool,
    properties: DialogProperties,
    on_dismiss_request: Option<ModalDismissRequest>,
    on_close: Option<ModalClosed>,
    layer: usize,
    shape: Option<CornerShape>,
    container_role: Option<ColorRole>,
    content_role: Option<ColorRole>,
    colors: SheetColors,
    tonal_elevation: Elevation,
    shadow_elevation: Option<Elevation>,
    scrim_opacity: f32,
    presence: Option<gpui_base::motion::PresenceSample>,
    layout_size: Option<Size<Pixels>>,
    peek_height: Option<Pixels>,
    max_width: Pixels,
    drag_enabled: bool,
    show_handle: bool,
    handle_content: Option<AnyElement>,
    handle_label: SharedString,
    thresholds: BottomSheetThresholds,
    content_insets: ScaffoldInsets,
}

impl Sheet {
    pub fn new(
        id: impl Into<ElementId>,
        state: &Entity<DialogState>,
        title: impl Into<SharedString>,
        content: impl IntoElement,
    ) -> Self {
        Self::with_state(
            id.into(),
            SheetBinding::Immediate(state.clone()),
            title.into(),
            content.into_any_element(),
        )
    }

    /// Creates a bottom sheet with retained stops and state-owned visibility.
    pub fn bottom(
        id: impl Into<ElementId>,
        state: &Entity<BottomSheetState>,
        title: impl Into<SharedString>,
        content: impl IntoElement,
    ) -> Self {
        Self::with_state(
            id.into(),
            SheetBinding::Retained(state.clone()),
            title.into(),
            content.into_any_element(),
        )
    }

    fn with_state(
        id: ElementId,
        state: SheetBinding,
        title: SharedString,
        content: AnyElement,
    ) -> Self {
        Self {
            id,
            state,
            title,
            content,
            edge: SheetEdge::Bottom,
            mode: SheetMode::Standard,
            extent: px(320.),
            expansion: None,
            placement: SheetPlacement::Docked,
            margins: SheetMargins::default(),
            header: None,
            back: None,
            close_content: None,
            close_label: "Close".into(),
            show_close: true,
            footer: None,
            footer_divider: false,
            edge_divider: false,
            properties: DialogProperties::default(),
            on_dismiss_request: None,
            on_close: None,
            layer: 0,
            shape: None,
            container_role: None,
            content_role: None,
            colors: SheetColors::default(),
            tonal_elevation: Elevation::Level0,
            shadow_elevation: None,
            scrim_opacity: 0.32,
            presence: None,
            layout_size: None,
            peek_height: None,
            max_width: px(640.),
            drag_enabled: true,
            show_handle: true,
            handle_content: None,
            handle_label: "Resize bottom sheet".into(),
            thresholds: BottomSheetThresholds::default(),
            content_insets: ScaffoldInsets::default(),
        }
    }

    #[cfg_attr(not(feature = "navigation"), allow(dead_code))]
    pub(crate) fn in_scaffold(mut self, size: Size<Pixels>) -> Self {
        self.edge = SheetEdge::Bottom;
        self.mode = SheetMode::Standard;
        self.layout_size = Some(size);
        self
    }

    /// Sets the retained bottom sheet's partial height. It fits the expanded extent.
    pub fn peek_height(mut self, height: Pixels) -> Self {
        if f32::from(height).is_finite() {
            self.peek_height = Some(height.max(px(48.)));
        }
        self
    }

    /// Sets the bottom sheet's maximum width in logical pixels.
    pub fn max_width(mut self, width: Pixels) -> Self {
        if f32::from(width).is_finite() && width > px(0.) {
            self.max_width = width;
        }
        self
    }

    pub fn drag_enabled(mut self, enabled: bool) -> Self {
        self.drag_enabled = enabled;
        self
    }

    pub fn drag_thresholds(mut self, thresholds: BottomSheetThresholds) -> Self {
        self.thresholds = thresholds.normalized();
        self
    }

    /// Replaces the grip decoration of the retained sheet's resize control.
    pub fn handle_content(mut self, content: impl IntoElement) -> Self {
        self.handle_content = Some(content.into_any_element());
        self
    }

    /// Names the resize action for keyboard and assistive technology.
    pub fn handle_label(mut self, label: impl Into<SharedString>) -> Self {
        self.handle_label = label.into();
        self
    }

    /// Uses a text height action in place of the grip when multiple stops are available.
    pub fn show_handle(mut self, show: bool) -> Self {
        self.show_handle = show;
        self
    }

    /// Sets logical padding within the bottom sheet's scrolling content.
    pub fn content_insets(mut self, insets: ScaffoldInsets) -> Self {
        self.content_insets = insets.normalized();
        self
    }

    pub fn edge(mut self, edge: SheetEdge) -> Self {
        if matches!(self.state, SheetBinding::Immediate(_)) {
            self.edge = edge;
        }
        self
    }

    pub fn mode(mut self, mode: SheetMode) -> Self {
        self.mode = mode;
        self
    }

    /// Sets expanded height for a bottom sheet, or width for a side sheet.
    pub fn extent(mut self, extent: Pixels) -> Self {
        self.extent = extent.max(px(48.));
        self
    }

    /// Enables a keyboard-operable expand and collapse action on a bottom sheet.
    pub fn expandable(
        mut self,
        expanded: bool,
        on_change: impl Fn(bool, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.expansion = Some((expanded, Rc::new(on_change)));
        self
    }

    pub fn placement(mut self, placement: SheetPlacement) -> Self {
        self.placement = placement;
        self
    }

    pub fn margins(mut self, margins: SheetMargins) -> Self {
        self.margins = margins;
        self
    }

    /// Replaces the visible heading while preserving the accessible sheet name.
    pub fn header_content(mut self, heading: impl IntoElement) -> Self {
        self.header = Some(heading.into_any_element());
        self
    }

    /// Adds a named back action. Navigation ownership remains with the caller.
    pub fn back(
        mut self,
        label: impl Into<SharedString>,
        on_back: impl Fn(&mut Window, &mut App) + 'static,
    ) -> Self {
        self.back = Some(
            IconButton::new(
                "back",
                label,
                Icon::new(IconName::ArrowForward)
                    .rotate(radians(std::f32::consts::PI))
                    .mirror_in_rtl(true),
            )
            .on_activate(on_back)
            .into_any_element(),
        );
        self
    }

    pub fn back_content(mut self, back: impl IntoElement) -> Self {
        self.back = Some(back.into_any_element());
        self
    }

    pub fn close_label(mut self, label: impl Into<SharedString>) -> Self {
        self.close_label = label.into();
        self
    }

    pub fn show_close(mut self, show: bool) -> Self {
        self.show_close = show;
        self
    }

    /// Builds a custom close control. Connect its activation to the supplied action.
    pub fn close_content<E: IntoElement>(
        mut self,
        close: impl FnOnce(SheetClose) -> E + 'static,
    ) -> Self {
        self.close_content = Some(Box::new(move |action| close(action).into_any_element()));
        self
    }

    pub fn footer(mut self, actions: impl IntoElement) -> Self {
        self.footer = Some(actions.into_any_element());
        self
    }

    pub fn footer_divider(mut self, visible: bool) -> Self {
        self.footer_divider = visible;
        self
    }

    /// Adds a divider between a docked Standard side sheet and its adjacent content.
    pub fn edge_divider(mut self, visible: bool) -> Self {
        self.edge_divider = visible;
        self
    }

    pub fn properties(mut self, properties: DialogProperties) -> Self {
        self.properties = properties;
        self
    }

    /// Return false to veto a user-initiated close.
    pub fn on_dismiss_request(
        mut self,
        request: impl Fn(DialogCloseReason, &mut Window, &mut App) -> bool + 'static,
    ) -> Self {
        self.on_dismiss_request = Some(Rc::new(request));
        self
    }

    /// Receives accepted modal closes or requests to remove a Standard sheet.
    pub fn on_close(
        mut self,
        close: impl Fn(DialogCloseReason, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_close = Some(Rc::new(close));
        self
    }

    /// Sets the stacking layer of a nested modal sheet.
    pub fn layer(mut self, layer: usize) -> Self {
        self.layer = layer;
        self
    }

    pub fn shape(mut self, shape: CornerShape) -> Self {
        self.shape = Some(shape);
        self
    }

    pub fn container_role(mut self, role: ColorRole) -> Self {
        self.container_role = Some(role);
        self
    }

    pub fn content_role(mut self, role: ColorRole) -> Self {
        self.content_role = Some(role);
        self
    }

    pub fn colors(mut self, colors: SheetColors) -> Self {
        self.colors = colors;
        self
    }

    pub fn tonal_elevation(mut self, elevation: Elevation) -> Self {
        self.tonal_elevation = elevation;
        self
    }

    pub fn shadow_elevation(mut self, elevation: Elevation) -> Self {
        self.shadow_elevation = Some(elevation);
        self
    }

    /// Reuses the enclosing component's sampled presence.
    #[cfg_attr(not(feature = "navigation"), allow(dead_code))]
    pub(crate) fn sampled_presence(mut self, presence: gpui_base::motion::PresenceSample) -> Self {
        self.presence = Some(presence);
        self
    }

    pub fn scrim_opacity(mut self, opacity: f32) -> Self {
        self.scrim_opacity = if opacity.is_nan() {
            0.32
        } else {
            opacity.clamp(0., 1.)
        };
        self
    }
}