From a11bcf5af0be26671ba90097c64021014ab2092d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 4 Jun 2020 07:13:38 +0200 Subject: Draft first-class `TitleBar` in `pane_grid` --- native/src/widget/pane_grid/configuration.rs | 30 +++++++ native/src/widget/pane_grid/content.rs | 112 ++++++++++++++++++++------- native/src/widget/pane_grid/state.rs | 21 ++--- native/src/widget/pane_grid/title_bar.rs | 6 ++ 4 files changed, 133 insertions(+), 36 deletions(-) create mode 100644 native/src/widget/pane_grid/configuration.rs create mode 100644 native/src/widget/pane_grid/title_bar.rs (limited to 'native/src/widget/pane_grid') diff --git a/native/src/widget/pane_grid/configuration.rs b/native/src/widget/pane_grid/configuration.rs new file mode 100644 index 00000000..1fed98b7 --- /dev/null +++ b/native/src/widget/pane_grid/configuration.rs @@ -0,0 +1,30 @@ +use crate::pane_grid::Axis; + +/// The arrangement of a [`PaneGrid`]. +/// +/// [`PaneGrid`]: struct.PaneGrid.html +#[derive(Debug, Clone)] +pub enum Configuration { + /// A split of the available space. + Split { + /// The direction of the split. + axis: Axis, + + /// The ratio of the split in [0.0, 1.0]. + ratio: f32, + + /// The left/top [`Content`] of the split. + /// + /// [`Configuration`]: enum.Node.html + a: Box>, + + /// The right/bottom [`Content`] of the split. + /// + /// [`Configuration`]: enum.Node.html + b: Box>, + }, + /// A [`Pane`]. + /// + /// [`Pane`]: struct.Pane.html + Pane(T), +} diff --git a/native/src/widget/pane_grid/content.rs b/native/src/widget/pane_grid/content.rs index 8822083e..e1374c82 100644 --- a/native/src/widget/pane_grid/content.rs +++ b/native/src/widget/pane_grid/content.rs @@ -1,30 +1,88 @@ -use crate::pane_grid::Axis; +use crate::layout; +use crate::pane_grid::{self, TitleBar}; +use crate::{Clipboard, Element, Event, Hasher, Layout, Point}; -/// The content of a [`PaneGrid`]. +/// The content of a [`Pane`]. /// -/// [`PaneGrid`]: struct.PaneGrid.html -#[derive(Debug, Clone)] -pub enum Content { - /// A split of the available space. - Split { - /// The direction of the split. - axis: Axis, - - /// The ratio of the split in [0.0, 1.0]. - ratio: f32, - - /// The left/top [`Content`] of the split. - /// - /// [`Content`]: enum.Node.html - a: Box>, - - /// The right/bottom [`Content`] of the split. - /// - /// [`Content`]: enum.Node.html - b: Box>, - }, - /// A [`Pane`]. - /// - /// [`Pane`]: struct.Pane.html - Pane(T), +/// [`Pane`]: struct.Pane.html +pub struct Content<'a, Message, Renderer> { + title_bar: Option>, + body: Element<'a, Message, Renderer>, +} + +impl<'a, Message, Renderer> Content<'a, Message, Renderer> { + pub fn new(body: impl Into>) -> Self { + Self { + title_bar: None, + body: body.into(), + } + } + + pub fn title_bar( + mut self, + title_bar: TitleBar<'a, Message, Renderer>, + ) -> Self { + self.title_bar = Some(title_bar); + self + } +} + +impl<'a, Message, Renderer> Content<'a, Message, Renderer> +where + Renderer: pane_grid::Renderer, +{ + pub fn draw( + &self, + renderer: &mut Renderer, + defaults: &Renderer::Defaults, + layout: Layout<'_>, + cursor_position: Point, + ) -> Renderer::Output { + renderer.draw_pane( + defaults, + self.title_bar.as_ref(), + &self.body, + layout, + cursor_position, + ) + } + + pub(crate) fn is_over_drag_target( + &self, + _layout: Layout<'_>, + _cursor_position: Point, + ) -> bool { + false + } + + pub(crate) fn layout( + &self, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + self.body.layout(renderer, limits) + } + + pub(crate) fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + renderer: &Renderer, + clipboard: Option<&dyn Clipboard>, + ) { + self.body.on_event( + event, + layout, + cursor_position, + messages, + renderer, + clipboard, + ) + } + + pub(crate) fn hash_layout(&self, state: &mut Hasher) { + self.body.hash_layout(state); + } } diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index 4b13fb8e..943120e3 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -1,6 +1,6 @@ use crate::{ keyboard, - pane_grid::{Axis, Content, Direction, Node, Pane, Split}, + pane_grid::{Axis, Configuration, Direction, Node, Pane, Split}, Hasher, Point, Rectangle, Size, }; @@ -53,18 +53,21 @@ impl State { /// [`State`]: struct.State.html /// [`Pane`]: struct.Pane.html pub fn new(first_pane_state: T) -> (Self, Pane) { - (Self::with_content(Content::Pane(first_pane_state)), Pane(0)) + ( + Self::with_configuration(Configuration::Pane(first_pane_state)), + Pane(0), + ) } - /// Creates a new [`State`] with the given [`Content`]. + /// Creates a new [`State`] with the given [`Configuration`]. /// /// [`State`]: struct.State.html - /// [`Content`]: enum.Content.html - pub fn with_content(content: impl Into>) -> Self { + /// [`Configuration`]: enum.Configuration.html + pub fn with_configuration(config: impl Into>) -> Self { let mut panes = HashMap::new(); let (layout, last_id) = - Self::distribute_content(&mut panes, content.into(), 0); + Self::distribute_content(&mut panes, config.into(), 0); State { panes, @@ -274,11 +277,11 @@ impl State { fn distribute_content( panes: &mut HashMap, - content: Content, + content: Configuration, next_id: usize, ) -> (Node, usize) { match content { - Content::Split { axis, ratio, a, b } => { + Configuration::Split { axis, ratio, a, b } => { let (a, next_id) = Self::distribute_content(panes, *a, next_id); let (b, next_id) = Self::distribute_content(panes, *b, next_id); @@ -293,7 +296,7 @@ impl State { next_id + 1, ) } - Content::Pane(state) => { + Configuration::Pane(state) => { let id = Pane(next_id); let _ = panes.insert(id, state); diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs new file mode 100644 index 00000000..fa868a20 --- /dev/null +++ b/native/src/widget/pane_grid/title_bar.rs @@ -0,0 +1,6 @@ +use crate::Element; + +pub struct TitleBar<'a, Message, Renderer> { + title: String, + buttons: Option>, +} -- cgit From 4e1e0e0890b83fb1c4c4406791e3aa63bba58a93 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Jun 2020 06:52:07 +0200 Subject: Draft drawing logic for `Content` and `TitleBar` --- native/src/widget/pane_grid/content.rs | 77 +++++++++++++++++--- native/src/widget/pane_grid/title_bar.rs | 121 ++++++++++++++++++++++++++++++- 2 files changed, 182 insertions(+), 16 deletions(-) (limited to 'native/src/widget/pane_grid') diff --git a/native/src/widget/pane_grid/content.rs b/native/src/widget/pane_grid/content.rs index e1374c82..a30b0e7d 100644 --- a/native/src/widget/pane_grid/content.rs +++ b/native/src/widget/pane_grid/content.rs @@ -1,16 +1,20 @@ +use crate::container; use crate::layout; use crate::pane_grid::{self, TitleBar}; -use crate::{Clipboard, Element, Event, Hasher, Layout, Point}; +use crate::{Clipboard, Element, Event, Hasher, Layout, Point, Size}; /// The content of a [`Pane`]. /// /// [`Pane`]: struct.Pane.html -pub struct Content<'a, Message, Renderer> { +pub struct Content<'a, Message, Renderer: container::Renderer> { title_bar: Option>, body: Element<'a, Message, Renderer>, } -impl<'a, Message, Renderer> Content<'a, Message, Renderer> { +impl<'a, Message, Renderer> Content<'a, Message, Renderer> +where + Renderer: container::Renderer, +{ pub fn new(body: impl Into>) -> Self { Self { title_bar: None, @@ -29,7 +33,7 @@ impl<'a, Message, Renderer> Content<'a, Message, Renderer> { impl<'a, Message, Renderer> Content<'a, Message, Renderer> where - Renderer: pane_grid::Renderer, + Renderer: pane_grid::Renderer + container::Renderer, { pub fn draw( &self, @@ -38,13 +42,25 @@ where layout: Layout<'_>, cursor_position: Point, ) -> Renderer::Output { - renderer.draw_pane( - defaults, - self.title_bar.as_ref(), - &self.body, - layout, - cursor_position, - ) + if let Some(title_bar) = &self.title_bar { + let mut children = layout.children(); + let title_bar_layout = children.next().unwrap(); + let body_layout = children.next().unwrap(); + + renderer.draw_pane( + defaults, + Some((title_bar, title_bar_layout)), + (&self.body, body_layout), + cursor_position, + ) + } else { + renderer.draw_pane( + defaults, + None, + (&self.body, layout), + cursor_position, + ) + } } pub(crate) fn is_over_drag_target( @@ -60,7 +76,34 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - self.body.layout(renderer, limits) + if let Some(title_bar) = &self.title_bar { + let max_size = limits.max(); + + let title_bar_layout = title_bar + .layout(renderer, &layout::Limits::new(Size::ZERO, max_size)); + + let title_bar_size = title_bar_layout.size(); + + let mut body_layout = self.body.layout( + renderer, + &layout::Limits::new( + Size::ZERO, + Size::new( + max_size.width, + max_size.height - title_bar_size.height, + ), + ), + ); + + body_layout.move_to(Point::new(0.0, title_bar_size.height)); + + layout::Node::with_children( + max_size, + vec![title_bar_layout, body_layout], + ) + } else { + self.body.layout(renderer, limits) + } } pub(crate) fn on_event( @@ -86,3 +129,13 @@ where self.body.hash_layout(state); } } + +impl<'a, T, Message, Renderer> From for Content<'a, Message, Renderer> +where + T: Into>, + Renderer: pane_grid::Renderer + container::Renderer, +{ + fn from(element: T) -> Self { + Self::new(element) + } +} diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs index fa868a20..1fd06411 100644 --- a/native/src/widget/pane_grid/title_bar.rs +++ b/native/src/widget/pane_grid/title_bar.rs @@ -1,6 +1,119 @@ -use crate::Element; +use crate::container; +use crate::layout; +use crate::pane_grid; +use crate::{Element, Layout, Point, Size}; -pub struct TitleBar<'a, Message, Renderer> { - title: String, - buttons: Option>, +pub struct TitleBar<'a, Message, Renderer: container::Renderer> { + title: Element<'a, Message, Renderer>, + controls: Option>, + padding: u16, + style: Renderer::Style, +} + +impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer> +where + Renderer: container::Renderer, +{ + pub fn new(title: impl Into>) -> Self { + Self { + title: title.into(), + controls: None, + padding: 0, + style: Renderer::Style::default(), + } + } + + pub fn controls( + mut self, + controls: impl Into>, + ) -> Self { + self.controls = Some(controls.into()); + self + } + + /// Sets the padding of the [`TitleBar`]. + /// + /// [`TitleBar`]: struct.TitleBar.html + pub fn padding(mut self, units: u16) -> Self { + self.padding = units; + self + } +} + +impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer> +where + Renderer: pane_grid::Renderer, +{ + pub fn draw( + &self, + renderer: &mut Renderer, + defaults: &Renderer::Defaults, + layout: Layout<'_>, + cursor_position: Point, + ) -> Renderer::Output { + if let Some(controls) = &self.controls { + let mut children = layout.children(); + let title_layout = children.next().unwrap(); + let controls_layout = children.next().unwrap(); + + renderer.draw_title_bar( + defaults, + &self.style, + (&self.title, title_layout), + Some((controls, controls_layout)), + cursor_position, + ) + } else { + renderer.draw_title_bar( + defaults, + &self.style, + (&self.title, layout), + None, + cursor_position, + ) + } + } + + pub(crate) fn layout( + &self, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let padding = f32::from(self.padding); + let limits = limits.pad(padding); + + let mut node = if let Some(controls) = &self.controls { + let max_size = limits.max(); + + let title_layout = self + .title + .layout(renderer, &layout::Limits::new(Size::ZERO, max_size)); + + let title_size = title_layout.size(); + + let mut controls_layout = controls.layout( + renderer, + &layout::Limits::new( + Size::ZERO, + Size::new( + max_size.width - title_size.width, + max_size.height, + ), + ), + ); + + controls_layout.move_to(Point::new(title_size.width, 0.0)); + + layout::Node::with_children( + max_size, + vec![title_layout, controls_layout], + ) + } else { + self.title.layout(renderer, &limits) + }; + + node.move_to(Point::new(padding, padding)); + + node + } } -- cgit From 4dc5bffdfbfb09a017f35c12b484301fcf044876 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Jun 2020 14:02:29 +0200 Subject: Draft draggable and graphics logic for `TitleBar` --- native/src/widget/pane_grid/content.rs | 60 +++++++++++++++--- native/src/widget/pane_grid/state.rs | 16 +++-- native/src/widget/pane_grid/title_bar.rs | 103 ++++++++++++++++++++++++++----- 3 files changed, 148 insertions(+), 31 deletions(-) (limited to 'native/src/widget/pane_grid') diff --git a/native/src/widget/pane_grid/content.rs b/native/src/widget/pane_grid/content.rs index a30b0e7d..843dcec4 100644 --- a/native/src/widget/pane_grid/content.rs +++ b/native/src/widget/pane_grid/content.rs @@ -1,7 +1,7 @@ use crate::container; use crate::layout; use crate::pane_grid::{self, TitleBar}; -use crate::{Clipboard, Element, Event, Hasher, Layout, Point, Size}; +use crate::{Clipboard, Element, Event, Hasher, Layout, Point, Size, Vector}; /// The content of a [`Pane`]. /// @@ -9,6 +9,7 @@ use crate::{Clipboard, Element, Event, Hasher, Layout, Point, Size}; pub struct Content<'a, Message, Renderer: container::Renderer> { title_bar: Option>, body: Element<'a, Message, Renderer>, + style: Renderer::Style, } impl<'a, Message, Renderer> Content<'a, Message, Renderer> @@ -19,6 +20,7 @@ where Self { title_bar: None, body: body.into(), + style: Renderer::Style::default(), } } @@ -29,6 +31,14 @@ where self.title_bar = Some(title_bar); self } + + /// Sets the style of the [`TitleBar`]. + /// + /// [`TitleBar`]: struct.TitleBar.html + pub fn style(mut self, style: impl Into) -> Self { + self.style = style.into(); + self + } } impl<'a, Message, Renderer> Content<'a, Message, Renderer> @@ -49,6 +59,8 @@ where renderer.draw_pane( defaults, + layout.bounds(), + &self.style, Some((title_bar, title_bar_layout)), (&self.body, body_layout), cursor_position, @@ -56,6 +68,8 @@ where } else { renderer.draw_pane( defaults, + layout.bounds(), + &self.style, None, (&self.body, layout), cursor_position, @@ -63,12 +77,25 @@ where } } - pub(crate) fn is_over_drag_target( + pub fn drag_origin( &self, - _layout: Layout<'_>, - _cursor_position: Point, - ) -> bool { - false + layout: Layout<'_>, + cursor_position: Point, + ) -> Option { + if let Some(title_bar) = &self.title_bar { + let mut children = layout.children(); + let title_bar_layout = children.next().unwrap(); + + if title_bar.is_over_draggable(title_bar_layout, cursor_position) { + let position = layout.position(); + + Some(cursor_position - Vector::new(position.x, position.y)) + } else { + None + } + } else { + None + } } pub(crate) fn layout( @@ -115,14 +142,31 @@ where renderer: &Renderer, clipboard: Option<&dyn Clipboard>, ) { + let body_layout = if let Some(title_bar) = &mut self.title_bar { + let mut children = layout.children(); + + title_bar.on_event( + event.clone(), + children.next().unwrap(), + cursor_position, + messages, + renderer, + clipboard, + ); + + children.next().unwrap() + } else { + layout + }; + self.body.on_event( event, - layout, + body_layout, cursor_position, messages, renderer, clipboard, - ) + ); } pub(crate) fn hash_layout(&self, state: &mut Hasher) { diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index 943120e3..a8431dec 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -313,13 +313,14 @@ pub struct Internal { action: Action, } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum Action { Idle { focus: Option, }, Dragging { pane: Pane, + origin: Point, }, Resizing { split: Split, @@ -334,7 +335,7 @@ impl Action { Action::Idle { focus } | Action::Resizing { focus, .. } => { focus.map(|pane| (pane, Focus::Idle)) } - Action::Dragging { pane } => Some((*pane, Focus::Dragging)), + Action::Dragging { pane, .. } => Some((*pane, Focus::Dragging)), } } } @@ -351,9 +352,9 @@ impl Internal { } } - pub fn picked_pane(&self) -> Option { + pub fn picked_pane(&self) -> Option<(Pane, Point)> { match self.action { - Action::Dragging { pane } => Some(pane), + Action::Dragging { pane, origin } => Some((pane, origin)), _ => None, } } @@ -385,8 +386,11 @@ impl Internal { self.action = Action::Idle { focus: Some(*pane) }; } - pub fn pick_pane(&mut self, pane: &Pane) { - self.action = Action::Dragging { pane: *pane }; + pub fn pick_pane(&mut self, pane: &Pane, origin: Point) { + self.action = Action::Dragging { + pane: *pane, + origin, + }; } pub fn pick_split(&mut self, split: &Split, axis: Axis) { diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs index 1fd06411..dc96dcbf 100644 --- a/native/src/widget/pane_grid/title_bar.rs +++ b/native/src/widget/pane_grid/title_bar.rs @@ -1,7 +1,7 @@ use crate::container; use crate::layout; use crate::pane_grid; -use crate::{Element, Layout, Point, Size}; +use crate::{Clipboard, Element, Event, Layout, Point, Size}; pub struct TitleBar<'a, Message, Renderer: container::Renderer> { title: Element<'a, Message, Renderer>, @@ -38,6 +38,14 @@ where self.padding = units; self } + + /// Sets the style of the [`TitleBar`]. + /// + /// [`TitleBar`]: struct.TitleBar.html + pub fn style(mut self, style: impl Into) -> Self { + self.style = style.into(); + self + } } impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer> @@ -50,30 +58,63 @@ where defaults: &Renderer::Defaults, layout: Layout<'_>, cursor_position: Point, + show_controls: bool, ) -> Renderer::Output { + let mut children = layout.children(); + let padded = children.next().unwrap(); + if let Some(controls) = &self.controls { - let mut children = layout.children(); + let mut children = padded.children(); let title_layout = children.next().unwrap(); let controls_layout = children.next().unwrap(); renderer.draw_title_bar( defaults, + layout.bounds(), &self.style, (&self.title, title_layout), - Some((controls, controls_layout)), + if show_controls { + Some((controls, controls_layout)) + } else { + None + }, cursor_position, ) } else { renderer.draw_title_bar( defaults, + layout.bounds(), &self.style, - (&self.title, layout), + (&self.title, padded), None, cursor_position, ) } } + pub fn is_over_draggable( + &self, + layout: Layout<'_>, + cursor_position: Point, + ) -> bool { + if layout.bounds().contains(cursor_position) { + let mut children = layout.children(); + let padded = children.next().unwrap(); + + if self.controls.is_some() { + let mut children = padded.children(); + let _ = children.next().unwrap(); + let controls_layout = children.next().unwrap(); + + !controls_layout.bounds().contains(cursor_position) + } else { + true + } + } else { + false + } + } + pub(crate) fn layout( &self, renderer: &Renderer, @@ -82,38 +123,66 @@ where let padding = f32::from(self.padding); let limits = limits.pad(padding); - let mut node = if let Some(controls) = &self.controls { + let node = if let Some(controls) = &self.controls { let max_size = limits.max(); - let title_layout = self - .title + let mut controls_layout = controls .layout(renderer, &layout::Limits::new(Size::ZERO, max_size)); - let title_size = title_layout.size(); + let controls_size = controls_layout.size(); + let space_before_controls = max_size.width - controls_size.width; - let mut controls_layout = controls.layout( + let mut title_layout = self.title.layout( renderer, &layout::Limits::new( Size::ZERO, - Size::new( - max_size.width - title_size.width, - max_size.height, - ), + Size::new(space_before_controls, max_size.height), ), ); - controls_layout.move_to(Point::new(title_size.width, 0.0)); + title_layout.move_to(Point::new(padding, padding)); + controls_layout + .move_to(Point::new(space_before_controls + padding, padding)); + + let title_size = title_layout.size(); + let height = title_size.height.max(controls_size.height); layout::Node::with_children( - max_size, + Size::new(max_size.width, height), vec![title_layout, controls_layout], ) } else { self.title.layout(renderer, &limits) }; - node.move_to(Point::new(padding, padding)); + layout::Node::with_children(node.size().pad(padding), vec![node]) + } + + pub(crate) fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + renderer: &Renderer, + clipboard: Option<&dyn Clipboard>, + ) { + if let Some(controls) = &mut self.controls { + let mut children = layout.children(); + let padded = children.next().unwrap(); + + let mut children = padded.children(); + let _ = children.next(); + let controls_layout = children.next().unwrap(); - node + controls.on_event( + event, + controls_layout, + cursor_position, + messages, + renderer, + clipboard, + ); + } } } -- cgit From 3cfe6e428be22fdbf715f1f28caec0c802fd069e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Jun 2020 16:27:28 +0200 Subject: Lay out title text dynamically in `TitleBar` --- native/src/widget/pane_grid/content.rs | 6 +-- native/src/widget/pane_grid/title_bar.rs | 90 ++++++++++++++++++++++---------- 2 files changed, 65 insertions(+), 31 deletions(-) (limited to 'native/src/widget/pane_grid') diff --git a/native/src/widget/pane_grid/content.rs b/native/src/widget/pane_grid/content.rs index 843dcec4..55fa4c60 100644 --- a/native/src/widget/pane_grid/content.rs +++ b/native/src/widget/pane_grid/content.rs @@ -6,7 +6,7 @@ use crate::{Clipboard, Element, Event, Hasher, Layout, Point, Size, Vector}; /// The content of a [`Pane`]. /// /// [`Pane`]: struct.Pane.html -pub struct Content<'a, Message, Renderer: container::Renderer> { +pub struct Content<'a, Message, Renderer: pane_grid::Renderer> { title_bar: Option>, body: Element<'a, Message, Renderer>, style: Renderer::Style, @@ -14,7 +14,7 @@ pub struct Content<'a, Message, Renderer: container::Renderer> { impl<'a, Message, Renderer> Content<'a, Message, Renderer> where - Renderer: container::Renderer, + Renderer: pane_grid::Renderer, { pub fn new(body: impl Into>) -> Self { Self { @@ -43,7 +43,7 @@ where impl<'a, Message, Renderer> Content<'a, Message, Renderer> where - Renderer: pane_grid::Renderer + container::Renderer, + Renderer: pane_grid::Renderer, { pub fn draw( &self, diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs index dc96dcbf..bdf87c17 100644 --- a/native/src/widget/pane_grid/title_bar.rs +++ b/native/src/widget/pane_grid/title_bar.rs @@ -1,10 +1,10 @@ -use crate::container; use crate::layout; use crate::pane_grid; -use crate::{Clipboard, Element, Event, Layout, Point, Size}; +use crate::{Clipboard, Element, Event, Layout, Point, Rectangle, Size}; -pub struct TitleBar<'a, Message, Renderer: container::Renderer> { - title: Element<'a, Message, Renderer>, +pub struct TitleBar<'a, Message, Renderer: pane_grid::Renderer> { + title: String, + title_size: Option, controls: Option>, padding: u16, style: Renderer::Style, @@ -12,17 +12,29 @@ pub struct TitleBar<'a, Message, Renderer: container::Renderer> { impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer> where - Renderer: container::Renderer, + Renderer: pane_grid::Renderer, { - pub fn new(title: impl Into>) -> Self { + pub fn new(title: impl Into) -> Self { Self { title: title.into(), + title_size: None, controls: None, padding: 0, style: Renderer::Style::default(), } } + /// Sets the size of the title of the [`TitleBar`]. + /// + /// [`TitleBar`]: struct.Text.html + pub fn title_size(mut self, size: u16) -> Self { + self.title_size = Some(size); + self + } + + /// Sets the controls of the [`TitleBar`]. + /// + /// [`TitleBar`]: struct.TitleBar.html pub fn controls( mut self, controls: impl Into>, @@ -68,24 +80,38 @@ where let title_layout = children.next().unwrap(); let controls_layout = children.next().unwrap(); + let (title_bounds, controls) = if 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, title_layout), - if show_controls { - Some((controls, controls_layout)) - } else { - None - }, + &self.title, + self.title_size.unwrap_or(Renderer::DEFAULT_SIZE), + Renderer::Font::default(), + title_bounds, + controls, cursor_position, ) } else { - renderer.draw_title_bar( + renderer.draw_title_bar::<()>( defaults, layout.bounds(), &self.style, - (&self.title, padded), + &self.title, + self.title_size.unwrap_or(Renderer::DEFAULT_SIZE), + Renderer::Font::default(), + padded.bounds(), None, cursor_position, ) @@ -122,39 +148,47 @@ where ) -> layout::Node { let padding = f32::from(self.padding); let limits = limits.pad(padding); + let max_size = limits.max(); - let node = if let Some(controls) = &self.controls { - 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 mut node = if let Some(controls) = &self.controls { let mut controls_layout = controls .layout(renderer, &layout::Limits::new(Size::ZERO, max_size)); let controls_size = controls_layout.size(); let space_before_controls = max_size.width - controls_size.width; - let mut title_layout = self.title.layout( - renderer, - &layout::Limits::new( - Size::ZERO, - Size::new(space_before_controls, max_size.height), - ), - ); - - title_layout.move_to(Point::new(padding, padding)); - controls_layout - .move_to(Point::new(space_before_controls + padding, padding)); + 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( Size::new(max_size.width, height), vec![title_layout, controls_layout], ) } else { - self.title.layout(renderer, &limits) + layout::Node::new(Size::new(title_width, title_height)) }; + node.move_to(Point::new(padding, padding)); + layout::Node::with_children(node.size().pad(padding), vec![node]) } -- cgit From 733ec6b2eafd3e7267409e49f2e6cf497f7c7074 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 8 Jul 2020 11:48:16 +0200 Subject: Fix default text size in `TitleBar` --- native/src/widget/pane_grid/title_bar.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'native/src/widget/pane_grid') diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs index bdf87c17..cf5f0a59 100644 --- a/native/src/widget/pane_grid/title_bar.rs +++ b/native/src/widget/pane_grid/title_bar.rs @@ -97,7 +97,7 @@ where layout.bounds(), &self.style, &self.title, - self.title_size.unwrap_or(Renderer::DEFAULT_SIZE), + self.title_size.unwrap_or(renderer.default_size()), Renderer::Font::default(), title_bounds, controls, @@ -109,7 +109,7 @@ where layout.bounds(), &self.style, &self.title, - self.title_size.unwrap_or(Renderer::DEFAULT_SIZE), + self.title_size.unwrap_or(renderer.default_size()), Renderer::Font::default(), padded.bounds(), None, @@ -150,7 +150,7 @@ where let limits = limits.pad(padding); let max_size = limits.max(); - let title_size = self.title_size.unwrap_or(Renderer::DEFAULT_SIZE); + let title_size = self.title_size.unwrap_or(renderer.default_size()); let title_font = Renderer::Font::default(); let (title_width, title_height) = renderer.measure( -- cgit From e3cd947437cad1a53715b77b63e6c7a348de97c5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 9 Jul 2020 05:26:11 +0200 Subject: Write documentation for new `PaneGrid` API --- native/src/widget/pane_grid/content.rs | 34 +++++++++++++++++++++----------- native/src/widget/pane_grid/title_bar.rs | 22 +++++++++++++++++++-- 2 files changed, 43 insertions(+), 13 deletions(-) (limited to 'native/src/widget/pane_grid') diff --git a/native/src/widget/pane_grid/content.rs b/native/src/widget/pane_grid/content.rs index 55fa4c60..1f5ce640 100644 --- a/native/src/widget/pane_grid/content.rs +++ b/native/src/widget/pane_grid/content.rs @@ -1,11 +1,12 @@ use crate::container; use crate::layout; use crate::pane_grid::{self, TitleBar}; -use crate::{Clipboard, Element, Event, Hasher, Layout, Point, Size, Vector}; +use crate::{Clipboard, Element, Event, Hasher, Layout, Point, Size}; /// The content of a [`Pane`]. /// /// [`Pane`]: struct.Pane.html +#[allow(missing_debug_implementations)] pub struct Content<'a, Message, Renderer: pane_grid::Renderer> { title_bar: Option>, body: Element<'a, Message, Renderer>, @@ -16,6 +17,9 @@ impl<'a, Message, Renderer> Content<'a, Message, Renderer> where Renderer: pane_grid::Renderer, { + /// Creates a new [`Content`] with the provided body. + /// + /// [`Content`]: struct.Content.html pub fn new(body: impl Into>) -> Self { Self { title_bar: None, @@ -24,6 +28,10 @@ where } } + /// Sets the [`TitleBar`] of this [`Content`]. + /// + /// [`TitleBar`]: struct.TitleBar.html + /// [`Content`]: struct.Content.html pub fn title_bar( mut self, title_bar: TitleBar<'a, Message, Renderer>, @@ -45,6 +53,11 @@ impl<'a, Message, Renderer> Content<'a, Message, Renderer> where Renderer: pane_grid::Renderer, { + /// Draws the [`Content`] with the provided [`Renderer`] and [`Layout`]. + /// + /// [`Content`]: struct.Content.html + /// [`Renderer`]: trait.Renderer.html + /// [`Layout`]: ../layout/struct.Layout.html pub fn draw( &self, renderer: &mut Renderer, @@ -77,24 +90,23 @@ where } } - pub fn drag_origin( + /// Returns whether the [`Content`] with the given [`Layout`] can be picked + /// at the provided cursor position. + /// + /// [`Content`]: struct.Content.html + /// [`Layout`]: ../layout/struct.Layout.html + pub fn can_be_picked_at( &self, layout: Layout<'_>, cursor_position: Point, - ) -> Option { + ) -> bool { if let Some(title_bar) = &self.title_bar { let mut children = layout.children(); let title_bar_layout = children.next().unwrap(); - if title_bar.is_over_draggable(title_bar_layout, cursor_position) { - let position = layout.position(); - - Some(cursor_position - Vector::new(position.x, position.y)) - } else { - None - } + title_bar.is_over_pick_area(title_bar_layout, cursor_position) } else { - None + false } } diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs index cf5f0a59..775c8a8f 100644 --- a/native/src/widget/pane_grid/title_bar.rs +++ b/native/src/widget/pane_grid/title_bar.rs @@ -2,6 +2,10 @@ use crate::layout; use crate::pane_grid; use crate::{Clipboard, Element, Event, Layout, Point, Rectangle, Size}; +/// The title bar of a [`Pane`]. +/// +/// [`Pane`]: struct.Pane.html +#[allow(missing_debug_implementations)] pub struct TitleBar<'a, Message, Renderer: pane_grid::Renderer> { title: String, title_size: Option, @@ -14,6 +18,9 @@ impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer> where Renderer: pane_grid::Renderer, { + /// Cretes a new [`TitleBar`] with the given title. + /// + /// [`TitleBar`]: struct.TitleBar.html pub fn new(title: impl Into) -> Self { Self { title: title.into(), @@ -26,7 +33,7 @@ where /// Sets the size of the title of the [`TitleBar`]. /// - /// [`TitleBar`]: struct.Text.html + /// [`TitleBar`]: struct.TitleBar.html pub fn title_size(mut self, size: u16) -> Self { self.title_size = Some(size); self @@ -64,6 +71,11 @@ impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer> where Renderer: pane_grid::Renderer, { + /// Draws the [`TitleBar`] with the provided [`Renderer`] and [`Layout`]. + /// + /// [`TitleBar`]: struct.TitleBar.html + /// [`Renderer`]: trait.Renderer.html + /// [`Layout`]: ../layout/struct.Layout.html pub fn draw( &self, renderer: &mut Renderer, @@ -118,7 +130,13 @@ where } } - pub fn is_over_draggable( + /// Returns whether the mouse cursor is over the pick area of the + /// [`TitleBar`] or not. + /// + /// The whole [`TitleBar`] is a pick area, except its controls. + /// + /// [`TitleBar`]: struct.TitleBar.html + pub fn is_over_pick_area( &self, layout: Layout<'_>, cursor_position: Point, -- cgit From 2334c7d1d558818ae469a4cf629eb2ba82eaa4dc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 9 Jul 2020 05:28:19 +0200 Subject: Stop tracking `pressed_modifiers` in `PaneGrid` --- native/src/widget/pane_grid/state.rs | 3 --- 1 file changed, 3 deletions(-) (limited to 'native/src/widget/pane_grid') diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index 3f0accc1..e1585968 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -1,5 +1,4 @@ use crate::{ - keyboard, pane_grid::{Axis, Configuration, Direction, Node, Pane, Split}, Hasher, Point, Rectangle, Size, }; @@ -25,7 +24,6 @@ use std::collections::HashMap; pub struct State { pub(super) panes: HashMap, pub(super) internal: Internal, - pub(super) modifiers: keyboard::ModifiersState, } /// The current focus of a [`Pane`]. @@ -76,7 +74,6 @@ impl State { last_id, action: Action::Idle { focus: None }, }, - modifiers: keyboard::ModifiersState::default(), } } -- cgit From 2f02ca3248bee529ebde7901375784dc9971eaf9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 9 Jul 2020 05:42:28 +0200 Subject: Fix layout of a `TitleBar` without controls --- native/src/widget/pane_grid/title_bar.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'native/src/widget/pane_grid') diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs index 775c8a8f..c74c9e20 100644 --- a/native/src/widget/pane_grid/title_bar.rs +++ b/native/src/widget/pane_grid/title_bar.rs @@ -202,7 +202,7 @@ where vec![title_layout, controls_layout], ) } else { - layout::Node::new(Size::new(title_width, title_height)) + layout::Node::new(Size::new(max_size.width, title_height)) }; node.move_to(Point::new(padding, padding)); -- cgit