Skip to main content

cosmic/theme/style/
iced.rs

1// Copyright 2022 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4//! Contains stylesheet implementations for widgets native to iced.
5
6use crate::theme::{CosmicComponent, TRANSPARENT_COMPONENT, Theme};
7use cosmic_theme::composite::over;
8use iced::overlay::menu;
9use iced::theme::Base;
10use iced::widget::slider::{self, Rail};
11use iced::widget::{
12    button as iced_button, checkbox as iced_checkbox, combo_box, container as iced_container,
13    pane_grid, pick_list, progress_bar, radio, rule, scrollable, svg, toggler,
14};
15use iced_core::{Background, Border, Color, Shadow, Vector};
16use iced_widget::pane_grid::Highlight;
17use iced_widget::scrollable::AutoScroll;
18use iced_widget::{text_editor, text_input};
19use palette::WithAlpha;
20use std::rc::Rc;
21
22pub mod application {
23    use crate::Theme;
24    use iced_runtime::Appearance;
25
26    #[derive(Default)]
27    pub enum Application {
28        #[default]
29        Default,
30        Custom(Box<dyn Fn(&Theme) -> Appearance>),
31    }
32
33    impl Application {
34        pub fn custom<F: Fn(&Theme) -> Appearance + 'static>(f: F) -> Self {
35            Self::Custom(Box::new(f))
36        }
37    }
38
39    pub fn style(theme: &Theme) -> iced::theme::Style {
40        let cosmic = theme.cosmic();
41
42        iced::theme::Style {
43            background_color: cosmic.bg_color().into(),
44            text_color: cosmic.on_bg_color().into(),
45            icon_color: cosmic.on_bg_color().into(),
46        }
47    }
48}
49
50/// Styles for the button widget from iced-rs.
51#[derive(Default)]
52pub enum Button {
53    Deactivated,
54    Destructive,
55    Positive,
56    #[default]
57    Primary,
58    Secondary,
59    Text,
60    Link,
61    LinkActive,
62    Transparent,
63    Card,
64    Custom(Box<dyn Fn(&Theme, iced_button::Status) -> iced_button::Style>),
65}
66
67impl iced_button::Catalog for Theme {
68    type Class<'a> = Button;
69
70    fn default<'a>() -> Self::Class<'a> {
71        Button::default()
72    }
73
74    fn style(&self, class: &Self::Class<'_>, status: iced_button::Status) -> iced_button::Style {
75        if let Button::Custom(f) = class {
76            return f(self, status);
77        }
78        let cosmic = self.cosmic();
79        let corner_radii = &cosmic.corner_radii;
80        let component = class.cosmic(self);
81
82        let mut appearance = iced_button::Style {
83            border_radius: match class {
84                Button::Link => corner_radii.radius_0.into(),
85                Button::Card => corner_radii.radius_xs.into(),
86                _ => corner_radii.radius_xl.into(),
87            },
88            border: Border {
89                radius: match class {
90                    Button::Link => corner_radii.radius_0.into(),
91                    Button::Card => corner_radii.radius_xs.into(),
92                    _ => corner_radii.radius_xl.into(),
93                },
94                ..Default::default()
95            },
96            background: match class {
97                Button::Link | Button::Text => None,
98                Button::LinkActive => Some(Background::Color(component.divider.into())),
99                _ => Some(Background::Color(component.base.into())),
100            },
101            text_color: match class {
102                Button::Link | Button::LinkActive => component.base.into(),
103                _ => component.on.into(),
104            },
105            ..iced_button::Style::default()
106        };
107
108        match status {
109            iced_button::Status::Active => {}
110            iced_button::Status::Hovered => {
111                appearance.background = match class {
112                    Button::Link => None,
113                    Button::LinkActive => Some(Background::Color(component.divider.into())),
114                    _ => Some(Background::Color(component.hover.into())),
115                };
116            }
117            iced_button::Status::Pressed => {
118                appearance.background = match class {
119                    Button::Link => None,
120                    Button::LinkActive => Some(Background::Color(component.divider.into())),
121                    _ => Some(Background::Color(component.pressed.into())),
122                };
123            }
124            iced_button::Status::Disabled => {
125                // Card color is not transparent when it isn't clickable
126                if matches!(class, Button::Card) {
127                    return appearance;
128                }
129                appearance.background = appearance.background.map(|background| match background {
130                    Background::Color(color) => Background::Color(Color {
131                        a: color.a * 0.5,
132                        ..color
133                    }),
134                    Background::Gradient(gradient) => {
135                        Background::Gradient(gradient.scale_alpha(0.5))
136                    }
137                });
138                appearance.text_color = Color {
139                    a: appearance.text_color.a * 0.5,
140                    ..appearance.text_color
141                };
142            }
143        };
144        appearance
145    }
146}
147
148impl Button {
149    #[allow(clippy::trivially_copy_pass_by_ref)]
150    #[allow(clippy::match_same_arms)]
151    fn cosmic<'a>(&'a self, theme: &'a Theme) -> &'a CosmicComponent {
152        let cosmic = theme.cosmic();
153        match self {
154            Self::Primary => &cosmic.accent_button,
155            Self::Secondary => &theme.current_container().component,
156            Self::Positive => &cosmic.success_button,
157            Self::Destructive => &cosmic.destructive_button,
158            Self::Text => &cosmic.text_button,
159            Self::Link => &cosmic.link_button,
160            Self::LinkActive => &cosmic.link_button,
161            Self::Transparent => &TRANSPARENT_COMPONENT,
162            Self::Deactivated => &theme.current_container().component,
163            Self::Card => &theme.current_container().component,
164            Self::Custom { .. } => &TRANSPARENT_COMPONENT,
165        }
166    }
167}
168
169/*
170 * TODO: Checkbox
171 */
172#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
173pub enum Checkbox {
174    #[default]
175    Primary,
176    Secondary,
177    Success,
178    Danger,
179}
180
181impl iced_checkbox::Catalog for Theme {
182    type Class<'a> = Checkbox;
183
184    fn default<'a>() -> Self::Class<'a> {
185        Checkbox::default()
186    }
187
188    #[allow(clippy::too_many_lines)]
189    fn style(
190        &self,
191        class: &Self::Class<'_>,
192        status: iced_checkbox::Status,
193    ) -> iced_checkbox::Style {
194        let cosmic = self.cosmic();
195
196        let corners = &cosmic.corner_radii;
197
198        let disabled = matches!(status, iced_checkbox::Status::Disabled { .. });
199        match status {
200            iced_checkbox::Status::Active { is_checked }
201            | iced_checkbox::Status::Disabled { is_checked } => {
202                let mut active = match class {
203                    Checkbox::Primary => iced_checkbox::Style {
204                        background: Background::Color(if is_checked {
205                            cosmic.accent.base.into()
206                        } else {
207                            self.current_container().small_widget.into()
208                        }),
209                        icon_color: cosmic.accent.on.into(),
210                        border: Border {
211                            radius: corners.radius_xs.into(),
212                            width: if is_checked { 0.0 } else { 1.0 },
213                            color: if is_checked {
214                                cosmic.accent.base
215                            } else {
216                                cosmic.palette.neutral_8
217                            }
218                            .into(),
219                        },
220
221                        text_color: None,
222                    },
223                    Checkbox::Secondary => iced_checkbox::Style {
224                        background: Background::Color(if is_checked {
225                            cosmic.background(self.transparent).component.base.into()
226                        } else {
227                            self.current_container().small_widget.into()
228                        }),
229                        icon_color: cosmic.background(self.transparent).on.into(),
230                        border: Border {
231                            radius: corners.radius_xs.into(),
232                            width: if is_checked { 0.0 } else { 1.0 },
233                            color: cosmic.palette.neutral_8.into(),
234                        },
235                        text_color: None,
236                    },
237                    Checkbox::Success => iced_checkbox::Style {
238                        background: Background::Color(if is_checked {
239                            cosmic.success.base.into()
240                        } else {
241                            self.current_container().small_widget.into()
242                        }),
243                        icon_color: cosmic.success.on.into(),
244                        border: Border {
245                            radius: corners.radius_xs.into(),
246                            width: if is_checked { 0.0 } else { 1.0 },
247                            color: if is_checked {
248                                cosmic.success.base
249                            } else {
250                                cosmic.palette.neutral_8
251                            }
252                            .into(),
253                        },
254                        text_color: None,
255                    },
256                    Checkbox::Danger => iced_checkbox::Style {
257                        background: Background::Color(if is_checked {
258                            cosmic.destructive.base.into()
259                        } else {
260                            self.current_container().small_widget.into()
261                        }),
262                        icon_color: cosmic.destructive.on.into(),
263                        border: Border {
264                            radius: corners.radius_xs.into(),
265                            width: if is_checked { 0.0 } else { 1.0 },
266                            color: if is_checked {
267                                cosmic.destructive.base
268                            } else {
269                                cosmic.palette.neutral_8
270                            }
271                            .into(),
272                        },
273                        text_color: None,
274                    },
275                };
276                if disabled {
277                    match &mut active.background {
278                        Background::Color(color) => {
279                            color.a /= 2.;
280                        }
281                        Background::Gradient(gradient) => {
282                            *gradient = gradient.scale_alpha(0.5);
283                        }
284                    }
285                    if let Some(c) = active.text_color.as_mut() {
286                        c.a /= 2.
287                    };
288                    active.border.color.a /= 2.;
289                }
290                active
291            }
292            iced_checkbox::Status::Hovered { is_checked } => {
293                let cur_container = self.current_container().small_widget;
294                // TODO: this should probably be done with a custom widget instead, or the theme needs more small widget variables.
295                let hovered_bg = over(cosmic.palette.neutral_0.with_alpha(0.1), cur_container);
296                match class {
297                    Checkbox::Primary => iced_checkbox::Style {
298                        background: Background::Color(if is_checked {
299                            cosmic.accent.hover_state_color().into()
300                        } else {
301                            hovered_bg.into()
302                        }),
303                        icon_color: cosmic.accent.on.into(),
304                        border: Border {
305                            radius: corners.radius_xs.into(),
306                            width: if is_checked { 0.0 } else { 1.0 },
307                            color: if is_checked {
308                                cosmic.accent.base
309                            } else {
310                                cosmic.palette.neutral_8
311                            }
312                            .into(),
313                        },
314                        text_color: None,
315                    },
316                    Checkbox::Secondary => iced_checkbox::Style {
317                        background: Background::Color(if is_checked {
318                            self.current_container().component.hover.into()
319                        } else {
320                            hovered_bg.into()
321                        }),
322                        icon_color: self.current_container().on.into(),
323                        border: Border {
324                            radius: corners.radius_xs.into(),
325                            width: if is_checked { 0.0 } else { 1.0 },
326                            color: if is_checked {
327                                self.current_container().base
328                            } else {
329                                cosmic.palette.neutral_8
330                            }
331                            .into(),
332                        },
333                        text_color: None,
334                    },
335                    Checkbox::Success => iced_checkbox::Style {
336                        background: Background::Color(if is_checked {
337                            cosmic.success.hover.into()
338                        } else {
339                            hovered_bg.into()
340                        }),
341                        icon_color: cosmic.success.on.into(),
342                        border: Border {
343                            radius: corners.radius_xs.into(),
344                            width: if is_checked { 0.0 } else { 1.0 },
345                            color: if is_checked {
346                                cosmic.success.base
347                            } else {
348                                cosmic.palette.neutral_8
349                            }
350                            .into(),
351                        },
352                        text_color: None,
353                    },
354                    Checkbox::Danger => iced_checkbox::Style {
355                        background: Background::Color(if is_checked {
356                            cosmic.destructive.hover.into()
357                        } else {
358                            hovered_bg.into()
359                        }),
360                        icon_color: cosmic.destructive.on.into(),
361                        border: Border {
362                            radius: corners.radius_xs.into(),
363                            width: if is_checked { 0.0 } else { 1.0 },
364                            color: if is_checked {
365                                cosmic.destructive.base
366                            } else {
367                                cosmic.palette.neutral_8
368                            }
369                            .into(),
370                        },
371                        text_color: None,
372                    },
373                }
374            }
375        }
376    }
377}
378
379/*
380 * TODO: Container
381 */
382#[derive(Default)]
383pub enum Container<'a> {
384    WindowBackground,
385    Background,
386    Card,
387    ContextDrawer {
388        transparent: bool,
389    },
390    Custom(Box<dyn Fn(&Theme) -> iced_container::Style + 'a>),
391    Dialog(bool),
392    Dropdown,
393    HeaderBar {
394        focused: bool,
395        sharp_corners: bool,
396        transparent: bool,
397    },
398    List,
399    Primary,
400    Secondary,
401    Tooltip,
402    #[default]
403    Transparent,
404}
405
406impl<'a> Container<'a> {
407    pub fn custom<F: Fn(&Theme) -> iced_container::Style + 'a>(f: F) -> Self {
408        Self::Custom(Box::new(f))
409    }
410
411    #[must_use]
412    pub fn background(theme: &cosmic_theme::Theme, transparent: bool) -> iced_container::Style {
413        iced_container::Style {
414            icon_color: Some(Color::from(theme.background(transparent).on)),
415            text_color: Some(Color::from(theme.background(transparent).on)),
416            background: Some(iced::Background::Color(
417                theme.background(transparent).base.into(),
418            )),
419            border: Border {
420                radius: theme.corner_radii.radius_s.into(),
421                ..Default::default()
422            },
423            shadow: Shadow::default(),
424            snap: true,
425        }
426    }
427
428    #[must_use]
429    pub fn primary(theme: &cosmic_theme::Theme, transparent: bool) -> iced_container::Style {
430        iced_container::Style {
431            icon_color: Some(Color::from(theme.primary(transparent).on)),
432            text_color: Some(Color::from(theme.primary(transparent).on)),
433            background: Some(iced::Background::Color(
434                theme.primary(transparent).base.into(),
435            )),
436            border: Border {
437                radius: theme.corner_radii.radius_s.into(),
438                ..Default::default()
439            },
440            shadow: Shadow::default(),
441            snap: true,
442        }
443    }
444
445    #[must_use]
446    pub fn secondary(theme: &cosmic_theme::Theme, transparent: bool) -> iced_container::Style {
447        iced_container::Style {
448            icon_color: Some(Color::from(theme.secondary(transparent).on)),
449            text_color: Some(Color::from(theme.secondary(transparent).on)),
450            background: Some(iced::Background::Color(
451                theme.secondary(transparent).base.into(),
452            )),
453            border: Border {
454                radius: theme.corner_radii.radius_s.into(),
455                ..Default::default()
456            },
457            shadow: Shadow::default(),
458            snap: true,
459        }
460    }
461}
462
463impl<'a> From<iced_container::StyleFn<'a, Theme>> for Container<'a> {
464    fn from(value: iced_container::StyleFn<'a, Theme>) -> Self {
465        Self::custom(value)
466    }
467}
468
469impl iced_container::Catalog for Theme {
470    type Class<'a> = Container<'a>;
471
472    fn default<'a>() -> Self::Class<'a> {
473        Container::default()
474    }
475
476    fn style(&self, class: &Self::Class<'_>) -> iced_container::Style {
477        let cosmic = self.cosmic();
478
479        // Ensures visually aligned radii for content and window corners
480        let window_corner_radius = cosmic.radius_s().map(|x| if x < 4.0 { x } else { x + 4.0 });
481
482        match class {
483            Container::Transparent => iced_container::Style::default(),
484
485            Container::Custom(f) => f(self),
486
487            Container::WindowBackground => iced_container::Style {
488                icon_color: Some(Color::from(cosmic.background(self.transparent).on)),
489                text_color: Some(Color::from(cosmic.background(self.transparent).on)),
490                background: Some(iced::Background::Color(
491                    cosmic.background(self.transparent).base.into(),
492                )),
493                border: Border {
494                    radius: [
495                        cosmic.corner_radii.radius_0[0],
496                        cosmic.corner_radii.radius_0[1],
497                        window_corner_radius[2],
498                        window_corner_radius[3],
499                    ]
500                    .into(),
501                    ..Default::default()
502                },
503                shadow: Shadow::default(),
504                snap: true,
505            },
506
507            Container::List => {
508                let component = &self.current_container().component;
509                iced_container::Style {
510                    icon_color: Some(component.on.into()),
511                    text_color: Some(component.on.into()),
512                    background: Some(Background::Color(component.base.into())),
513                    border: iced::Border {
514                        radius: cosmic.corner_radii.radius_s.into(),
515                        ..Default::default()
516                    },
517                    shadow: Shadow::default(),
518                    snap: true,
519                }
520            }
521
522            Container::HeaderBar {
523                focused,
524                sharp_corners,
525                transparent,
526            } => {
527                let (icon_color, text_color) = if *focused {
528                    (
529                        Color::from(cosmic.accent_text_color()),
530                        Color::from(cosmic.background(self.transparent).on),
531                    )
532                } else {
533                    use crate::ext::ColorExt;
534                    let unfocused_color =
535                        Color::from(cosmic.background(self.transparent).component.on)
536                            .blend_alpha(cosmic.background(self.transparent).base.into(), 0.5);
537                    (unfocused_color, unfocused_color)
538                };
539
540                iced_container::Style {
541                    icon_color: Some(icon_color),
542                    text_color: Some(text_color),
543                    background: if *transparent {
544                        None
545                    } else {
546                        Some(iced::Background::Color(
547                            cosmic.background(self.transparent).base.into(),
548                        ))
549                    },
550                    border: Border {
551                        radius: [
552                            if *sharp_corners {
553                                cosmic.corner_radii.radius_0[0]
554                            } else {
555                                window_corner_radius[0]
556                            },
557                            if *sharp_corners {
558                                cosmic.corner_radii.radius_0[1]
559                            } else {
560                                window_corner_radius[1]
561                            },
562                            cosmic.corner_radii.radius_0[2],
563                            cosmic.corner_radii.radius_0[3],
564                        ]
565                        .into(),
566                        ..Default::default()
567                    },
568                    snap: true,
569                    shadow: Shadow::default(),
570                }
571            }
572
573            Container::ContextDrawer { transparent } => {
574                let mut a = Container::primary(cosmic, self.transparent && *transparent);
575
576                if cosmic.is_high_contrast {
577                    a.border.width = 1.;
578                    a.border.color = cosmic.primary(self.transparent).divider.into();
579                }
580                a
581            }
582
583            Container::Background => Container::background(cosmic, self.transparent),
584
585            Container::Primary => Container::primary(cosmic, self.transparent),
586
587            Container::Secondary => Container::secondary(cosmic, self.transparent),
588
589            Container::Dropdown => iced_container::Style {
590                icon_color: None,
591                text_color: None,
592                background: Some(iced::Background::Color(cosmic.bg_component_color().into())),
593                border: Border {
594                    color: cosmic.bg_component_divider().into(),
595                    width: 1.0,
596                    radius: cosmic.corner_radii.radius_s.into(),
597                },
598                shadow: Shadow::default(),
599                snap: true,
600            },
601
602            Container::Tooltip => iced_container::Style {
603                icon_color: None,
604                text_color: None,
605                background: Some(iced::Background::Color(cosmic.palette.neutral_2.into())),
606                border: Border {
607                    radius: cosmic.corner_radii.radius_l.into(),
608                    ..Default::default()
609                },
610                shadow: Shadow::default(),
611                snap: true,
612            },
613
614            Container::Card => {
615                let cosmic = self.cosmic();
616
617                match self.layer {
618                    cosmic_theme::Layer::Background => iced_container::Style {
619                        icon_color: Some(Color::from(
620                            cosmic.background(self.transparent).component.on,
621                        )),
622                        text_color: Some(Color::from(
623                            cosmic.background(self.transparent).component.on,
624                        )),
625                        background: Some(iced::Background::Color(
626                            cosmic.background(self.transparent).component.base.into(),
627                        )),
628                        border: Border {
629                            radius: cosmic.corner_radii.radius_s.into(),
630                            ..Default::default()
631                        },
632                        shadow: Shadow::default(),
633                        snap: true,
634                    },
635                    cosmic_theme::Layer::Primary => iced_container::Style {
636                        icon_color: Some(Color::from(
637                            cosmic.primary(self.transparent).component.on,
638                        )),
639                        text_color: Some(Color::from(
640                            cosmic.primary(self.transparent).component.on,
641                        )),
642                        background: Some(iced::Background::Color(
643                            cosmic.primary(self.transparent).component.base.into(),
644                        )),
645                        border: Border {
646                            radius: cosmic.corner_radii.radius_s.into(),
647                            ..Default::default()
648                        },
649                        shadow: Shadow::default(),
650                        snap: true,
651                    },
652                    cosmic_theme::Layer::Secondary => iced_container::Style {
653                        icon_color: Some(Color::from(
654                            cosmic.secondary(self.transparent).component.on,
655                        )),
656                        text_color: Some(Color::from(
657                            cosmic.secondary(self.transparent).component.on,
658                        )),
659                        background: Some(iced::Background::Color(
660                            cosmic.secondary(self.transparent).component.base.into(),
661                        )),
662                        border: Border {
663                            radius: cosmic.corner_radii.radius_s.into(),
664                            ..Default::default()
665                        },
666                        shadow: Shadow::default(),
667                        snap: true,
668                    },
669                }
670            }
671
672            Container::Dialog(is_overlay) => iced_container::Style {
673                icon_color: Some(Color::from(cosmic.primary(self.transparent).on)),
674                text_color: Some(Color::from(cosmic.primary(self.transparent).on)),
675                background: Some(iced::Background::Color(
676                    cosmic.primary(self.transparent && !is_overlay).base.into(),
677                )),
678                border: Border {
679                    color: cosmic
680                        .primary(self.transparent && !is_overlay)
681                        .divider
682                        .into(),
683                    width: 1.0,
684                    radius: cosmic.corner_radii.radius_m.into(),
685                },
686                shadow: Shadow {
687                    color: cosmic.shade.into(),
688                    offset: Vector::new(0.0, 4.0),
689                    blur_radius: 16.0,
690                },
691                snap: true,
692            },
693        }
694    }
695}
696
697#[derive(Default)]
698pub enum Slider {
699    #[default]
700    Standard,
701    Custom {
702        active: Rc<dyn Fn(&Theme) -> slider::Style>,
703        hovered: Rc<dyn Fn(&Theme) -> slider::Style>,
704        dragging: Rc<dyn Fn(&Theme) -> slider::Style>,
705    },
706}
707
708/*
709 * Slider
710 */
711impl slider::Catalog for Theme {
712    type Class<'a> = Slider;
713
714    fn default<'a>() -> Self::Class<'a> {
715        Slider::default()
716    }
717
718    fn style(&self, class: &Self::Class<'_>, status: slider::Status) -> slider::Style {
719        let cosmic: &cosmic_theme::Theme = self.cosmic();
720        let hc = self.theme_type.is_high_contrast();
721        let is_dark = self.theme_type.is_dark();
722
723        let mut appearance = match class {
724            Slider::Standard =>
725            //TODO: no way to set rail thickness
726            {
727                let (active_track, inactive_track) = if hc {
728                    (
729                        cosmic.accent_text_color(),
730                        if is_dark {
731                            cosmic.palette.neutral_5
732                        } else {
733                            cosmic.palette.neutral_3
734                        },
735                    )
736                } else {
737                    (cosmic.accent.base, cosmic.palette.neutral_6)
738                };
739                slider::Style {
740                    rail: Rail {
741                        backgrounds: (
742                            Background::Color(active_track.into()),
743                            Background::Color(inactive_track.into()),
744                        ),
745                        border: Border {
746                            radius: cosmic.corner_radii.radius_xs.into(),
747                            color: if hc && !is_dark {
748                                self.current_container().component.border.into()
749                            } else {
750                                Color::TRANSPARENT
751                            },
752                            width: if hc && !is_dark { 1. } else { 0. },
753                        },
754                        width: 4.0,
755                    },
756
757                    handle: slider::Handle {
758                        shape: slider::HandleShape::Rectangle {
759                            height: 26,
760                            width: 26,
761                            border_radius: cosmic.corner_radii.radius_m.into(),
762                        },
763                        border_color: Color::TRANSPARENT,
764                        border_width: 3.0,
765                        background: Background::Color(cosmic.accent.base.into()),
766                    },
767
768                    breakpoint: slider::Breakpoint {
769                        color: cosmic.on_bg_color().into(),
770                    },
771                }
772            }
773            Slider::Custom { active, .. } => active(self),
774        };
775        match status {
776            slider::Status::Active => appearance,
777            slider::Status::Hovered => match class {
778                Slider::Standard => {
779                    appearance.handle.border_color =
780                        self.cosmic().palette.neutral_10.with_alpha(0.1).into();
781                    appearance
782                }
783                Slider::Custom { hovered, .. } => hovered(self),
784            },
785            slider::Status::Dragged => match class {
786                Slider::Standard => {
787                    appearance.handle.border_color =
788                        self.cosmic().palette.neutral_10.with_alpha(0.2).into();
789                    appearance
790                }
791                Slider::Custom { dragging, .. } => dragging(self),
792            },
793        }
794    }
795}
796
797impl menu::Catalog for Theme {
798    type Class<'a> = ();
799
800    fn default<'a>() -> <Self as menu::Catalog>::Class<'a> {}
801
802    fn style(&self, _class: &<Self as menu::Catalog>::Class<'_>) -> menu::Style {
803        let cosmic = self.cosmic();
804
805        menu::Style {
806            text_color: cosmic.on_bg_color().into(),
807            background: Background::Color(cosmic.background(self.transparent).base.into()),
808            border: Border {
809                radius: cosmic.corner_radii.radius_m.into(),
810                ..Default::default()
811            },
812            selected_text_color: cosmic.accent_text_color().into(),
813            selected_background: Background::Color(
814                cosmic.background(self.transparent).component.hover.into(),
815            ),
816            shadow: Default::default(),
817        }
818    }
819}
820
821impl pick_list::Catalog for Theme {
822    type Class<'a> = ();
823
824    fn default<'a>() -> <Self as pick_list::Catalog>::Class<'a> {}
825
826    fn style(
827        &self,
828        _class: &<Self as pick_list::Catalog>::Class<'_>,
829        status: pick_list::Status,
830    ) -> pick_list::Style {
831        let cosmic = &self.cosmic();
832        let hc = cosmic.is_high_contrast;
833        let appearance = pick_list::Style {
834            text_color: cosmic.on_bg_color().into(),
835            background: Color::TRANSPARENT.into(),
836            placeholder_color: cosmic.on_bg_color().into(),
837            border: Border {
838                radius: cosmic.corner_radii.radius_m.into(),
839                width: if hc { 1. } else { 0. },
840                color: if hc {
841                    self.current_container().component.border.into()
842                } else {
843                    Color::TRANSPARENT
844                },
845            },
846            // icon_size: 0.7, // TODO: how to replace
847            handle_color: cosmic.on_bg_color().into(),
848        };
849
850        match status {
851            pick_list::Status::Active => appearance,
852            pick_list::Status::Hovered => pick_list::Style {
853                background: Background::Color(cosmic.background(self.transparent).base.into()),
854                ..appearance
855            },
856            pick_list::Status::Opened { is_hovered: _ } => appearance,
857        }
858    }
859}
860
861/*
862 * TODO: Radio
863 */
864impl radio::Catalog for Theme {
865    type Class<'a> = ();
866
867    fn default<'a>() -> Self::Class<'a> {}
868
869    fn style(&self, _class: &Self::Class<'_>, status: radio::Status) -> radio::Style {
870        let cur_container = self.current_container();
871        let theme = self.cosmic();
872
873        match status {
874            radio::Status::Active { is_selected } => radio::Style {
875                background: if is_selected {
876                    Color::from(theme.accent.base).into()
877                } else {
878                    // TODO: this seems to be defined weirdly in FIGMA
879                    Color::from(cur_container.small_widget).into()
880                },
881                dot_color: theme.accent.on.into(),
882                border_width: 1.0,
883                border_color: if is_selected {
884                    Color::from(theme.accent.base)
885                } else {
886                    Color::from(theme.palette.neutral_8)
887                },
888                text_color: None,
889            },
890            radio::Status::Hovered { is_selected } => {
891                let bg = if is_selected {
892                    theme.accent.base
893                } else {
894                    self.current_container().small_widget
895                };
896                // TODO: this should probably be done with a custom widget instead, or the theme needs more small widget variables.
897                let hovered_bg = Color::from(over(theme.palette.neutral_0.with_alpha(0.1), bg));
898                radio::Style {
899                    background: hovered_bg.into(),
900                    dot_color: theme.accent.on.into(),
901                    border_width: 1.0,
902                    border_color: if is_selected {
903                        Color::from(theme.accent.base)
904                    } else {
905                        Color::from(theme.palette.neutral_8)
906                    },
907                    text_color: None,
908                }
909            }
910        }
911    }
912}
913
914/*
915 * Toggler
916 */
917impl toggler::Catalog for Theme {
918    type Class<'a> = ();
919
920    fn default<'a>() -> Self::Class<'a> {}
921
922    fn style(&self, _class: &Self::Class<'_>, status: toggler::Status) -> toggler::Style {
923        let cosmic = self.cosmic();
924        const HANDLE_MARGIN: f32 = 2.0;
925        let neutral_10 = cosmic.palette.neutral_10.with_alpha(0.1);
926
927        let mut active = toggler::Style {
928            background: if matches!(
929                status,
930                toggler::Status::Active { is_toggled: true }
931                    | toggler::Status::Disabled { is_toggled: true }
932            ) {
933                cosmic.accent.base.into()
934            } else if cosmic.is_dark {
935                cosmic.palette.neutral_6.into()
936            } else {
937                cosmic.palette.neutral_5.into()
938            },
939            foreground: cosmic.palette.neutral_2.into(),
940            border_radius: cosmic.radius_xl().into(),
941            handle_radius: cosmic
942                .radius_xl()
943                .map(|x| (x - HANDLE_MARGIN).max(0.0))
944                .into(),
945            handle_margin: HANDLE_MARGIN,
946            background_border_width: 0.0,
947            background_border_color: Color::TRANSPARENT,
948            foreground_border_width: 0.0,
949            foreground_border_color: Color::TRANSPARENT,
950            text_color: None,
951            padding_ratio: 0.0,
952        };
953        match status {
954            toggler::Status::Active { is_toggled: _ } => active,
955            toggler::Status::Hovered { is_toggled: _ } => {
956                let is_active = matches!(status, toggler::Status::Hovered { is_toggled: true });
957                toggler::Style {
958                    background: if is_active {
959                        over(neutral_10, cosmic.accent_color())
960                    } else {
961                        over(
962                            neutral_10,
963                            if cosmic.is_dark {
964                                cosmic.palette.neutral_6
965                            } else {
966                                cosmic.palette.neutral_5
967                            },
968                        )
969                    }
970                    .into(),
971                    ..active
972                }
973            }
974            toggler::Status::Disabled { is_toggled: _ } => {
975                active.background = active.background.scale_alpha(0.5);
976                active.foreground = active.foreground.scale_alpha(0.5);
977                active
978            }
979        }
980    }
981}
982
983/*
984 * TODO: Pane Grid
985 */
986impl pane_grid::Catalog for Theme {
987    type Class<'a> = ();
988
989    fn default<'a>() -> <Self as pane_grid::Catalog>::Class<'a> {}
990
991    fn style(&self, _class: &<Self as pane_grid::Catalog>::Class<'_>) -> pane_grid::Style {
992        let theme = self.cosmic();
993
994        pane_grid::Style {
995            hovered_region: Highlight {
996                background: Background::Color(theme.bg_color().into()),
997                border: Border {
998                    radius: theme.corner_radii.radius_0.into(),
999                    width: 2.0,
1000                    color: theme.bg_divider().into(),
1001                },
1002            },
1003            picked_split: pane_grid::Line {
1004                color: theme.accent.base.into(),
1005                width: 2.0,
1006            },
1007            hovered_split: pane_grid::Line {
1008                color: theme.accent.hover.into(),
1009                width: 2.0,
1010            },
1011        }
1012    }
1013}
1014
1015/*
1016 * TODO: Progress Bar
1017 */
1018#[derive(Default)]
1019pub enum ProgressBar {
1020    #[default]
1021    Primary,
1022    Success,
1023    Danger,
1024    Custom(Box<dyn Fn(&Theme) -> progress_bar::Style>),
1025}
1026
1027impl ProgressBar {
1028    pub fn custom<F: Fn(&Theme) -> progress_bar::Style + 'static>(f: F) -> Self {
1029        Self::Custom(Box::new(f))
1030    }
1031}
1032
1033impl progress_bar::Catalog for Theme {
1034    type Class<'a> = ProgressBar;
1035
1036    fn default<'a>() -> Self::Class<'a> {
1037        ProgressBar::default()
1038    }
1039
1040    fn style(&self, class: &Self::Class<'_>) -> progress_bar::Style {
1041        let theme = self.cosmic();
1042
1043        let (active_track, inactive_track) = if theme.is_high_contrast {
1044            (
1045                theme.accent_text_color(),
1046                if theme.is_dark {
1047                    theme.palette.neutral_6
1048                } else {
1049                    theme.palette.neutral_4
1050                },
1051            )
1052        } else {
1053            (
1054                theme.accent.base,
1055                theme.background(self.transparent).divider,
1056            )
1057        };
1058        let border = Border {
1059            radius: theme.corner_radii.radius_xl.into(),
1060            color: if theme.is_high_contrast && !theme.is_dark {
1061                self.current_container().component.border.into()
1062            } else {
1063                Color::TRANSPARENT
1064            },
1065            width: if theme.is_high_contrast && !theme.is_dark {
1066                1.
1067            } else {
1068                0.
1069            },
1070        };
1071        match class {
1072            ProgressBar::Primary => progress_bar::Style {
1073                background: Color::from(inactive_track).into(),
1074                bar: Color::from(active_track).into(),
1075                border,
1076            },
1077            ProgressBar::Success => progress_bar::Style {
1078                background: Color::from(inactive_track).into(),
1079                bar: Color::from(theme.success.base).into(),
1080                border,
1081            },
1082            ProgressBar::Danger => progress_bar::Style {
1083                background: Color::from(inactive_track).into(),
1084                bar: Color::from(theme.destructive.base).into(),
1085                border,
1086            },
1087            ProgressBar::Custom(f) => f(self),
1088        }
1089    }
1090}
1091
1092/*
1093 * TODO: Rule
1094 */
1095#[derive(Default)]
1096pub enum Rule {
1097    #[default]
1098    Default,
1099    LightDivider,
1100    HeavyDivider,
1101    Custom(Box<dyn Fn(&Theme) -> rule::Style>),
1102}
1103
1104impl Rule {
1105    pub fn custom<F: Fn(&Theme) -> rule::Style + 'static>(f: F) -> Self {
1106        Self::Custom(Box::new(f))
1107    }
1108}
1109
1110impl rule::Catalog for Theme {
1111    type Class<'a> = Rule;
1112
1113    fn default<'a>() -> Self::Class<'a> {
1114        Rule::default()
1115    }
1116
1117    fn style(&self, class: &Self::Class<'_>) -> rule::Style {
1118        match class {
1119            Rule::Default => rule::Style {
1120                color: self.current_container().divider.into(),
1121                radius: 0.0.into(),
1122                fill_mode: rule::FillMode::Full,
1123                snap: true,
1124            },
1125            Rule::LightDivider => rule::Style {
1126                color: self.current_container().divider.into(),
1127                radius: 0.0.into(),
1128                fill_mode: rule::FillMode::Padded(8),
1129                snap: true,
1130            },
1131            Rule::HeavyDivider => rule::Style {
1132                color: self.current_container().divider.into(),
1133                radius: 2.0.into(),
1134                fill_mode: rule::FillMode::Full,
1135                snap: true,
1136            },
1137            Rule::Custom(f) => f(self),
1138        }
1139    }
1140}
1141
1142#[derive(Default, Clone, Copy)]
1143pub enum Scrollable {
1144    #[default]
1145    Permanent,
1146    Minimal,
1147}
1148
1149/*
1150 * TODO: Scrollable
1151 */
1152impl scrollable::Catalog for Theme {
1153    type Class<'a> = Scrollable;
1154
1155    fn default<'a>() -> Self::Class<'a> {
1156        Scrollable::default()
1157    }
1158
1159    fn style(&self, class: &Self::Class<'_>, status: scrollable::Status) -> scrollable::Style {
1160        match status {
1161            scrollable::Status::Active {
1162                is_horizontal_scrollbar_disabled: _,
1163                is_vertical_scrollbar_disabled: _,
1164            } => {
1165                let cosmic = self.cosmic();
1166                let neutral_5 = cosmic.palette.neutral_5.with_alpha(0.7);
1167                let neutral_6 = cosmic.palette.neutral_6.with_alpha(0.7);
1168                let mut a = scrollable::Style {
1169                    container: iced_container::transparent(self),
1170                    vertical_rail: scrollable::Rail {
1171                        border: Border {
1172                            radius: cosmic.corner_radii.radius_s.into(),
1173                            ..Default::default()
1174                        },
1175                        background: None,
1176                        scroller: scrollable::Scroller {
1177                            background: if cosmic.is_dark {
1178                                neutral_6.into()
1179                            } else {
1180                                neutral_5.into()
1181                            },
1182                            border: Border {
1183                                radius: cosmic.corner_radii.radius_s.into(),
1184                                ..Default::default()
1185                            },
1186                        },
1187                    },
1188                    horizontal_rail: scrollable::Rail {
1189                        border: Border {
1190                            radius: cosmic.corner_radii.radius_s.into(),
1191                            ..Default::default()
1192                        },
1193                        background: None,
1194                        scroller: scrollable::Scroller {
1195                            background: if cosmic.is_dark {
1196                                neutral_6.into()
1197                            } else {
1198                                neutral_5.into()
1199                            },
1200                            border: Border {
1201                                radius: cosmic.corner_radii.radius_s.into(),
1202                                ..Default::default()
1203                            },
1204                        },
1205                    },
1206                    gap: None,
1207                    // TODO: what is auto scroll?
1208                    auto_scroll: AutoScroll {
1209                        background: Color::TRANSPARENT.into(),
1210                        border: Border::default(),
1211                        shadow: Shadow::default(),
1212                        icon: Color::TRANSPARENT,
1213                    },
1214                };
1215                let small_widget_container = self.current_container().small_widget.with_alpha(0.7);
1216
1217                if matches!(class, Scrollable::Permanent) {
1218                    a.horizontal_rail.background =
1219                        Some(Background::Color(small_widget_container.into()));
1220                    a.vertical_rail.background =
1221                        Some(Background::Color(small_widget_container.into()));
1222                }
1223
1224                a
1225            }
1226            // TODO handle vertical / horizontal
1227            scrollable::Status::Hovered { .. } | scrollable::Status::Dragged { .. } => {
1228                let cosmic = self.cosmic();
1229                let neutral_5 = cosmic.palette.neutral_5.with_alpha(0.7);
1230                let neutral_6 = cosmic.palette.neutral_6.with_alpha(0.7);
1231
1232                // if is_mouse_over_scrollbar {
1233                //     let hover_overlay = cosmic.palette.neutral_0.with_alpha(0.2);
1234                //     neutral_5 = over(hover_overlay, neutral_5);
1235                // }
1236                let mut a: scrollable::Style = scrollable::Style {
1237                    container: iced_container::Style::default(),
1238                    vertical_rail: scrollable::Rail {
1239                        border: Border {
1240                            radius: cosmic.corner_radii.radius_s.into(),
1241                            ..Default::default()
1242                        },
1243                        background: None,
1244                        scroller: scrollable::Scroller {
1245                            background: if cosmic.is_dark {
1246                                neutral_6.into()
1247                            } else {
1248                                neutral_5.into()
1249                            },
1250                            border: Border {
1251                                radius: cosmic.corner_radii.radius_s.into(),
1252                                ..Default::default()
1253                            },
1254                        },
1255                    },
1256                    horizontal_rail: scrollable::Rail {
1257                        border: Border {
1258                            radius: cosmic.corner_radii.radius_s.into(),
1259                            ..Default::default()
1260                        },
1261                        background: None,
1262                        scroller: scrollable::Scroller {
1263                            background: if cosmic.is_dark {
1264                                neutral_6.into()
1265                            } else {
1266                                neutral_5.into()
1267                            },
1268                            border: Border {
1269                                radius: cosmic.corner_radii.radius_s.into(),
1270                                ..Default::default()
1271                            },
1272                        },
1273                    },
1274                    gap: None,
1275                    // TODO: what is auto scroll?
1276                    auto_scroll: AutoScroll {
1277                        background: Color::TRANSPARENT.into(),
1278                        border: Border::default(),
1279                        shadow: Shadow::default(),
1280                        icon: Color::TRANSPARENT,
1281                    },
1282                };
1283
1284                if matches!(class, Scrollable::Permanent) {
1285                    let small_widget_container =
1286                        self.current_container().small_widget.with_alpha(0.7);
1287
1288                    a.horizontal_rail.background =
1289                        Some(Background::Color(small_widget_container.into()));
1290                    a.vertical_rail.background =
1291                        Some(Background::Color(small_widget_container.into()));
1292                }
1293
1294                a
1295            }
1296        }
1297    }
1298}
1299
1300#[derive(Clone, Default)]
1301pub enum Svg {
1302    /// Apply a custom appearance filter
1303    Custom(Rc<dyn Fn(&Theme) -> svg::Style>),
1304    /// No filtering is applied
1305    #[default]
1306    Default,
1307}
1308
1309impl Svg {
1310    pub fn custom<F: Fn(&Theme) -> svg::Style + 'static>(f: F) -> Self {
1311        Self::Custom(Rc::new(f))
1312    }
1313}
1314
1315impl svg::Catalog for Theme {
1316    type Class<'a> = Svg;
1317
1318    fn default<'a>() -> Self::Class<'a> {
1319        Svg::default()
1320    }
1321
1322    fn style(&self, class: &Self::Class<'_>, _status: svg::Status) -> svg::Style {
1323        #[allow(clippy::match_same_arms)]
1324        match class {
1325            Svg::Default => svg::Style::default(),
1326            Svg::Custom(appearance) => appearance(self),
1327        }
1328    }
1329}
1330
1331/*
1332 * TODO: Text
1333 */
1334#[derive(Clone, Copy, Default)]
1335pub enum Text {
1336    Accent,
1337    #[default]
1338    Default,
1339    Color(Color),
1340    // TODO: Can't use dyn Fn since this must be copy
1341    Custom(fn(&Theme) -> iced_widget::text::Style),
1342}
1343
1344impl From<Color> for Text {
1345    fn from(color: Color) -> Self {
1346        Self::Color(color)
1347    }
1348}
1349
1350impl iced_widget::text::Catalog for Theme {
1351    type Class<'a> = Text;
1352
1353    fn default<'a>() -> Self::Class<'a> {
1354        Text::default()
1355    }
1356
1357    fn style(&self, class: &Self::Class<'_>) -> iced_widget::text::Style {
1358        let selected_fill = self.cosmic().accent.base.into();
1359        let selected_text_color = Some(self.cosmic().on_accent_color().into());
1360        match class {
1361            Text::Accent => iced_widget::text::Style {
1362                color: Some(self.cosmic().accent_text_color().into()),
1363                selected_fill,
1364                selected_text_color,
1365            },
1366            Text::Default => iced_widget::text::Style {
1367                color: None,
1368                selected_fill,
1369                selected_text_color,
1370            },
1371            Text::Color(c) => iced_widget::text::Style {
1372                color: Some(*c),
1373                selected_fill,
1374                selected_text_color,
1375            },
1376            Text::Custom(f) => f(self),
1377        }
1378    }
1379}
1380
1381#[derive(Copy, Clone, Default)]
1382pub enum TextInput {
1383    #[default]
1384    Default,
1385    Search,
1386}
1387
1388/*
1389 * TODO: Text Input
1390 */
1391impl text_input::Catalog for Theme {
1392    type Class<'a> = TextInput;
1393
1394    fn default<'a>() -> Self::Class<'a> {
1395        TextInput::default()
1396    }
1397
1398    fn style(&self, class: &Self::Class<'_>, status: text_input::Status) -> text_input::Style {
1399        let palette = self.cosmic();
1400        let bg = self.current_container().small_widget.with_alpha(0.25);
1401
1402        let neutral_9 = palette.palette.neutral_9;
1403        let value = neutral_9.into();
1404        let placeholder = neutral_9.with_alpha(0.7).into();
1405        let selection = palette.accent.base.into();
1406
1407        let mut appearance = match class {
1408            TextInput::Default => text_input::Style {
1409                background: Color::from(bg).into(),
1410                border: Border {
1411                    radius: palette.corner_radii.radius_s.into(),
1412                    width: 1.0,
1413                    color: self.current_container().component.divider.into(),
1414                },
1415                icon: self.current_container().on.into(),
1416                placeholder,
1417                value,
1418                selection,
1419            },
1420            TextInput::Search => text_input::Style {
1421                background: Color::from(bg).into(),
1422                border: Border {
1423                    radius: palette.corner_radii.radius_m.into(),
1424                    ..Default::default()
1425                },
1426                icon: self.current_container().on.into(),
1427                placeholder,
1428                value,
1429                selection,
1430            },
1431        };
1432
1433        match status {
1434            text_input::Status::Active => appearance,
1435            text_input::Status::Hovered => {
1436                let bg = self.current_container().small_widget.with_alpha(0.25);
1437
1438                match class {
1439                    TextInput::Default => text_input::Style {
1440                        background: Color::from(bg).into(),
1441                        border: Border {
1442                            radius: palette.corner_radii.radius_s.into(),
1443                            width: 1.0,
1444                            color: self.current_container().on.into(),
1445                        },
1446                        icon: self.current_container().on.into(),
1447                        placeholder,
1448                        value,
1449                        selection,
1450                    },
1451                    TextInput::Search => text_input::Style {
1452                        background: Color::from(bg).into(),
1453                        border: Border {
1454                            radius: palette.corner_radii.radius_m.into(),
1455                            ..Default::default()
1456                        },
1457                        icon: self.current_container().on.into(),
1458                        placeholder,
1459                        value,
1460                        selection,
1461                    },
1462                }
1463            }
1464            text_input::Status::Focused { is_hovered: _ } => {
1465                let bg = self.current_container().small_widget.with_alpha(0.25);
1466
1467                match class {
1468                    TextInput::Default => text_input::Style {
1469                        background: Color::from(bg).into(),
1470                        border: Border {
1471                            radius: palette.corner_radii.radius_s.into(),
1472                            width: 1.0,
1473                            color: palette.accent.base.into(),
1474                        },
1475                        icon: self.current_container().on.into(),
1476                        placeholder,
1477                        value,
1478                        selection,
1479                    },
1480                    TextInput::Search => text_input::Style {
1481                        background: Color::from(bg).into(),
1482                        border: Border {
1483                            radius: palette.corner_radii.radius_m.into(),
1484                            ..Default::default()
1485                        },
1486                        icon: self.current_container().on.into(),
1487                        placeholder,
1488                        value,
1489                        selection,
1490                    },
1491                }
1492            }
1493            text_input::Status::Disabled => {
1494                appearance.background = match appearance.background {
1495                    Background::Color(color) => Background::Color(Color {
1496                        a: color.a * 0.5,
1497                        ..color
1498                    }),
1499                    Background::Gradient(gradient) => {
1500                        Background::Gradient(gradient.scale_alpha(0.5))
1501                    }
1502                };
1503                appearance.border.color.a /= 2.;
1504                appearance.icon.a /= 2.;
1505                appearance.placeholder.a /= 2.;
1506                appearance.value.a /= 2.;
1507                appearance
1508            }
1509        }
1510    }
1511}
1512
1513#[derive(Default)]
1514pub enum TextEditor<'a> {
1515    #[default]
1516    Default,
1517    Custom(text_editor::StyleFn<'a, Theme>),
1518}
1519
1520impl<'a> From<text_editor::StyleFn<'a, Theme>> for TextEditor<'a> {
1521    fn from(style: text_editor::StyleFn<'a, Theme>) -> Self {
1522        Self::Custom(style)
1523    }
1524}
1525
1526impl iced_widget::text_editor::Catalog for Theme {
1527    type Class<'a> = TextEditor<'a>;
1528
1529    fn default<'a>() -> Self::Class<'a> {
1530        TextEditor::default()
1531    }
1532
1533    fn style(
1534        &self,
1535        class: &Self::Class<'_>,
1536        status: iced_widget::text_editor::Status,
1537    ) -> iced_widget::text_editor::Style {
1538        if let TextEditor::Custom(style) = class {
1539            return style(self, status);
1540        }
1541
1542        let cosmic = self.cosmic();
1543
1544        let selection = cosmic.accent.base.into();
1545        let value = cosmic.palette.neutral_9.into();
1546        let placeholder = cosmic.palette.neutral_9.with_alpha(0.7).into();
1547        let _icon: Color = cosmic.background(self.transparent).on.into();
1548        // TODO do we need to add icon color back?
1549
1550        match status {
1551            iced_widget::text_editor::Status::Active
1552            | iced_widget::text_editor::Status::Hovered
1553            | iced_widget::text_editor::Status::Disabled => iced_widget::text_editor::Style {
1554                background: iced::Color::from(cosmic.bg_color()).into(),
1555                border: Border {
1556                    radius: cosmic.corner_radii.radius_0.into(),
1557                    width: f32::from(cosmic.space_xxxs()),
1558                    color: iced::Color::from(cosmic.bg_divider()),
1559                },
1560                placeholder,
1561                value,
1562                selection,
1563            },
1564            iced_widget::text_editor::Status::Focused { is_hovered: _ } => {
1565                iced_widget::text_editor::Style {
1566                    background: iced::Color::from(cosmic.bg_color()).into(),
1567                    border: Border {
1568                        radius: cosmic.corner_radii.radius_0.into(),
1569                        width: f32::from(cosmic.space_xxxs()),
1570                        color: iced::Color::from(cosmic.accent.base),
1571                    },
1572                    placeholder,
1573                    value,
1574                    selection,
1575                }
1576            }
1577        }
1578    }
1579}
1580
1581#[cfg(feature = "markdown")]
1582impl iced_widget::markdown::Catalog for Theme {
1583    fn code_block<'a>() -> <Self as iced_container::Catalog>::Class<'a> {
1584        Container::custom(|_| iced_container::Style {
1585            background: Some(iced::color!(0x111111).into()),
1586            text_color: Some(Color::WHITE),
1587            border: iced::border::rounded(2),
1588            ..iced_container::Style::default()
1589        })
1590    }
1591}
1592
1593impl iced_widget::table::Catalog for Theme {
1594    type Class<'a> = iced_widget::table::StyleFn<'a, Self>;
1595
1596    fn default<'a>() -> Self::Class<'a> {
1597        Box::new(|theme| iced_widget::table::Style {
1598            separator_x: theme.current_container().divider.into(),
1599            separator_y: theme.current_container().divider.into(),
1600        })
1601    }
1602
1603    fn style(&self, class: &Self::Class<'_>) -> iced_widget::table::Style {
1604        class(self)
1605    }
1606}
1607
1608#[cfg(feature = "qr_code")]
1609impl iced_widget::qr_code::Catalog for Theme {
1610    type Class<'a> = iced_widget::qr_code::StyleFn<'a, Self>;
1611
1612    fn default<'a>() -> Self::Class<'a> {
1613        Box::new(|_theme| iced_widget::qr_code::Style {
1614            cell: Color::BLACK,
1615            background: Color::WHITE,
1616        })
1617    }
1618
1619    fn style(&self, class: &Self::Class<'_>) -> iced_widget::qr_code::Style {
1620        class(self)
1621    }
1622}
1623
1624impl combo_box::Catalog for Theme {}
1625
1626impl Base for Theme {
1627    fn default(preference: iced::theme::Mode) -> Self {
1628        match preference {
1629            iced::theme::Mode::Light => Theme::light(),
1630            iced::theme::Mode::Dark | iced::theme::Mode::None => Theme::dark(),
1631        }
1632    }
1633
1634    fn mode(&self) -> iced::theme::Mode {
1635        if self.theme_type.is_dark() {
1636            iced::theme::Mode::Dark
1637        } else {
1638            iced::theme::Mode::Light
1639        }
1640    }
1641
1642    fn base(&self) -> iced::theme::Style {
1643        iced::theme::Style {
1644            background_color: self.cosmic().bg_color().into(),
1645            text_color: self.cosmic().on_bg_color().into(),
1646            icon_color: self.cosmic().on_bg_color().into(),
1647        }
1648    }
1649
1650    fn palette(&self) -> Option<iced::theme::Palette> {
1651        Some(iced::theme::Palette {
1652            primary: self.cosmic().accent.base.into(),
1653            success: self.cosmic().success.base.into(),
1654            warning: self.cosmic().warning.base.into(),
1655            danger: self.cosmic().destructive.base.into(),
1656            background: iced::Color::from(self.cosmic().bg_color()),
1657            text: iced::Color::from(self.cosmic().on_bg_color()),
1658        })
1659    }
1660
1661    fn name(&self) -> &str {
1662        match &self.theme_type {
1663            crate::theme::ThemeType::Dark => "Cosmic Dark Theme",
1664            crate::theme::ThemeType::Light => "Cosmic Light Theme",
1665            crate::theme::ThemeType::HighContrastDark => "Cosmic High Contrast Dark Theme",
1666            crate::theme::ThemeType::HighContrastLight => "Cosmic High Contrast Light Theme",
1667            crate::theme::ThemeType::Custom(_theme) => "Custom Cosmic Theme",
1668            crate::theme::ThemeType::System {
1669                prefer_dark: _,
1670                theme,
1671            } => &theme.name,
1672        }
1673    }
1674}