Replace FocusedPane with Action in pane_grid

This commit is contained in:
Héctor Ramón Jiménez 2020-03-14 06:26:09 +01:00
parent 5c8ec4504b
commit 00c2b55b56
3 changed files with 52 additions and 50 deletions

View file

@ -36,17 +36,19 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> {
) -> Element<'a, Message, Renderer>, ) -> Element<'a, Message, Renderer>,
) -> Self { ) -> Self {
let elements = { let elements = {
let focused_pane = state.internal.focused(); let action = state.internal.action();
let current_focus = action.focus();
state state
.panes .panes
.iter_mut() .iter_mut()
.map(move |(pane, pane_state)| { .map(move |(pane, pane_state)| {
let focus = match focused_pane { let focus = match current_focus {
state::FocusedPane::Some { Some((focused_pane, focus))
pane: focused_pane, if *pane == focused_pane =>
focus, {
} if *pane == focused_pane => Some(focus), Some(focus)
}
_ => None, _ => None,
}; };

View file

@ -8,6 +8,7 @@ use std::collections::HashMap;
#[derive(Debug, Clone, Hash)] #[derive(Debug, Clone, Hash)]
pub enum Node { pub enum Node {
Split { Split {
id: usize,
kind: Split, kind: Split,
ratio: u32, ratio: u32,
a: Box<Node>, 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 { *self = Node::Split {
id,
kind, kind,
ratio: 500_000, ratio: 500_000,
a: Box::new(self.clone()), a: Box::new(self.clone()),
@ -112,7 +114,9 @@ impl Node {
regions: &mut HashMap<Pane, Rectangle>, regions: &mut HashMap<Pane, Rectangle>,
) { ) {
match self { match self {
Node::Split { kind, ratio, a, b } => { Node::Split {
kind, ratio, a, b, ..
} => {
let ratio = *ratio as f32 / 1_000_000.0; let ratio = *ratio as f32 / 1_000_000.0;
let (region_a, region_b) = let (region_a, region_b) =
kind.apply(current, ratio, halved_spacing); kind.apply(current, ratio, halved_spacing);

View file

@ -31,8 +31,8 @@ impl<T> State<T> {
panes, panes,
internal: Internal { internal: Internal {
layout: Node::Pane(first_pane), layout: Node::Pane(first_pane),
last_pane: 0, last_id: 0,
focused_pane: FocusedPane::None, action: Action::Idle { focus: None },
}, },
modifiers: keyboard::ModifiersState::default(), modifiers: keyboard::ModifiersState::default(),
}, },
@ -56,25 +56,14 @@ impl<T> State<T> {
self.panes.iter_mut() self.panes.iter_mut()
} }
pub fn focused_pane(&self) -> Option<Pane> { pub fn active(&self) -> Option<Pane> {
match self.internal.focused_pane { match self.internal.action {
FocusedPane::Some { Action::Idle { focus } => focus,
pane, _ => None,
focus: Focus::Idle,
} => Some(pane),
FocusedPane::Some {
focus: Focus::Dragging,
..
} => None,
FocusedPane::None => None,
} }
} }
pub fn adjacent_pane( pub fn adjacent(&self, pane: &Pane, direction: Direction) -> Option<Pane> {
&self,
pane: &Pane,
direction: Direction,
) -> Option<Pane> {
let regions = let regions =
self.internal.layout.regions(0.0, Size::new(4096.0, 4096.0)); 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 node = self.internal.layout.find(pane)?;
let new_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_pane) Pane(self.internal.last_id)
}; };
node.split(kind, new_pane); let split_id = {
self.internal.last_id = self.internal.last_id.checked_add(1)?;
self.internal.last_id
};
node.split(split_id, kind, new_pane);
let _ = self.panes.insert(new_pane, state); let _ = self.panes.insert(new_pane, state);
self.focus(&new_pane); self.focus(&new_pane);
@ -169,26 +164,33 @@ impl<T> State<T> {
#[derive(Debug)] #[derive(Debug)]
pub struct Internal { pub struct Internal {
layout: Node, layout: Node,
last_pane: usize, last_id: usize,
focused_pane: FocusedPane, action: Action,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FocusedPane { pub enum Action {
None, Idle { focus: Option<Pane> },
Some { pane: Pane, focus: Focus }, 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 { impl Internal {
pub fn focused(&self) -> FocusedPane { pub fn action(&self) -> Action {
self.focused_pane self.action
} }
pub fn dragged(&self) -> Option<Pane> { pub fn dragged(&self) -> Option<Pane> {
match self.focused_pane { match self.action {
FocusedPane::Some { Action::Dragging { pane } => Some(pane),
pane,
focus: Focus::Dragging,
} => Some(pane),
_ => None, _ => None,
} }
} }
@ -202,21 +204,15 @@ impl Internal {
} }
pub fn focus(&mut self, pane: &Pane) { pub fn focus(&mut self, pane: &Pane) {
self.focused_pane = FocusedPane::Some { self.action = Action::Idle { focus: Some(*pane) };
pane: *pane,
focus: Focus::Idle,
};
} }
pub fn drag(&mut self, pane: &Pane) { pub fn drag(&mut self, pane: &Pane) {
self.focused_pane = FocusedPane::Some { self.action = Action::Dragging { pane: *pane };
pane: *pane,
focus: Focus::Dragging,
};
} }
pub fn unfocus(&mut self) { pub fn unfocus(&mut self) {
self.focused_pane = FocusedPane::None; self.action = Action::Idle { focus: None };
} }
pub fn hash_layout(&self, hasher: &mut Hasher) { pub fn hash_layout(&self, hasher: &mut Hasher) {