Skip to main content

material_ui_rs/design/style/
text_editor.rs

1use iced_widget::core::{Background, Border, Color};
2use iced_widget::text_editor::{Catalog, Status, Style, StyleFn};
3
4use crate::Theme;
5use crate::tokens;
6use crate::utils::{disabled_text, state_layer};
7
8impl Catalog for Theme {
9    type Class<'a> = StyleFn<'a, Self>;
10
11    fn default<'a>() -> Self::Class<'a> {
12        Box::new(default)
13    }
14
15    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
16        class(self, status)
17    }
18}
19
20pub fn default(theme: &Theme, status: Status) -> Style {
21    let surface = theme.colors().surface;
22    let primary = theme.colors().primary;
23
24    let active = Style {
25        background: Background::Color(Color::TRANSPARENT),
26        border: Border {
27            color: theme.colors().outline.color,
28            width: tokens::component::text_field::OUTLINE_WIDTH,
29            radius: tokens::component::text_field::CONTAINER_SHAPE.into(),
30        },
31        placeholder: surface.text_variant,
32        value: surface.text,
33        selection: disabled_text(primary.color),
34    };
35
36    match status {
37        Status::Active => active,
38        Status::Hovered => Style {
39            border: Border {
40                color: surface.text,
41                ..active.border
42            },
43            ..active
44        },
45        Status::Focused { .. } => Style {
46            border: Border {
47                color: primary.color,
48                width: tokens::component::text_field::FOCUS_OUTLINE_WIDTH,
49                ..active.border
50            },
51            ..active
52        },
53        Status::Disabled => Style {
54            background: Color::TRANSPARENT.into(),
55            border: Border {
56                color: state_layer(
57                    surface.text,
58                    tokens::component::text_field::DISABLED_OUTLINE_OPACITY,
59                ),
60                ..active.border
61            },
62            placeholder: state_layer(
63                surface.text,
64                tokens::component::text_field::DISABLED_INPUT_TEXT_OPACITY,
65            ),
66            value: state_layer(
67                surface.text,
68                tokens::component::text_field::DISABLED_INPUT_TEXT_OPACITY,
69            ),
70            selection: disabled_text(surface.text),
71        },
72    }
73}
74
75#[cfg(test)]
76#[path = "../../../tests/design/style/text_editor.rs"]
77mod tests;