diff options
author | 2020-03-14 06:26:09 +0100 | |
---|---|---|
committer | 2020-03-14 06:26:09 +0100 | |
commit | 00c2b55b569ea2ff2fc9de9bbf02475c6ede7e42 (patch) | |
tree | b1c7c222e561d11272879c94731c36aa145391bd /native | |
parent | 5c8ec4504b6541cdc588b91a6b2c7100b4a7cc77 (diff) | |
download | iced-00c2b55b569ea2ff2fc9de9bbf02475c6ede7e42.tar.gz iced-00c2b55b569ea2ff2fc9de9bbf02475c6ede7e42.tar.bz2 iced-00c2b55b569ea2ff2fc9de9bbf02475c6ede7e42.zip |
Replace `FocusedPane` with `Action` in `pane_grid`
Diffstat (limited to 'native')
-rw-r--r-- | native/src/widget/pane_grid.rs | 14 | ||||
-rw-r--r-- | native/src/widget/pane_grid/node.rs | 8 | ||||
-rw-r--r-- | native/src/widget/pane_grid/state.rs | 80 |
3 files changed, 52 insertions, 50 deletions
diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 2272d32c..e446b235 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -36,17 +36,19 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> { ) -> Element<'a, Message, Renderer>, ) -> Self { let elements = { - let focused_pane = state.internal.focused(); + let action = state.internal.action(); + let current_focus = action.focus(); state .panes .iter_mut() .map(move |(pane, pane_state)| { - let focus = match focused_pane { - state::FocusedPane::Some { - pane: focused_pane, - focus, - } if *pane == focused_pane => Some(focus), + let focus = match current_focus { + Some((focused_pane, focus)) + if *pane == focused_pane => + { + Some(focus) + } _ => None, }; diff --git a/native/src/widget/pane_grid/node.rs b/native/src/widget/pane_grid/node.rs index a9aa7fdc..744e3e17 100644 --- a/native/src/widget/pane_grid/node.rs +++ b/native/src/widget/pane_grid/node.rs @@ -8,6 +8,7 @@ use std::collections::HashMap; #[derive(Debug, Clone, Hash)] pub enum Node { Split { + id: usize, kind: Split, ratio: u32, a: Box<Node>, @@ -32,8 +33,9 @@ impl Node { } } - pub fn split(&mut self, kind: Split, new_pane: Pane) { + pub fn split(&mut self, id: usize, kind: Split, new_pane: Pane) { *self = Node::Split { + id, kind, ratio: 500_000, a: Box::new(self.clone()), @@ -112,7 +114,9 @@ impl Node { regions: &mut HashMap<Pane, Rectangle>, ) { match self { - Node::Split { kind, ratio, a, b } => { + Node::Split { + kind, ratio, a, b, .. + } => { let ratio = *ratio as f32 / 1_000_000.0; let (region_a, region_b) = kind.apply(current, ratio, halved_spacing); diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index 130c7e34..61576a29 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -31,8 +31,8 @@ impl<T> State<T> { panes, internal: Internal { layout: Node::Pane(first_pane), - last_pane: 0, - focused_pane: FocusedPane::None, + last_id: 0, + action: Action::Idle { focus: None }, }, modifiers: keyboard::ModifiersState::default(), }, @@ -56,25 +56,14 @@ impl<T> State<T> { self.panes.iter_mut() } - pub fn focused_pane(&self) -> Option<Pane> { - match self.internal.focused_pane { - FocusedPane::Some { - pane, - focus: Focus::Idle, - } => Some(pane), - FocusedPane::Some { - focus: Focus::Dragging, - .. - } => None, - FocusedPane::None => None, + pub fn active(&self) -> Option<Pane> { + match self.internal.action { + Action::Idle { focus } => focus, + _ => None, } } - pub fn adjacent_pane( - &self, - pane: &Pane, - direction: Direction, - ) -> Option<Pane> { + pub fn adjacent(&self, pane: &Pane, direction: Direction) -> Option<Pane> { let regions = self.internal.layout.regions(0.0, Size::new(4096.0, 4096.0)); @@ -130,12 +119,18 @@ impl<T> State<T> { let node = self.internal.layout.find(pane)?; let new_pane = { - self.internal.last_pane = self.internal.last_pane.checked_add(1)?; + self.internal.last_id = self.internal.last_id.checked_add(1)?; + + Pane(self.internal.last_id) + }; + + let split_id = { + self.internal.last_id = self.internal.last_id.checked_add(1)?; - Pane(self.internal.last_pane) + self.internal.last_id }; - node.split(kind, new_pane); + node.split(split_id, kind, new_pane); let _ = self.panes.insert(new_pane, state); self.focus(&new_pane); @@ -169,26 +164,33 @@ impl<T> State<T> { #[derive(Debug)] pub struct Internal { layout: Node, - last_pane: usize, - focused_pane: FocusedPane, + last_id: usize, + action: Action, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum FocusedPane { - None, - Some { pane: Pane, focus: Focus }, +pub enum Action { + Idle { focus: Option<Pane> }, + Dragging { pane: Pane }, +} + +impl Action { + pub fn focus(&self) -> Option<(Pane, Focus)> { + match self { + Action::Idle { focus } => focus.map(|pane| (pane, Focus::Idle)), + Action::Dragging { pane } => Some((*pane, Focus::Dragging)), + } + } } impl Internal { - pub fn focused(&self) -> FocusedPane { - self.focused_pane + pub fn action(&self) -> Action { + self.action } + pub fn dragged(&self) -> Option<Pane> { - match self.focused_pane { - FocusedPane::Some { - pane, - focus: Focus::Dragging, - } => Some(pane), + match self.action { + Action::Dragging { pane } => Some(pane), _ => None, } } @@ -202,21 +204,15 @@ impl Internal { } pub fn focus(&mut self, pane: &Pane) { - self.focused_pane = FocusedPane::Some { - pane: *pane, - focus: Focus::Idle, - }; + self.action = Action::Idle { focus: Some(*pane) }; } pub fn drag(&mut self, pane: &Pane) { - self.focused_pane = FocusedPane::Some { - pane: *pane, - focus: Focus::Dragging, - }; + self.action = Action::Dragging { pane: *pane }; } pub fn unfocus(&mut self) { - self.focused_pane = FocusedPane::None; + self.action = Action::Idle { focus: None }; } pub fn hash_layout(&self, hasher: &mut Hasher) { |