1mod anchor;
10mod badges;
11mod change;
12mod clamped_text;
13mod drag;
14mod controls;
15mod field;
16mod layout;
17mod marquee;
18mod progress;
19mod panel;
20mod ring;
21mod scrim;
22mod selection;
23mod suggestions;
24mod wheel_zoom;
25
26pub use anchor::{anchored, Anchored};
27pub use badges::{chip, config_line, keycap, removable_chip};
28pub use change::{change_row, Change};
29pub use drag::{drag_card, drop_target_style, DragCard, DropLook};
30pub use clamped_text::{clamp, clamped_text, ClampedText, ELLIPSIS};
31pub use controls::{
32 dropdown_menu_style, dropdown_style, segment_style, segmented, segmented_choice,
33 slider_style, step_index, stepped_slider, toggle, toggle_style, value_slider, SegmentLook,
34};
35pub use field::{inset_field_style, inset_input_style, search_field, token_field};
36pub use layout::{
37 hero_card, hint_text, page_header, pending_bar, pending_label, setting_list, setting_row,
38 setting_row_style, status_dot, SETTING_ROW_GAP,
39};
40pub use marquee::{band_rect, edge_scroll, marquee, Marquee};
41pub use progress::{popover_card, progress_line, progress_line_style, stacked_bar};
42pub use panel::{fact, fact_row, panel_tabs};
43pub use ring::{countdown_ring, remaining_fraction};
44pub use scrim::{scrim, scrim_color, SCRIM_ALPHA};
45pub use selection::{section_label, selectable_row_style, spaced_caps, Tint};
46pub use suggestions::{suggestions, Suggestion};
47pub use wheel_zoom::{steps_from_pixels, wheel_zoom, WheelZoom, PIXELS_PER_STEP};
48
49use crate::theme::{self, spacing, surface, FontScale};
50use iced::widget::{button, column, container, row, text, text::IntoFragment, Text};
51use iced::{Background, Border, Color, Element, Length, Theme};
52
53pub fn scaled_text<'a>(
58 content: impl IntoFragment<'a>,
59 base_size: f32,
60 scale: FontScale,
61) -> Text<'a> {
62 text(content).size(scale.apply(base_size))
63}
64
65pub fn meta_text<'a>(content: impl IntoFragment<'a>, base_size: f32, scale: FontScale) -> Text<'a> {
69 scaled_text(content, base_size, scale).color(theme::text_dim())
70}
71
72fn card_style(_theme: &Theme) -> container::Style {
77 container::Style {
78 background: Some(Background::Color(surface::card())),
79 border: Border {
80 radius: crate::density::card_radius().into(),
81 width: 1.0,
82 color: surface::card_border(),
83 },
84 ..container::Style::default()
85 }
86}
87
88pub fn section<'a, Message: 'a>(
94 title: impl AsRef<str>,
95 scale: FontScale,
96 content: impl Into<Element<'a, Message>>,
97) -> Element<'a, Message> {
98 column![
99 section_label(title.as_ref(), scale),
102 container(content.into())
103 .padding(spacing::MD)
104 .width(Length::Fill)
105 .style(card_style)
106 ]
107 .spacing(spacing::SM)
108 .into()
109}
110
111pub fn divider<'a, Message: 'a>() -> Element<'a, Message> {
115 container(column![])
116 .width(Length::Fill)
117 .height(Length::Fixed(1.0))
118 .style(|_theme: &Theme| container::Style {
119 background: Some(Background::Color(surface::card_border())),
120 ..container::Style::default()
121 })
122 .into()
123}
124
125pub fn vertical_divider<'a, Message: 'a>() -> Element<'a, Message> {
129 container(column![])
130 .width(Length::Fixed(1.0))
131 .height(Length::Fill)
132 .style(|_theme: &Theme| container::Style {
133 background: Some(Background::Color(surface::card_border())),
134 ..container::Style::default()
135 })
136 .into()
137}
138
139const CONTROL_MAX_WIDTH: f32 = 340.0;
146
147pub fn row_field<'a, Message: 'a>(
159 label: impl IntoFragment<'a>,
160 control: impl Into<Element<'a, Message>>,
161) -> Element<'a, Message> {
162 let scale = FontScale(theme::active().font_scale);
163 setting_row(
164 1,
165 label,
166 None,
167 container(control.into())
173 .max_width(CONTROL_MAX_WIDTH)
174 .width(Length::Fill)
175 .align_x(iced::alignment::Horizontal::Right),
176 scale,
177 )
178}
179
180fn primary_style(theme: &Theme, status: button::Status) -> button::Style {
181 let palette = theme.extended_palette();
182 let base = button::Style {
183 background: Some(Background::Color(palette.primary.base.color)),
184 text_color: palette.primary.base.text,
185 border: Border {
186 radius: 8.0.into(),
187 ..Border::default()
188 },
189 ..button::Style::default()
190 };
191 match status {
192 button::Status::Hovered => button::Style {
193 background: Some(Background::Color(palette.primary.strong.color)),
194 ..base
195 },
196 button::Status::Disabled => button::Style {
197 background: base.background.map(|b| match b {
198 Background::Color(c) => Background::Color(Color { a: 0.4, ..c }),
199 other => other,
200 }),
201 text_color: Color {
202 a: 0.4,
203 ..base.text_color
204 },
205 ..base
206 },
207 _ => base,
208 }
209}
210
211fn secondary_style(_theme: &Theme, status: button::Status) -> button::Style {
212 let base = button::Style {
213 background: Some(Background::Color(surface::row())),
214 text_color: crate::theme::text(),
215 border: Border {
216 radius: 8.0.into(),
217 width: 1.0,
218 color: surface::card_border(),
219 },
220 ..button::Style::default()
221 };
222 match status {
223 button::Status::Hovered => button::Style {
224 background: Some(Background::Color(surface::card_border())),
225 ..base
226 },
227 button::Status::Disabled => button::Style {
228 text_color: Color {
229 a: 0.4,
230 ..base.text_color
231 },
232 ..base
233 },
234 _ => base,
235 }
236}
237
238fn danger_style(theme: &Theme, status: button::Status) -> button::Style {
239 let palette = theme.extended_palette();
240 let base = button::Style {
241 background: Some(Background::Color(surface::row())),
242 text_color: palette.danger.base.color,
243 border: Border {
244 radius: 8.0.into(),
245 width: 1.0,
246 color: palette.danger.base.color,
247 },
248 ..button::Style::default()
249 };
250 match status {
251 button::Status::Hovered => button::Style {
256 background: Some(Background::Color(palette.primary.strong.color)),
257 text_color: palette.primary.strong.text,
258 ..base
259 },
260 _ => base,
261 }
262}
263
264pub fn primary_button<'a, Message: Clone + 'a>(
267 label: impl IntoFragment<'a>,
268) -> button::Button<'a, Message> {
269 button(text(label)).style(primary_style)
270}
271
272pub fn secondary_button<'a, Message: Clone + 'a>(
275 label: impl IntoFragment<'a>,
276) -> button::Button<'a, Message> {
277 button(text(label)).style(secondary_style)
278}
279
280pub fn danger_button<'a, Message: Clone + 'a>(
284 label: &'a str,
285 on_press: Message,
286) -> Element<'a, Message> {
287 button(text(label))
288 .on_press(on_press)
289 .style(danger_style)
290 .into()
291}
292
293pub fn tri_state<'a, Message: Clone + 'a>(
298 value: Option<bool>,
299 on_select: impl Fn(Option<bool>) -> Message,
300) -> Element<'a, Message> {
301 let option = |label: &'a str, choice: Option<bool>| {
302 let button = if value == choice {
303 primary_button(label)
304 } else {
305 secondary_button(label)
306 };
307 button.on_press(on_select(choice))
308 };
309 row![
310 option("Any", None),
311 option("Yes", Some(true)),
312 option("No", Some(false)),
313 ]
314 .spacing(spacing::XS)
315 .into()
316}
317
318pub fn confirm_dialog<'a, Message: Clone + 'a>(
322 title: &'a str,
323 body: &'a str,
324 diff_text: &'a str,
325 on_confirm: Message,
326 on_cancel: Message,
327) -> Element<'a, Message> {
328 let diff = container(text(diff_text).font(crate::theme::mono_font()).size(13))
329 .padding(spacing::SM)
330 .width(Length::Fill)
331 .style(|_theme: &Theme| container::Style {
332 background: Some(Background::Color(surface::row())),
333 border: Border {
334 radius: 6.0.into(),
335 width: 1.0,
336 color: surface::card_border(),
337 },
338 ..container::Style::default()
339 });
340
341 container(
342 column![
343 text(title).size(18),
344 text(body),
345 diff,
346 row![
347 secondary_button("Cancel").on_press(on_cancel),
348 primary_button("Confirm").on_press(on_confirm),
349 ]
350 .spacing(spacing::SM),
351 ]
352 .spacing(spacing::MD)
353 .padding(spacing::LG)
354 .max_width(560.0),
355 )
356 .style(card_style)
357 .into()
358}
359
360
361
362#[cfg(test)]
363mod tests {
364 use super::*;
365
366 #[test]
370 fn a_primary_button_stays_the_accent_when_hovered() {
371 let t = theme::app_theme();
372 let palette = t.extended_palette();
373 for status in [button::Status::Active, button::Status::Hovered] {
374 let style = primary_style(&t, status);
375 assert_ne!(
376 style.background,
377 Some(Background::Color(palette.danger.base.color)),
378 "{status:?}"
379 );
380 }
381 assert_eq!(
382 primary_style(&t, button::Status::Hovered).background,
383 Some(Background::Color(palette.primary.strong.color))
384 );
385 }
386}