diff options
Diffstat (limited to '')
| -rw-r--r-- | style/Cargo.toml | 30 | ||||
| -rw-r--r-- | style/src/lib.rs | 13 | ||||
| -rw-r--r-- | style/src/rule.rs | 2 | ||||
| -rw-r--r-- | style/src/text_editor.rs | 47 | ||||
| -rw-r--r-- | style/src/theme.rs | 146 | 
5 files changed, 204 insertions, 34 deletions
diff --git a/style/Cargo.toml b/style/Cargo.toml index 8af4a9b3..3f00e787 100644 --- a/style/Cargo.toml +++ b/style/Cargo.toml @@ -1,22 +1,18 @@  [package]  name = "iced_style" -version = "0.8.0" -authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"] -edition = "2021"  description = "The default set of styles of Iced" -license = "MIT" -repository = "https://github.com/iced-rs/iced" -documentation = "https://docs.rs/iced_style" -keywords = ["gui", "ui", "graphics", "interface", "widgets"] -categories = ["gui"] +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true +categories.workspace = true +keywords.workspace = true -[dependencies.iced_core] -version = "0.9" -path = "../core" -features = ["palette"] +[dependencies] +iced_core.workspace = true +iced_core.features = ["palette"] -[dependencies.palette] -version = "0.7" - -[dependencies.once_cell] -version = "1.15" +palette.workspace = true +once_cell.workspace = true diff --git a/style/src/lib.rs b/style/src/lib.rs index 286ff9db..e4097434 100644 --- a/style/src/lib.rs +++ b/style/src/lib.rs @@ -7,17 +7,13 @@  #![doc(      html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"  )] +#![forbid(unsafe_code, rust_2018_idioms)]  #![deny(      unused_results, -    clippy::extra_unused_lifetimes, -    clippy::from_over_into, -    clippy::needless_borrow, -    clippy::new_without_default, -    clippy::useless_conversion +    missing_docs, +    unused_results, +    rustdoc::broken_intra_doc_links  )] -#![deny(missing_docs, unused_results)] -#![forbid(unsafe_code, rust_2018_idioms)] -#![allow(clippy::inherent_to_string, clippy::type_complexity)]  pub use iced_core as core;  pub mod application; @@ -33,6 +29,7 @@ pub mod rule;  pub mod scrollable;  pub mod slider;  pub mod svg; +pub mod text_editor;  pub mod text_input;  pub mod theme;  pub mod toggler; diff --git a/style/src/rule.rs b/style/src/rule.rs index afae085c..efbe7444 100644 --- a/style/src/rule.rs +++ b/style/src/rule.rs @@ -47,7 +47,7 @@ impl FillMode {      ///      /// # Returns      /// -    /// * (starting_offset, length) +    /// * (`starting_offset`, `length`)      pub fn fill(&self, space: f32) -> (f32, f32) {          match *self {              FillMode::Full => (0.0, space), diff --git a/style/src/text_editor.rs b/style/src/text_editor.rs new file mode 100644 index 00000000..f6bae7e6 --- /dev/null +++ b/style/src/text_editor.rs @@ -0,0 +1,47 @@ +//! Change the appearance of a text editor. +use crate::core::{Background, BorderRadius, 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 radius of the text input. +    pub border_radius: BorderRadius, +    /// The border width of the text input. +    pub border_width: f32, +    /// The border [`Color`] of the text input. +    pub border_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; +} diff --git a/style/src/theme.rs b/style/src/theme.rs index 64497181..47010728 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -1,8 +1,7 @@  //! Use the built-in theme and styles.  pub mod palette; -use self::palette::Extended; -pub use self::palette::Palette; +pub use palette::Palette;  use crate::application;  use crate::button; @@ -18,6 +17,7 @@ use crate::rule;  use crate::scrollable;  use crate::slider;  use crate::svg; +use crate::text_editor;  use crate::text_input;  use crate::toggler; @@ -40,7 +40,16 @@ pub enum Theme {  impl Theme {      /// Creates a new custom [`Theme`] from the given [`Palette`].      pub fn custom(palette: Palette) -> Self { -        Self::Custom(Box::new(Custom::new(palette))) +        Self::custom_with_fn(palette, palette::Extended::generate) +    } + +    /// Creates a new custom [`Theme`] from the given [`Palette`], with +    /// a custom generator of a [`palette::Extended`]. +    pub fn custom_with_fn( +        palette: Palette, +        generate: impl FnOnce(Palette) -> palette::Extended, +    ) -> Self { +        Self::Custom(Box::new(Custom::with_fn(palette, generate)))      }      /// Returns the [`Palette`] of the [`Theme`]. @@ -66,15 +75,24 @@ impl Theme {  #[derive(Debug, Clone, Copy, PartialEq)]  pub struct Custom {      palette: Palette, -    extended: Extended, +    extended: palette::Extended,  }  impl Custom {      /// Creates a [`Custom`] theme from the given [`Palette`].      pub fn new(palette: Palette) -> Self { +        Self::with_fn(palette, palette::Extended::generate) +    } + +    /// Creates a [`Custom`] theme from the given [`Palette`] with +    /// a custom generator of a [`palette::Extended`]. +    pub fn with_fn( +        palette: Palette, +        generate: impl FnOnce(Palette) -> palette::Extended, +    ) -> Self {          Self {              palette, -            extended: Extended::generate(palette), +            extended: generate(palette),          }      }  } @@ -376,7 +394,7 @@ impl container::StyleSheet for Theme {      fn appearance(&self, style: &Self::Style) -> container::Appearance {          match style { -            Container::Transparent => Default::default(), +            Container::Transparent => container::Appearance::default(),              Container::Box => {                  let palette = self.extended_palette(); @@ -887,7 +905,7 @@ impl svg::StyleSheet for Theme {      fn appearance(&self, style: &Self::Style) -> svg::Appearance {          match style { -            Svg::Default => Default::default(), +            Svg::Default => svg::Appearance::default(),              Svg::Custom(custom) => custom.appearance(self),          }      } @@ -1036,7 +1054,7 @@ impl text::StyleSheet for Theme {      fn appearance(&self, style: Self::Style) -> text::Appearance {          match style { -            Text::Default => Default::default(), +            Text::Default => text::Appearance::default(),              Text::Color(c) => text::Appearance { color: Some(c) },          }      } @@ -1157,3 +1175,115 @@ impl text_input::StyleSheet for Theme {          self.placeholder_color(style)      }  } + +/// The style of a text input. +#[derive(Default)] +pub enum TextEditor { +    /// The default style. +    #[default] +    Default, +    /// A custom style. +    Custom(Box<dyn text_editor::StyleSheet<Style = Theme>>), +} + +impl text_editor::StyleSheet for Theme { +    type Style = TextEditor; + +    fn active(&self, style: &Self::Style) -> text_editor::Appearance { +        if let TextEditor::Custom(custom) = style { +            return custom.active(self); +        } + +        let palette = self.extended_palette(); + +        text_editor::Appearance { +            background: palette.background.base.color.into(), +            border_radius: 2.0.into(), +            border_width: 1.0, +            border_color: palette.background.strong.color, +        } +    } + +    fn hovered(&self, style: &Self::Style) -> text_editor::Appearance { +        if let TextEditor::Custom(custom) = style { +            return custom.hovered(self); +        } + +        let palette = self.extended_palette(); + +        text_editor::Appearance { +            background: palette.background.base.color.into(), +            border_radius: 2.0.into(), +            border_width: 1.0, +            border_color: palette.background.base.text, +        } +    } + +    fn focused(&self, style: &Self::Style) -> text_editor::Appearance { +        if let TextEditor::Custom(custom) = style { +            return custom.focused(self); +        } + +        let palette = self.extended_palette(); + +        text_editor::Appearance { +            background: palette.background.base.color.into(), +            border_radius: 2.0.into(), +            border_width: 1.0, +            border_color: palette.primary.strong.color, +        } +    } + +    fn placeholder_color(&self, style: &Self::Style) -> Color { +        if let TextEditor::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 TextEditor::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 TextEditor::Custom(custom) = style { +            return custom.selection_color(self); +        } + +        let palette = self.extended_palette(); + +        palette.primary.weak.color +    } + +    fn disabled(&self, style: &Self::Style) -> text_editor::Appearance { +        if let TextEditor::Custom(custom) = style { +            return custom.disabled(self); +        } + +        let palette = self.extended_palette(); + +        text_editor::Appearance { +            background: palette.background.weak.color.into(), +            border_radius: 2.0.into(), +            border_width: 1.0, +            border_color: palette.background.strong.color, +        } +    } + +    fn disabled_color(&self, style: &Self::Style) -> Color { +        if let TextEditor::Custom(custom) = style { +            return custom.disabled_color(self); +        } + +        self.placeholder_color(style) +    } +}  | 
