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