Implement hotkey logic in pane_grid example
This commit is contained in:
parent
a280dcda23
commit
6f9cf6c70d
2 changed files with 41 additions and 2 deletions
|
|
@ -7,3 +7,4 @@ publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
iced = { path = "../.." }
|
iced = { path = "../.." }
|
||||||
|
iced_native = { path = "../../native" }
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use iced::{
|
||||||
button, pane_grid, scrollable, Align, Button, Column, Container, Element,
|
button, pane_grid, scrollable, Align, Button, Column, Container, Element,
|
||||||
HorizontalAlignment, Length, PaneGrid, Sandbox, Scrollable, Settings, Text,
|
HorizontalAlignment, Length, PaneGrid, Sandbox, Scrollable, Settings, Text,
|
||||||
};
|
};
|
||||||
|
use iced_native::input::keyboard;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
Example::run(Settings::default())
|
Example::run(Settings::default())
|
||||||
|
|
@ -16,9 +17,11 @@ struct Example {
|
||||||
enum Message {
|
enum Message {
|
||||||
Split(pane_grid::Axis, pane_grid::Pane),
|
Split(pane_grid::Axis, pane_grid::Pane),
|
||||||
SplitFocused(pane_grid::Axis),
|
SplitFocused(pane_grid::Axis),
|
||||||
|
FocusAdjacent(pane_grid::Direction),
|
||||||
Dragged(pane_grid::DragEvent),
|
Dragged(pane_grid::DragEvent),
|
||||||
Resized(pane_grid::ResizeEvent),
|
Resized(pane_grid::ResizeEvent),
|
||||||
Close(pane_grid::Pane),
|
Close(pane_grid::Pane),
|
||||||
|
CloseFocused,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sandbox for Example {
|
impl Sandbox for Example {
|
||||||
|
|
@ -59,6 +62,15 @@ impl Sandbox for Example {
|
||||||
self.panes_created += 1;
|
self.panes_created += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Message::FocusAdjacent(direction) => {
|
||||||
|
if let Some(pane) = self.panes.active() {
|
||||||
|
if let Some(adjacent) =
|
||||||
|
self.panes.adjacent(&pane, direction)
|
||||||
|
{
|
||||||
|
self.panes.focus(&adjacent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Message::Resized(pane_grid::ResizeEvent { split, ratio }) => {
|
Message::Resized(pane_grid::ResizeEvent { split, ratio }) => {
|
||||||
self.panes.resize(&split, ratio);
|
self.panes.resize(&split, ratio);
|
||||||
}
|
}
|
||||||
|
|
@ -72,6 +84,11 @@ impl Sandbox for Example {
|
||||||
Message::Close(pane) => {
|
Message::Close(pane) => {
|
||||||
let _ = self.panes.close(&pane);
|
let _ = self.panes.close(&pane);
|
||||||
}
|
}
|
||||||
|
Message::CloseFocused => {
|
||||||
|
if let Some(pane) = self.panes.active() {
|
||||||
|
let _ = self.panes.close(&pane);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,7 +103,8 @@ impl Sandbox for Example {
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.spacing(5)
|
.spacing(5)
|
||||||
.on_drag(Message::Dragged)
|
.on_drag(Message::Dragged)
|
||||||
.on_resize(Message::Resized);
|
.on_resize(Message::Resized)
|
||||||
|
.on_key_press(handle_hotkey);
|
||||||
|
|
||||||
Column::new()
|
Column::new()
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
|
|
@ -97,6 +115,26 @@ impl Sandbox for Example {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_hotkey(key_code: keyboard::KeyCode) -> Option<Message> {
|
||||||
|
use keyboard::KeyCode;
|
||||||
|
use pane_grid::{Axis, Direction};
|
||||||
|
|
||||||
|
let direction = match key_code {
|
||||||
|
KeyCode::Up => Some(Direction::Up),
|
||||||
|
KeyCode::Down => Some(Direction::Down),
|
||||||
|
KeyCode::Left => Some(Direction::Left),
|
||||||
|
KeyCode::Right => Some(Direction::Right),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
match key_code {
|
||||||
|
KeyCode::V => Some(Message::SplitFocused(Axis::Vertical)),
|
||||||
|
KeyCode::H => Some(Message::SplitFocused(Axis::Horizontal)),
|
||||||
|
KeyCode::W => Some(Message::CloseFocused),
|
||||||
|
_ => direction.map(Message::FocusAdjacent),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct Content {
|
struct Content {
|
||||||
id: usize,
|
id: usize,
|
||||||
scroll: scrollable::State,
|
scroll: scrollable::State,
|
||||||
|
|
@ -189,7 +227,7 @@ mod style {
|
||||||
fn style(&self) -> container::Style {
|
fn style(&self) -> container::Style {
|
||||||
container::Style {
|
container::Style {
|
||||||
background: Some(Background::Color(Color::WHITE)),
|
background: Some(Background::Color(Color::WHITE)),
|
||||||
border_width: 1,
|
border_width: if self.is_focused { 2 } else { 1 },
|
||||||
border_color: if self.is_focused {
|
border_color: if self.is_focused {
|
||||||
Color::from_rgb8(0x25, 0x7A, 0xFD)
|
Color::from_rgb8(0x25, 0x7A, 0xFD)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue