From 5681c83d3c30cfb6940de734b60e61da9571ed0b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Nov 2020 01:14:19 +0100 Subject: Remove focus concept from `pane_grid` --- native/src/widget/pane_grid.rs | 113 +++-------------------------------------- 1 file changed, 7 insertions(+), 106 deletions(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 276bfae3..43d57e19 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -73,7 +73,7 @@ use crate::{ /// let (mut state, _) = pane_grid::State::new(PaneState::SomePane); /// /// let pane_grid = -/// PaneGrid::new(&mut state, |pane, state, focus| { +/// PaneGrid::new(&mut state, |pane, state| { /// pane_grid::Content::new(match state { /// PaneState::SomePane => Text::new("This is some pane"), /// PaneState::AnotherKindOfPane => Text::new("This is another kind of pane"), @@ -92,10 +92,8 @@ pub struct PaneGrid<'a, Message, Renderer: self::Renderer> { width: Length, height: Length, spacing: u16, - modifier_keys: keyboard::ModifiersState, on_drag: Option Message + 'a>>, on_resize: Option<(u16, Box Message + 'a>)>, - on_key_press: Option Option + 'a>>, } impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> @@ -112,31 +110,13 @@ where /// [`Pane`]: struct.Pane.html pub fn new( state: &'a mut State, - view: impl Fn( - Pane, - &'a mut T, - Option, - ) -> Content<'a, Message, Renderer>, + view: impl Fn(Pane, &'a mut T) -> Content<'a, Message, Renderer>, ) -> Self { let elements = { - let action = state.internal.action(); - let current_focus = action.focus(); - state .panes .iter_mut() - .map(move |(pane, pane_state)| { - let focus = match current_focus { - Some((focused_pane, focus)) - if *pane == focused_pane => - { - Some(focus) - } - _ => None, - }; - - (*pane, view(*pane, pane_state, focus)) - }) + .map(move |(pane, pane_state)| (*pane, view(*pane, pane_state))) .collect() }; @@ -146,13 +126,8 @@ where width: Length::Fill, height: Length::Fill, spacing: 0, - modifier_keys: keyboard::ModifiersState { - control: true, - ..Default::default() - }, on_drag: None, on_resize: None, - on_key_press: None, } } @@ -180,21 +155,6 @@ where self } - /// Sets the modifier keys of the [`PaneGrid`]. - /// - /// The modifier keys will need to be pressed to trigger key events. - /// - /// The default modifier key is `Ctrl`. - /// - /// [`PaneGrid`]: struct.PaneGrid.html - pub fn modifier_keys( - mut self, - modifier_keys: keyboard::ModifiersState, - ) -> Self { - self.modifier_keys = modifier_keys; - self - } - /// Enables the drag and drop interactions of the [`PaneGrid`], which will /// use the provided function to produce messages. /// @@ -225,31 +185,6 @@ where self.on_resize = Some((leeway, Box::new(f))); self } - - /// Captures hotkey interactions with the [`PaneGrid`], using the provided - /// function to produce messages. - /// - /// The function will be called when: - /// - a [`Pane`] is focused - /// - a key is pressed - /// - all the modifier keys are pressed - /// - /// If the function returns `None`, the key press event will be discarded - /// without producing any message. - /// - /// This method is particularly useful to implement hotkey interactions. - /// For instance, you can use it to enable splitting, swapping, or resizing - /// panes by pressing combinations of keys. - /// - /// [`PaneGrid`]: struct.PaneGrid.html - /// [`Pane`]: struct.Pane.html - pub fn on_key_press(mut self, f: F) -> Self - where - F: 'a + Fn(KeyPressEvent) -> Option, - { - self.on_key_press = Some(Box::new(f)); - self - } } impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> @@ -280,13 +215,9 @@ where messages .push(on_drag(DragEvent::Picked { pane: *pane })); - } else { - self.state.focus(pane); } } - None => { - self.state.focus(pane); - } + None => {} } } } @@ -495,17 +426,10 @@ where ); } } - } else { - // TODO: Encode cursor availability in the type system - if cursor_position.x > 0.0 && cursor_position.y > 0.0 { - self.state.unfocus(); - } } } mouse::Event::ButtonReleased(mouse::Button::Left) => { if let Some((pane, _)) = self.state.picked_pane() { - self.state.focus(&pane); - if let Some(on_drag) = &self.on_drag { let mut dropped_region = self .elements @@ -527,8 +451,10 @@ where messages.push(on_drag(event)); } + + self.state.idle(); } else if self.state.picked_split().is_some() { - self.state.drop_split(); + self.state.idle(); } } mouse::Event::CursorMoved { .. } => { @@ -536,31 +462,6 @@ where } _ => {} }, - Event::Keyboard(keyboard_event) => { - match keyboard_event { - keyboard::Event::KeyPressed { - modifiers, - key_code, - } => { - if let Some(on_key_press) = &self.on_key_press { - // TODO: Discard when event is captured - if let Some(_) = self.state.active_pane() { - if modifiers.matches(self.modifier_keys) { - if let Some(message) = - on_key_press(KeyPressEvent { - key_code, - modifiers, - }) - { - messages.push(message); - } - } - } - } - } - _ => {} - } - } _ => {} } -- cgit From 8008ea52862735aae326c8833355b3ecacb8fed1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Nov 2020 01:48:11 +0100 Subject: Introduce `on_click` handler in `PaneGrid` --- native/src/widget/pane_grid.rs | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 43d57e19..584b2ba4 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -92,6 +92,7 @@ pub struct PaneGrid<'a, Message, Renderer: self::Renderer> { width: Length, height: Length, spacing: u16, + on_click: Option Message + 'a>>, on_drag: Option Message + 'a>>, on_resize: Option<(u16, Box Message + 'a>)>, } @@ -126,6 +127,7 @@ where width: Length::Fill, height: Length::Fill, spacing: 0, + on_click: None, on_drag: None, on_resize: None, } @@ -155,6 +157,19 @@ where self } + /// Sets the message that will be produced when a [`Pane`] of the + /// [`PaneGrid`] is clicked. + /// + /// [`Pane`]: struct.Pane.html + /// [`PaneGrid`]: struct.PaneGrid.html + pub fn on_click(mut self, f: F) -> Self + where + F: 'a + Fn(Pane) -> Message, + { + self.on_click = Some(Box::new(f)); + self + } + /// Enables the drag and drop interactions of the [`PaneGrid`], which will /// use the provided function to produce messages. /// @@ -203,21 +218,21 @@ where ); if let Some(((pane, content), layout)) = clicked_region.next() { - match &self.on_drag { - Some(on_drag) => { - if content.can_be_picked_at(layout, cursor_position) { - let pane_position = layout.position(); + if let Some(on_click) = &self.on_click { + messages.push(on_click(*pane)); + } - let origin = cursor_position - - Vector::new(pane_position.x, pane_position.y); + if let Some(on_drag) = &self.on_drag { + if content.can_be_picked_at(layout, cursor_position) { + let pane_position = layout.position(); - self.state.pick_pane(pane, origin); + let origin = cursor_position + - Vector::new(pane_position.x, pane_position.y); - messages - .push(on_drag(DragEvent::Picked { pane: *pane })); - } + self.state.pick_pane(pane, origin); + + messages.push(on_drag(DragEvent::Picked { pane: *pane })); } - None => {} } } } -- cgit 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/widget/pane_grid.rs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'native/src/widget/pane_grid.rs') 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 -- cgit From 3b2ed0d6f02ecacafd19a8cba7a5e54a54b163a0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Nov 2020 03:18:46 +0100 Subject: Remove unnecessary `move` in `PaneGrid::new` --- native/src/widget/pane_grid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 7d9659e9..9d36bae6 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -117,7 +117,7 @@ where state .panes .iter_mut() - .map(move |(pane, pane_state)| (*pane, view(*pane, pane_state))) + .map(|(pane, pane_state)| (*pane, view(*pane, pane_state))) .collect() }; -- cgit