Add PaneGrid::on_key_press
for hotkey logic
This commit is contained in:
parent
56ba6215a2
commit
a280dcda23
@ -25,8 +25,10 @@ pub struct PaneGrid<'a, Message, Renderer> {
|
|||||||
width: Length,
|
width: Length,
|
||||||
height: Length,
|
height: Length,
|
||||||
spacing: u16,
|
spacing: u16,
|
||||||
|
modifier_keys: keyboard::ModifiersState,
|
||||||
on_drag: Option<Box<dyn Fn(DragEvent) -> Message>>,
|
on_drag: Option<Box<dyn Fn(DragEvent) -> Message>>,
|
||||||
on_resize: Option<Box<dyn Fn(ResizeEvent) -> Message>>,
|
on_resize: Option<Box<dyn Fn(ResizeEvent) -> Message>>,
|
||||||
|
on_key_press: Option<Box<dyn Fn(keyboard::KeyCode) -> Option<Message>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> {
|
impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> {
|
||||||
@ -67,8 +69,13 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> {
|
|||||||
width: Length::Fill,
|
width: Length::Fill,
|
||||||
height: Length::Fill,
|
height: Length::Fill,
|
||||||
spacing: 0,
|
spacing: 0,
|
||||||
|
modifier_keys: keyboard::ModifiersState {
|
||||||
|
control: true,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
on_drag: None,
|
on_drag: None,
|
||||||
on_resize: None,
|
on_resize: None,
|
||||||
|
on_key_press: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,6 +103,14 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn modifier_keys(
|
||||||
|
mut self,
|
||||||
|
modifier_keys: keyboard::ModifiersState,
|
||||||
|
) -> Self {
|
||||||
|
self.modifier_keys = modifier_keys;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn on_drag(
|
pub fn on_drag(
|
||||||
mut self,
|
mut self,
|
||||||
f: impl Fn(DragEvent) -> Message + 'static,
|
f: impl Fn(DragEvent) -> Message + 'static,
|
||||||
@ -112,6 +127,14 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn on_key_press(
|
||||||
|
mut self,
|
||||||
|
f: impl Fn(keyboard::KeyCode) -> Option<Message> + 'static,
|
||||||
|
) -> Self {
|
||||||
|
self.on_key_press = Some(Box::new(f));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
fn trigger_resize(
|
fn trigger_resize(
|
||||||
&mut self,
|
&mut self,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
@ -230,7 +253,9 @@ where
|
|||||||
|
|
||||||
if let Some(((pane, _), _)) = clicked_region.next() {
|
if let Some(((pane, _), _)) = clicked_region.next() {
|
||||||
match &self.on_drag {
|
match &self.on_drag {
|
||||||
Some(on_drag) if self.modifiers.alt => {
|
Some(on_drag)
|
||||||
|
if *self.modifiers == self.modifier_keys =>
|
||||||
|
{
|
||||||
self.state.pick_pane(pane);
|
self.state.pick_pane(pane);
|
||||||
|
|
||||||
messages.push(on_drag(DragEvent::Picked {
|
messages.push(on_drag(DragEvent::Picked {
|
||||||
@ -278,7 +303,7 @@ where
|
|||||||
state: ButtonState::Pressed,
|
state: ButtonState::Pressed,
|
||||||
}) if self.on_resize.is_some()
|
}) if self.on_resize.is_some()
|
||||||
&& self.state.picked_pane().is_none()
|
&& self.state.picked_pane().is_none()
|
||||||
&& self.modifiers.alt =>
|
&& *self.modifiers == self.modifier_keys =>
|
||||||
{
|
{
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
let relative_cursor = Point::new(
|
let relative_cursor = Point::new(
|
||||||
@ -334,7 +359,24 @@ where
|
|||||||
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
|
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
|
||||||
self.trigger_resize(layout, cursor_position, messages);
|
self.trigger_resize(layout, cursor_position, messages);
|
||||||
}
|
}
|
||||||
Event::Keyboard(keyboard::Event::Input { modifiers, .. }) => {
|
Event::Keyboard(keyboard::Event::Input {
|
||||||
|
modifiers,
|
||||||
|
key_code,
|
||||||
|
state,
|
||||||
|
}) => {
|
||||||
|
if let Some(on_key_press) = &self.on_key_press {
|
||||||
|
// TODO: Discard when event is captured
|
||||||
|
if state == ButtonState::Pressed {
|
||||||
|
if let Some(_) = self.state.idle_pane() {
|
||||||
|
if modifiers == self.modifier_keys {
|
||||||
|
if let Some(message) = on_key_press(key_code) {
|
||||||
|
messages.push(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
*self.modifiers = modifiers;
|
*self.modifiers = modifiers;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -186,6 +186,13 @@ impl Internal {
|
|||||||
self.action
|
self.action
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn idle_pane(&self) -> Option<Pane> {
|
||||||
|
match self.action {
|
||||||
|
Action::Idle { focus } => focus,
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn picked_pane(&self) -> Option<Pane> {
|
pub fn picked_pane(&self) -> Option<Pane> {
|
||||||
match self.action {
|
match self.action {
|
||||||
Action::Dragging { pane } => Some(pane),
|
Action::Dragging { pane } => Some(pane),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user