Skip to main content

brep_app/ui_kit/
mod.rs

1//! The refreshed UI's components (section 5).
2//!
3//! STATUS: rollout step 1. The components exist and are shown in the developer
4//! gallery ([`gallery`](crate::ui_kit::gallery)); no panel calls them yet. `form.rs`'s helpers become
5//! wrappers over them in step 3, after the owner has signed off on the look.
6//!
7//! Every component reads its colours, sizes and durations from the
8//! [`crate::theme::Theme`] of its `Ui` ([`crate::theme::Theme::of`]) and returns
9//! the `egui::Response` of the control, so a caller publishes its automation
10//! hit key from the rect exactly as it does for the egui widget it replaces.
11//! Enumerations are [`dropdown`](crate::ui_kit::dropdown())s; there is no radio row here by design.
12pub mod button;
13pub mod chips;
14pub mod dropdown;
15pub mod gallery;
16pub mod icon_button;
17pub mod input;
18pub mod paint;
19pub mod preview;
20pub mod surface;
21pub mod tabs;
22pub mod toggle;
23
24pub use button::{Button, Size, Variant};
25pub use chips::{reference_field, Chip, ReferenceField, ReferenceOutput};
26pub use dropdown::{dropdown, Dropdown};
27pub use icon_button::{BarSide, IconButton};
28pub use input::{number_input, search_input, text_input, NumberInput, TextInput};
29pub use tabs::{document_tabs, tab_bar, DocumentTab, DocumentTabsOutput};
30pub use toggle::{checkbox, slider, switch, Check};
31
32use eframe::egui;
33
34/// A state a component is DRAWN in whatever the pointer is doing: the gallery
35/// uses it to show hover, pressed and focus side by side. Panels leave it at
36/// [`Show::Live`].
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
38pub enum Show {
39    #[default]
40    Live,
41    Hover,
42    Pressed,
43    Focus,
44}
45
46/// What a control is doing this frame, with [`Show`] folded in.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
48pub(crate) struct Interact {
49    pub hovered: bool,
50    pub pressed: bool,
51    pub focused: bool,
52}
53
54impl Interact {
55    pub fn of(response: &egui::Response, show: Show, enabled: bool) -> Self {
56        if !enabled {
57            return Interact::default();
58        }
59        Interact {
60            hovered: response.hovered() || matches!(show, Show::Hover | Show::Pressed),
61            pressed: response.is_pointer_button_down_on() || show == Show::Pressed,
62            focused: response.has_focus() || show == Show::Focus,
63        }
64    }
65}
66
67/// Run `contents` with `theme` as the theme of every component in it and its
68/// egui style on the `Ui`, whatever the app's own style is.
69pub fn themed<R>(ui: &mut egui::Ui, theme: std::sync::Arc<crate::theme::Theme>, contents: impl FnOnce(&mut egui::Ui) -> R) -> R {
70    let before = crate::theme::stored(ui.ctx());
71    crate::theme::set(ui.ctx(), theme.clone());
72    let out = ui
73        .scope(|ui| {
74            let style = theme.style(ui.style());
75            ui.set_style(style);
76            contents(ui)
77        })
78        .inner;
79    match before {
80        Some(theme) => crate::theme::set(ui.ctx(), theme),
81        None => crate::theme::clear(ui.ctx()),
82    }
83    out
84}
85