Implement split highlight on hover for PaneGrid

This commit is contained in:
Héctor Ramón Jiménez 2021-01-01 15:28:38 +01:00
parent f8aef03456
commit a7bb7bb2ea
9 changed files with 191 additions and 37 deletions

View file

@ -8,16 +8,19 @@
//!
//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.2/examples/pane_grid
use crate::defaults;
use crate::{Backend, Primitive, Renderer};
use crate::{Backend, Color, Primitive, Renderer};
use iced_native::container;
use iced_native::mouse;
use iced_native::pane_grid;
use iced_native::{Element, Layout, Point, Rectangle, Vector};
pub use iced_native::pane_grid::{
Axis, Configuration, Content, Direction, DragEvent, Pane, ResizeEvent,
Split, State, TitleBar,
Axis, Configuration, Content, Direction, DragEvent, Node, Pane,
ResizeEvent, Split, State, TitleBar,
};
pub use iced_style::pane_grid::{Line, StyleSheet};
/// A collection of panes distributed using either vertical or horizontal splits
/// to completely fill the space available.
///
@ -31,13 +34,16 @@ impl<B> pane_grid::Renderer for Renderer<B>
where
B: Backend,
{
type Style = Box<dyn StyleSheet>;
fn draw<Message>(
&mut self,
defaults: &Self::Defaults,
content: &[(Pane, Content<'_, Message, Self>)],
dragging: Option<(Pane, Point)>,
resizing: Option<Axis>,
resizing: Option<(Axis, Rectangle, bool)>,
layout: Layout<'_>,
style_sheet: &<Self as pane_grid::Renderer>::Style,
cursor_position: Point,
) -> Self::Output {
let pane_cursor_position = if dragging.is_some() {
@ -73,7 +79,8 @@ where
})
.collect();
let primitives = if let Some((index, layout, origin)) = dragged_pane {
let mut primitives = if let Some((index, layout, origin)) = dragged_pane
{
let pane = panes.remove(index);
let bounds = layout.bounds();
@ -103,15 +110,62 @@ where
panes
};
let (primitives, mouse_interaction) =
if let Some((axis, split_region, is_picked)) = resizing {
let highlight = if is_picked {
style_sheet.picked_split()
} else {
style_sheet.hovered_split()
};
if let Some(highlight) = highlight {
primitives.push(Primitive::Quad {
bounds: match axis {
Axis::Horizontal => Rectangle {
x: split_region.x,
y: (split_region.y
+ (split_region.height - highlight.width)
/ 2.0)
.round(),
width: split_region.width,
height: highlight.width,
},
Axis::Vertical => Rectangle {
x: (split_region.x
+ (split_region.width - highlight.width)
/ 2.0)
.round(),
y: split_region.y,
width: highlight.width,
height: split_region.height,
},
},
background: highlight.color.into(),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
});
}
(
primitives,
match axis {
Axis::Horizontal => {
mouse::Interaction::ResizingVertically
}
Axis::Vertical => {
mouse::Interaction::ResizingHorizontally
}
},
)
} else {
(primitives, mouse_interaction)
};
(
Primitive::Group { primitives },
if dragging.is_some() {
mouse::Interaction::Grabbing
} else if let Some(axis) = resizing {
match axis {
Axis::Horizontal => mouse::Interaction::ResizingVertically,
Axis::Vertical => mouse::Interaction::ResizingHorizontally,
}
} else {
mouse_interaction
},
@ -122,7 +176,7 @@ where
&mut self,
defaults: &Self::Defaults,
bounds: Rectangle,
style_sheet: &Self::Style,
style_sheet: &<Self as container::Renderer>::Style,
title_bar: Option<(&TitleBar<'_, Message, Self>, Layout<'_>)>,
body: (&Element<'_, Message, Self>, Layout<'_>),
cursor_position: Point,
@ -182,7 +236,7 @@ where
&mut self,
defaults: &Self::Defaults,
bounds: Rectangle,
style_sheet: &Self::Style,
style_sheet: &<Self as container::Renderer>::Style,
content: (&Element<'_, Message, Self>, Layout<'_>),
controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
cursor_position: Point,