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>,
) -> 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,
};

View file

@ -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);

View file

@ -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_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);
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) {