Implement mouse-based pane resizing for PaneGrid
This commit is contained in:
parent
db441a64b1
commit
f08cb4ad56
7 changed files with 265 additions and 11 deletions
|
|
@ -14,7 +14,7 @@ pub use state::{Focus, State};
|
|||
use crate::{
|
||||
input::{keyboard, mouse, ButtonState},
|
||||
layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, Size,
|
||||
Widget,
|
||||
Vector, Widget,
|
||||
};
|
||||
|
||||
#[allow(missing_debug_implementations)]
|
||||
|
|
@ -26,6 +26,7 @@ pub struct PaneGrid<'a, Message, Renderer> {
|
|||
height: Length,
|
||||
spacing: u16,
|
||||
on_drag: Option<Box<dyn Fn(DragEvent) -> Message>>,
|
||||
on_resize: Option<Box<dyn Fn(ResizeEvent) -> Message>>,
|
||||
}
|
||||
|
||||
impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> {
|
||||
|
|
@ -67,6 +68,7 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> {
|
|||
height: Length::Fill,
|
||||
spacing: 0,
|
||||
on_drag: None,
|
||||
on_resize: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -101,6 +103,14 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> {
|
|||
self.on_drag = Some(Box::new(f));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_resize(
|
||||
mut self,
|
||||
f: impl Fn(ResizeEvent) -> Message + 'static,
|
||||
) -> Self {
|
||||
self.on_resize = Some(Box::new(f));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
|
@ -110,6 +120,12 @@ pub enum DragEvent {
|
|||
Canceled { pane: Pane },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ResizeEvent {
|
||||
pub split: Split,
|
||||
pub ratio: f32,
|
||||
}
|
||||
|
||||
impl<'a, Message, Renderer> Widget<Message, Renderer>
|
||||
for PaneGrid<'a, Message, Renderer>
|
||||
where
|
||||
|
|
@ -178,7 +194,7 @@ where
|
|||
if let Some(((pane, _), _)) = clicked_region.next() {
|
||||
match &self.on_drag {
|
||||
Some(on_drag) if self.modifiers.alt => {
|
||||
self.state.drag(pane);
|
||||
self.state.pick_pane(pane);
|
||||
|
||||
messages.push(on_drag(DragEvent::Picked {
|
||||
pane: *pane,
|
||||
|
|
@ -193,7 +209,7 @@ where
|
|||
}
|
||||
}
|
||||
ButtonState::Released => {
|
||||
if let Some(pane) = self.state.dragged() {
|
||||
if let Some(pane) = self.state.picked_pane() {
|
||||
self.state.focus(&pane);
|
||||
|
||||
if let Some(on_drag) = &self.on_drag {
|
||||
|
|
@ -220,13 +236,101 @@ where
|
|||
}
|
||||
}
|
||||
},
|
||||
Event::Mouse(mouse::Event::Input {
|
||||
button: mouse::Button::Right,
|
||||
state,
|
||||
}) if self.on_resize.is_some()
|
||||
&& self.state.picked_pane().is_none()
|
||||
&& self.modifiers.alt =>
|
||||
{
|
||||
match state {
|
||||
ButtonState::Pressed => {
|
||||
let bounds = layout.bounds();
|
||||
|
||||
let splits = self.state.splits(
|
||||
f32::from(self.spacing),
|
||||
Size::new(bounds.width, bounds.height),
|
||||
);
|
||||
|
||||
let mut sorted_splits: Vec<_> = splits.iter().collect();
|
||||
let offset = Vector::new(bounds.x, bounds.y);
|
||||
|
||||
sorted_splits.sort_by_key(
|
||||
|(_, (axis, rectangle, ratio))| {
|
||||
let center = match axis {
|
||||
Axis::Horizontal => Point::new(
|
||||
rectangle.x + rectangle.width / 2.0,
|
||||
rectangle.y + rectangle.height * ratio,
|
||||
),
|
||||
|
||||
Axis::Vertical => Point::new(
|
||||
rectangle.x + rectangle.width * ratio,
|
||||
rectangle.y + rectangle.height / 2.0,
|
||||
),
|
||||
};
|
||||
|
||||
cursor_position
|
||||
.distance(center + offset)
|
||||
.round()
|
||||
as u32
|
||||
},
|
||||
);
|
||||
|
||||
if let Some((split, (axis, _, _))) =
|
||||
sorted_splits.first()
|
||||
{
|
||||
self.state.pick_split(split, *axis);
|
||||
}
|
||||
}
|
||||
ButtonState::Released => {
|
||||
self.state.drop_split();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
|
||||
if let Some(on_resize) = &self.on_resize {
|
||||
if let Some((split, _)) = self.state.picked_split() {
|
||||
let bounds = layout.bounds();
|
||||
|
||||
let splits = self.state.splits(
|
||||
f32::from(self.spacing),
|
||||
Size::new(bounds.width, bounds.height),
|
||||
);
|
||||
|
||||
if let Some((axis, rectangle, _)) = splits.get(&split) {
|
||||
let ratio = match axis {
|
||||
Axis::Horizontal => {
|
||||
let position = cursor_position.x - bounds.x
|
||||
+ rectangle.x;
|
||||
|
||||
(position / (rectangle.x + rectangle.width))
|
||||
.max(0.1)
|
||||
.min(0.9)
|
||||
}
|
||||
Axis::Vertical => {
|
||||
let position = cursor_position.y - bounds.y
|
||||
+ rectangle.y;
|
||||
|
||||
(position
|
||||
/ (rectangle.y + rectangle.height))
|
||||
.max(0.1)
|
||||
.min(0.9)
|
||||
}
|
||||
};
|
||||
|
||||
messages
|
||||
.push(on_resize(ResizeEvent { split, ratio }));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::Keyboard(keyboard::Event::Input { modifiers, .. }) => {
|
||||
*self.modifiers = modifiers;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if self.state.dragged().is_none() {
|
||||
if self.state.picked_pane().is_none() {
|
||||
{
|
||||
self.elements.iter_mut().zip(layout.children()).for_each(
|
||||
|((_, pane), layout)| {
|
||||
|
|
@ -254,7 +358,8 @@ where
|
|||
renderer.draw(
|
||||
defaults,
|
||||
&self.elements,
|
||||
self.state.dragged(),
|
||||
self.state.picked_pane(),
|
||||
self.state.picked_split().map(|(_, axis)| axis),
|
||||
layout,
|
||||
cursor_position,
|
||||
)
|
||||
|
|
@ -297,6 +402,7 @@ pub trait Renderer: crate::Renderer + Sized {
|
|||
defaults: &Self::Defaults,
|
||||
content: &[(Pane, Element<'_, Message, Self>)],
|
||||
dragging: Option<Pane>,
|
||||
resizing: Option<Axis>,
|
||||
layout: Layout<'_>,
|
||||
cursor_position: Point,
|
||||
) -> Self::Output;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue