1mod anchor;
10mod badges;
11mod change;
12mod clamped_text;
13mod drag;
14mod controls;
15mod field;
16mod layout;
17mod marquee;
18mod menu;
19mod progress;
20mod panel;
21mod ring;
22mod scrim;
23mod selection;
24mod split;
25mod suggestions;
26mod wheel_zoom;
27
28pub use anchor::{anchored, Anchored};
29pub use badges::{chip, config_line, keycap, removable_chip};
30pub use change::{change_row, Change};
31pub use drag::{drag_card, drop_target_style, DragCard, DropLook};
32pub use clamped_text::{clamp, clamped_text, ClampedText, ELLIPSIS};
33pub use controls::{
34 dropdown_menu_style, dropdown_style, segment_style, segmented, segmented_choice,
35 slider_style, step_index, stepped_slider, toggle, toggle_style, value_slider, SegmentLook,
36};
37pub use field::{inset_field_style, inset_input_style, search_field, token_field};
38pub use layout::{
39 hero_card, hint_text, page_header, pending_bar, pending_label, setting_list, setting_row,
40 setting_row_style, status_dot, SETTING_ROW_GAP,
41};
42pub use marquee::{band_rect, edge_scroll, marquee, Marquee};
43pub use menu::{context_menu, menu_item_style, menu_overlay, menu_padding, menu_separator_height, menu_size, place_menu, pointer, MenuRow, MENU_SEPARATOR, MENU_WIDTH};
44pub use progress::{popover_card, progress_line, progress_line_style, stacked_bar};
45pub use panel::{fact, fact_row, panel_tabs};
46pub use ring::{countdown_ring, remaining_fraction};
47pub use scrim::{scrim, scrim_color, SCRIM_ALPHA};
48pub use selection::{section_label, selectable_row_style, spaced_caps, Tint};
49pub use split::{split_handle, split_ratio, SPLIT_HANDLE_WIDTH};
50pub use suggestions::{suggestions, Suggestion};
51pub use wheel_zoom::{steps_from_pixels, wheel_zoom, WheelZoom, PIXELS_PER_STEP};
52
53use crate::theme::{self, spacing, surface, FontScale};
54use iced::widget::{button, column, container, row, text, text::IntoFragment, Text};
55use iced::{Background, Border, Color, Element, Length, Theme};
56
57pub fn scaled_text<'a>(
62 content: impl IntoFragment<'a>,
63 base_size: f32,
64 scale: FontScale,
65) -> Text<'a> {
66 text(content).size(scale.apply(base_size))
67}
68
69pub fn meta_text<'a>(content: impl IntoFragment<'a>, base_size: f32, scale: FontScale) -> Text<'a> {
73 scaled_text(content, base_size, scale).color(theme::text_dim())
74}
75
76fn card_style(_theme: &Theme) -> container::Style {
81 container::Style {
82 background: Some(Background::Color(surface::card())),
83 border: Border {
84 radius: crate::density::card_radius().into(),
85 width: 1.0,
86 color: surface::card_border(),
87 },
88 ..container::Style::default()
89 }
90}
91
92pub fn section<'a, Message: 'a>(
98 title: impl AsRef<str>,
99 scale: FontScale,
100 content: impl Into<Element<'a, Message>>,
101) -> Element<'a, Message> {
102 column![
103 section_label(title.as_ref(), scale),
106 container(content.into())
107 .padding(spacing::MD)
108 .width(Length::Fill)
109 .style(card_style)
110 ]
111 .spacing(spacing::SM)
112 .into()
113}
114
115pub fn divider<'a, Message: 'a>() -> Element<'a, Message> {
119 container(column![])
120 .width(Length::Fill)
121 .height(Length::Fixed(1.0))
122 .style(|_theme: &Theme| container::Style {
123 background: Some(Background::Color(surface::card_border())),
124 ..container::Style::default()
125 })
126 .into()
127}
128
129pub fn vertical_divider<'a, Message: 'a>() -> Element<'a, Message> {
133 container(column![])
134 .width(Length::Fixed(1.0))
135 .height(Length::Fill)
136 .style(|_theme: &Theme| container::Style {
137 background: Some(Background::Color(surface::card_border())),
138 ..container::Style::default()
139 })
140 .into()
141}
142
143const CONTROL_MAX_WIDTH: f32 = 340.0;
150
151pub fn row_field<'a, Message: 'a>(
163 label: impl IntoFragment<'a>,
164 control: impl Into<Element<'a, Message>>,
165) -> Element<'a, Message> {
166 let scale = FontScale(theme::active().font_scale);
167 setting_row(
168 1,
169 label,
170 None,
171 container(control.into())
177 .max_width(CONTROL_MAX_WIDTH)
178 .width(Length::Fill)
179 .align_x(iced::alignment::Horizontal::Right),
180 scale,
181 )
182}
183
184fn primary_style(theme: &Theme, status: button::Status) -> button::Style {
185 let palette = theme.extended_palette();
186 let base = button::Style {
187 background: Some(Background::Color(palette.primary.base.color)),
188 text_color: palette.primary.base.text,
189 border: Border {
190 radius: 8.0.into(),
191 ..Border::default()
192 },
193 ..button::Style::default()
194 };
195 match status {
196 button::Status::Hovered => button::Style {
197 background: Some(Background::Color(palette.primary.strong.color)),
198 ..base
199 },
200 button::Status::Disabled => button::Style {
201 background: base.background.map(|b| match b {
202 Background::Color(c) => Background::Color(Color { a: 0.4, ..c }),
203 other => other,
204 }),
205 text_color: Color {
206 a: 0.4,
207 ..base.text_color
208 },
209 ..base
210 },
211 _ => base,
212 }
213}
214
215fn secondary_style(_theme: &Theme, status: button::Status) -> button::Style {
216 let base = button::Style {
217 background: Some(Background::Color(surface::row())),
218 text_color: crate::theme::text(),
219 border: Border {
220 radius: 8.0.into(),
221 width: 1.0,
222 color: surface::card_border(),
223 },
224 ..button::Style::default()
225 };
226 match status {
227 button::Status::Hovered => button::Style {
228 background: Some(Background::Color(surface::card_border())),
229 ..base
230 },
231 button::Status::Disabled => button::Style {
232 text_color: Color {
233 a: 0.4,
234 ..base.text_color
235 },
236 ..base
237 },
238 _ => base,
239 }
240}
241
242fn danger_style(theme: &Theme, status: button::Status) -> button::Style {
243 let palette = theme.extended_palette();
244 let base = button::Style {
245 background: Some(Background::Color(surface::row())),
246 text_color: palette.danger.base.color,
247 border: Border {
248 radius: 8.0.into(),
249 width: 1.0,
250 color: palette.danger.base.color,
251 },
252 ..button::Style::default()
253 };
254 match status {
255 button::Status::Hovered => button::Style {
260 background: Some(Background::Color(palette.primary.strong.color)),
261 text_color: palette.primary.strong.text,
262 ..base
263 },
264 _ => base,
265 }
266}
267
268pub fn primary_button<'a, Message: Clone + 'a>(
271 label: impl IntoFragment<'a>,
272) -> button::Button<'a, Message> {
273 button(text(label)).style(primary_style)
274}
275
276pub fn secondary_button<'a, Message: Clone + 'a>(
279 label: impl IntoFragment<'a>,
280) -> button::Button<'a, Message> {
281 button(text(label)).style(secondary_style)
282}
283
284pub fn danger_button<'a, Message: Clone + 'a>(
288 label: &'a str,
289 on_press: Message,
290) -> Element<'a, Message> {
291 button(text(label))
292 .on_press(on_press)
293 .style(danger_style)
294 .into()
295}
296
297pub fn tri_state<'a, Message: Clone + 'a>(
302 value: Option<bool>,
303 on_select: impl Fn(Option<bool>) -> Message,
304) -> Element<'a, Message> {
305 let option = |label: &'a str, choice: Option<bool>| {
306 let button = if value == choice {
307 primary_button(label)
308 } else {
309 secondary_button(label)
310 };
311 button.on_press(on_select(choice))
312 };
313 row![
314 option("Any", None),
315 option("Yes", Some(true)),
316 option("No", Some(false)),
317 ]
318 .spacing(spacing::XS)
319 .into()
320}
321
322pub fn confirm_dialog<'a, Message: Clone + 'a>(
326 title: &'a str,
327 body: &'a str,
328 diff_text: &'a str,
329 on_confirm: Message,
330 on_cancel: Message,
331) -> Element<'a, Message> {
332 let diff = container(text(diff_text).font(crate::theme::mono_font()).size(13))
333 .padding(spacing::SM)
334 .width(Length::Fill)
335 .style(|_theme: &Theme| container::Style {
336 background: Some(Background::Color(surface::row())),
337 border: Border {
338 radius: 6.0.into(),
339 width: 1.0,
340 color: surface::card_border(),
341 },
342 ..container::Style::default()
343 });
344
345 container(
346 column![
347 text(title).size(18),
348 text(body),
349 diff,
350 row![
351 secondary_button("Cancel").on_press(on_cancel),
352 primary_button("Confirm").on_press(on_confirm),
353 ]
354 .spacing(spacing::SM),
355 ]
356 .spacing(spacing::MD)
357 .padding(spacing::LG)
358 .max_width(560.0),
359 )
360 .style(card_style)
361 .into()
362}
363
364
365
366#[cfg(test)]
367mod tests {
368 use super::*;
369
370 #[test]
374 fn a_primary_button_stays_the_accent_when_hovered() {
375 let t = theme::app_theme();
376 let palette = t.extended_palette();
377 for status in [button::Status::Active, button::Status::Hovered] {
378 let style = primary_style(&t, status);
379 assert_ne!(
380 style.background,
381 Some(Background::Color(palette.danger.base.color)),
382 "{status:?}"
383 );
384 }
385 assert_eq!(
386 primary_style(&t, button::Status::Hovered).background,
387 Some(Background::Color(palette.primary.strong.color))
388 );
389 }
390}