elegance/lib.rs
1//! Elegance — opinionated, beautiful widgets for egui.
2//!
3//! Elegance is a small companion crate to [`egui`] that provides a cohesive
4//! design system inspired by modern web UIs: chunky rounded buttons in a
5//! handful of accent colors, crisp inputs with a focus ring, pill-shaped
6//! status indicators, cards, tabs, segmented buttons, and a matching colour
7//! palette. Four palettes ship built-in — two dark
8//! ([`Theme::slate`] and [`Theme::charcoal`]) and two light
9//! ([`Theme::frost`] and [`Theme::paper`]) — paired so you can toggle without
10//! a layout shift.
11//!
12//! # Getting started
13//!
14//! ```no_run
15//! use eframe::egui;
16//! use elegance::{Theme, Button, Card, Accent};
17//!
18//! fn main() -> eframe::Result<()> {
19//! eframe::run_ui_native(
20//! "elegance demo",
21//! eframe::NativeOptions::default(),
22//! |ui, _| {
23//! Theme::slate().install(ui.ctx());
24//! egui::CentralPanel::default().show_inside(ui, |ui| {
25//! Card::new().heading("Hello").show(ui, |ui| {
26//! if ui.add(Button::new("Click me").accent(Accent::Blue))
27//! .clicked()
28//! {
29//! println!("clicked!");
30//! }
31//! });
32//! });
33//! },
34//! )
35//! }
36//! ```
37//!
38//! # Design
39//!
40//! All visuals are driven by a [`Theme`] value. Calling [`Theme::install`]
41//! once at startup configures [`egui::Style`] so that built-in widgets
42//! (labels, sliders, etc.) inherit the elegance look, and it stores the
43//! theme in `ctx` memory so elegance widgets can pick it up automatically.
44
45#![warn(missing_debug_implementations)]
46#![deny(missing_docs)]
47
48mod accordion;
49mod badge;
50mod button;
51mod callout;
52mod card;
53mod checkbox;
54mod collapsing;
55mod color_picker;
56mod drawer;
57mod file_drop_zone;
58mod flash;
59mod indicator;
60mod input;
61mod log_bar;
62mod menu;
63mod menu_bar;
64mod modal;
65mod multi_terminal;
66mod pairing;
67mod pill;
68mod popover;
69mod progress_bar;
70mod progress_ring;
71mod range_slider;
72mod segmented;
73mod select;
74mod slider;
75mod spinner;
76mod steps;
77mod switch;
78mod tabs;
79mod text_area;
80mod theme;
81mod theme_switcher;
82mod toast;
83mod tooltip;
84
85pub use accordion::{Accordion, AccordionItem, AccordionUi};
86pub use badge::{Badge, BadgeTone};
87pub use button::{Button, ButtonSize};
88pub use callout::{Callout, CalloutTone};
89pub use card::Card;
90pub use checkbox::Checkbox;
91pub use collapsing::CollapsingSection;
92pub use color_picker::ColorPicker;
93pub use drawer::{Drawer, DrawerSide};
94pub use file_drop_zone::{FileDropResponse, FileDropZone};
95pub use flash::{flash_error, flash_success, FlashKind, ResponseFlashExt, FLASH_DURATION};
96pub use indicator::{Indicator, IndicatorState};
97pub use input::TextInput;
98pub use log_bar::{LogBar, LogEntry, LogKind};
99pub use menu::{Menu, MenuItem, SubMenuItem};
100pub use menu_bar::{MenuBar, MenuBarUi};
101pub use modal::Modal;
102pub use multi_terminal::{
103 LineKind, MultiTerminal, TerminalEvent, TerminalLine, TerminalPane, TerminalStatus,
104};
105pub use pairing::{PairItem, Pairing};
106pub use pill::StatusPill;
107pub use popover::{Popover, PopoverSide};
108pub use progress_bar::ProgressBar;
109pub use progress_ring::ProgressRing;
110pub use range_slider::RangeSlider;
111pub use segmented::SegmentedButton;
112pub use select::Select;
113pub use slider::Slider;
114pub use spinner::Spinner;
115pub use steps::{Steps, StepsStyle};
116pub use switch::Switch;
117pub use tabs::TabBar;
118pub use text_area::TextArea;
119pub use theme::{Accent, BuiltInTheme, Palette, Theme, Typography};
120pub use theme_switcher::ThemeSwitcher;
121pub use toast::{Toast, Toasts};
122pub use tooltip::{Tooltip, TooltipSide};
123
124/// Re-export of [`egui`] for convenience.
125pub use egui;
126
127/// Stable codepoints for the icon glyphs bundled in the Elegance Symbols
128/// font. All icons are sourced from [Lucide](https://lucide.dev) and are
129/// kept in sync via `scripts/update_lucide_glyphs.py`. Use these in
130/// [`egui::RichText`] when you want one of elegance's icons in your own
131/// UI.
132///
133/// ```no_run
134/// # use elegance::glyphs;
135/// # egui::__run_test_ui(|ui| {
136/// ui.label(egui::RichText::new(glyphs::UPLOAD).size(24.0));
137/// # });
138/// ```
139pub mod glyphs {
140 /// Upload-tray icon. Source: [Lucide `upload`](https://lucide.dev/icons/upload).
141 pub const UPLOAD: char = '\u{E000}';
142 /// Download-tray icon. Source: [Lucide `download`](https://lucide.dev/icons/download).
143 pub const DOWNLOAD: char = '\u{E001}';
144 /// Search / magnifier icon. Source: [Lucide `search`](https://lucide.dev/icons/search).
145 pub const SEARCH: char = '\u{E002}';
146 /// Pin icon. Source: [Lucide `pin`](https://lucide.dev/icons/pin).
147 pub const PIN: char = '\u{E003}';
148 /// Copy / duplicate icon. Source: [Lucide `copy`](https://lucide.dev/icons/copy).
149 pub const COPY: char = '\u{E004}';
150 /// Circular alert icon. Source: [Lucide `circle-alert`](https://lucide.dev/icons/circle-alert).
151 pub const CIRCLE_ALERT: char = '\u{E005}';
152 /// Network / hub icon. Source: [Lucide `network`](https://lucide.dev/icons/network).
153 pub const NETWORK: char = '\u{E006}';
154 /// Check / done mark, mapped at standard U+2713 so plain `'✓'` literals
155 /// also pick up the elegance treatment.
156 /// Source: [Lucide `check`](https://lucide.dev/icons/check).
157 pub const CHECK: char = '\u{2713}';
158 /// Cross / dismiss mark, mapped at standard U+2717 so plain `'✗'` literals
159 /// also pick up the elegance treatment.
160 /// Source: [Lucide `x`](https://lucide.dev/icons/x).
161 pub const X: char = '\u{2717}';
162}
163
164/// Request a repaint such that the next paint comes ~`1/hz` seconds from now,
165/// independent of display refresh rate.
166///
167/// [`egui::Context::request_repaint_after`] internally subtracts `predicted_dt`
168/// from the requested delay to budget for the paint taking time. On a 60 Hz
169/// integration (egui's default) that subtraction is ~16.7 ms, so a naive
170/// `request_repaint_after(1/30 s)` lands on the very next vsync and produces
171/// ~60 Hz — double the rate you asked for. This helper adds `predicted_dt`
172/// back in so the effective cadence lands near `1/hz` on any refresh rate.
173///
174/// Typical use: throttle continuously-animating widgets (spinners, progress
175/// fills) to 20–30 Hz so they don't burn a full vsync budget on motion the
176/// eye can't resolve.
177#[track_caller]
178pub fn request_repaint_at_rate(ctx: &egui::Context, hz: f32) {
179 let pd = ctx.input(|i| i.predicted_dt);
180 if let Ok(d) = std::time::Duration::try_from_secs_f32(1.0 / hz + pd) {
181 ctx.request_repaint_after(d);
182 }
183}