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