summaryrefslogtreecommitdiffstats
path: root/native/src/widget/pane_grid/state.rs
blob: 456ad78aba06c5cb57256045a656861be7fa52d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use crate::{
    input::keyboard,
    pane_grid::{node::Node, Axis, Direction, Pane, Split},
    Hasher, Point, Rectangle, Size,
};

use std::collections::HashMap;

#[derive(Debug)]
pub struct State<T> {
    pub(super) panes: HashMap<Pane, T>,
    pub(super) internal: Internal,
    pub(super) modifiers: keyboard::ModifiersState,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Focus {
    Idle,
    Dragging,
}

impl<T> State<T> {
    pub fn new(first_pane_state: T) -> (Self, Pane) {
        let first_pane = Pane(0);

        let mut panes = HashMap::new();
        let _ = panes.insert(first_pane, first_pane_state);

        (
            State {
                panes,
                internal: Internal {
                    layout: Node::Pane(first_pane),
                    last_id: 0,
                    action: Action::Idle { focus: None },
                },
                modifiers: keyboard::ModifiersState::default(),
            },
            first_pane,
        )
    }

    pub fn len(&self) -> usize {
        self.panes.len()
    }

    pub fn get_mut(&mut self, pane: &Pane) -> Option<&mut T> {
        self.panes.get_mut(pane)
    }

    pub fn iter(&self) -> impl Iterator<Item = (&Pane, &T)> {
        self.panes.iter()
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Pane, &mut T)> {
        self.panes.iter_mut()
    }

    pub fn active(&self) -> Option<Pane> {
        match self.internal.action {
            Action::Idle { focus } => focus,
            _ => None,
        }
    }

    pub fn adjacent(&self, pane: &Pane, direction: Direction) -> Option<Pane> {
        let regions =
            self.internal.layout.regions(0.0, Size::new(4096.0, 4096.0));

        let current_region = regions.get(pane)?;

        let target = match direction {
            Direction::Left => {
                Point::new(current_region.x - 1.0, current_region.y + 1.0)
            }
            Direction::Right => Point::new(
                current_region.x + current_region.width + 1.0,
                current_region.y + 1.0,
            ),
            Direction::Up => {
                Point::new(current_region.x + 1.0, current_region.y - 1.0)
            }
            Direction::Down => Point::new(
                current_region.x + 1.0,
                current_region.y + current_region.height + 1.0,
            ),
        };

        let mut colliding_regions =
            regions.iter().filter(|(_, region)| region.contains(target));

        let (pane, _) = colliding_regions.next()?;

        Some(*pane)
    }

    pub fn focus(&mut self, pane: &Pane) {
        self.internal.focus(pane);
    }

    pub fn split(&mut self, axis: Axis, pane: &Pane, state: T) -> Option<Pane> {
        let node = self.internal.layout.find(pane)?;

        let new_pane = {
            self.internal.last_id = self.internal.last_id.checked_add(1)?;

            Pane(self.internal.last_id)
        };

        let new_split = {
            self.internal.last_id = self.internal.last_id.checked_add(1)?;

            Split(self.internal.last_id)
        };

        node.split(new_split, axis, new_pane);

        let _ = self.panes.insert(new_pane, state);
        self.focus(&new_pane);

        Some(new_pane)
    }

    pub fn swap(&mut self, a: &Pane, b: &Pane) {
        self.internal.layout.update(&|node| match node {
            Node::Split { .. } => {}
            Node::Pane(pane) => {
                if pane == a {
                    *node = Node::Pane(*b);
                } else if pane == b {
                    *node = Node::Pane(*a);
                }
            }
        });
    }

    pub fn resize(&mut self, split: &Split, percentage: f32) {
        let _ = self.internal.layout.resize(split, percentage);
    }

    pub fn close(&mut self, pane: &Pane) -> Option<T> {
        if let Some(sibling) = self.internal.layout.remove(pane) {
            self.focus(&sibling);
            self.panes.remove(pane)
        } else {
            None
        }
    }
}

#[derive(Debug)]
pub struct Internal {
    layout: Node,
    last_id: usize,
    action: Action,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
    Idle {
        focus: Option<Pane>,
    },
    Dragging {
        pane: Pane,
    },
    Resizing {
        split: Split,
        axis: Axis,
        focus: Option<Pane>,
    },
}

impl Action {
    pub fn focus(&self) -> Option<(Pane, Focus)> {
        match self {
            Action::Idle { focus } | Action::Resizing { focus, .. } => {
                focus.map(|pane| (pane, Focus::Idle))
            }
            Action::Dragging { pane } => Some((*pane, Focus::Dragging)),
        }
    }
}

impl Internal {
    pub fn action(&self) -> Action {
        self.action
    }

    pub fn picked_pane(&self) -> Option<Pane> {
        match self.action {
            Action::Dragging { pane } => Some(pane),
            _ => None,
        }
    }

    pub fn picked_split(&self) -> Option<(Split, Axis)> {
        match self.action {
            Action::Resizing { split, axis, .. } => Some((split, axis)),
            _ => None,
        }
    }

    pub fn regions(
        &self,
        spacing: f32,
        size: Size,
    ) -> HashMap<Pane, Rectangle> {
        self.layout.regions(spacing, size)
    }

    pub fn splits(
        &self,
        spacing: f32,
        size: Size,
    ) -> HashMap<Split, (Axis, Rectangle, f32)> {
        self.layout.splits(spacing, size)
    }

    pub fn focus(&mut self, pane: &Pane) {
        self.action = Action::Idle { focus: Some(*pane) };
    }

    pub fn pick_pane(&mut self, pane: &Pane) {
        self.action = Action::Dragging { pane: *pane };
    }

    pub fn pick_split(&mut self, split: &Split, axis: Axis) {
        // TODO: Obtain `axis` from layout itself. Maybe we should implement
        // `Node::find_split`
        if self.picked_pane().is_some() {
            return;
        }

        let focus = self.action.focus().map(|(pane, _)| pane);

        self.action = Action::Resizing {
            split: *split,
            axis,
            focus,
        };
    }

    pub fn drop_split(&mut self) {
        match self.action {
            Action::Resizing { focus, .. } => {
                self.action = Action::Idle { focus };
            }
            _ => {}
        }
    }

    pub fn unfocus(&mut self) {
        self.action = Action::Idle { focus: None };
    }

    pub fn hash_layout(&self, hasher: &mut Hasher) {
        use std::hash::Hash;

        self.layout.hash(hasher);
    }
}