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
109const CONTROL_MAX_WIDTH: f32 = 340.0;
116
117pub fn row_field<'a, Message: 'a>(
129 label: impl IntoFragment<'a>,
130 control: impl Into<Element<'a, Message>>,
131) -> Element<'a, Message> {
132 let scale = FontScale(theme::active().font_scale);
133 setting_row(
134 1,
135 label,
136 None,
137 container(control.into())
143 .max_width(CONTROL_MAX_WIDTH)
144 .width(Length::Fill)
145 .align_x(iced::alignment::Horizontal::Right),
146 scale,
147 )
148}
149
150fn primary_style(theme: &Theme, status: button::Status) -> button::Style {
151 let palette = theme.extended_palette();
152 let base = button::Style {
153 background: Some(Background::Color(palette.primary.base.color)),
154 text_color: palette.primary.base.text,
155 border: Border {
156 radius: 8.0.into(),
157 ..Border::default()
158 },
159 ..button::Style::default()
160 };
161 match status {
162 button::Status::Hovered => button::Style {
163 background: Some(Background::Color(palette.primary.strong.color)),
164 ..base
165 },
166 button::Status::Disabled => button::Style {
167 background: base.background.map(|b| match b {
168 Background::Color(c) => Background::Color(Color { a: 0.4, ..c }),
169 other => other,
170 }),
171 text_color: Color {
172 a: 0.4,
173 ..base.text_color
174 },
175 ..base
176 },
177 _ => base,
178 }
179}
180
181fn secondary_style(_theme: &Theme, status: button::Status) -> button::Style {
182 let base = button::Style {
183 background: Some(Background::Color(surface::row())),
184 text_color: crate::theme::text(),
185 border: Border {
186 radius: 8.0.into(),
187 width: 1.0,
188 color: surface::card_border(),
189 },
190 ..button::Style::default()
191 };
192 match status {
193 button::Status::Hovered => button::Style {
194 background: Some(Background::Color(surface::card_border())),
195 ..base
196 },
197 button::Status::Disabled => button::Style {
198 text_color: Color {
199 a: 0.4,
200 ..base.text_color
201 },
202 ..base
203 },
204 _ => base,
205 }
206}
207
208fn danger_style(theme: &Theme, status: button::Status) -> button::Style {
209 let palette = theme.extended_palette();
210 let base = button::Style {
211 background: Some(Background::Color(surface::row())),
212 text_color: palette.danger.base.color,
213 border: Border {
214 radius: 8.0.into(),
215 width: 1.0,
216 color: palette.danger.base.color,
217 },
218 ..button::Style::default()
219 };
220 match status {
221 button::Status::Hovered => button::Style {
226 background: Some(Background::Color(palette.primary.strong.color)),
227 text_color: palette.primary.strong.text,
228 ..base
229 },
230 _ => base,
231 }
232}
233
234pub fn primary_button<'a, Message: Clone + 'a>(
237 label: impl IntoFragment<'a>,
238) -> button::Button<'a, Message> {
239 button(text(label)).style(primary_style)
240}
241
242pub fn secondary_button<'a, Message: Clone + 'a>(
245 label: impl IntoFragment<'a>,
246) -> button::Button<'a, Message> {
247 button(text(label)).style(secondary_style)
248}
249
250pub fn danger_button<'a, Message: Clone + 'a>(
254 label: &'a str,
255 on_press: Message,
256) -> Element<'a, Message> {
257 button(text(label))
258 .on_press(on_press)
259 .style(danger_style)
260 .into()
261}
262
263pub fn tri_state<'a, Message: Clone + 'a>(
268 value: Option<bool>,
269 on_select: impl Fn(Option<bool>) -> Message,
270) -> Element<'a, Message> {
271 let option = |label: &'a str, choice: Option<bool>| {
272 let button = if value == choice {
273 primary_button(label)
274 } else {
275 secondary_button(label)
276 };
277 button.on_press(on_select(choice))
278 };
279 row![
280 option("Any", None),
281 option("Yes", Some(true)),
282 option("No", Some(false)),
283 ]
284 .spacing(spacing::XS)
285 .into()
286}
287
288pub fn confirm_dialog<'a, Message: Clone + 'a>(
292 title: &'a str,
293 body: &'a str,
294 diff_text: &'a str,
295 on_confirm: Message,
296 on_cancel: Message,
297) -> Element<'a, Message> {
298 let diff = container(text(diff_text).font(crate::theme::mono_font()).size(13))
299 .padding(spacing::SM)
300 .width(Length::Fill)
301 .style(|_theme: &Theme| container::Style {
302 background: Some(Background::Color(surface::row())),
303 border: Border {
304 radius: 6.0.into(),
305 width: 1.0,
306 color: surface::card_border(),
307 },
308 ..container::Style::default()
309 });
310
311 container(
312 column![
313 text(title).size(18),
314 text(body),
315 diff,
316 row![
317 secondary_button("Cancel").on_press(on_cancel),
318 primary_button("Confirm").on_press(on_confirm),
319 ]
320 .spacing(spacing::SM),
321 ]
322 .spacing(spacing::MD)
323 .padding(spacing::LG)
324 .max_width(560.0),
325 )
326 .style(card_style)
327 .into()
328}
329
330
331
332#[cfg(test)]
333mod tests {
334 use super::*;
335
336 #[test]
340 fn a_primary_button_stays_the_accent_when_hovered() {
341 let t = theme::app_theme();
342 let palette = t.extended_palette();
343 for status in [button::Status::Active, button::Status::Hovered] {
344 let style = primary_style(&t, status);
345 assert_ne!(
346 style.background,
347 Some(Background::Color(palette.danger.base.color)),
348 "{status:?}"
349 );
350 }
351 assert_eq!(
352 primary_style(&t, button::Status::Hovered).background,
353 Some(Background::Color(palette.primary.strong.color))
354 );
355 }
356}