Simplify theming for TextInput widget
This commit is contained in:
parent
d681aaa57e
commit
704ec9cb5c
9 changed files with 855 additions and 1056 deletions
|
|
@ -27,7 +27,6 @@ pub mod rule;
|
||||||
pub mod slider;
|
pub mod slider;
|
||||||
pub mod svg;
|
pub mod svg;
|
||||||
pub mod text_editor;
|
pub mod text_editor;
|
||||||
pub mod text_input;
|
|
||||||
pub mod theme;
|
pub mod theme;
|
||||||
pub mod toggler;
|
pub mod toggler;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
@ -15,7 +15,6 @@ use crate::rule;
|
||||||
use crate::slider;
|
use crate::slider;
|
||||||
use crate::svg;
|
use crate::svg;
|
||||||
use crate::text_editor;
|
use crate::text_editor;
|
||||||
use crate::text_input;
|
|
||||||
use crate::toggler;
|
use crate::toggler;
|
||||||
|
|
||||||
use crate::core::{Background, Border, Color};
|
use crate::core::{Background, Border, Color};
|
||||||
|
|
@ -793,130 +792,6 @@ impl svg::StyleSheet for fn(&Theme) -> svg::Appearance {
|
||||||
|
|
||||||
impl text::StyleSheet for Theme {}
|
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.
|
/// The style of a text input.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub enum TextEditor {
|
pub enum TextEditor {
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ pub struct ComboBox<
|
||||||
Theme = crate::Theme,
|
Theme = crate::Theme,
|
||||||
Renderer = crate::Renderer,
|
Renderer = crate::Renderer,
|
||||||
> where
|
> where
|
||||||
Theme: text_input::StyleSheet + menu::StyleSheet,
|
Theme: text_input::Style + menu::StyleSheet,
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
state: &'a State<T>,
|
state: &'a State<T>,
|
||||||
|
|
@ -51,7 +51,7 @@ pub struct ComboBox<
|
||||||
impl<'a, T, Message, Theme, Renderer> ComboBox<'a, T, Message, Theme, Renderer>
|
impl<'a, T, Message, Theme, Renderer> ComboBox<'a, T, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
T: std::fmt::Display + Clone,
|
T: std::fmt::Display + Clone,
|
||||||
Theme: text_input::StyleSheet + menu::StyleSheet,
|
Theme: text_input::Style + menu::StyleSheet,
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
/// Creates a new [`ComboBox`] with the given list of options, a placeholder,
|
/// Creates a new [`ComboBox`] with the given list of options, a placeholder,
|
||||||
|
|
@ -121,20 +121,17 @@ where
|
||||||
// TODO: Define its own `StyleSheet` trait
|
// TODO: Define its own `StyleSheet` trait
|
||||||
pub fn style<S>(mut self, style: S) -> Self
|
pub fn style<S>(mut self, style: S) -> Self
|
||||||
where
|
where
|
||||||
S: Into<<Theme as text_input::StyleSheet>::Style>
|
S: Into<<Theme as menu::StyleSheet>::Style>,
|
||||||
+ Into<<Theme as menu::StyleSheet>::Style>
|
|
||||||
+ Clone,
|
|
||||||
{
|
{
|
||||||
self.menu_style = style.clone().into();
|
self.menu_style = style.into();
|
||||||
self.text_input = self.text_input.style(style);
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the style of the [`TextInput`] of the [`ComboBox`].
|
/// Sets the style of the [`TextInput`] of the [`ComboBox`].
|
||||||
pub fn text_input_style<S>(mut self, style: S) -> Self
|
pub fn text_input_style(
|
||||||
where
|
mut self,
|
||||||
S: Into<<Theme as text_input::StyleSheet>::Style> + Clone,
|
style: fn(&Theme, text_input::Status) -> text_input::Appearance,
|
||||||
{
|
) -> Self {
|
||||||
self.text_input = self.text_input.style(style);
|
self.text_input = self.text_input.style(style);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
@ -300,8 +297,8 @@ where
|
||||||
T: Display + Clone + 'static,
|
T: Display + Clone + 'static,
|
||||||
Message: Clone,
|
Message: Clone,
|
||||||
Theme: container::Style
|
Theme: container::Style
|
||||||
+ text_input::StyleSheet
|
+ text_input::Style
|
||||||
+ scrollable::Tradition
|
+ scrollable::Style
|
||||||
+ menu::StyleSheet,
|
+ menu::StyleSheet,
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
|
|
@ -720,8 +717,8 @@ where
|
||||||
T: Display + Clone + 'static,
|
T: Display + Clone + 'static,
|
||||||
Message: Clone + 'a,
|
Message: Clone + 'a,
|
||||||
Theme: container::Style
|
Theme: container::Style
|
||||||
+ text_input::StyleSheet
|
+ text_input::Style
|
||||||
+ scrollable::Tradition
|
+ scrollable::Style
|
||||||
+ menu::StyleSheet
|
+ menu::StyleSheet
|
||||||
+ 'a,
|
+ 'a,
|
||||||
Renderer: text::Renderer + 'a,
|
Renderer: text::Renderer + 'a,
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,7 @@ pub fn scrollable<'a, Message, Theme, Renderer>(
|
||||||
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
||||||
) -> Scrollable<'a, Message, Theme, Renderer>
|
) -> Scrollable<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Theme: scrollable::Tradition,
|
Theme: scrollable::Style,
|
||||||
Renderer: core::Renderer,
|
Renderer: core::Renderer,
|
||||||
{
|
{
|
||||||
Scrollable::new(content)
|
Scrollable::new(content)
|
||||||
|
|
@ -209,7 +209,7 @@ pub fn text_input<'a, Message, Theme, Renderer>(
|
||||||
) -> TextInput<'a, Message, Theme, Renderer>
|
) -> TextInput<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Message: Clone,
|
Message: Clone,
|
||||||
Theme: text_input::StyleSheet,
|
Theme: text_input::Style,
|
||||||
Renderer: core::text::Renderer,
|
Renderer: core::text::Renderer,
|
||||||
{
|
{
|
||||||
TextInput::new(placeholder, value)
|
TextInput::new(placeholder, value)
|
||||||
|
|
@ -276,7 +276,7 @@ where
|
||||||
Message: Clone,
|
Message: Clone,
|
||||||
Renderer: core::text::Renderer,
|
Renderer: core::text::Renderer,
|
||||||
Theme: pick_list::StyleSheet
|
Theme: pick_list::StyleSheet
|
||||||
+ scrollable::Tradition
|
+ scrollable::Style
|
||||||
+ overlay::menu::StyleSheet
|
+ overlay::menu::StyleSheet
|
||||||
+ container::Style,
|
+ container::Style,
|
||||||
<Theme as overlay::menu::StyleSheet>::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>
|
) -> ComboBox<'a, T, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
T: std::fmt::Display + Clone,
|
T: std::fmt::Display + Clone,
|
||||||
Theme: text_input::StyleSheet + overlay::menu::StyleSheet,
|
Theme: text_input::Style + overlay::menu::StyleSheet,
|
||||||
Renderer: core::text::Renderer,
|
Renderer: core::text::Renderer,
|
||||||
{
|
{
|
||||||
ComboBox::new(state, placeholder, selection, on_selected)
|
ComboBox::new(state, placeholder, selection, on_selected)
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ impl<'a, T, Message, Theme, Renderer> Menu<'a, T, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
T: ToString + Clone,
|
T: ToString + Clone,
|
||||||
Message: 'a,
|
Message: 'a,
|
||||||
Theme: StyleSheet + container::Style + scrollable::Tradition + 'a,
|
Theme: StyleSheet + container::Style + scrollable::Style + 'a,
|
||||||
Renderer: text::Renderer + 'a,
|
Renderer: text::Renderer + 'a,
|
||||||
{
|
{
|
||||||
/// Creates a new [`Menu`] with the given [`State`], a list of options, and
|
/// 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>
|
impl<'a, Message, Theme, Renderer> Overlay<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Message: 'a,
|
Message: 'a,
|
||||||
Theme: StyleSheet + container::Style + scrollable::Tradition + 'a,
|
Theme: StyleSheet + container::Style + scrollable::Style + 'a,
|
||||||
Renderer: text::Renderer + 'a,
|
Renderer: text::Renderer + 'a,
|
||||||
{
|
{
|
||||||
pub fn new<T>(
|
pub fn new<T>(
|
||||||
|
|
|
||||||
|
|
@ -61,10 +61,7 @@ where
|
||||||
L: Borrow<[T]> + 'a,
|
L: Borrow<[T]> + 'a,
|
||||||
V: Borrow<T> + 'a,
|
V: Borrow<T> + 'a,
|
||||||
Message: Clone,
|
Message: Clone,
|
||||||
Theme: StyleSheet
|
Theme: StyleSheet + scrollable::Style + menu::StyleSheet + container::Style,
|
||||||
+ scrollable::Tradition
|
|
||||||
+ menu::StyleSheet
|
|
||||||
+ container::Style,
|
|
||||||
<Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>,
|
<Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>,
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
|
|
@ -176,10 +173,7 @@ where
|
||||||
L: Borrow<[T]>,
|
L: Borrow<[T]>,
|
||||||
V: Borrow<T>,
|
V: Borrow<T>,
|
||||||
Message: Clone + 'a,
|
Message: Clone + 'a,
|
||||||
Theme: StyleSheet
|
Theme: StyleSheet + scrollable::Style + menu::StyleSheet + container::Style,
|
||||||
+ scrollable::Tradition
|
|
||||||
+ menu::StyleSheet
|
|
||||||
+ container::Style,
|
|
||||||
<Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>,
|
<Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>,
|
||||||
Renderer: text::Renderer + 'a,
|
Renderer: text::Renderer + 'a,
|
||||||
{
|
{
|
||||||
|
|
@ -318,7 +312,7 @@ where
|
||||||
V: Borrow<T> + 'a,
|
V: Borrow<T> + 'a,
|
||||||
Message: Clone + 'a,
|
Message: Clone + 'a,
|
||||||
Theme: StyleSheet
|
Theme: StyleSheet
|
||||||
+ scrollable::Tradition
|
+ scrollable::Style
|
||||||
+ menu::StyleSheet
|
+ menu::StyleSheet
|
||||||
+ container::Style
|
+ container::Style
|
||||||
+ 'a,
|
+ 'a,
|
||||||
|
|
@ -628,7 +622,7 @@ where
|
||||||
T: Clone + ToString,
|
T: Clone + ToString,
|
||||||
Message: 'a,
|
Message: 'a,
|
||||||
Theme: StyleSheet
|
Theme: StyleSheet
|
||||||
+ scrollable::Tradition
|
+ scrollable::Style
|
||||||
+ menu::StyleSheet
|
+ menu::StyleSheet
|
||||||
+ container::Style
|
+ container::Style
|
||||||
+ 'a,
|
+ 'a,
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ where
|
||||||
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
||||||
) -> Self
|
) -> Self
|
||||||
where
|
where
|
||||||
Theme: Tradition,
|
Theme: Style,
|
||||||
{
|
{
|
||||||
Self::with_direction(content, Direction::default())
|
Self::with_direction(content, Direction::default())
|
||||||
}
|
}
|
||||||
|
|
@ -60,7 +60,7 @@ where
|
||||||
direction: Direction,
|
direction: Direction,
|
||||||
) -> Self
|
) -> Self
|
||||||
where
|
where
|
||||||
Theme: Tradition,
|
Theme: Style,
|
||||||
{
|
{
|
||||||
let content = content.into();
|
let content = content.into();
|
||||||
|
|
||||||
|
|
@ -83,7 +83,7 @@ where
|
||||||
direction,
|
direction,
|
||||||
content,
|
content,
|
||||||
on_scroll: None,
|
on_scroll: None,
|
||||||
style: Theme::tradition(),
|
style: Theme::style(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1653,14 +1653,14 @@ pub struct Scroller {
|
||||||
pub border: Border,
|
pub border: Border,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The definition of the traditional style of a [`Scrollable`].
|
/// The definition of the default style of a [`Scrollable`].
|
||||||
pub trait Tradition {
|
pub trait Style {
|
||||||
/// Returns the traditional style of a [`Scrollable`].
|
/// Returns the default style of a [`Scrollable`].
|
||||||
fn tradition() -> fn(&Self, Status) -> Appearance;
|
fn style() -> fn(&Self, Status) -> Appearance;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tradition for Theme {
|
impl Style for Theme {
|
||||||
fn tradition() -> fn(&Self, Status) -> Appearance {
|
fn style() -> fn(&Self, Status) -> Appearance {
|
||||||
default
|
default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue