1mod badges;
8mod controls;
9mod field;
10mod layout;
11mod ring;
12mod selection;
13
14pub use badges::{chip, config_line, keycap};
15pub use controls::{
16 dropdown_menu_style, dropdown_style, segment_style, segmented, segmented_choice,
17 slider_style, step_index, stepped_slider, toggle, toggle_style, value_slider, SegmentLook,
18};
19pub use field::{inset_field_style, inset_input_style, search_field};
20pub use layout::{
21 hero_card, hint_text, page_header, pending_bar, pending_label, setting_list, setting_row,
22 setting_row_style, status_dot, SETTING_ROW_GAP,
23};
24pub use ring::{countdown_ring, remaining_fraction};
25pub use selection::{section_label, selectable_row_style, spaced_caps, Tint};
26
27use crate::theme::{self, spacing, surface, FontScale};
28use iced::widget::{button, column, container, row, text, text::IntoFragment, Text};
29use iced::{Background, Border, Color, Element, Length, Theme};
30
31pub fn scaled_text<'a>(
36 content: impl IntoFragment<'a>,
37 base_size: f32,
38 scale: FontScale,
39) -> Text<'a> {
40 text(content).size(scale.apply(base_size))
41}
42
43pub fn meta_text<'a>(content: impl IntoFragment<'a>, base_size: f32, scale: FontScale) -> Text<'a> {
47 scaled_text(content, base_size, scale).color(theme::text_dim())
48}
49
50fn card_style(_theme: &Theme) -> container::Style {
55 container::Style {
56 background: Some(Background::Color(surface::card())),
57 border: Border {
58 radius: crate::density::card_radius().into(),
59 width: 1.0,
60 color: surface::card_border(),
61 },
62 ..container::Style::default()
63 }
64}
65
66pub fn section<'a, Message: 'a>(
72 title: impl AsRef<str>,
73 scale: FontScale,
74 content: impl Into<Element<'a, Message>>,
75) -> Element<'a, Message> {
76 column![
77 section_label(title.as_ref(), scale),
80 container(content.into())
81 .padding(spacing::MD)
82 .width(Length::Fill)
83 .style(card_style)
84 ]
85 .spacing(spacing::SM)
86 .into()
87}
88
89pub fn divider<'a, Message: 'a>() -> Element<'a, Message> {
93 container(column![])
94 .width(Length::Fill)
95 .height(Length::Fixed(1.0))
96 .style(|_theme: &Theme| container::Style {
97 background: Some(Background::Color(surface::card_border())),
98 ..container::Style::default()
99 })
100 .into()
101}
102
103const CONTROL_MAX_WIDTH: f32 = 340.0;
110
111pub fn row_field<'a, Message: 'a>(
123 label: impl IntoFragment<'a>,
124 control: impl Into<Element<'a, Message>>,
125) -> Element<'a, Message> {
126 let scale = FontScale(theme::active().font_scale);
127 setting_row(
128 1,
129 label,
130 None,
131 container(control.into())
137 .max_width(CONTROL_MAX_WIDTH)
138 .width(Length::Fill)
139 .align_x(iced::alignment::Horizontal::Right),
140 scale,
141 )
142}
143
144fn primary_style(theme: &Theme, status: button::Status) -> button::Style {
145 let palette = theme.extended_palette();
146 let base = button::Style {
147 background: Some(Background::Color(palette.primary.base.color)),
148 text_color: palette.primary.base.text,
149 border: Border {
150 radius: 8.0.into(),
151 ..Border::default()
152 },
153 ..button::Style::default()
154 };
155 match status {
156 button::Status::Hovered => button::Style {
157 background: Some(Background::Color(palette.primary.strong.color)),
158 ..base
159 },
160 button::Status::Disabled => button::Style {
161 background: base.background.map(|b| match b {
162 Background::Color(c) => Background::Color(Color { a: 0.4, ..c }),
163 other => other,
164 }),
165 text_color: Color {
166 a: 0.4,
167 ..base.text_color
168 },
169 ..base
170 },
171 _ => base,
172 }
173}
174
175fn secondary_style(_theme: &Theme, status: button::Status) -> button::Style {
176 let base = button::Style {
177 background: Some(Background::Color(surface::row())),
178 text_color: crate::theme::text(),
179 border: Border {
180 radius: 8.0.into(),
181 width: 1.0,
182 color: surface::card_border(),
183 },
184 ..button::Style::default()
185 };
186 match status {
187 button::Status::Hovered => button::Style {
188 background: Some(Background::Color(surface::card_border())),
189 ..base
190 },
191 button::Status::Disabled => button::Style {
192 text_color: Color {
193 a: 0.4,
194 ..base.text_color
195 },
196 ..base
197 },
198 _ => base,
199 }
200}
201
202fn danger_style(theme: &Theme, status: button::Status) -> button::Style {
203 let palette = theme.extended_palette();
204 let base = button::Style {
205 background: Some(Background::Color(surface::row())),
206 text_color: palette.danger.base.color,
207 border: Border {
208 radius: 8.0.into(),
209 width: 1.0,
210 color: palette.danger.base.color,
211 },
212 ..button::Style::default()
213 };
214 match status {
215 button::Status::Hovered => button::Style {
220 background: Some(Background::Color(palette.primary.strong.color)),
221 text_color: palette.primary.strong.text,
222 ..base
223 },
224 _ => base,
225 }
226}
227
228pub fn primary_button<'a, Message: Clone + 'a>(
231 label: impl IntoFragment<'a>,
232) -> button::Button<'a, Message> {
233 button(text(label)).style(primary_style)
234}
235
236pub fn secondary_button<'a, Message: Clone + 'a>(
239 label: impl IntoFragment<'a>,
240) -> button::Button<'a, Message> {
241 button(text(label)).style(secondary_style)
242}
243
244pub fn danger_button<'a, Message: Clone + 'a>(
248 label: &'a str,
249 on_press: Message,
250) -> Element<'a, Message> {
251 button(text(label))
252 .on_press(on_press)
253 .style(danger_style)
254 .into()
255}
256
257pub fn tri_state<'a, Message: Clone + 'a>(
262 value: Option<bool>,
263 on_select: impl Fn(Option<bool>) -> Message,
264) -> Element<'a, Message> {
265 let option = |label: &'a str, choice: Option<bool>| {
266 let button = if value == choice {
267 primary_button(label)
268 } else {
269 secondary_button(label)
270 };
271 button.on_press(on_select(choice))
272 };
273 row![
274 option("Any", None),
275 option("Yes", Some(true)),
276 option("No", Some(false)),
277 ]
278 .spacing(spacing::XS)
279 .into()
280}
281
282pub fn confirm_dialog<'a, Message: Clone + 'a>(
286 title: &'a str,
287 body: &'a str,
288 diff_text: &'a str,
289 on_confirm: Message,
290 on_cancel: Message,
291) -> Element<'a, Message> {
292 let diff = container(text(diff_text).font(crate::theme::mono_font()).size(13))
293 .padding(spacing::SM)
294 .width(Length::Fill)
295 .style(|_theme: &Theme| container::Style {
296 background: Some(Background::Color(surface::row())),
297 border: Border {
298 radius: 6.0.into(),
299 width: 1.0,
300 color: surface::card_border(),
301 },
302 ..container::Style::default()
303 });
304
305 container(
306 column![
307 text(title).size(18),
308 text(body),
309 diff,
310 row![
311 secondary_button("Cancel").on_press(on_cancel),
312 primary_button("Confirm").on_press(on_confirm),
313 ]
314 .spacing(spacing::SM),
315 ]
316 .spacing(spacing::MD)
317 .padding(spacing::LG)
318 .max_width(560.0),
319 )
320 .style(card_style)
321 .into()
322}
323
324
325
326#[cfg(test)]
327mod tests {
328 use super::*;
329
330 #[test]
334 fn a_primary_button_stays_the_accent_when_hovered() {
335 let t = theme::app_theme();
336 let palette = t.extended_palette();
337 for status in [button::Status::Active, button::Status::Hovered] {
338 let style = primary_style(&t, status);
339 assert_ne!(
340 style.background,
341 Some(Background::Color(palette.danger.base.color)),
342 "{status:?}"
343 );
344 }
345 assert_eq!(
346 primary_style(&t, button::Status::Hovered).background,
347 Some(Background::Color(palette.primary.strong.color))
348 );
349 }
350}