diff options
| author | 2020-12-08 18:47:01 -0600 | |
|---|---|---|
| committer | 2020-12-10 12:17:36 -0600 | |
| commit | f54590d7adac611db84b88cbcbf4f56c7542039c (patch) | |
| tree | d2f15219f22ae1e1c3e1566163e413c466962be7 /native/src/widget | |
| parent | d16b9cf7cd98a3d65ea5408ac9b72298cb267e85 (diff) | |
| download | iced-f54590d7adac611db84b88cbcbf4f56c7542039c.tar.gz iced-f54590d7adac611db84b88cbcbf4f56c7542039c.tar.bz2 iced-f54590d7adac611db84b88cbcbf4f56c7542039c.zip | |
Replace TitleBar string title with generic Content
Diffstat (limited to '')
| -rw-r--r-- | native/src/widget/pane_grid.rs | 9 | ||||
| -rw-r--r-- | native/src/widget/pane_grid/title_bar.rs | 111 | 
2 files changed, 41 insertions, 79 deletions
| diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index ff19cbc2..85ef021f 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -586,18 +586,15 @@ pub trait Renderer:      /// It receives:      /// - the bounds, style of the [`TitleBar`]      /// - the style of the [`TitleBar`] -    /// - the title of the [`TitleBar`] with its size, font, and bounds -    /// - the controls of the [`TitleBar`] with their [`Layout`+, if any +    /// - the content of the [`TitleBar`] with its layout +    /// - the controls of the [`TitleBar`] with their [`Layout`], if any      /// - the cursor position      fn draw_title_bar<Message>(          &mut self,          defaults: &Self::Defaults,          bounds: Rectangle,          style: &Self::Style, -        title: &str, -        title_size: u16, -        title_font: Self::Font, -        title_bounds: Rectangle, +        content: (&Element<'_, Message, Self>, Layout<'_>),          controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,          cursor_position: Point,      ) -> Self::Output; diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs index 475cb9ae..a73acff7 100644 --- a/native/src/widget/pane_grid/title_bar.rs +++ b/native/src/widget/pane_grid/title_bar.rs @@ -1,30 +1,31 @@  use crate::event::{self, Event};  use crate::layout;  use crate::pane_grid; -use crate::{Clipboard, Element, Hasher, Layout, Point, Rectangle, Size}; +use crate::{Clipboard, Element, Hasher, Layout, Point, Size};  /// The title bar of a [`Pane`].  ///  /// [`Pane`]: crate::widget::pane_grid::Pane  #[allow(missing_debug_implementations)]  pub struct TitleBar<'a, Message, Renderer: pane_grid::Renderer> { -    title: String, -    title_size: Option<u16>, +    content: Element<'a, Message, Renderer>,      controls: Option<Element<'a, Message, Renderer>>,      padding: u16,      always_show_controls: bool,      style: Renderer::Style,  } -impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer> +impl<'a, Message, Renderer: 'a> TitleBar<'a, Message, Renderer>  where      Renderer: pane_grid::Renderer,  { -    /// Creates a new [`TitleBar`] with the given title. -    pub fn new(title: impl Into<String>) -> Self { +    /// Creates a new [`TitleBar`] with the given content. +    pub fn new<E>(content: E) -> Self +    where +        E: Into<Element<'a, Message, Renderer>>, +    {          Self { -            title: title.into(), -            title_size: None, +            content: content.into(),              controls: None,              padding: 0,              always_show_controls: false, @@ -32,12 +33,6 @@ where          }      } -    /// Sets the size of the title of the [`TitleBar`]. -    pub fn title_size(mut self, size: u16) -> Self { -        self.title_size = Some(size); -        self -    } -      /// Sets the controls of the [`TitleBar`].      pub fn controls(          mut self, @@ -91,48 +86,29 @@ where          let mut children = layout.children();          let padded = children.next().unwrap(); -        if let Some(controls) = &self.controls { -            let mut children = padded.children(); -            let title_layout = children.next().unwrap(); +        let mut children = padded.children(); +        let title_layout = children.next().unwrap(); + +        let controls = if let Some(controls) = &self.controls {              let controls_layout = children.next().unwrap(); -            let (title_bounds, controls) = -                if show_controls || self.always_show_controls { -                    (title_layout.bounds(), Some((controls, controls_layout))) -                } else { -                    ( -                        Rectangle { -                            width: padded.bounds().width, -                            ..title_layout.bounds() -                        }, -                        None, -                    ) -                }; - -            renderer.draw_title_bar( -                defaults, -                layout.bounds(), -                &self.style, -                &self.title, -                self.title_size.unwrap_or(renderer.default_size()), -                Renderer::Font::default(), -                title_bounds, -                controls, -                cursor_position, -            ) +            if show_controls || self.always_show_controls { +                Some((controls, controls_layout)) +            } else { +                None +            }          } else { -            renderer.draw_title_bar::<()>( -                defaults, -                layout.bounds(), -                &self.style, -                &self.title, -                self.title_size.unwrap_or(renderer.default_size()), -                Renderer::Font::default(), -                padded.bounds(), -                None, -                cursor_position, -            ) -        } +            None +        }; + +        renderer.draw_title_bar( +            defaults, +            layout.bounds(), +            &self.style, +            (&self.content, title_layout), +            controls, +            cursor_position, +        )      }      /// Returns whether the mouse cursor is over the pick area of the @@ -165,8 +141,7 @@ where      pub(crate) fn hash_layout(&self, hasher: &mut Hasher) {          use std::hash::Hash; -        self.title.hash(hasher); -        self.title_size.hash(hasher); +        self.content.hash_layout(hasher);          self.padding.hash(hasher);      } @@ -179,15 +154,10 @@ where          let limits = limits.pad(padding);          let max_size = limits.max(); -        let title_size = self.title_size.unwrap_or(renderer.default_size()); -        let title_font = Renderer::Font::default(); - -        let (title_width, title_height) = renderer.measure( -            &self.title, -            title_size, -            title_font, -            Size::new(f32::INFINITY, max_size.height), -        ); +        let title_layout = self +            .content +            .layout(renderer, &layout::Limits::new(Size::ZERO, max_size)); +        let title_size = title_layout.size();          let mut node = if let Some(controls) = &self.controls {              let mut controls_layout = controls @@ -196,16 +166,8 @@ where              let controls_size = controls_layout.size();              let space_before_controls = max_size.width - controls_size.width; -            let mut title_layout = layout::Node::new(Size::new( -                title_width.min(space_before_controls), -                title_height, -            )); - -            let title_size = title_layout.size();              let height = title_size.height.max(controls_size.height); -            title_layout -                .move_to(Point::new(0.0, (height - title_size.height) / 2.0));              controls_layout.move_to(Point::new(space_before_controls, 0.0));              layout::Node::with_children( @@ -213,7 +175,10 @@ where                  vec![title_layout, controls_layout],              )          } else { -            layout::Node::new(Size::new(max_size.width, title_height)) +            layout::Node::with_children( +                Size::new(max_size.width, title_size.height), +                vec![title_layout], +            )          };          node.move_to(Point::new(padding, padding)); | 
