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