Simplify theming for TextInput widget

This commit is contained in:
Héctor Ramón Jiménez 2024-03-05 15:53:59 +01:00
parent d681aaa57e
commit 704ec9cb5c
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
9 changed files with 855 additions and 1056 deletions

View file

@ -27,7 +27,6 @@ pub mod rule;
pub mod slider;
pub mod svg;
pub mod text_editor;
pub mod text_input;
pub mod theme;
pub mod toggler;

View file

@ -1,45 +0,0 @@
//! Change the appearance of a text input.
use iced_core::{Background, Border, Color};
/// The appearance of a text input.
#[derive(Debug, Clone, Copy)]
pub struct Appearance {
/// The [`Background`] of the text input.
pub background: Background,
/// The [`Border`] of the text input.
pub border: Border,
/// The icon [`Color`] of the text input.
pub icon_color: Color,
}
/// A set of rules that dictate the style of a text input.
pub trait StyleSheet {
/// The supported style of the [`StyleSheet`].
type Style: Default;
/// Produces the style of an active text input.
fn active(&self, style: &Self::Style) -> Appearance;
/// Produces the style of a focused text input.
fn focused(&self, style: &Self::Style) -> Appearance;
/// Produces the [`Color`] of the placeholder of a text input.
fn placeholder_color(&self, style: &Self::Style) -> Color;
/// Produces the [`Color`] of the value of a text input.
fn value_color(&self, style: &Self::Style) -> Color;
/// Produces the [`Color`] of the value of a disabled text input.
fn disabled_color(&self, style: &Self::Style) -> Color;
/// Produces the [`Color`] of the selection of a text input.
fn selection_color(&self, style: &Self::Style) -> Color;
/// Produces the style of an hovered text input.
fn hovered(&self, style: &Self::Style) -> Appearance {
self.focused(style)
}
/// Produces the style of a disabled text input.
fn disabled(&self, style: &Self::Style) -> Appearance;
}

View file

@ -15,7 +15,6 @@ use crate::rule;
use crate::slider;
use crate::svg;
use crate::text_editor;
use crate::text_input;
use crate::toggler;
use crate::core::{Background, Border, Color};
@ -793,130 +792,6 @@ impl svg::StyleSheet for fn(&Theme) -> svg::Appearance {
impl text::StyleSheet for Theme {}
/// The style of a text input.
#[derive(Default)]
pub enum TextInput {
/// The default style.
#[default]
Default,
/// A custom style.
Custom(Box<dyn text_input::StyleSheet<Style = Theme>>),
}
impl text_input::StyleSheet for Theme {
type Style = TextInput;
fn active(&self, style: &Self::Style) -> text_input::Appearance {
if let TextInput::Custom(custom) = style {
return custom.active(self);
}
let palette = self.extended_palette();
text_input::Appearance {
background: palette.background.base.color.into(),
border: Border {
radius: 2.0.into(),
width: 1.0,
color: palette.background.strong.color,
},
icon_color: palette.background.weak.text,
}
}
fn hovered(&self, style: &Self::Style) -> text_input::Appearance {
if let TextInput::Custom(custom) = style {
return custom.hovered(self);
}
let palette = self.extended_palette();
text_input::Appearance {
background: palette.background.base.color.into(),
border: Border {
radius: 2.0.into(),
width: 1.0,
color: palette.background.base.text,
},
icon_color: palette.background.weak.text,
}
}
fn focused(&self, style: &Self::Style) -> text_input::Appearance {
if let TextInput::Custom(custom) = style {
return custom.focused(self);
}
let palette = self.extended_palette();
text_input::Appearance {
background: palette.background.base.color.into(),
border: Border {
radius: 2.0.into(),
width: 1.0,
color: palette.primary.strong.color,
},
icon_color: palette.background.weak.text,
}
}
fn placeholder_color(&self, style: &Self::Style) -> Color {
if let TextInput::Custom(custom) = style {
return custom.placeholder_color(self);
}
let palette = self.extended_palette();
palette.background.strong.color
}
fn value_color(&self, style: &Self::Style) -> Color {
if let TextInput::Custom(custom) = style {
return custom.value_color(self);
}
let palette = self.extended_palette();
palette.background.base.text
}
fn selection_color(&self, style: &Self::Style) -> Color {
if let TextInput::Custom(custom) = style {
return custom.selection_color(self);
}
let palette = self.extended_palette();
palette.primary.weak.color
}
fn disabled(&self, style: &Self::Style) -> text_input::Appearance {
if let TextInput::Custom(custom) = style {
return custom.disabled(self);
}
let palette = self.extended_palette();
text_input::Appearance {
background: palette.background.weak.color.into(),
border: Border {
radius: 2.0.into(),
width: 1.0,
color: palette.background.strong.color,
},
icon_color: palette.background.strong.color,
}
}
fn disabled_color(&self, style: &Self::Style) -> Color {
if let TextInput::Custom(custom) = style {
return custom.disabled_color(self);
}
self.placeholder_color(style)
}
}
/// The style of a text input.
#[derive(Default)]
pub enum TextEditor {

View file

@ -32,7 +32,7 @@ pub struct ComboBox<
Theme = crate::Theme,
Renderer = crate::Renderer,
> where
Theme: text_input::StyleSheet + menu::StyleSheet,
Theme: text_input::Style + menu::StyleSheet,
Renderer: text::Renderer,
{
state: &'a State<T>,
@ -51,7 +51,7 @@ pub struct ComboBox<
impl<'a, T, Message, Theme, Renderer> ComboBox<'a, T, Message, Theme, Renderer>
where
T: std::fmt::Display + Clone,
Theme: text_input::StyleSheet + menu::StyleSheet,
Theme: text_input::Style + menu::StyleSheet,
Renderer: text::Renderer,
{
/// Creates a new [`ComboBox`] with the given list of options, a placeholder,
@ -121,20 +121,17 @@ where
// TODO: Define its own `StyleSheet` trait
pub fn style<S>(mut self, style: S) -> Self
where
S: Into<<Theme as text_input::StyleSheet>::Style>
+ Into<<Theme as menu::StyleSheet>::Style>
+ Clone,
S: Into<<Theme as menu::StyleSheet>::Style>,
{
self.menu_style = style.clone().into();
self.text_input = self.text_input.style(style);
self.menu_style = style.into();
self
}
/// Sets the style of the [`TextInput`] of the [`ComboBox`].
pub fn text_input_style<S>(mut self, style: S) -> Self
where
S: Into<<Theme as text_input::StyleSheet>::Style> + Clone,
{
pub fn text_input_style(
mut self,
style: fn(&Theme, text_input::Status) -> text_input::Appearance,
) -> Self {
self.text_input = self.text_input.style(style);
self
}
@ -300,8 +297,8 @@ where
T: Display + Clone + 'static,
Message: Clone,
Theme: container::Style
+ text_input::StyleSheet
+ scrollable::Tradition
+ text_input::Style
+ scrollable::Style
+ menu::StyleSheet,
Renderer: text::Renderer,
{
@ -720,8 +717,8 @@ where
T: Display + Clone + 'static,
Message: Clone + 'a,
Theme: container::Style
+ text_input::StyleSheet
+ scrollable::Tradition
+ text_input::Style
+ scrollable::Style
+ menu::StyleSheet
+ 'a,
Renderer: text::Renderer + 'a,

View file

@ -104,7 +104,7 @@ pub fn scrollable<'a, Message, Theme, Renderer>(
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Scrollable<'a, Message, Theme, Renderer>
where
Theme: scrollable::Tradition,
Theme: scrollable::Style,
Renderer: core::Renderer,
{
Scrollable::new(content)
@ -209,7 +209,7 @@ pub fn text_input<'a, Message, Theme, Renderer>(
) -> TextInput<'a, Message, Theme, Renderer>
where
Message: Clone,
Theme: text_input::StyleSheet,
Theme: text_input::Style,
Renderer: core::text::Renderer,
{
TextInput::new(placeholder, value)
@ -276,7 +276,7 @@ where
Message: Clone,
Renderer: core::text::Renderer,
Theme: pick_list::StyleSheet
+ scrollable::Tradition
+ scrollable::Style
+ overlay::menu::StyleSheet
+ container::Style,
<Theme as overlay::menu::StyleSheet>::Style:
@ -296,7 +296,7 @@ pub fn combo_box<'a, T, Message, Theme, Renderer>(
) -> ComboBox<'a, T, Message, Theme, Renderer>
where
T: std::fmt::Display + Clone,
Theme: text_input::StyleSheet + overlay::menu::StyleSheet,
Theme: text_input::Style + overlay::menu::StyleSheet,
Renderer: core::text::Renderer,
{
ComboBox::new(state, placeholder, selection, on_selected)

View file

@ -47,7 +47,7 @@ impl<'a, T, Message, Theme, Renderer> Menu<'a, T, Message, Theme, Renderer>
where
T: ToString + Clone,
Message: 'a,
Theme: StyleSheet + container::Style + scrollable::Tradition + 'a,
Theme: StyleSheet + container::Style + scrollable::Style + 'a,
Renderer: text::Renderer + 'a,
{
/// Creates a new [`Menu`] with the given [`State`], a list of options, and
@ -179,7 +179,7 @@ where
impl<'a, Message, Theme, Renderer> Overlay<'a, Message, Theme, Renderer>
where
Message: 'a,
Theme: StyleSheet + container::Style + scrollable::Tradition + 'a,
Theme: StyleSheet + container::Style + scrollable::Style + 'a,
Renderer: text::Renderer + 'a,
{
pub fn new<T>(

View file

@ -61,10 +61,7 @@ where
L: Borrow<[T]> + 'a,
V: Borrow<T> + 'a,
Message: Clone,
Theme: StyleSheet
+ scrollable::Tradition
+ menu::StyleSheet
+ container::Style,
Theme: StyleSheet + scrollable::Style + menu::StyleSheet + container::Style,
<Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>,
Renderer: text::Renderer,
{
@ -176,10 +173,7 @@ where
L: Borrow<[T]>,
V: Borrow<T>,
Message: Clone + 'a,
Theme: StyleSheet
+ scrollable::Tradition
+ menu::StyleSheet
+ container::Style,
Theme: StyleSheet + scrollable::Style + menu::StyleSheet + container::Style,
<Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>,
Renderer: text::Renderer + 'a,
{
@ -318,7 +312,7 @@ where
V: Borrow<T> + 'a,
Message: Clone + 'a,
Theme: StyleSheet
+ scrollable::Tradition
+ scrollable::Style
+ menu::StyleSheet
+ container::Style
+ 'a,
@ -628,7 +622,7 @@ where
T: Clone + ToString,
Message: 'a,
Theme: StyleSheet
+ scrollable::Tradition
+ scrollable::Style
+ menu::StyleSheet
+ container::Style
+ 'a,

View file

@ -49,7 +49,7 @@ where
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self
where
Theme: Tradition,
Theme: Style,
{
Self::with_direction(content, Direction::default())
}
@ -60,7 +60,7 @@ where
direction: Direction,
) -> Self
where
Theme: Tradition,
Theme: Style,
{
let content = content.into();
@ -83,7 +83,7 @@ where
direction,
content,
on_scroll: None,
style: Theme::tradition(),
style: Theme::style(),
}
}
@ -1653,14 +1653,14 @@ pub struct Scroller {
pub border: Border,
}
/// The definition of the traditional style of a [`Scrollable`].
pub trait Tradition {
/// Returns the traditional style of a [`Scrollable`].
fn tradition() -> fn(&Self, Status) -> Appearance;
/// The definition of the default style of a [`Scrollable`].
pub trait Style {
/// Returns the default style of a [`Scrollable`].
fn style() -> fn(&Self, Status) -> Appearance;
}
impl Tradition for Theme {
fn tradition() -> fn(&Self, Status) -> Appearance {
impl Style for Theme {
fn style() -> fn(&Self, Status) -> Appearance {
default
}
}

File diff suppressed because it is too large Load diff