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

View File

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