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` --- graphics/src/widget/pane_grid.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 56af683d..7a840689 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -14,8 +14,8 @@ use iced_native::pane_grid; use iced_native::{Element, Layout, Point, Rectangle, Vector}; pub use iced_native::pane_grid::{ - Axis, Direction, DragEvent, Focus, KeyPressEvent, Pane, ResizeEvent, Split, - State, + Axis, Configuration, Content, Direction, DragEvent, Focus, KeyPressEvent, + Pane, ResizeEvent, Split, State, TitleBar, }; /// A collection of panes distributed using either vertical or horizontal splits @@ -34,7 +34,7 @@ where fn draw( &mut self, defaults: &Self::Defaults, - content: &[(Pane, Element<'_, Message, Self>)], + content: &[(Pane, Content<'_, Message, Self>)], dragging: Option, resizing: Option, layout: Layout<'_>, @@ -115,4 +115,15 @@ where }, ) } + + fn draw_pane( + &mut self, + defaults: &Self::Defaults, + _title_bar: Option<&TitleBar<'_, Message, Self>>, + body: &Element<'_, Message, Self>, + layout: Layout<'_>, + cursor_position: Point, + ) -> Self::Output { + body.draw(self, defaults, layout, cursor_position) + } } -- 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` --- graphics/src/widget/pane_grid.rs | 60 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 7a840689..71b1658a 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -119,11 +119,63 @@ where fn draw_pane( &mut self, defaults: &Self::Defaults, - _title_bar: Option<&TitleBar<'_, Message, Self>>, - body: &Element<'_, Message, Self>, - layout: Layout<'_>, + title_bar: Option<(&TitleBar<'_, Message, Self>, Layout<'_>)>, + body: (&Element<'_, Message, Self>, Layout<'_>), + cursor_position: Point, + ) -> Self::Output { + let (body, body_layout) = body; + + let (body_primitive, body_interaction) = + body.draw(self, defaults, body_layout, cursor_position); + + if let Some((title_bar, title_bar_layout)) = title_bar { + let (title_bar_primitive, title_bar_interaction) = title_bar.draw( + self, + defaults, + title_bar_layout, + cursor_position, + ); + + ( + Primitive::Group { + primitives: vec![title_bar_primitive, body_primitive], + }, + if title_bar_interaction > body_interaction { + title_bar_interaction + } else { + body_interaction + }, + ) + } else { + (body_primitive, body_interaction) + } + } + + fn draw_title_bar( + &mut self, + defaults: &Self::Defaults, + style: &::Style, + title: (&Element<'_, Message, Self>, Layout<'_>), + controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>, cursor_position: Point, ) -> Self::Output { - body.draw(self, defaults, layout, cursor_position) + let (title, title_layout) = title; + + let (title_primitive, _) = + title.draw(self, defaults, title_layout, cursor_position); + + if let Some((controls, controls_layout)) = controls { + let (controls_primitive, controls_interaction) = + controls.draw(self, defaults, controls_layout, cursor_position); + + ( + Primitive::Group { + primitives: vec![title_primitive, controls_primitive], + }, + controls_interaction, + ) + } else { + (title_primitive, mouse::Interaction::default()) + } } } -- 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` --- graphics/src/widget/container.rs | 33 ++++++++++------- graphics/src/widget/pane_grid.rs | 78 ++++++++++++++++++++++++++++++++-------- 2 files changed, 85 insertions(+), 26 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/widget/container.rs b/graphics/src/widget/container.rs index 070cb48b..576062d4 100644 --- a/graphics/src/widget/container.rs +++ b/graphics/src/widget/container.rs @@ -39,20 +39,10 @@ where let (content, mouse_interaction) = content.draw(self, &defaults, content_layout, cursor_position); - if style.background.is_some() || style.border_width > 0 { - let quad = Primitive::Quad { - bounds, - background: style - .background - .unwrap_or(Background::Color(Color::TRANSPARENT)), - border_radius: style.border_radius, - border_width: style.border_width, - border_color: style.border_color, - }; - + if let Some(background) = background(bounds, &style) { ( Primitive::Group { - primitives: vec![quad, content], + primitives: vec![background, content], }, mouse_interaction, ) @@ -61,3 +51,22 @@ where } } } + +pub(crate) fn background( + bounds: Rectangle, + style: &container::Style, +) -> Option { + if style.background.is_none() && style.border_width > 0 { + return None; + } + + Some(Primitive::Quad { + bounds, + background: style + .background + .unwrap_or(Background::Color(Color::TRANSPARENT)), + border_radius: style.border_radius, + border_width: style.border_width, + border_color: style.border_color, + }) +} diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 71b1658a..27b4f211 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -35,7 +35,7 @@ where &mut self, defaults: &Self::Defaults, content: &[(Pane, Content<'_, Message, Self>)], - dragging: Option, + dragging: Option<(Pane, Point)>, resizing: Option, layout: Layout<'_>, cursor_position: Point, @@ -63,32 +63,40 @@ where mouse_interaction = new_mouse_interaction; } - if Some(*id) == dragging { - dragged_pane = Some((i, layout)); + if let Some((dragging, origin)) = dragging { + if *id == dragging { + dragged_pane = Some((i, layout, origin)); + } } primitive }) .collect(); - let primitives = if let Some((index, layout)) = dragged_pane { + let primitives = if let Some((index, layout, origin)) = dragged_pane { let pane = panes.remove(index); let bounds = layout.bounds(); + if let Primitive::Group { primitives } = &pane { + panes.push( + primitives.first().cloned().unwrap_or(Primitive::None), + ); + } + // TODO: Fix once proper layering is implemented. // This is a pretty hacky way to achieve layering. let clip = Primitive::Clip { bounds: Rectangle { - x: cursor_position.x - bounds.width / 2.0, - y: cursor_position.y - bounds.height / 2.0, + x: cursor_position.x - origin.x, + y: cursor_position.y - origin.y, width: bounds.width + 0.5, height: bounds.height + 0.5, }, offset: Vector::new(0, 0), content: Box::new(Primitive::Translate { translation: Vector::new( - cursor_position.x - bounds.x - bounds.width / 2.0, - cursor_position.y - bounds.y - bounds.height / 2.0, + cursor_position.x - bounds.x - origin.x, + cursor_position.y - bounds.y - origin.y, ), content: Box::new(pane), }), @@ -119,48 +127,77 @@ where fn draw_pane( &mut self, defaults: &Self::Defaults, + bounds: Rectangle, + style_sheet: &Self::Style, title_bar: Option<(&TitleBar<'_, Message, Self>, Layout<'_>)>, body: (&Element<'_, Message, Self>, Layout<'_>), cursor_position: Point, ) -> Self::Output { + let style = style_sheet.style(); let (body, body_layout) = body; let (body_primitive, body_interaction) = body.draw(self, defaults, body_layout, cursor_position); + let background = crate::widget::container::background(bounds, &style); + if let Some((title_bar, title_bar_layout)) = title_bar { + let show_controls = bounds.contains(cursor_position); + let is_over_draggable = + title_bar.is_over_draggable(title_bar_layout, cursor_position); + let (title_bar_primitive, title_bar_interaction) = title_bar.draw( self, defaults, title_bar_layout, cursor_position, + show_controls, ); ( Primitive::Group { - primitives: vec![title_bar_primitive, body_primitive], + primitives: vec![ + background.unwrap_or(Primitive::None), + title_bar_primitive, + body_primitive, + ], }, - if title_bar_interaction > body_interaction { + if is_over_draggable { + mouse::Interaction::Grab + } else if title_bar_interaction > body_interaction { title_bar_interaction } else { body_interaction }, ) } else { - (body_primitive, body_interaction) + ( + if let Some(background) = background { + Primitive::Group { + primitives: vec![background, body_primitive], + } + } else { + body_primitive + }, + body_interaction, + ) } } fn draw_title_bar( &mut self, defaults: &Self::Defaults, - style: &::Style, + bounds: Rectangle, + style_sheet: &Self::Style, title: (&Element<'_, Message, Self>, Layout<'_>), controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>, cursor_position: Point, ) -> Self::Output { + let style = style_sheet.style(); let (title, title_layout) = title; + let background = crate::widget::container::background(bounds, &style); + let (title_primitive, _) = title.draw(self, defaults, title_layout, cursor_position); @@ -170,12 +207,25 @@ where ( Primitive::Group { - primitives: vec![title_primitive, controls_primitive], + primitives: vec![ + background.unwrap_or(Primitive::None), + title_primitive, + controls_primitive, + ], }, controls_interaction, ) } else { - (title_primitive, mouse::Interaction::default()) + ( + if let Some(background) = background { + Primitive::Group { + primitives: vec![background, title_primitive], + } + } else { + title_primitive + }, + mouse::Interaction::default(), + ) } } } -- 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` --- graphics/src/widget/pane_grid.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 27b4f211..ec607c47 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -8,10 +8,15 @@ //! //! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.1/examples/pane_grid //! [`PaneGrid`]: type.PaneGrid.html -use crate::{Backend, Primitive, Renderer}; +use crate::backend::{self, Backend}; +use crate::{Primitive, Renderer}; use iced_native::mouse; use iced_native::pane_grid; -use iced_native::{Element, Layout, Point, Rectangle, Vector}; +use iced_native::text; +use iced_native::{ + Element, HorizontalAlignment, Layout, Point, Rectangle, Vector, + VerticalAlignment, +}; pub use iced_native::pane_grid::{ Axis, Configuration, Content, Direction, DragEvent, Focus, KeyPressEvent, @@ -29,7 +34,7 @@ pub type PaneGrid<'a, Message, Backend> = impl pane_grid::Renderer for Renderer where - B: Backend, + B: Backend + backend::Text, { fn draw( &mut self, @@ -189,17 +194,28 @@ where defaults: &Self::Defaults, bounds: Rectangle, style_sheet: &Self::Style, - title: (&Element<'_, Message, Self>, Layout<'_>), + title: &str, + title_size: u16, + title_font: Self::Font, + title_bounds: Rectangle, controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>, cursor_position: Point, ) -> Self::Output { let style = style_sheet.style(); - let (title, title_layout) = title; let background = crate::widget::container::background(bounds, &style); - let (title_primitive, _) = - title.draw(self, defaults, title_layout, cursor_position); + let (title_primitive, _) = text::Renderer::draw( + self, + defaults, + title_bounds, + title, + title_size, + title_font, + None, + HorizontalAlignment::Left, + VerticalAlignment::Top, + ); if let Some((controls, controls_layout)) = controls { let (controls_primitive, controls_interaction) = -- 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 --- graphics/src/widget/pane_grid.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index ec607c47..c8d9625b 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -148,8 +148,8 @@ where if let Some((title_bar, title_bar_layout)) = title_bar { let show_controls = bounds.contains(cursor_position); - let is_over_draggable = - title_bar.is_over_draggable(title_bar_layout, cursor_position); + let is_over_pick_area = + title_bar.is_over_pick_area(title_bar_layout, cursor_position); let (title_bar_primitive, title_bar_interaction) = title_bar.draw( self, @@ -167,7 +167,7 @@ where body_primitive, ], }, - if is_over_draggable { + if is_over_pick_area { mouse::Interaction::Grab } else if title_bar_interaction > body_interaction { title_bar_interaction -- cgit From ef553cd12456a3fd70df26c936953887a78a1bba Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 9 Jul 2020 05:44:48 +0200 Subject: Stop drawing pane background when dragged --- graphics/src/widget/pane_grid.rs | 6 ------ 1 file changed, 6 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index c8d9625b..8ec5c547 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -82,12 +82,6 @@ where let pane = panes.remove(index); let bounds = layout.bounds(); - if let Primitive::Group { primitives } = &pane { - panes.push( - primitives.first().cloned().unwrap_or(Primitive::None), - ); - } - // TODO: Fix once proper layering is implemented. // This is a pretty hacky way to achieve layering. let clip = Primitive::Clip { -- cgit From 1319c25f20a274d163d8009805b6fd3bb55ac88a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 9 Jul 2020 06:27:12 +0200 Subject: Respect `TitleBar` text color style when drawing --- graphics/src/widget/pane_grid.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 8ec5c547..aa8a3f7c 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -9,6 +9,7 @@ //! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.1/examples/pane_grid //! [`PaneGrid`]: type.PaneGrid.html use crate::backend::{self, Backend}; +use crate::defaults; use crate::{Primitive, Renderer}; use iced_native::mouse; use iced_native::pane_grid; @@ -197,11 +198,17 @@ where ) -> Self::Output { let style = style_sheet.style(); + let defaults = Self::Defaults { + text: defaults::Text { + color: style.text_color.unwrap_or(defaults.text.color), + }, + }; + let background = crate::widget::container::background(bounds, &style); let (title_primitive, _) = text::Renderer::draw( self, - defaults, + &defaults, title_bounds, title, title_size, @@ -212,8 +219,12 @@ where ); if let Some((controls, controls_layout)) = controls { - let (controls_primitive, controls_interaction) = - controls.draw(self, defaults, controls_layout, cursor_position); + let (controls_primitive, controls_interaction) = controls.draw( + self, + &defaults, + controls_layout, + cursor_position, + ); ( Primitive::Group { -- cgit