gpui-material-extras 0.4.1

Optional Material 3 Expressive component families 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
use std::{cell::Cell, rc::Rc};

use gpui::{
    AccessibleAction, AnyElement, App, Div, ElementId, Entity, Hsla, InteractiveElement,
    IntoElement, MouseButton, ParentElement, RenderOnce, Role, SharedString, Stateful,
    StatefulInteractiveElement, Styled, Window,
    accesskit::{ActionData, Orientation},
    canvas, div,
    prelude::FluentBuilder as _,
    px,
};
use gpui_base::TestSupportExt;
use gpui_material_components::Text;
use gpui_material_foundation::{
    InteractionFeedback, InteractionSource, InteractionState, InteractionTracker, Shape, animate,
    color, current_material_theme,
};
use gpui_material_theme::{
    ColorRole, CornerRole, CornerShape, MaterialTheme, MotionKind, MotionSpec, MotionSpeed,
    TypeRole,
};

use super::state::{HEIGHT, ROW_HEIGHT, ScrollFieldState, ScrollOrigin, wrap};

/// Optional overrides for the window theme's scroll field colors.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ScrollFieldColors {
    /// Background fill color when enabled.
    pub container: Option<Hsla>,
    /// Text color of non-selected rows when enabled.
    pub content: Option<Hsla>,
    /// Text color of the centered selected row when enabled.
    pub selected_content: Option<Hsla>,
    /// Background fill color when disabled.
    pub disabled_container: Option<Hsla>,
    /// Text color of non-selected rows when disabled.
    pub disabled_content: Option<Hsla>,
    /// Text color of the centered selected row when disabled.
    pub disabled_selected_content: Option<Hsla>,
}

impl ScrollFieldColors {
    fn resolve(self, enabled: bool, theme: &MaterialTheme) -> (Hsla, Hsla, Hsla) {
        let container = color(theme.colors.color(ColorRole::SurfaceContainerLowest));
        let selected = color(theme.colors.color(ColorRole::OnSurface));
        if enabled {
            (
                self.container.unwrap_or(container),
                self.content
                    .unwrap_or_else(|| color(theme.colors.color(ColorRole::Outline))),
                self.selected_content.unwrap_or(selected),
            )
        } else {
            (
                self.disabled_container
                    .unwrap_or_else(|| container.opacity(0.38)),
                self.disabled_content
                    .unwrap_or_else(|| selected.opacity(0.38)),
                self.disabled_selected_content
                    .unwrap_or_else(|| selected.opacity(0.38)),
            )
        }
    }
}

type ItemContent = Box<dyn Fn(usize, bool, bool) -> AnyElement>;
type ValueDescription = Box<dyn Fn(usize) -> SharedString>;

/// A vertically scrolling selection wheel with a single accessible focus stop.
/// The field fills its parent's width and displays three rows at a time.
#[derive(IntoElement)]
pub struct ScrollField {
    id: ElementId,
    name: SharedString,
    state: Entity<ScrollFieldState>,
    disabled: bool,
    colors: ScrollFieldColors,
    time_field_shape: Option<CornerShape>,
    numeric_offset: usize,
    source: Option<Entity<InteractionSource>>,
    item: Option<ItemContent>,
    description: Option<ValueDescription>,
}

impl ScrollField {
    /// Creates a scroll field with an element `id`, accessible `name`, and caller-owned `state`.
    pub fn new(
        id: impl Into<ElementId>,
        name: impl Into<SharedString>,
        state: &Entity<ScrollFieldState>,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            state: state.clone(),
            disabled: false,
            colors: ScrollFieldColors::default(),
            time_field_shape: None,
            numeric_offset: 0,
            source: None,
            item: None,
            description: None,
        }
    }

    /// Disables pointer, scroll, keyboard, and accessibility interaction when `true`.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Overrides the scroll field's local colors.
    pub fn colors(mut self, colors: ScrollFieldColors) -> Self {
        self.colors = colors;
        self
    }

    pub(in crate::date_time) fn time_field(
        mut self,
        shape: Option<CornerShape>,
        numeric_offset: usize,
    ) -> Self {
        self.time_field_shape = shape;
        self.numeric_offset = numeric_offset;
        self
    }

    /// Connects an [`InteractionSource`] to observe hover, press, drag, and focus states.
    pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
        self.source = Some(source.clone());
        self
    }

    /// Renders a decorative row with its index, selected state and enabled state.
    pub fn item_content<E: IntoElement>(
        mut self,
        item: impl Fn(usize, bool, bool) -> E + 'static,
    ) -> Self {
        self.item = Some(Box::new(move |index, selected, enabled| {
            item(index, selected, enabled).into_any_element()
        }));
        self
    }

    /// Describes an option to assistive technology. By default, uses its index.
    pub fn value_description<S: Into<SharedString>>(
        mut self,
        description: impl Fn(usize) -> S + 'static,
    ) -> Self {
        self.description = Some(Box::new(move |index| description(index).into()));
        self
    }
}

impl RenderOnce for ScrollField {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = current_material_theme(window, cx);
        let corner = self
            .time_field_shape
            .unwrap_or_else(|| CornerRole::Large.shape());
        let direction = theme.layout.direction;
        let [tl, tr, br, bl] = corner.radii(HEIGHT, HEIGHT, direction).map(px);
        let ring = color(theme.colors.color(ColorRole::Secondary));
        let motion_spec =
            theme
                .motion
                .spec(MotionKind::Spatial, MotionSpeed::Fast, cx.reduce_motion());
        if self.disabled && self.state.read(cx).has_pending_interaction() {
            self.state
                .update(cx, |state, cx| state.scroll_to_option(state.target, cx));
        }
        let motion = self.state.read(cx).motion;
        if let Some(motion) = motion {
            let key = (self.id.clone(), "scroll-position");
            if motion.restart {
                animate(key.clone(), motion.start, MotionSpec::Instant, window, cx);
            }
            let offset = animate(key, 0., motion_spec, window, cx);
            self.state.update(cx, |state, cx| state.sample(offset, cx));
        }
        let state = self.state.read(cx);
        let count = state.item_count;
        let position = state.position;
        let target = state.target;
        let focus = state.focus.clone();
        let enabled = !self.disabled && count > 0;
        let (container, content, selected_content) = self.colors.resolve(enabled, &theme);
        let feedback = window
            .use_keyed_state((self.id.clone(), "scroll-field-feedback"), cx, |_, _| {
                Rc::new(Cell::new(InteractionState::default()))
            })
            .read(cx)
            .clone();
        let input = Input {
            tracker: self
                .source
                .as_ref()
                .map(|source| InteractionSource::track(source, &self.id, window, cx)),
            state: self.state,
            feedback,
            numeric_offset: self.numeric_offset,
        };
        let mut interaction = input.feedback.get();
        if !enabled || !window.is_window_active() {
            interaction = InteractionState::default();
        }
        interaction.disabled = !enabled;
        input.update_feedback(interaction, window, cx);
        let feedback = InteractionFeedback::new(input.feedback.get(), selected_content, ring);
        let label = (count > 0).then(|| {
            self.description
                .as_ref()
                .map_or_else(|| target.to_string().into(), |describe| describe(target))
        });
        let rows = (-2_isize..=2)
            .filter(|row| count > 1 || (count == 1 && *row == 0))
            .map(|row| {
                let index = wrap(position.index, row, count);
                let selected = row == 0;
                let visual = self.item.as_ref().map_or_else(
                    || {
                        Text::new(crate::date_time::format_two_digits(index))
                            .role(if selected {
                                TypeRole::DisplayLarge
                            } else {
                                TypeRole::DisplayMedium
                            })
                            .emphasized(selected)
                            .into_any_element()
                    },
                    |item| item(index, selected, enabled),
                );
                let input = input.state.clone();
                div()
                    .absolute()
                    .top(px((1. + row as f32 - position.fraction) * ROW_HEIGHT))
                    .h(px(ROW_HEIGHT))
                    .w_full()
                    .flex()
                    .items_center()
                    .justify_center()
                    .text_color(if selected { selected_content } else { content })
                    .id(("scroll-field-row", (row + 2) as usize))
                    .role(Role::Group)
                    .a11y_synthetic_children(|tree| tree.parent_node().set_hidden())
                    .when(enabled, |row_element| {
                        row_element.on_click(move |_, window, cx| {
                            input.update(cx, |state, cx| {
                                state.focus.focus(window, cx);
                                state.select_row(row, cx);
                            });
                        })
                    })
                    .child(visual)
                    .test_support()
            });
        let field = div()
            .id(self.id)
            .relative()
            .w_full()
            .min_w_0()
            .h(px(HEIGHT))
            .flex_none()
            .overflow_hidden()
            .rounded_tl(tl)
            .rounded_tr(tr)
            .rounded_br(br)
            .rounded_bl(bl)
            .bg(container)
            .role(Role::Slider)
            .aria_label(self.name)
            .aria_orientation(Orientation::Vertical)
            .when_some(label, |field, label| {
                field
                    .aria_value(label)
                    .aria_numeric_value(target as f64 + self.numeric_offset as f64)
                    .aria_min_numeric_value(self.numeric_offset as f64)
                    .aria_max_numeric_value((count - 1) as f64 + self.numeric_offset as f64)
                    .aria_numeric_value_step(1.)
            })
            .when(!enabled, |field| {
                field.a11y_synthetic_children(|tree| tree.parent_node().set_disabled())
            })
            .children(rows)
            .child(
                canvas(
                    |_, _, _| (),
                    move |bounds, _, window, cx| {
                        if let Some(shape) = Shape::rounded(bounds, corner, direction) {
                            feedback.paint(
                                &shape,
                                None,
                                cx.background_executor().now(),
                                cx.reduce_motion(),
                                window,
                            );
                        }
                    },
                )
                .absolute()
                .inset_0(),
            );
        field
            .when(enabled, |field| input.bind(field))
            .test_support()
            .when(enabled || focus.is_focused(window), |field| {
                field.track_focus(&focus.tab_stop(enabled))
            })
    }
}

#[derive(Clone)]
struct Input {
    state: Entity<ScrollFieldState>,
    feedback: Rc<Cell<InteractionState>>,
    numeric_offset: usize,
    tracker: Option<InteractionTracker>,
}

impl Input {
    fn update_feedback(&self, mut feedback: InteractionState, window: &mut Window, cx: &mut App) {
        let state = self.state.read(cx);
        feedback.focus_visible = !feedback.disabled
            && state.focus.is_focused(window)
            && window.last_input_was_keyboard();
        feedback.dragged = !feedback.disabled && state.gesture_start.is_some();
        if self.feedback.replace(feedback) != feedback {
            window.refresh();
        }
        if let Some(tracker) = &self.tracker {
            tracker.record(feedback, cx);
        }
    }

    fn bind(&self, field: Stateful<Div>) -> Stateful<Div> {
        let scroll = self.clone();
        let hover = self.clone();
        let press = self.clone();
        let release = self.clone();
        let release_out = self.clone();
        let keyboard = self.state.clone();
        let increment = self.state.clone();
        let decrement = self.state.clone();
        let set_value = self.state.clone();
        let numeric_offset = self.numeric_offset as f64;
        field
            .on_scroll(move |event, window, cx| {
                if scroll
                    .state
                    .update(cx, |state, cx| state.scroll(event, window, cx))
                {
                    scroll.update_feedback(scroll.feedback.get(), window, cx);
                    cx.stop_propagation();
                }
            })
            .on_hover(move |hovered, window, cx| {
                let mut feedback = hover.feedback.get();
                feedback.hovered = *hovered;
                hover.update_feedback(feedback, window, cx);
            })
            .on_mouse_down(MouseButton::Left, move |_, window, cx| {
                press.pointer(true, window, cx)
            })
            .on_mouse_up(MouseButton::Left, move |_, window, cx| {
                release.pointer(false, window, cx)
            })
            .on_mouse_up_out(MouseButton::Left, move |_, window, cx| {
                release_out.pointer(false, window, cx)
            })
            .on_key_down(move |event, _, cx| {
                if event.keystroke.modifiers.modified() {
                    return;
                }
                match event.keystroke.key.as_str() {
                    "up" => keyboard.update(cx, |state, cx| state.step(-1, cx)),
                    "down" => keyboard.update(cx, |state, cx| state.step(1, cx)),
                    "home" => keyboard.update(cx, |state, cx| {
                        state.animate_to(0, ScrollOrigin::Interaction, cx)
                    }),
                    "end" => keyboard.update(cx, |state, cx| {
                        state.animate_to(
                            state.item_count.saturating_sub(1),
                            ScrollOrigin::Interaction,
                            cx,
                        )
                    }),
                    _ => return,
                }
                cx.stop_propagation();
            })
            .on_a11y_action(AccessibleAction::Increment, move |_, _, cx| {
                increment.update(cx, |state, cx| state.step(1, cx))
            })
            .on_a11y_action(AccessibleAction::Decrement, move |_, _, cx| {
                decrement.update(cx, |state, cx| state.step(-1, cx))
            })
            .on_a11y_action(AccessibleAction::SetValue, move |data, _, cx| {
                let value = match data {
                    Some(ActionData::NumericValue(value)) => Some(*value),
                    Some(ActionData::Value(value)) => value.trim().parse::<f64>().ok(),
                    _ => None,
                };
                if let Some(value) = value.filter(|value| value.is_finite()) {
                    set_value.update(cx, |state, cx| {
                        state.animate_to(
                            (value - numeric_offset).round() as usize,
                            ScrollOrigin::Interaction,
                            cx,
                        )
                    });
                }
            })
    }

    fn pointer(&self, pressed: bool, window: &mut Window, cx: &mut App) {
        let mut feedback = self.feedback.get();
        feedback.pressed = pressed;
        self.update_feedback(feedback, window, cx);
    }
}