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