Implement event capturing for PaneGrid

This commit is contained in:
Héctor Ramón Jiménez 2020-11-12 00:40:00 +01:00
parent 3bcee62beb
commit 31c509b206
3 changed files with 47 additions and 28 deletions

View File

@ -249,7 +249,7 @@ where
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
) {
) -> event::Status {
if let Some((_, on_resize)) = &self.on_resize {
if let Some((split, _)) = self.state.picked_split() {
let bounds = layout.bounds();
@ -276,9 +276,13 @@ where
};
messages.push(on_resize(ResizeEvent { split, ratio }));
return event::Status::Captured;
}
}
}
event::Status::Ignored
}
}
@ -394,12 +398,16 @@ where
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
) -> event::Status {
let mut event_status = event::Status::Ignored;
match event {
Event::Mouse(mouse_event) => match mouse_event {
mouse::Event::ButtonPressed(mouse::Button::Left) => {
let bounds = layout.bounds();
if bounds.contains(cursor_position) {
event_status = event::Status::Captured;
match self.on_resize {
Some((leeway, _)) => {
let relative_cursor = Point::new(
@ -463,11 +471,16 @@ where
}
self.state.idle();
event_status = event::Status::Captured;
} else if self.state.picked_split().is_some() {
self.state.idle();
event_status = event::Status::Captured;
}
}
mouse::Event::CursorMoved { .. } => {
event_status =
self.trigger_resize(layout, cursor_position, messages);
}
_ => {}
@ -476,9 +489,10 @@ where
}
if self.state.picked_pane().is_none() {
{
self.elements.iter_mut().zip(layout.children()).for_each(
|((_, pane), layout)| {
self.elements
.iter_mut()
.zip(layout.children())
.map(|((_, pane), layout)| {
pane.on_event(
event.clone(),
layout,
@ -487,14 +501,13 @@ where
renderer,
clipboard,
)
},
);
})
.fold(event_status, event::Status::merge)
} else {
event::Status::Captured
}
}
event::Status::Ignored
}
fn draw(
&self,
renderer: &mut Renderer,

View File

@ -1,8 +1,9 @@
use crate::container;
use crate::event::{self, Event};
use crate::layout;
use crate::overlay;
use crate::pane_grid::{self, TitleBar};
use crate::{Clipboard, Element, Event, Hasher, Layout, Point, Size};
use crate::{Clipboard, Element, Hasher, Layout, Point, Size};
/// The content of a [`Pane`].
///
@ -154,11 +155,13 @@ where
messages: &mut Vec<Message>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
) {
) -> event::Status {
let mut event_status = event::Status::Ignored;
let body_layout = if let Some(title_bar) = &mut self.title_bar {
let mut children = layout.children();
title_bar.on_event(
event_status = title_bar.on_event(
event.clone(),
children.next().unwrap(),
cursor_position,
@ -172,7 +175,7 @@ where
layout
};
let _ = self.body.on_event(
let body_status = self.body.on_event(
event,
body_layout,
cursor_position,
@ -180,6 +183,8 @@ where
renderer,
clipboard,
);
event_status.merge(body_status)
}
pub(crate) fn hash_layout(&self, state: &mut Hasher) {

View File

@ -1,8 +1,7 @@
use crate::event::{self, Event};
use crate::layout;
use crate::pane_grid;
use crate::{
Clipboard, Element, Event, Hasher, Layout, Point, Rectangle, Size,
};
use crate::{Clipboard, Element, Hasher, Layout, Point, Rectangle, Size};
/// The title bar of a [`Pane`].
///
@ -245,7 +244,7 @@ where
messages: &mut Vec<Message>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
) {
) -> event::Status {
if let Some(controls) = &mut self.controls {
let mut children = layout.children();
let padded = children.next().unwrap();
@ -254,14 +253,16 @@ where
let _ = children.next();
let controls_layout = children.next().unwrap();
let _ = controls.on_event(
controls.on_event(
event,
controls_layout,
cursor_position,
messages,
renderer,
clipboard,
);
)
} else {
event::Status::Ignored
}
}
}