1mod anchor;
10mod badges;
11mod controls;
12mod field;
13mod layout;
14mod ring;
15mod selection;
16mod suggestions;
17
18pub use anchor::{anchored, Anchored};
19pub use badges::{chip, config_line, keycap};
20pub use controls::{
21 dropdown_menu_style, dropdown_style, segment_style, segmented, segmented_choice,
22 slider_style, step_index, stepped_slider, toggle, toggle_style, value_slider, SegmentLook,
23};
24pub use field::{inset_field_style, inset_input_style, search_field};
25pub use layout::{
26 hero_card, hint_text, page_header, pending_bar, pending_label, setting_list, setting_row,
27 setting_row_style, status_dot, SETTING_ROW_GAP,
28};
29pub use ring::{countdown_ring, remaining_fraction};
30pub use selection::{section_label, selectable_row_style, spaced_caps, Tint};
31pub use suggestions::{suggestions, Suggestion};
32
33use crate::theme::{self, spacing, surface, FontScale};
34use iced::widget::{button, column, container, row, text, text::IntoFragment, Text};
35use iced::{Background, Border, Color, Element, Length, Theme};
36
37pub fn scaled_text<'a>(
42 content: impl IntoFragment<'a>,
43 base_size: f32,
44 scale: FontScale,
45) -> Text<'a> {
46 text(content).size(scale.apply(base_size))
47}
48
49pub fn meta_text<'a>(content: impl IntoFragment<'a>, base_size: f32, scale: FontScale) -> Text<'a> {
53 scaled_text(content, base_size, scale).color(theme::text_dim())
54}
55
56fn card_style(_theme: &Theme) -> container::Style {
61 container::Style {
62 background: Some(Background::Color(surface::card())),
63 border: Border {
64 radius: crate::density::card_radius().into(),
65 width: 1.0,
66 color: surface::card_border(),
67 },
68 ..container::Style::default()
69 }
70}
71
72pub fn section<'a, Message: 'a>(
78 title: impl AsRef<str>,
79 scale: FontScale,
80 content: impl Into<Element<'a, Message>>,
81) -> Element<'a, Message> {
82 column![
83 section_label(title.as_ref(), scale),
86 container(content.into())
87 .padding(spacing::MD)
88 .width(Length::Fill)
89 .style(card_style)
90 ]
91 .spacing(spacing::SM)
92 .into()
93}
94
95pub fn divider<'a, Message: 'a>() -> Element<'a, Message> {
99 container(column![])
100 .width(Length::Fill)
101 .height(Length::Fixed(1.0))
102 .style(|_theme: &Theme| container::Style {
103 background: Some(Background::Color(surface::card_border())),
104 ..container::Style::default()
105 })
106 .into()
107}
108
109pub fn vertical_divider<'a, Message: 'a>() -> Element<'a, Message> {
113 container(column![])
114 .width(Length::Fixed(1.0))
115 .height(Length::Fill)
116 .style(|_theme: &Theme| container::Style {
117 background: Some(Background::Color(surface::card_border())),
118 ..container::Style::default()
119 })
120 .into()
121}
122
123const CONTROL_MAX_WIDTH: f32 = 340.0;
130
131pub fn row_field<'a, Message: 'a>(
143 label: impl IntoFragment<'a>,
144 control: impl Into<Element<'a, Message>>,
145) -> Element<'a, Message> {
146 let scale = FontScale(theme::active().font_scale);
147 setting_row(
148 1,
149 label,
150 None,
151 container(control.into())
157 .max_width(CONTROL_MAX_WIDTH)
158 .width(Length::Fill)
159 .align_x(iced::alignment::Horizontal::Right),
160 scale,
161 )
162}
163
164fn primary_style(theme: &Theme, status: button::Status) -> button::Style {
165 let palette = theme.extended_palette();
166 let base = button::Style {
167 background: Some(Background::Color(palette.primary.base.color)),
168 text_color: palette.primary.base.text,
169 border: Border {
170 radius: 8.0.into(),
171 ..Border::default()
172 },
173 ..button::Style::default()
174 };
175 match status {
176 button::Status::Hovered => button::Style {
177 background: Some(Background::Color(palette.primary.strong.color)),
178 ..base
179 },
180 button::Status::Disabled => button::Style {
181 background: base.background.map(|b| match b {
182 Background::Color(c) => Background::Color(Color { a: 0.4, ..c }),
183 other => other,
184 }),
185 text_color: Color {
186 a: 0.4,
187 ..base.text_color
188 },
189 ..base
190 },
191 _ => base,
192 }
193}
194
195fn secondary_style(_theme: &Theme, status: button::Status) -> button::Style {
196 let base = button::Style {
197 background: Some(Background::Color(surface::row())),
198 text_color: crate::theme::text(),
199 border: Border {
200 radius: 8.0.into(),
201 width: 1.0,
202 color: surface::card_border(),
203 },
204 ..button::Style::default()
205 };
206 match status {
207 button::Status::Hovered => button::Style {
208 background: Some(Background::Color(surface::card_border())),
209 ..base
210 },
211 button::Status::Disabled => button::Style {
212 text_color: Color {
213 a: 0.4,
214 ..base.text_color
215 },
216 ..base
217 },
218 _ => base,
219 }
220}
221
222fn danger_style(theme: &Theme, status: button::Status) -> button::Style {
223 let palette = theme.extended_palette();
224 let base = button::Style {
225 background: Some(Background::Color(surface::row())),
226 text_color: palette.danger.base.color,
227 border: Border {
228 radius: 8.0.into(),
229 width: 1.0,
230 color: palette.danger.base.color,
231 },
232 ..button::Style::default()
233 };
234 match status {
235 button::Status::Hovered => button::Style {
240 background: Some(Background::Color(palette.primary.strong.color)),
241 text_color: palette.primary.strong.text,
242 ..base
243 },
244 _ => base,
245 }
246}
247
248pub fn primary_button<'a, Message: Clone + 'a>(
251 label: impl IntoFragment<'a>,
252) -> button::Button<'a, Message> {
253 button(text(label)).style(primary_style)
254}
255
256pub fn secondary_button<'a, Message: Clone + 'a>(
259 label: impl IntoFragment<'a>,
260) -> button::Button<'a, Message> {
261 button(text(label)).style(secondary_style)
262}
263
264pub fn danger_button<'a, Message: Clone + 'a>(
268 label: &'a str,
269 on_press: Message,
270) -> Element<'a, Message> {
271 button(text(label))
272 .on_press(on_press)
273 .style(danger_style)
274 .into()
275}
276
277pub fn tri_state<'a, Message: Clone + 'a>(
282 value: Option<bool>,
283 on_select: impl Fn(Option<bool>) -> Message,
284) -> Element<'a, Message> {
285 let option = |label: &'a str, choice: Option<bool>| {
286 let button = if value == choice {
287 primary_button(label)
288 } else {
289 secondary_button(label)
290 };
291 button.on_press(on_select(choice))
292 };
293 row![
294 option("Any", None),
295 option("Yes", Some(true)),
296 option("No", Some(false)),
297 ]
298 .spacing(spacing::XS)
299 .into()
300}
301
302pub fn confirm_dialog<'a, Message: Clone + 'a>(
306 title: &'a str,
307 body: &'a str,
308 diff_text: &'a str,
309 on_confirm: Message,
310 on_cancel: Message,
311) -> Element<'a, Message> {
312 let diff = container(text(diff_text).font(crate::theme::mono_font()).size(13))
313 .padding(spacing::SM)
314 .width(Length::Fill)
315 .style(|_theme: &Theme| container::Style {
316 background: Some(Background::Color(surface::row())),
317 border: Border {
318 radius: 6.0.into(),
319 width: 1.0,
320 color: surface::card_border(),
321 },
322 ..container::Style::default()
323 });
324
325 container(
326 column![
327 text(title).size(18),
328 text(body),
329 diff,
330 row![
331 secondary_button("Cancel").on_press(on_cancel),
332 primary_button("Confirm").on_press(on_confirm),
333 ]
334 .spacing(spacing::SM),
335 ]
336 .spacing(spacing::MD)
337 .padding(spacing::LG)
338 .max_width(560.0),
339 )
340 .style(card_style)
341 .into()
342}
343
344
345
346#[cfg(test)]
347mod tests {
348 use super::*;
349
350 #[test]
354 fn a_primary_button_stays_the_accent_when_hovered() {
355 let t = theme::app_theme();
356 let palette = t.extended_palette();
357 for status in [button::Status::Active, button::Status::Hovered] {
358 let style = primary_style(&t, status);
359 assert_ne!(
360 style.background,
361 Some(Background::Color(palette.danger.base.color)),
362 "{status:?}"
363 );
364 }
365 assert_eq!(
366 primary_style(&t, button::Status::Hovered).background,
367 Some(Background::Color(palette.primary.strong.color))
368 );
369 }
370}