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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/
//! This module contains extra settling functionality for `frames::Frame`.
// -------------------------------------------------------------------------------------------------
use qualia::{Area, Direction, Position, Vector, Size, SurfaceAccess, SurfaceId};
use frame::{Frame, Geometry, Mobility, Mode, Side};
use searching::Searching;
use packing::Packing;
// -------------------------------------------------------------------------------------------------
/// Extension trait for `Frame` adding more settling functionality.
pub trait Settling {
/// Settle self in buildable of target and relax it.
///
/// If `area` is provided settle the surface as floating with given position and size.
fn settle(&mut self, target: &mut Frame, area: Option<Area>, sa: &mut SurfaceAccess);
/// Remove given frame, relax old parent and settle the frame on given target.
///
/// If frame is floating and optional position is given it will be used to place the frame.
fn resettle(&mut self, target: &mut Frame, position: Option<Position>, sa: &mut SurfaceAccess);
/// Pop the surface `pop` and its parents inside surface `self`.
///
/// After calling this function `pop` will be most recently used frame inside `self`.
fn pop_recursively(&mut self, pop: &mut Frame);
/// Changes frames geometry and resizes all subframe accordingly.
fn change_geometry(&mut self, geometry: Geometry, sa: &mut SurfaceAccess);
/// Adds another container into given place in frame layout if needed.
///
/// This method is used when jumping into leaf frame to create container to handle the leaf
/// and jumped frame.
///
/// Returns
/// - `self` if it is container with one child,
/// - parent if parent has one child
/// - newly created container frame otherwise
///
/// Returned frame is guarantied to have exactly one child.
fn ramify(&mut self, geometry: Geometry) -> Frame;
/// Removes unnecessary layers of container frames containing only one container or leaf frame.
fn deramify(&mut self);
/// Places frame `self` on given `side` of `target` frame.
fn jumpin(&mut self, side: Side, target: &mut Frame, sa: &mut SurfaceAccess);
/// Removes frame `self` from frame layout and then places it using `jumpin` method.
fn jump(&mut self, side: Side, target: &mut Frame, sa: &mut SurfaceAccess);
/// Places frame `self` in `target` frame as dock.
fn dock(&mut self, target: &mut Frame, size: Size, sa: &mut SurfaceAccess);
/// Anchorizes floating frame.
fn anchorize(&mut self, sa: &mut SurfaceAccess);
/// Deanchorizes frame. Floating frame must be attached to workspace so it will be resettled if
/// necessary.
fn deanchorize(&mut self, area: Area, sa: &mut SurfaceAccess);
/// Resize the frame. `direction` indicates the border which will be moved and `magnitude` is
/// move distance in pixels.
fn resize(&mut self, direction: Direction, magnitude: isize, sa: &mut SurfaceAccess);
/// Move the frame and all subframes by given vector.
fn move_with_contents(&mut self, vector: Vector);
/// Removes frame `self`, relaxes old parent and destroys the frame.
fn destroy_self(&mut self, sa: &mut SurfaceAccess);
}
// -------------------------------------------------------------------------------------------------
impl Settling for Frame {
fn settle(&mut self, target: &mut Frame, area: Option<Area>, sa: &mut SurfaceAccess) {
if let Some(ref mut buildable) = target.find_buildable() {
if buildable.get_geometry() == Geometry::Stacked {
buildable.prepend(self);
if let Some(area) = area {
self.set_plumbing_mobility(Mobility::Floating);
self.set_size(area.size, sa);
self.set_plumbing_position(area.pos);
} else {
self.set_plumbing_mobility(Mobility::Anchored);
}
} else {
buildable.append(self);
self.set_plumbing_mobility(Mobility::Anchored);
}
buildable.relax(sa);
}
}
fn resettle(&mut self, target: &mut Frame, position: Option<Position>, sa: &mut SurfaceAccess) {
let area = {
// Preserve area if resettling to another workspace
if self.get_mobility().is_floating() && target.get_mode().is_workspace() {
let mut area = self.get_area();
if let Some(position) = position {
area.pos = position;
}
Some(area)
} else {
None
}
};
self.remove_self(sa);
self.settle(target, area, sa);
}
fn pop_recursively(&mut self, pop: &mut Frame) {
// If we reached `self` we can finish
if self.equals_exact(pop) {
return;
}
// If there's nothing above we can finish
if let Some(ref mut parent) = pop.get_parent() {
// If it is `stacked` frame we have to pop it also spatially
if parent.get_geometry() == Geometry::Stacked {
pop.remove();
parent.prepend(pop);
}
// Pop in temporal order
pop.pop();
// Do the same recursively on trunk
self.pop_recursively(parent);
}
}
fn change_geometry(&mut self, geometry: Geometry, sa: &mut SurfaceAccess) {
self.set_plumbing_geometry(geometry);
self.homogenize(sa);
}
fn ramify(&mut self, geometry: Geometry) -> Frame {
if !self.is_top() {
let parent = self.get_parent().expect("should have parent");
if self.count_children() == 1 {
return self.clone();
}
if parent.count_children() == 1 {
return parent;
}
}
let (distancer_mobility, distancer_mode) = if self.is_top() {
(self.get_mobility(), self.get_mode())
} else {
(Mobility::Anchored, Mode::Container)
};
let frame_mode = if self.get_mode().is_leaf() {
self.get_mode()
} else {
Mode::Container
};
let mut distancer = Frame::new(SurfaceId::invalid(),
geometry,
distancer_mobility,
distancer_mode,
self.get_position(),
self.get_size(),
self.get_title());
self.prejoin(&mut distancer);
self.remove();
self.set_plumbing_mobility(Mobility::Anchored);
self.set_plumbing_mode(frame_mode);
self.set_plumbing_position(Position::default());
distancer.prepend(self);
distancer
}
fn deramify(&mut self) {
let len = self.count_children();
if len == 1 {
let mut first = self.get_first_time().expect("should have exactly one child");
let len = first.count_children();
if len == 1 {
let mut second = first.get_first_time().expect("should have exactly one child");
first.remove();
second.remove();
self.prepend(&mut second);
first.destroy();
} else if len == 0 {
self.set_plumbing_mode(first.get_mode());
self.set_plumbing_sid(first.get_sid());
first.remove();
first.destroy();
}
}
}
fn jumpin(&mut self, side: Side, target: &mut Frame, sa: &mut SurfaceAccess) {
if let Some(mut target_parent) = target.get_parent() {
match side {
Side::Before => {
target.prejoin(self);
target_parent.relax(sa);
}
Side::After => {
target.adjoin(self);
target_parent.relax(sa);
}
Side::On => {
let mut new_target = {
if !target_parent.is_top() &&
target_parent.count_children() == 1 {
target_parent.clone()
} else if target.get_mode().is_leaf() {
target.ramify(Geometry::Stacked)
} else {
target.clone()
}
};
self.settle(&mut new_target, None, sa);
}
}
}
}
fn jump(&mut self, side: Side, target: &mut Frame, sa: &mut SurfaceAccess) {
self.remove_self(sa);
self.jumpin(side, target, sa);
}
fn dock(&mut self, target: &mut Frame, size: Size, sa: &mut SurfaceAccess) {
target.set_plumbing_geometry(Geometry::Vertical);
self.set_plumbing_mobility(Mobility::Docked);
self.set_plumbing_size(size);
self.set_plumbing_position(Position::default());
target.prepend(self);
target.relax(sa);
}
fn anchorize(&mut self, sa: &mut SurfaceAccess) {
if self.is_reanchorizable() && self.get_mobility().is_floating() {
// NOTE: Floating surface must be direct child of workspace.
let parent = self.get_parent().expect("should have parent");
self.set_size(parent.get_size(), sa);
self.set_plumbing_position(Position::default());
self.set_plumbing_mobility(Mobility::Anchored);
}
}
fn deanchorize(&mut self, area: Area, sa: &mut SurfaceAccess) {
if self.is_reanchorizable() && self.get_mobility().is_anchored() {
let mut workspace = self.find_top().expect("should have toplevel");
let parent = self.get_parent().expect("should have parent");
if !parent.equals_exact(&workspace) {
self.remove_self(sa);
workspace.prepend(self);
}
self.set_size(area.size, sa);
self.set_plumbing_position(area.pos);
self.set_plumbing_mobility(Mobility::Floating);
}
}
fn resize(&mut self, direction: Direction, magnitude: isize, sa: &mut SurfaceAccess) {
if direction.is_planar() && !self.is_top() {
match self.get_mobility() {
Mobility::Floating => {
let move_vector = {
if direction == Direction::North {
Vector::new(0, -magnitude)
} else if direction == Direction::West {
Vector::new(-magnitude, 0)
} else {
Vector::new(0, 0)
}
};
let resize_vector = {
if direction == Direction::North || direction == Direction::South {
Vector::new(0, magnitude)
} else if direction == Direction::West || direction == Direction::East {
Vector::new(magnitude, 0)
} else {
Vector::new(0, 0)
}
};
let new_size = self.get_size().sized(resize_vector);
self.set_size(new_size, sa);
self.move_with_contents(move_vector);
}
Mobility::Anchored => {
if let Some(neighbour) = self.find_neighbouring(direction) {
if neighbour.get_mobility().is_anchored() {
let (mut first, mut second) = {
if direction == Direction::North || direction == Direction::West {
(neighbour.clone(), self.clone())
} else {
(self.clone(), neighbour.clone())
}
};
let (move_vector, resize_vector) = {
if direction == Direction::North {
(Vector::new(0, -magnitude), Vector::new(0, magnitude))
} else if direction == Direction::East {
(Vector::new(magnitude, 0), Vector::new(-magnitude, 0))
} else if direction == Direction::South {
(Vector::new(0, magnitude), Vector::new(0, -magnitude))
} else if direction == Direction::West {
(Vector::new(-magnitude, 0), Vector::new(magnitude, 0))
} else {
(Vector::new(0, 0), Vector::new(0, 0))
}
};
first.change_size(resize_vector.opposite(), sa);
second.change_size(resize_vector, sa);
second.move_with_contents(move_vector);
}
} else if let Some(mut parent) = self.get_parent() {
parent.resize(direction, magnitude, sa);
}
}
Mobility::Docked => {
// Nothing to do
}
}
}
}
fn move_with_contents(&mut self, vector: Vector) {
// Update frames position
let new_position = self.get_position() + vector.clone();
self.set_plumbing_position(new_position);
}
fn destroy_self(&mut self, sa: &mut SurfaceAccess) {
self.remove_self(sa);
self.destroy();
}
}
// -------------------------------------------------------------------------------------------------