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` --- examples/pane_grid/src/main.rs | 46 ++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 22 deletions(-) (limited to 'examples/pane_grid/src/main.rs') diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index c4946645..42184114 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -11,6 +11,7 @@ pub fn main() -> iced::Result { struct Example { panes: pane_grid::State, panes_created: usize, + focus: Option, } #[derive(Debug, Clone, Copy)] @@ -33,6 +34,7 @@ impl Sandbox for Example { Example { panes, panes_created: 1, + focus: None, } } @@ -52,7 +54,7 @@ impl Sandbox for Example { self.panes_created += 1; } Message::SplitFocused(axis) => { - if let Some(pane) = self.panes.active() { + if let Some(pane) = self.focus { let _ = self.panes.split( axis, &pane, @@ -63,11 +65,11 @@ impl Sandbox for Example { } } Message::FocusAdjacent(direction) => { - if let Some(pane) = self.panes.active() { + if let Some(pane) = self.focus { if let Some(adjacent) = self.panes.adjacent(&pane, direction) { - self.panes.focus(&adjacent); + self.focus = Some(adjacent); } } } @@ -85,7 +87,7 @@ impl Sandbox for Example { let _ = self.panes.close(&pane); } Message::CloseFocused => { - if let Some(pane) = self.panes.active() { + if let Some(pane) = self.focus { let _ = self.panes.close(&pane); } } @@ -93,26 +95,26 @@ impl Sandbox for Example { } fn view(&mut self) -> Element { + let focus = self.focus; let total_panes = self.panes.len(); - let pane_grid = - PaneGrid::new(&mut self.panes, |pane, content, focus| { - let is_focused = focus.is_some(); - let title_bar = - pane_grid::TitleBar::new(format!("Pane {}", content.id)) - .padding(10) - .style(style::TitleBar { is_focused }); - - pane_grid::Content::new(content.view(pane, total_panes)) - .title_bar(title_bar) - .style(style::Pane { is_focused }) - }) - .width(Length::Fill) - .height(Length::Fill) - .spacing(10) - .on_drag(Message::Dragged) - .on_resize(10, Message::Resized) - .on_key_press(handle_hotkey); + let pane_grid = PaneGrid::new(&mut self.panes, |pane, content| { + let is_focused = focus == Some(pane); + + let title_bar = + pane_grid::TitleBar::new(format!("Pane {}", content.id)) + .padding(10) + .style(style::TitleBar { is_focused }); + + pane_grid::Content::new(content.view(pane, total_panes)) + .title_bar(title_bar) + .style(style::Pane { is_focused }) + }) + .width(Length::Fill) + .height(Length::Fill) + .spacing(10) + .on_drag(Message::Dragged) + .on_resize(10, Message::Resized); Container::new(pane_grid) .width(Length::Fill) -- 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` --- examples/pane_grid/src/main.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'examples/pane_grid/src/main.rs') diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 42184114..36485e9a 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -19,6 +19,7 @@ enum Message { Split(pane_grid::Axis, pane_grid::Pane), SplitFocused(pane_grid::Axis), FocusAdjacent(pane_grid::Direction), + Clicked(pane_grid::Pane), Dragged(pane_grid::DragEvent), Resized(pane_grid::ResizeEvent), Close(pane_grid::Pane), @@ -45,22 +46,30 @@ impl Sandbox for Example { fn update(&mut self, message: Message) { match message { Message::Split(axis, pane) => { - let _ = self.panes.split( + let result = self.panes.split( axis, &pane, Content::new(self.panes_created), ); + if let Some((pane, _)) = result { + self.focus = Some(pane); + } + self.panes_created += 1; } Message::SplitFocused(axis) => { if let Some(pane) = self.focus { - let _ = self.panes.split( + let result = self.panes.split( axis, &pane, Content::new(self.panes_created), ); + if let Some((pane, _)) = result { + self.focus = Some(pane); + } + self.panes_created += 1; } } @@ -73,6 +82,9 @@ impl Sandbox for Example { } } } + Message::Clicked(pane) => { + self.focus = Some(pane); + } Message::Resized(pane_grid::ResizeEvent { split, ratio }) => { self.panes.resize(&split, ratio); } @@ -113,6 +125,7 @@ impl Sandbox for Example { .width(Length::Fill) .height(Length::Fill) .spacing(10) + .on_click(Message::Clicked) .on_drag(Message::Dragged) .on_resize(10, Message::Resized); -- 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` --- examples/pane_grid/src/main.rs | 54 +++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 16 deletions(-) (limited to 'examples/pane_grid/src/main.rs') diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 36485e9a..d149f9c6 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -1,8 +1,9 @@ use iced::{ - button, keyboard, pane_grid, scrollable, Align, Button, Column, Container, - Element, HorizontalAlignment, Length, PaneGrid, Sandbox, Scrollable, - Settings, Text, + button, executor, keyboard, pane_grid, scrollable, Align, Application, + Button, Column, Command, Container, Element, HorizontalAlignment, Length, + PaneGrid, Scrollable, Settings, Subscription, Text, }; +use iced_native::{subscription, Event}; pub fn main() -> iced::Result { Example::run(Settings::default()) @@ -26,24 +27,29 @@ enum Message { CloseFocused, } -impl Sandbox for Example { +impl Application for Example { type Message = Message; + type Executor = executor::Default; + type Flags = (); - fn new() -> Self { + fn new(_flags: ()) -> (Self, Command) { let (panes, _) = pane_grid::State::new(Content::new(0)); - Example { - panes, - panes_created: 1, - focus: None, - } + ( + Example { + panes, + panes_created: 1, + focus: None, + }, + Command::none(), + ) } fn title(&self) -> String { String::from("Pane grid - Iced") } - fn update(&mut self, message: Message) { + fn update(&mut self, message: Message) -> Command { match message { Message::Split(axis, pane) => { let result = self.panes.split( @@ -96,14 +102,30 @@ impl Sandbox for Example { } Message::Dragged(_) => {} Message::Close(pane) => { - let _ = self.panes.close(&pane); + if let Some((_, sibling)) = self.panes.close(&pane) { + self.focus = Some(sibling); + } } Message::CloseFocused => { if let Some(pane) = self.focus { - let _ = self.panes.close(&pane); + if let Some((_, sibling)) = self.panes.close(&pane) { + self.focus = Some(sibling); + } } } } + + Command::none() + } + + fn subscription(&self) -> Subscription { + subscription::events_with(|event| match event { + Event::Keyboard(keyboard::Event::KeyPressed { + modifiers, + key_code, + }) if modifiers.control => handle_hotkey(key_code), + _ => None, + }) } fn view(&mut self) -> Element { @@ -137,11 +159,11 @@ impl Sandbox for Example { } } -fn handle_hotkey(event: pane_grid::KeyPressEvent) -> Option { +fn handle_hotkey(key_code: keyboard::KeyCode) -> Option { use keyboard::KeyCode; use pane_grid::{Axis, Direction}; - let direction = match event.key_code { + let direction = match key_code { KeyCode::Up => Some(Direction::Up), KeyCode::Down => Some(Direction::Down), KeyCode::Left => Some(Direction::Left), @@ -149,7 +171,7 @@ fn handle_hotkey(event: pane_grid::KeyPressEvent) -> Option { _ => None, }; - match event.key_code { + match key_code { KeyCode::V => Some(Message::SplitFocused(Axis::Vertical)), KeyCode::H => Some(Message::SplitFocused(Axis::Horizontal)), KeyCode::W => Some(Message::CloseFocused), -- cgit From 86fa12229e7117306b8297a09aa22c99802600c8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Nov 2020 21:18:21 +0100 Subject: Introduce `is_command_pressed` to `ModifiersState` --- examples/pane_grid/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/pane_grid/src/main.rs') diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index d149f9c6..e786eadd 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -123,7 +123,7 @@ impl Application for Example { Event::Keyboard(keyboard::Event::KeyPressed { modifiers, key_code, - }) if modifiers.control => handle_hotkey(key_code), + }) if modifiers.is_command_pressed() => handle_hotkey(key_code), _ => None, }) } -- cgit