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 chrome_parts;
15pub mod chrome_toolbar;
16pub mod dropdown;
17pub mod gallery;
18pub mod icon_button;
19pub mod input;
20pub mod paint;
21pub mod preview;
22pub mod surface;
23pub mod tabs;
24pub mod toggle;
25pub mod canvas;
26pub mod status;
27
28pub use button::{Button, Size, Variant};
29pub use chips::{reference_field, Chip, ReferenceField, ReferenceOutput};
30pub use dropdown::{dropdown, Dropdown};
31pub use icon_button::{BarSide, IconButton};
32pub use input::{number_input, search_input, text_input, NumberInput, TextInput};
33pub use tabs::{document_tabs, tab_bar, DocumentTab, DocumentTabsOutput};
34pub use toggle::{checkbox, slider, switch, Check};
35
36use eframe::egui;
37
38/// A state a component is DRAWN in whatever the pointer is doing: the gallery
39/// uses it to show hover, pressed and focus side by side. Panels leave it at
40/// [`Show::Live`].
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
42pub enum Show {
43    #[default]
44    Live,
45    Hover,
46    Pressed,
47    Focus,
48}
49
50/// What a control is doing this frame, with [`Show`] folded in.
51#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
52pub(crate) struct Interact {
53    pub hovered: bool,
54    pub pressed: bool,
55    pub focused: bool,
56}
57
58impl Interact {
59    pub fn of(response: &egui::Response, show: Show, enabled: bool) -> Self {
60        if !enabled {
61            return Interact::default();
62        }
63        Interact {
64            hovered: response.hovered() || matches!(show, Show::Hover | Show::Pressed),
65            pressed: response.is_pointer_button_down_on() || show == Show::Pressed,
66            focused: response.has_focus() || show == Show::Focus,
67        }
68    }
69}
70
71/// Run `contents` with `theme` as the theme of every component in it and its
72/// egui style on the `Ui`, whatever the app's own style is.
73pub fn themed<R>(ui: &mut egui::Ui, theme: std::sync::Arc<crate::theme::Theme>, contents: impl FnOnce(&mut egui::Ui) -> R) -> R {
74    let before = crate::theme::stored(ui.ctx());
75    crate::theme::set(ui.ctx(), theme.clone());
76    let out = ui
77        .scope(|ui| {
78            let style = theme.style(ui.style());
79            ui.set_style(style);
80            contents(ui)
81        })
82        .inner;
83    match before {
84        Some(theme) => crate::theme::set(ui.ctx(), theme),
85        None => crate::theme::clear(ui.ctx()),
86    }
87    out
88}
89