diff options
-rw-r--r-- | examples/pane_grid/src/main.rs | 2 | ||||
-rw-r--r-- | glow/src/widget/pane_grid.rs | 4 | ||||
-rw-r--r-- | graphics/src/widget/container.rs | 33 | ||||
-rw-r--r-- | graphics/src/widget/pane_grid.rs | 135 | ||||
-rw-r--r-- | native/src/layout.rs | 9 | ||||
-rw-r--r-- | native/src/lib.rs | 4 | ||||
-rw-r--r-- | native/src/renderer/null.rs | 21 | ||||
-rw-r--r-- | native/src/widget/pane_grid.rs | 98 | ||||
-rw-r--r-- | native/src/widget/pane_grid/configuration.rs | 30 | ||||
-rw-r--r-- | native/src/widget/pane_grid/content.rs | 207 | ||||
-rw-r--r-- | native/src/widget/pane_grid/state.rs | 37 | ||||
-rw-r--r-- | native/src/widget/pane_grid/title_bar.rs | 188 | ||||
-rw-r--r-- | wgpu/src/widget/pane_grid.rs | 16 |
13 files changed, 683 insertions, 101 deletions
diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index a821072f..069e2f59 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -97,7 +97,7 @@ impl Sandbox for Example { let pane_grid = PaneGrid::new(&mut self.panes, |pane, content, focus| { - content.view(pane, focus, total_panes) + pane_grid::Content::new(content.view(pane, focus, total_panes)) }) .width(Length::Fill) .height(Length::Fill) diff --git a/glow/src/widget/pane_grid.rs b/glow/src/widget/pane_grid.rs index 6f437df7..d9fec026 100644 --- a/glow/src/widget/pane_grid.rs +++ b/glow/src/widget/pane_grid.rs @@ -11,8 +11,8 @@ use crate::Renderer; pub use iced_native::pane_grid::{ - Axis, Content, Direction, DragEvent, Focus, KeyPressEvent, Node, Pane, - ResizeEvent, Split, State, + Axis, Configuration, Content, Direction, DragEvent, Focus, KeyPressEvent, + Node, Pane, ResizeEvent, Split, State, }; /// A collection of panes distributed using either vertical or horizontal splits 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<Primitive> { + 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 56af683d..27b4f211 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,8 +34,8 @@ where fn draw<Message>( &mut self, defaults: &Self::Defaults, - content: &[(Pane, Element<'_, Message, Self>)], - dragging: Option<Pane>, + content: &[(Pane, Content<'_, Message, Self>)], + dragging: Option<(Pane, Point)>, resizing: Option<Axis>, 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), }), @@ -115,4 +123,109 @@ where }, ) } + + fn draw_pane<Message>( + &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![ + background.unwrap_or(Primitive::None), + title_bar_primitive, + body_primitive, + ], + }, + if is_over_draggable { + mouse::Interaction::Grab + } else if title_bar_interaction > body_interaction { + title_bar_interaction + } else { + body_interaction + }, + ) + } else { + ( + if let Some(background) = background { + Primitive::Group { + primitives: vec![background, body_primitive], + } + } else { + body_primitive + }, + body_interaction, + ) + } + } + + fn draw_title_bar<Message>( + &mut self, + defaults: &Self::Defaults, + 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); + + if let Some((controls, controls_layout)) = controls { + let (controls_primitive, controls_interaction) = + controls.draw(self, defaults, controls_layout, cursor_position); + + ( + Primitive::Group { + primitives: vec![ + background.unwrap_or(Primitive::None), + title_primitive, + controls_primitive, + ], + }, + controls_interaction, + ) + } else { + ( + if let Some(background) = background { + Primitive::Group { + primitives: vec![background, title_primitive], + } + } else { + title_primitive + }, + mouse::Interaction::default(), + ) + } + } } diff --git a/native/src/layout.rs b/native/src/layout.rs index 4a3ab94a..93d95e17 100644 --- a/native/src/layout.rs +++ b/native/src/layout.rs @@ -34,7 +34,14 @@ impl<'a> Layout<'a> { } } - /// Gets the bounds of the [`Layout`]. + /// Returns the position of the [`Layout`]. + /// + /// [`Layout`]: struct.Layout.html + pub fn position(&self) -> Point { + self.position + } + + /// Returns the bounds of the [`Layout`]. /// /// The returned [`Rectangle`] describes the position and size of a /// [`Node`]. diff --git a/native/src/lib.rs b/native/src/lib.rs index b67ff2a1..86046f63 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -30,8 +30,8 @@ //! [`Widget`]: widget/trait.Widget.html //! [`UserInterface`]: struct.UserInterface.html //! [renderer]: renderer/index.html -#![deny(missing_docs)] -#![deny(missing_debug_implementations)] +//#![deny(missing_docs)] +//#![deny(missing_debug_implementations)] #![deny(unused_results)] #![forbid(unsafe_code)] #![forbid(rust_2018_idioms)] diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index b8695b9c..29df3e14 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -1,7 +1,7 @@ use crate::{ - button, checkbox, column, progress_bar, radio, row, scrollable, slider, - text, text_input, Color, Element, Font, HorizontalAlignment, Layout, Point, - Rectangle, Renderer, Size, VerticalAlignment, + button, checkbox, column, container, progress_bar, radio, row, scrollable, + slider, text, text_input, Color, Element, Font, HorizontalAlignment, + Layout, Point, Rectangle, Renderer, Size, VerticalAlignment, }; /// A renderer that does nothing. @@ -226,3 +226,18 @@ impl progress_bar::Renderer for Null { ) { } } + +impl container::Renderer for Null { + type Style = (); + + fn draw<Message>( + &mut self, + _defaults: &Self::Defaults, + _bounds: Rectangle, + _cursor_position: Point, + _style: &Self::Style, + _content: &Element<'_, Message, Self>, + _content_layout: Layout<'_>, + ) { + } +} diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index f8788932..8fb6c20c 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -9,24 +9,28 @@ //! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.1/examples/pane_grid //! [`PaneGrid`]: struct.PaneGrid.html mod axis; +mod configuration; mod content; mod direction; mod node; mod pane; mod split; mod state; +mod title_bar; pub use axis::Axis; +pub use configuration::Configuration; pub use content::Content; pub use direction::Direction; pub use node::Node; pub use pane::Pane; pub use split::Split; pub use state::{Focus, State}; +pub use title_bar::TitleBar; use crate::{ - keyboard, layout, mouse, Clipboard, Element, Event, Hasher, Layout, Length, - Point, Rectangle, Size, Widget, + container, keyboard, layout, mouse, row, Clipboard, Element, Event, Hasher, + Layout, Length, Point, Rectangle, Size, Widget, }; /// A collection of panes distributed using either vertical or horizontal splits @@ -70,10 +74,10 @@ use crate::{ /// /// let pane_grid = /// PaneGrid::new(&mut state, |pane, state, focus| { -/// match state { +/// pane_grid::Content::new(match state { /// PaneState::SomePane => Text::new("This is some pane"), /// PaneState::AnotherKindOfPane => Text::new("This is another kind of pane"), -/// }.into() +/// }) /// }) /// .on_drag(Message::PaneDragged) /// .on_resize(10, Message::PaneResized); @@ -82,10 +86,10 @@ use crate::{ /// [`PaneGrid`]: struct.PaneGrid.html /// [`State`]: struct.State.html #[allow(missing_debug_implementations)] -pub struct PaneGrid<'a, Message, Renderer> { +pub struct PaneGrid<'a, Message, Renderer: container::Renderer> { state: &'a mut state::Internal, pressed_modifiers: &'a mut keyboard::ModifiersState, - elements: Vec<(Pane, Element<'a, Message, Renderer>)>, + elements: Vec<(Pane, Content<'a, Message, Renderer>)>, width: Length, height: Length, spacing: u16, @@ -95,7 +99,10 @@ pub struct PaneGrid<'a, Message, Renderer> { on_key_press: Option<Box<dyn Fn(KeyPressEvent) -> Option<Message> + 'a>>, } -impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> { +impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> +where + Renderer: container::Renderer, +{ /// Creates a [`PaneGrid`] with the given [`State`] and view function. /// /// The view function will be called to display each [`Pane`] present in the @@ -110,7 +117,7 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> { Pane, &'a mut T, Option<Focus>, - ) -> Element<'a, Message, Renderer>, + ) -> Content<'a, Message, Renderer>, ) -> Self { let elements = { let action = state.internal.action(); @@ -194,8 +201,6 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> { /// Enables the drag and drop interactions of the [`PaneGrid`], which will /// use the provided function to produce messages. /// - /// Panes can be dragged using `Modifier keys + Left click`. - /// /// [`PaneGrid`]: struct.PaneGrid.html pub fn on_drag<F>(mut self, f: F) -> Self where @@ -244,7 +249,12 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> { self.on_key_press = Some(Box::new(f)); self } +} +impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> +where + Renderer: self::Renderer, +{ fn click_pane( &mut self, layout: Layout<'_>, @@ -256,16 +266,21 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> { |(_, layout)| layout.bounds().contains(cursor_position), ); - if let Some(((pane, _), _)) = clicked_region.next() { + if let Some(((pane, content), layout)) = clicked_region.next() { match &self.on_drag { - Some(on_drag) - if self.pressed_modifiers.matches(self.modifier_keys) => - { - self.state.pick_pane(pane); - - messages.push(on_drag(DragEvent::Picked { pane: *pane })); + Some(on_drag) => { + if let Some(origin) = + content.drag_origin(layout, cursor_position) + { + self.state.pick_pane(pane, origin); + + messages + .push(on_drag(DragEvent::Picked { pane: *pane })); + } else { + self.state.focus(pane); + } } - _ => { + None => { self.state.focus(pane); } } @@ -388,7 +403,7 @@ pub struct KeyPressEvent { impl<'a, Message, Renderer> Widget<Message, Renderer> for PaneGrid<'a, Message, Renderer> where - Renderer: self::Renderer, + Renderer: self::Renderer + container::Renderer, { fn width(&self) -> Length { self.width @@ -481,7 +496,7 @@ where } } mouse::Event::ButtonReleased(mouse::Button::Left) => { - if let Some(pane) = self.state.picked_pane() { + if let Some((pane, _)) = self.state.picked_pane() { self.state.focus(&pane); if let Some(on_drag) = &self.on_drag { @@ -551,7 +566,7 @@ where { self.elements.iter_mut().zip(layout.children()).for_each( |((_, pane), layout)| { - pane.widget.on_event( + pane.on_event( event.clone(), layout, cursor_position, @@ -598,7 +613,8 @@ where }) .map(|(_, axis)| axis); - renderer.draw( + self::Renderer::draw( + renderer, defaults, &self.elements, self.state.picked_pane(), @@ -630,7 +646,7 @@ where /// /// [`PaneGrid`]: struct.PaneGrid.html /// [renderer]: ../../renderer/index.html -pub trait Renderer: crate::Renderer + Sized { +pub trait Renderer: crate::Renderer + container::Renderer + Sized { /// Draws a [`PaneGrid`]. /// /// It receives: @@ -646,18 +662,48 @@ pub trait Renderer: crate::Renderer + Sized { fn draw<Message>( &mut self, defaults: &Self::Defaults, - content: &[(Pane, Element<'_, Message, Self>)], - dragging: Option<Pane>, + content: &[(Pane, Content<'_, Message, Self>)], + dragging: Option<(Pane, Point)>, resizing: Option<Axis>, layout: Layout<'_>, cursor_position: Point, ) -> Self::Output; + + /// Draws a [`Pane`]. + /// + /// It receives: + /// - the [`TitleBar`] of the [`Pane`], if any + /// - the [`Content`] of the [`Pane`] + /// - the [`Layout`] of the [`Pane`] and its elements + /// - the cursor position + /// + /// [`Pane`]: struct.Pane.html + /// [`Layout`]: ../layout/struct.Layout.html + fn draw_pane<Message>( + &mut self, + defaults: &Self::Defaults, + bounds: Rectangle, + style: &Self::Style, + title_bar: Option<(&TitleBar<'_, Message, Self>, Layout<'_>)>, + body: (&Element<'_, Message, Self>, Layout<'_>), + cursor_position: Point, + ) -> Self::Output; + + fn draw_title_bar<Message>( + &mut self, + defaults: &Self::Defaults, + bounds: Rectangle, + style: &Self::Style, + title: (&Element<'_, Message, Self>, Layout<'_>), + controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>, + cursor_position: Point, + ) -> Self::Output; } impl<'a, Message, Renderer> From<PaneGrid<'a, Message, Renderer>> for Element<'a, Message, Renderer> where - Renderer: 'a + self::Renderer, + Renderer: 'a + self::Renderer + row::Renderer, Message: 'a, { fn from( 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<T> { + /// 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<Configuration<T>>, + + /// The right/bottom [`Content`] of the split. + /// + /// [`Configuration`]: enum.Node.html + b: Box<Configuration<T>>, + }, + /// 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..843dcec4 100644 --- a/native/src/widget/pane_grid/content.rs +++ b/native/src/widget/pane_grid/content.rs @@ -1,30 +1,185 @@ -use crate::pane_grid::Axis; +use crate::container; +use crate::layout; +use crate::pane_grid::{self, TitleBar}; +use crate::{Clipboard, Element, Event, Hasher, Layout, Point, Size, Vector}; -/// The content of a [`PaneGrid`]. +/// The content of a [`Pane`]. /// -/// [`PaneGrid`]: struct.PaneGrid.html -#[derive(Debug, Clone)] -pub enum Content<T> { - /// 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<Content<T>>, - - /// The right/bottom [`Content`] of the split. - /// - /// [`Content`]: enum.Node.html - b: Box<Content<T>>, - }, - /// A [`Pane`]. +/// [`Pane`]: struct.Pane.html +pub struct Content<'a, Message, Renderer: container::Renderer> { + title_bar: Option<TitleBar<'a, Message, Renderer>>, + body: Element<'a, Message, Renderer>, + style: Renderer::Style, +} + +impl<'a, Message, Renderer> Content<'a, Message, Renderer> +where + Renderer: container::Renderer, +{ + pub fn new(body: impl Into<Element<'a, Message, Renderer>>) -> Self { + Self { + title_bar: None, + body: body.into(), + style: Renderer::Style::default(), + } + } + + pub fn title_bar( + mut self, + title_bar: TitleBar<'a, Message, Renderer>, + ) -> Self { + self.title_bar = Some(title_bar); + self + } + + /// Sets the style of the [`TitleBar`]. /// - /// [`Pane`]: struct.Pane.html - Pane(T), + /// [`TitleBar`]: struct.TitleBar.html + pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self { + self.style = style.into(); + self + } +} + +impl<'a, Message, Renderer> Content<'a, Message, Renderer> +where + Renderer: pane_grid::Renderer + container::Renderer, +{ + pub fn draw( + &self, + renderer: &mut Renderer, + defaults: &Renderer::Defaults, + layout: Layout<'_>, + cursor_position: Point, + ) -> Renderer::Output { + 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, + layout.bounds(), + &self.style, + Some((title_bar, title_bar_layout)), + (&self.body, body_layout), + cursor_position, + ) + } else { + renderer.draw_pane( + defaults, + layout.bounds(), + &self.style, + None, + (&self.body, layout), + cursor_position, + ) + } + } + + pub fn drag_origin( + &self, + layout: Layout<'_>, + cursor_position: Point, + ) -> Option<Point> { + 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( + &self, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + 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( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec<Message>, + 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, + body_layout, + cursor_position, + messages, + renderer, + clipboard, + ); + } + + pub(crate) fn hash_layout(&self, state: &mut Hasher) { + self.body.hash_layout(state); + } +} + +impl<'a, T, Message, Renderer> From<T> for Content<'a, Message, Renderer> +where + T: Into<Element<'a, Message, Renderer>>, + Renderer: pane_grid::Renderer + container::Renderer, +{ + fn from(element: T) -> Self { + Self::new(element) + } } diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index 4b13fb8e..a8431dec 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<T> State<T> { /// [`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<Content<T>>) -> Self { + /// [`Configuration`]: enum.Configuration.html + pub fn with_configuration(config: impl Into<Configuration<T>>) -> 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<T> State<T> { fn distribute_content( panes: &mut HashMap<Pane, T>, - content: Content<T>, + content: Configuration<T>, 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<T> State<T> { next_id + 1, ) } - Content::Pane(state) => { + Configuration::Pane(state) => { let id = Pane(next_id); let _ = panes.insert(id, state); @@ -310,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<Pane>, }, Dragging { pane: Pane, + origin: Point, }, Resizing { split: Split, @@ -331,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)), } } } @@ -348,9 +352,9 @@ impl Internal { } } - pub fn picked_pane(&self) -> Option<Pane> { + pub fn picked_pane(&self) -> Option<(Pane, Point)> { match self.action { - Action::Dragging { pane } => Some(pane), + Action::Dragging { pane, origin } => Some((pane, origin)), _ => None, } } @@ -382,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 new file mode 100644 index 00000000..dc96dcbf --- /dev/null +++ b/native/src/widget/pane_grid/title_bar.rs @@ -0,0 +1,188 @@ +use crate::container; +use crate::layout; +use crate::pane_grid; +use crate::{Clipboard, Element, Event, Layout, Point, Size}; + +pub struct TitleBar<'a, Message, Renderer: container::Renderer> { + title: Element<'a, Message, Renderer>, + controls: Option<Element<'a, Message, Renderer>>, + padding: u16, + style: Renderer::Style, +} + +impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer> +where + Renderer: container::Renderer, +{ + pub fn new(title: impl Into<Element<'a, Message, Renderer>>) -> Self { + Self { + title: title.into(), + controls: None, + padding: 0, + style: Renderer::Style::default(), + } + } + + pub fn controls( + mut self, + controls: impl Into<Element<'a, Message, Renderer>>, + ) -> 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 + } + + /// Sets the style of the [`TitleBar`]. + /// + /// [`TitleBar`]: struct.TitleBar.html + pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self { + self.style = style.into(); + 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, + show_controls: bool, + ) -> Renderer::Output { + 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 controls_layout = children.next().unwrap(); + + renderer.draw_title_bar( + defaults, + layout.bounds(), + &self.style, + (&self.title, title_layout), + if show_controls { + Some((controls, controls_layout)) + } else { + None + }, + cursor_position, + ) + } else { + renderer.draw_title_bar( + defaults, + layout.bounds(), + &self.style, + (&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, + limits: &layout::Limits, + ) -> layout::Node { + let padding = f32::from(self.padding); + let limits = limits.pad(padding); + + let node = if let Some(controls) = &self.controls { + let max_size = limits.max(); + + 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 title_size = title_layout.size(); + let height = title_size.height.max(controls_size.height); + + layout::Node::with_children( + Size::new(max_size.width, height), + vec![title_layout, controls_layout], + ) + } else { + self.title.layout(renderer, &limits) + }; + + 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<Message>, + 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(); + + controls.on_event( + event, + controls_layout, + cursor_position, + messages, + renderer, + clipboard, + ); + } + } +} diff --git a/wgpu/src/widget/pane_grid.rs b/wgpu/src/widget/pane_grid.rs index 6f437df7..3c47b562 100644 --- a/wgpu/src/widget/pane_grid.rs +++ b/wgpu/src/widget/pane_grid.rs @@ -11,8 +11,8 @@ use crate::Renderer; pub use iced_native::pane_grid::{ - Axis, Content, Direction, DragEvent, Focus, KeyPressEvent, Node, Pane, - ResizeEvent, Split, State, + Axis, Configuration, Direction, DragEvent, Focus, KeyPressEvent, Node, + Pane, ResizeEvent, Split, State, }; /// A collection of panes distributed using either vertical or horizontal splits @@ -22,3 +22,15 @@ pub use iced_native::pane_grid::{ /// /// This is an alias of an `iced_native` pane grid with an `iced_wgpu::Renderer`. pub type PaneGrid<'a, Message> = iced_native::PaneGrid<'a, Message, Renderer>; + +/// The content of a [`Pane`]. +/// +/// [`Pane`]: struct.Pane.html +pub type Content<'a, Message> = + iced_native::pane_grid::Content<'a, Message, Renderer>; + +/// The title bar of a [`Pane`]. +/// +/// [`Pane`]: struct.Pane.html +pub type TitleBar<'a, Message> = + iced_native::pane_grid::TitleBar<'a, Message, Renderer>; |