diff options
| author | pml68 <contact@pml68.dev> | 2025-04-22 14:37:00 +0200 |
|---|---|---|
| committer | pml68 <contact@pml68.dev> | 2025-04-22 14:37:00 +0200 |
| commit | 21648601fa642e560772506967ce0fd589aebfaa (patch) | |
| tree | a49e3c57c8a946c14d567ab2d38e5386763a65e7 | |
| parent | fix(material_theme): highlighted text not being visible in `TextInput` (diff) | |
| download | iced-builder-21648601fa642e560772506967ce0fd589aebfaa.tar.gz | |
feat(material_theme): implement `text_editor::Catalog`
Diffstat (limited to '')
| -rw-r--r-- | crates/material_theme/src/text_editor.rs | 85 | ||||
| -rw-r--r-- | theme_test/src/main.rs | 19 |
2 files changed, 100 insertions, 4 deletions
diff --git a/crates/material_theme/src/text_editor.rs b/crates/material_theme/src/text_editor.rs new file mode 100644 index 0000000..daad7d2 --- /dev/null +++ b/crates/material_theme/src/text_editor.rs @@ -0,0 +1,85 @@ +use iced_widget::core::{Background, Border, Color, border}; +use iced_widget::text_editor::{Catalog, Status, Style, StyleFn}; + +use super::Theme; +use crate::utils::DISABLED_TEXT_OPACITY; + +impl Catalog for Theme { + type Class<'a> = StyleFn<'a, Self>; + + fn default<'a>() -> Self::Class<'a> { + Box::new(default) + } + + fn style(&self, class: &Self::Class<'_>, status: Status) -> Style { + class(self, status) + } +} + +pub fn default(theme: &Theme, status: Status) -> Style { + let surface = theme.colorscheme.surface; + let primary = theme.colorscheme.primary; + + let active = Style { + background: Background::Color(surface.surface_container.highest), + border: Border { + color: theme.colorscheme.outline.color, + width: 1.0, + radius: 4.into(), + }, + icon: surface.on_surface_variant, + placeholder: surface.on_surface_variant, + value: surface.on_surface, + selection: Color { + a: DISABLED_TEXT_OPACITY, + ..primary.color + }, + }; + + match status { + Status::Active => active, + Status::Hovered => Style { + border: Border { + color: surface.on_surface, + ..active.border + }, + ..active + }, + Status::Focused { .. } => Style { + border: Border { + color: primary.color, + width: 2.0, + ..active.border + }, + placeholder: primary.color, + ..active + }, + Status::Disabled => Style { + background: Color::TRANSPARENT.into(), + border: Border { + color: Color { + a: DISABLED_TEXT_OPACITY, + ..surface.on_surface + }, + width: 1.0, + radius: border::radius(4), + }, + icon: Color { + a: DISABLED_TEXT_OPACITY, + ..surface.on_surface + }, + placeholder: Color { + a: DISABLED_TEXT_OPACITY, + ..surface.on_surface + }, + selection: Color { + a: DISABLED_TEXT_OPACITY, + ..surface.on_surface + }, + value: Color { + a: DISABLED_TEXT_OPACITY, + ..surface.on_surface + }, + }, + } +} diff --git a/theme_test/src/main.rs b/theme_test/src/main.rs index b8653b5..3331196 100644 --- a/theme_test/src/main.rs +++ b/theme_test/src/main.rs @@ -1,7 +1,6 @@ -use iced::Length::Fill; use iced::widget::{ button, center, checkbox, column, container, horizontal_rule, pane_grid, - pick_list, radio, row, slider, text_input, toggler, + pick_list, radio, row, slider, text_editor, text_input, toggler, }; use iced::{Element, Length}; use iced_anim::{Animated, Animation, Event}; @@ -32,6 +31,7 @@ enum Message { Bool(bool), Radio(Choice), Slider(f32), + Edit(text_editor::Action), Resize(pane_grid::ResizeEvent), SwitchTheme(Event<Theme>), } @@ -44,6 +44,7 @@ pub struct State { is_checked: bool, selection: Option<Choice>, value: f32, + editor_content: text_editor::Content, panes: pane_grid::State<Pane>, } @@ -56,6 +57,7 @@ impl Default for State { is_checked: Default::default(), selection: Default::default(), value: Default::default(), + editor_content: text_editor::Content::new(), panes: pane_grid::State::with_configuration( pane_grid::Configuration::Split { axis: pane_grid::Axis::Vertical, @@ -95,6 +97,7 @@ impl State { Message::Bool(is_checked) => self.is_checked = is_checked, Message::Radio(choice) => self.selection = Some(choice), Message::Slider(value) => self.value = value, + Message::Edit(action) => self.editor_content.perform(action), Message::Resize(pane_grid::ResizeEvent { split, ratio }) => { self.panes.resize(split, ratio); } @@ -193,12 +196,13 @@ impl State { ) .placeholder("Select a theme..."), horizontal_rule(1), - // Button + // Dialog button("Open Dialog").on_press(Message::OpenDialog), horizontal_rule(1), // Text Input text_input("Type something here...", &self.content) .on_input(Message::Input), + text_input("Disabled", "Disabled"), horizontal_rule(1), // Checkbox checkbox("Normal", self.is_checked) @@ -238,7 +242,14 @@ impl State { // Toggler toggler(self.is_checked) .on_toggle(Message::Bool) - .size(24.0) + .size(24.0), + toggler(self.is_checked).size(24.0), + horizontal_rule(1), + // Text Editor + text_editor(&self.editor_content) + .on_action(Message::Edit), + text_editor(&self.editor_content) + .placeholder("Disabled") ] .spacing(10), ) |
