diff options
| author | 2024-03-06 20:30:58 +0100 | |
|---|---|---|
| committer | 2024-03-06 20:30:58 +0100 | |
| commit | 34e7c6593a9e0f56cee5db18b7258717cf6bc11b (patch) | |
| tree | 7c65a58e9052f2f95a0025355679b13c7002eeab /widget/src/pane_grid | |
| parent | 8a63774b24488f71147a728123551ae72c080d14 (diff) | |
| download | iced-34e7c6593a9e0f56cee5db18b7258717cf6bc11b.tar.gz iced-34e7c6593a9e0f56cee5db18b7258717cf6bc11b.tar.bz2 iced-34e7c6593a9e0f56cee5db18b7258717cf6bc11b.zip | |
Use `Style` struct pattern instead of trait for all widgets
Diffstat (limited to '')
| -rw-r--r-- | widget/src/pane_grid.rs | 36 | ||||
| -rw-r--r-- | widget/src/pane_grid/content.rs | 12 | ||||
| -rw-r--r-- | widget/src/pane_grid/title_bar.rs | 15 | 
3 files changed, 38 insertions, 25 deletions
| diff --git a/widget/src/pane_grid.rs b/widget/src/pane_grid.rs index 62067e66..ae9cd825 100644 --- a/widget/src/pane_grid.rs +++ b/widget/src/pane_grid.rs @@ -112,7 +112,7 @@ pub struct PaneGrid<      on_click: Option<Box<dyn Fn(Pane) -> Message + 'a>>,      on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>,      on_resize: Option<(f32, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>, -    style: fn(&Theme) -> Appearance, +    style: Style<Theme>,  }  impl<'a, Message, Theme, Renderer> PaneGrid<'a, Message, Theme, Renderer> @@ -128,7 +128,7 @@ where          view: impl Fn(Pane, &'a T, bool) -> Content<'a, Message, Theme, Renderer>,      ) -> Self      where -        Theme: Style, +        Style<Theme>: Default,      {          let contents = if let Some((pane, pane_state)) =              state.maximized.and_then(|pane| { @@ -160,7 +160,7 @@ where              on_click: None,              on_drag: None,              on_resize: None, -            style: Theme::style(), +            style: Style::default(),          }      } @@ -221,7 +221,7 @@ where      /// Sets the style of the [`PaneGrid`].      pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { -        self.style = style; +        self.style = Style(style);          self      } @@ -679,7 +679,7 @@ where              None          }; -        let appearance = (self.style)(theme); +        let appearance = (self.style.0)(theme);          for ((id, (content, tree)), pane_layout) in              contents.zip(layout.children()) @@ -1147,15 +1147,27 @@ pub struct Line {      pub width: f32,  } -/// The definiton of the default style of a [`PaneGrid`]. -pub trait Style { -    /// Returns the default style of a [`PaneGrid`]. -    fn style() -> fn(&Self) -> Appearance; +/// The style of a [`PaneGrid`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style<Theme>(fn(&Theme) -> Appearance); + +impl<Theme> Clone for Style<Theme> { +    fn clone(&self) -> Self { +        *self +    } +} + +impl<Theme> Copy for Style<Theme> {} + +impl Default for Style<Theme> { +    fn default() -> Self { +        Style(default) +    }  } -impl Style for Theme { -    fn style() -> fn(&Self) -> Appearance { -        default +impl<Theme> From<fn(&Theme) -> Appearance> for Style<Theme> { +    fn from(f: fn(&Theme) -> Appearance) -> Self { +        Style(f)      }  } diff --git a/widget/src/pane_grid/content.rs b/widget/src/pane_grid/content.rs index 25b64e17..ce29e8d0 100644 --- a/widget/src/pane_grid/content.rs +++ b/widget/src/pane_grid/content.rs @@ -24,7 +24,7 @@ pub struct Content<  {      title_bar: Option<TitleBar<'a, Message, Theme, Renderer>>,      body: Element<'a, Message, Theme, Renderer>, -    style: fn(&Theme, container::Status) -> container::Appearance, +    style: container::Style<Theme>,  }  impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer> @@ -34,12 +34,12 @@ where      /// Creates a new [`Content`] with the provided body.      pub fn new(body: impl Into<Element<'a, Message, Theme, Renderer>>) -> Self      where -        Theme: container::Style, +        container::Style<Theme>: Default,      {          Self {              title_bar: None,              body: body.into(), -            style: Theme::style(), +            style: container::Style::default(),          }      } @@ -57,7 +57,7 @@ where          mut self,          style: fn(&Theme, container::Status) -> container::Appearance,      ) -> Self { -        self.style = style; +        self.style = style.into();          self      }  } @@ -114,7 +114,7 @@ where                      container::Status::Idle                  }; -                (self.style)(theme, status) +                self.style.resolve(theme, status)              };              container::draw_background(renderer, &style, bounds); @@ -403,8 +403,8 @@ impl<'a, T, Message, Theme, Renderer> From<T>      for Content<'a, Message, Theme, Renderer>  where      T: Into<Element<'a, Message, Theme, Renderer>>, -    Theme: container::Style,      Renderer: crate::core::Renderer, +    container::Style<Theme>: Default,  {      fn from(element: T) -> Self {          Self::new(element) diff --git a/widget/src/pane_grid/title_bar.rs b/widget/src/pane_grid/title_bar.rs index 787510cc..b1cdcde3 100644 --- a/widget/src/pane_grid/title_bar.rs +++ b/widget/src/pane_grid/title_bar.rs @@ -25,7 +25,7 @@ pub struct TitleBar<      controls: Option<Element<'a, Message, Theme, Renderer>>,      padding: Padding,      always_show_controls: bool, -    style: fn(&Theme, container::Status) -> container::Appearance, +    style: container::Style<Theme>,  }  impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer> @@ -33,17 +33,18 @@ where      Renderer: crate::core::Renderer,  {      /// Creates a new [`TitleBar`] with the given content. -    pub fn new<E>(content: E) -> Self +    pub fn new( +        content: impl Into<Element<'a, Message, Theme, Renderer>>, +    ) -> Self      where -        Theme: container::Style, -        E: Into<Element<'a, Message, Theme, Renderer>>, +        container::Style<Theme>: Default,      {          Self {              content: content.into(),              controls: None,              padding: Padding::ZERO,              always_show_controls: false, -            style: Theme::style(), +            style: container::Style::default(),          }      } @@ -67,7 +68,7 @@ where          mut self,          style: fn(&Theme, container::Status) -> container::Appearance,      ) -> Self { -        self.style = style; +        self.style = style.into();          self      } @@ -137,7 +138,7 @@ where                  container::Status::Idle              }; -            (self.style)(theme, status) +            self.style.resolve(theme, status)          };          let inherited_style = renderer::Style { | 
