From d6d5cf0294b1231f4909a782e90b491cc6838fae Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Nov 2020 02:32:57 +0100 Subject: Restore hotkeys in `pane_grid` example - Implement `subscription::events_with` - Remove `pane_grid::KeyPressEvent` - Return closest sibling in `pane_grid::State::close` --- native/src/subscription.rs | 22 +++++++++++++++++++++- native/src/subscription/events.rs | 19 +++++++++++++++---- native/src/widget/pane_grid.rs | 16 ++-------------- native/src/widget/pane_grid/state.rs | 9 +++++---- 4 files changed, 43 insertions(+), 23 deletions(-) (limited to 'native') diff --git a/native/src/subscription.rs b/native/src/subscription.rs index 0d002c6c..18750abf 100644 --- a/native/src/subscription.rs +++ b/native/src/subscription.rs @@ -43,5 +43,25 @@ use events::Events; /// [`Subscription`]: type.Subscription.html /// [`Event`]: ../enum.Event.html pub fn events() -> Subscription { - Subscription::from_recipe(Events) + Subscription::from_recipe(Events { f: Some }) +} + +/// Returns a [`Subscription`] that filters all the runtime events with the +/// provided function, producing messages accordingly. +/// +/// This subscription will call the provided function for every [`Event`] +/// handled by the runtime. If the function: +/// +/// - Returns `None`, the [`Event`] will be discarded. +/// - Returns `Some` message, the `Message` will be produced. +/// +/// [`Subscription`]: type.Subscription.html +/// [`Event`]: ../enum.Event.html +pub fn events_with( + f: fn(Event) -> Option, +) -> Subscription +where + Message: 'static + Send, +{ + Subscription::from_recipe(Events { f }) } diff --git a/native/src/subscription/events.rs b/native/src/subscription/events.rs index ceae467d..a1ae6051 100644 --- a/native/src/subscription/events.rs +++ b/native/src/subscription/events.rs @@ -2,17 +2,26 @@ use crate::{ subscription::{EventStream, Recipe}, Event, Hasher, }; +use iced_futures::futures::future; +use iced_futures::futures::StreamExt; use iced_futures::BoxStream; -pub struct Events; +pub struct Events { + pub(super) f: fn(Event) -> Option, +} -impl Recipe for Events { - type Output = Event; +impl Recipe for Events +where + Message: 'static + Send, +{ + type Output = Message; fn hash(&self, state: &mut Hasher) { use std::hash::Hash; - std::any::TypeId::of::().hash(state); + struct Marker; + std::any::TypeId::of::().hash(state); + self.f.hash(state); } fn stream( @@ -20,5 +29,7 @@ impl Recipe for Events { event_stream: EventStream, ) -> BoxStream { event_stream + .filter_map(move |event| future::ready((self.f)(event))) + .boxed() } } diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 584b2ba4..7d9659e9 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -29,8 +29,8 @@ pub use state::{Focus, State}; pub use title_bar::TitleBar; use crate::{ - container, keyboard, layout, mouse, overlay, row, text, Clipboard, Element, - Event, Hasher, Layout, Length, Point, Rectangle, Size, Vector, Widget, + container, layout, mouse, overlay, row, text, Clipboard, Element, Event, + Hasher, Layout, Length, Point, Rectangle, Size, Vector, Widget, }; /// A collection of panes distributed using either vertical or horizontal splits @@ -336,18 +336,6 @@ pub struct ResizeEvent { pub ratio: f32, } -/// An event produced during a key press interaction of a [`PaneGrid`]. -/// -/// [`PaneGrid`]: struct.PaneGrid.html -#[derive(Debug, Clone, Copy)] -pub struct KeyPressEvent { - /// The key that was pressed. - pub key_code: keyboard::KeyCode, - - /// The state of the modifier keys when the key was pressed. - pub modifiers: keyboard::ModifiersState, -} - impl<'a, Message, Renderer> Widget for PaneGrid<'a, Message, Renderer> where diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index e2793641..be36b070 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -227,12 +227,13 @@ impl State { let _ = self.internal.layout.resize(split, ratio); } - /// Closes the given [`Pane`] and returns its internal state, if it exists. + /// Closes the given [`Pane`] and returns its internal state and its closest + /// sibling, if it exists. /// /// [`Pane`]: struct.Pane.html - pub fn close(&mut self, pane: &Pane) -> Option { - if let Some(_) = self.internal.layout.remove(pane) { - self.panes.remove(pane) + pub fn close(&mut self, pane: &Pane) -> Option<(T, Pane)> { + if let Some(sibling) = self.internal.layout.remove(pane) { + self.panes.remove(pane).map(|state| (state, sibling)) } else { None } -- cgit