Skip to main content

bhoo/systems/material/
mod.rs

1//! Initial controls using the shared input, motion, semantics and draw systems.
2//! Reference behavior and remaining gaps: docs/implementation-progress.md.
3use crate::*;
4
5mod alert;
6mod app_bar;
7mod autocomplete;
8mod avatar;
9mod badge;
10mod banner;
11mod breadcrumb;
12mod button;
13mod button_group;
14mod card;
15mod carousel;
16mod chat;
17mod checkbox;
18mod chip;
19mod color_picker;
20mod command_palette;
21mod data_table;
22mod date_picker;
23mod divider;
24mod empty_state;
25mod expansion_panel;
26mod fab;
27mod fab_menu;
28mod grid_list;
29mod helpers;
30mod icons;
31mod layout;
32mod link;
33mod list;
34pub mod loading;
35mod menu;
36mod navigation;
37mod overlay;
38mod pagination;
39pub mod progress;
40pub mod pull_refresh;
41mod radio;
42mod rating;
43mod scaffold;
44mod scrollbar;
45mod search;
46mod segmented;
47mod select;
48mod shortcut_hint;
49mod skeleton;
50mod slider;
51pub mod snackbar;
52mod spin_box;
53mod split_button;
54mod split_pane;
55mod stepper;
56mod switch;
57mod tabs;
58mod text_field;
59mod tile;
60mod time_picker;
61mod toast;
62mod toolbar;
63mod tooltip;
64mod transitions;
65mod tree;
66mod window_frame;
67
68pub use alert::{AlertOptions, AlertSeverity, AlertVariant};
69pub use app_bar::{
70    AppBarAction, BottomAppBarOverflowState, BottomAppBarScrollBehavior, BottomAppBarState,
71    FloatingPillSlot, TopAppBarMaterialAction, TopAppBarScrollBehavior, TopAppBarState,
72    TopAppBarVariant,
73};
74pub use autocomplete::AutocompleteOptions;
75pub use avatar::{AvatarContent, AvatarGroupCounts, AvatarGroupOptions, AvatarShape};
76pub use badge::{BadgeContent, BadgeGravity, BadgeNumberFormat, BadgeNumberLimit, BadgeOptions};
77pub use banner::BannerOptions;
78pub use button::{ButtonVariant, Options};
79pub use button_group::ButtonGroupState;
80pub(crate) use card::CardAppearance;
81pub use card::{
82    CardIconGravity, CardOptions, CardVariant, CardVisualState, SwipeDismissState,
83    segmented_card_radii,
84};
85pub use carousel::{CarouselItem, CarouselLayout, CarouselOptions, CarouselState};
86pub use chat::ChatMessage;
87pub use checkbox::CheckState;
88pub use chip::{ChipKind, ChipOptions};
89pub use color_picker::hue_to_color;
90pub use command_palette::CommandItem;
91pub use data_table::{SortDirection, TableColumn, TableSortState};
92pub use date_picker::{DatePickerOptions, DatePickerState, DatePickerView, month_days, weekday};
93pub use empty_state::EmptyStateOptions;
94pub use expansion_panel::ExpansionPanelOptions;
95pub use fab::{FabColor, FabElevation, FabOptions};
96pub use fab_menu::{FabMenuAction, FabMenuOptions, FabMenuState};
97pub use grid_list::{GridListItem, GridListOptions};
98pub use icons::{AppBarIcon, MaterialIcon};
99pub use list::ListState;
100pub use menu::MenuTypeaheadState;
101pub use navigation::{NavigationDestination, NavigationIcon};
102pub(crate) use overlay::OverlayPhase;
103pub use overlay::{
104    OverlayDismissPolicy, OverlayKind, OverlaySection, OverlayState, OverlayStyle, SelectionDismiss,
105};
106pub use pagination::{PageIndicatorOptions, PaginationOptions};
107pub use radio::Choice;
108pub use scaffold::{ScaffoldAdaptiveOptions, ScaffoldOptions, ScaffoldSlots};
109pub use scrollbar::{ScrollbarDragState, ScrollbarIndicatorState, ScrollbarOrientation};
110pub use search::SearchOverlayState;
111pub use select::SelectState;
112pub use skeleton::{SkeletonAnimation, SkeletonOptions};
113pub use slider::SliderExpressiveOptions;
114pub(crate) use slider::SliderTintColors;
115pub use split_button::{SplitButtonOptions, SplitButtonSize};
116pub use split_pane::{SplitPaneOptions, SplitPaneOrientation};
117pub use stepper::StepperState;
118pub use switch::SwitchIconMode;
119pub(crate) use switch::{SwitchLayout, switch_widths};
120pub use tabs::{TabItem, TabOptions, TabStyle};
121pub use text_field::FieldAffordance;
122pub use tile::ActionTileOptions;
123pub use time_picker::{
124    TimePickerMode, TimePickerOptions, TimePickerState, time_picker_label, time_picker_label_24h,
125    time_picker_label_step,
126};
127pub use toast::{ToastOptions, ToastState};
128pub use toolbar::{
129    DockedToolbarEdge, DockedToolbarInsets, DockedToolbarScrollState,
130    FloatingToolbarExpansionScrollState, ToolbarAction, ToolbarFabPosition, ToolbarFabSelection,
131    ToolbarKind, ToolbarState,
132};
133pub use transitions::{
134    PageDirection, RouteTransitionKind, RouteTransitionState, ShapeRotationState, SlideDirection,
135    container_transform_rect,
136};
137pub use tree::{TreeItem, TreeState};
138pub use window_frame::{WindowFrameAction, WindowFrameOptions, WindowFrameState};
139
140impl Frame<'_> {
141    pub(super) fn control_color(&mut self, response: Response, base: Color) -> Color {
142        let h = self.animate(
143            response.id,
144            0,
145            if response.hovered { 1.0 } else { 0.0 },
146            0.0,
147        );
148        let p = self.animate(
149            response.id,
150            1,
151            if response.pressed { 1.0 } else { 0.0 },
152            0.0,
153        );
154        if response.enabled {
155            base.mix(
156                self.ui.theme.text,
157                if response.pressed {
158                    0.12 * p
159                } else if response.focused && self.ui.keyboard_focus {
160                    0.12
161                } else {
162                    0.08 * h
163                },
164            )
165        } else {
166            self.ui.theme.background.mix(self.ui.theme.text, 0.12)
167        }
168    }
169    pub fn elevation(&mut self, b: Rect, radius: f32, level: u8) {
170        let black = Color(0, 0, 0, 255);
171        let specs = if level >= 6 {
172            [
173                (0.0, 3.0, 5.0, -1.0, 0.2),
174                (0.0, 6.0, 10.0, 0.0, 0.14),
175                (0.0, 1.0, 18.0, 0.0, 0.12),
176            ]
177        } else if level <= 1 {
178            [
179                (0.0, 2.0, 1.0, -1.0, 0.2),
180                (0.0, 1.0, 1.0, 0.0, 0.14),
181                (0.0, 1.0, 3.0, 0.0, 0.12),
182            ]
183        } else {
184            [
185                (0.0, 3.0, 1.0, -2.0, 0.2),
186                (0.0, 2.0, 2.0, 0.0, 0.14),
187                (0.0, 1.0, 5.0, 0.0, 0.12),
188            ]
189        };
190        for (x, y, blur, spread, alpha) in specs {
191            self.shadow(b, radius, [x, y], blur, spread, black.opacity(alpha));
192        }
193    }
194    /// Approximate a continuous Android elevation in dp using the existing
195    /// shadow profiles; actual platform shadows vary by renderer and device.
196    pub(crate) fn elevation_dp(&mut self, b: Rect, radius: f32, dp: f32) {
197        if !dp.is_finite() || dp <= 0.0 {
198            return;
199        }
200        const LOW: [[f32; 5]; 3] = [
201            [0., 2., 1., -1., 0.2],
202            [0., 1., 1., 0., 0.14],
203            [0., 1., 3., 0., 0.12],
204        ];
205        const MID: [[f32; 5]; 3] = [
206            [0., 3., 1., -2., 0.2],
207            [0., 2., 2., 0., 0.14],
208            [0., 1., 5., 0., 0.12],
209        ];
210        const HIGH: [[f32; 5]; 3] = [
211            [0., 3., 5., -1., 0.2],
212            [0., 6., 10., 0., 0.14],
213            [0., 1., 18., 0., 0.12],
214        ];
215        let (a, b_spec, t) = if dp <= 1.0 {
216            (LOW, LOW, 0.0)
217        } else if dp <= 2.0 {
218            (LOW, MID, dp - 1.0)
219        } else {
220            (MID, HIGH, ((dp - 2.0) / 4.0).min(1.0))
221        };
222        let scale = if dp > 6.0 {
223            (dp / 6.0).sqrt().min(2.0)
224        } else {
225            1.0
226        };
227        for i in 0..3 {
228            let p = std::array::from_fn::<_, 5, _>(|j| a[i][j] + (b_spec[i][j] - a[i][j]) * t);
229            self.shadow(
230                b,
231                radius,
232                [p[0] * scale, p[1] * scale],
233                p[2] * scale,
234                p[3] * scale,
235                Color(0, 0, 0, 255).opacity(p[4] * dp.min(1.0)),
236            );
237        }
238    }
239    /// Fixed-size grouping surface; children are clipped and use the same stable ID namespace.
240    pub fn panel(&mut self, height: f32, build: impl FnOnce(&mut Self)) {
241        let b = self.allocate(height.max(0.0));
242        self.rect(b, self.ui.theme.radius, self.ui.theme.control);
243        self.within(b.inset(12.0), build);
244    }
245    pub fn within(&mut self, bounds: Rect, build: impl FnOnce(&mut Self)) {
246        let previous = (self.bounds, self.clip, self.cursor);
247        self.bounds = bounds;
248        self.clip = self.clip.intersect(bounds);
249        self.cursor = bounds.y;
250        self.commands.push(DrawCommand::PushClip(self.clip));
251        build(self);
252        self.commands.push(DrawCommand::PopClip);
253        (self.bounds, self.clip, self.cursor) = previous;
254    }
255    /// Lay out children within bounds while preserving the existing paint clip.
256    /// This allows effects such as shadows to extend beyond the layout bounds.
257    pub fn within_unclipped(&mut self, bounds: Rect, build: impl FnOnce(&mut Self)) {
258        let previous = (self.bounds, self.cursor);
259        self.bounds = bounds;
260        self.cursor = bounds.y;
261        build(self);
262        (self.bounds, self.cursor) = previous;
263    }
264}