atagen

monolith

public
Star0
Watch0
bc51c38a6fd9fb1c6c73b495137fc844d250e22f
1
ssh://brewery@pls.lobotomise.me:33/atagen/monolith.git
crates/layout/src/lib.rsRust990 lines29.7 KB
1
//! Window organisation without smithay. Tags with per-output views, one
2
//! tiling tree per output filtered by those views, floating windows,
3
//! scratchpads, focus, and restoring windows when an output comes back.
4
5
mod tree;
6
7
#[cfg(test)]
8
mod tests;
9
10
pub use monolith_planner::Rect;
11
pub use tree::{Axis, Dir, Tree};
12
13
use slotmap::{SlotMap, new_key_type};
14
use std::collections::HashMap;
15
16
new_key_type! {
17
    pub struct WindowId;
18
    pub struct TagId;
19
    pub struct OutputId;
20
}
21
22
pub const HOMELESS: &str = "homeless";
23
24
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25
pub enum ExpandScope {
26
    Window,
27
    Container,
28
    All,
29
}
30
31
/// Space taken from an output's edges by bars and other panels.
32
#[derive(Clone, Copy, Debug, PartialEq, Default)]
33
pub struct Insets {
34
    pub top: f64,
35
    pub right: f64,
36
    pub bottom: f64,
37
    pub left: f64,
38
}
39
40
#[derive(Clone, Copy, Debug, PartialEq)]
41
pub struct Gaps {
42
    pub inner: f64,
43
    pub outer: f64,
44
}
45
46
impl Default for Gaps {
47
    fn default() -> Self {
48
        Self {
49
            inner: 8.0,
50
            outer: 8.0,
51
        }
52
    }
53
}
54
55
#[derive(Clone, Debug)]
56
pub struct Window {
57
    pub output: Option<OutputId>,
58
    pub tag: TagId,
59
    pub floating: bool,
60
    pub float_rect: Option<Rect>,
61
    pub fullscreen: bool,
62
    pub sticky: bool,
63
    pub scratchpad: Option<usize>,
64
    evicted_from: Option<String>,
65
}
66
67
#[derive(Clone, Debug)]
68
pub struct Tag {
69
    pub name: String,
70
    pub urgent: bool,
71
}
72
73
#[derive(Clone, Debug)]
74
pub struct Output {
75
    pub name: String,
76
    pub area: Rect,
77
    pub reserved: Insets,
78
    pub tree: Tree<WindowId>,
79
    /// Viewed tags besides the homeless tag, which is always viewed.
80
    pub viewed: Vec<TagId>,
81
    /// Floating windows, bottom to top.
82
    pub floating: Vec<WindowId>,
83
}
84
85
#[derive(Clone, Debug)]
86
pub struct Scratchpad {
87
    pub name: String,
88
    pub windows: Vec<WindowId>,
89
    pub shown: Option<usize>,
90
    pub output: Option<OutputId>,
91
}
92
93
#[derive(Clone, Debug)]
94
struct Parked {
95
    tree: Tree<WindowId>,
96
    viewed: Vec<TagId>,
97
    floating: Vec<WindowId>,
98
    area: Rect,
99
}
100
101
#[derive(Clone, Debug, Default)]
102
pub struct Arrangement {
103
    /// Every tiled window on the output. Hidden ones are zero-size seeds.
104
    pub tiled: Vec<Placed>,
105
    /// Visible floating windows and shown scratchpads, bottom to top.
106
    pub floating: Vec<(WindowId, Rect)>,
107
    pub fullscreen: Option<WindowId>,
108
}
109
110
#[derive(Clone, Copy, Debug)]
111
pub struct Placed {
112
    pub window: WindowId,
113
    pub rect: Rect,
114
    pub visible: bool,
115
}
116
117
#[derive(Clone, Debug)]
118
pub struct Layout {
119
    windows: SlotMap<WindowId, Window>,
120
    tags: SlotMap<TagId, Tag>,
121
    tag_order: Vec<TagId>,
122
    homeless: TagId,
123
    outputs: SlotMap<OutputId, Output>,
124
    active: Option<OutputId>,
125
    /// Focus history, most recent last.
126
    history: Vec<WindowId>,
127
    scratchpads: Vec<Scratchpad>,
128
    last_scratchpad: Option<usize>,
129
    parked: HashMap<String, Parked>,
130
    pub gaps: Gaps,
131
}
132
133
impl Default for Layout {
134
    fn default() -> Self {
135
        let mut tags = SlotMap::with_key();
136
        let homeless = tags.insert(Tag {
137
            name: HOMELESS.into(),
138
            urgent: false,
139
        });
140
        Self {
141
            windows: SlotMap::with_key(),
142
            tags,
143
            tag_order: vec![homeless],
144
            homeless,
145
            outputs: SlotMap::with_key(),
146
            active: None,
147
            history: Vec::new(),
148
            scratchpads: Vec::new(),
149
            last_scratchpad: None,
150
            parked: HashMap::new(),
151
            gaps: Gaps::default(),
152
        }
153
    }
154
}
155
156
impl Layout {
157
    pub fn homeless(&self) -> TagId {
158
        self.homeless
159
    }
160
161
    /// Looks a tag up by name, creating it at the end of the order.
162
    pub fn tag(&mut self, name: &str) -> TagId {
163
        if let Some(id) = self.tag_id(name) {
164
            return id;
165
        }
166
        let id = self.tags.insert(Tag {
167
            name: name.into(),
168
            urgent: false,
169
        });
170
        self.tag_order.push(id);
171
        id
172
    }
173
174
    pub fn tag_id(&self, name: &str) -> Option<TagId> {
175
        self.tag_order
176
            .iter()
177
            .copied()
178
            .find(|&t| self.tags[t].name == name)
179
    }
180
181
    pub fn tags(&self) -> impl Iterator<Item = (TagId, &Tag)> {
182
        self.tag_order.iter().map(|&t| (t, &self.tags[t]))
183
    }
184
185
    pub fn reorder_tag(&mut self, tag: TagId, index: usize) {
186
        if let Some(i) = self.tag_order.iter().position(|&t| t == tag) {
187
            self.tag_order.remove(i);
188
            let index = index.min(self.tag_order.len());
189
            self.tag_order.insert(index, tag);
190
        }
191
    }
192
193
    pub fn window(&self, w: WindowId) -> Option<&Window> {
194
        self.windows.get(w)
195
    }
196
197
    pub fn windows(&self) -> impl Iterator<Item = (WindowId, &Window)> {
198
        self.windows.iter()
199
    }
200
201
    pub fn output(&self, o: OutputId) -> Option<&Output> {
202
        self.outputs.get(o)
203
    }
204
205
    pub fn outputs(&self) -> impl Iterator<Item = (OutputId, &Output)> {
206
        self.outputs.iter()
207
    }
208
209
    pub fn scratchpads(&self) -> &[Scratchpad] {
210
        &self.scratchpads
211
    }
212
213
    pub fn active_output(&self) -> Option<OutputId> {
214
        self.active
215
    }
216
217
    pub fn set_active_output(&mut self, o: OutputId) {
218
        if self.outputs.contains_key(o) {
219
            self.active = Some(o);
220
        }
221
    }
222
223
    /// Adds an output. One that was unplugged before gets back its tree,
224
    /// views and the windows that had to leave it.
225
    pub fn add_output(&mut self, name: &str, area: Rect) -> OutputId {
226
        let id = self.outputs.insert(Output {
227
            name: name.into(),
228
            area,
229
            reserved: Insets::default(),
230
            tree: Tree::default(),
231
            viewed: Vec::new(),
232
            floating: Vec::new(),
233
        });
234
        if let Some(parked) = self.parked.remove(name) {
235
            self.restore(id, parked);
236
        }
237
        self.active.get_or_insert(id);
238
        let detached: Vec<WindowId> = self
239
            .windows
240
            .iter()
241
            .filter(|(_, w)| w.output.is_none())
242
            .map(|(id, _)| id)
243
            .collect();
244
        for w in detached {
245
            self.windows[w].output = Some(id);
246
            self.place(w);
247
        }
248
        id
249
    }
250
251
    fn restore(&mut self, id: OutputId, parked: Parked) {
252
        let name = self.outputs[id].name.clone();
253
        let ours = |l: &Self, w: WindowId| {
254
            l.windows
255
                .get(w)
256
                .is_some_and(|x| x.evicted_from.as_deref() == Some(&name) && x.scratchpad.is_none())
257
        };
258
        let mut tree = parked.tree;
259
        tree.retain(|w| ours(self, w) && !self.windows[w].floating);
260
        let floating: Vec<WindowId> = parked
261
            .floating
262
            .into_iter()
263
            .filter(|&w| ours(self, w) && self.windows[w].floating)
264
            .collect();
265
        for w in tree
266
            .windows()
267
            .chain(floating.iter().copied())
268
            .collect::<Vec<_>>()
269
        {
270
            self.unplace(w);
271
            let win = &mut self.windows[w];
272
            if let (Some(from), Some(rect)) = (win.output, win.float_rect) {
273
                win.float_rect = Some(translate(rect, self.outputs[from].area, parked.area));
274
            }
275
            win.output = Some(id);
276
            win.evicted_from = None;
277
        }
278
        let out = &mut self.outputs[id];
279
        out.tree = tree;
280
        out.floating = floating;
281
        out.viewed = parked
282
            .viewed
283
            .into_iter()
284
            .filter(|&t| self.tags.contains_key(t))
285
            .collect();
286
    }
287
288
    /// Removes an output. Its windows move to an output viewing their tag,
289
    /// or the active one, and remember where they came from.
290
    pub fn remove_output(&mut self, o: OutputId) {
291
        let Some(out) = self.outputs.remove(o) else {
292
            return;
293
        };
294
        self.parked.insert(
295
            out.name.clone(),
296
            Parked {
297
                tree: out.tree,
298
                viewed: out.viewed,
299
                floating: out.floating,
300
                area: out.area,
301
            },
302
        );
303
        if self.active == Some(o) {
304
            self.active = self.outputs.keys().next();
305
        }
306
        for sp in &mut self.scratchpads {
307
            if sp.output == Some(o) {
308
                sp.output = self.active;
309
            }
310
        }
311
        let leaving: Vec<WindowId> = self
312
            .windows
313
            .iter()
314
            .filter(|(_, w)| w.output == Some(o))
315
            .map(|(id, _)| id)
316
            .collect();
317
        for w in leaving {
318
            let tag = self.windows[w].tag;
319
            let target = self
320
                .outputs
321
                .keys()
322
                .find(|&t| self.is_viewed(t, tag))
323
                .or(self.active);
324
            let win = &mut self.windows[w];
325
            if let (Some(t), Some(rect)) = (target, win.float_rect) {
326
                win.float_rect = Some(translate(rect, out.area, self.outputs[t].area));
327
            }
328
            win.output = target;
329
            if win.scratchpad.is_some() {
330
                continue;
331
            }
332
            win.evicted_from = Some(out.name.clone());
333
            if target.is_some() {
334
                self.place(w);
335
            }
336
        }
337
    }
338
339
    pub fn set_output_area(&mut self, o: OutputId, area: Rect) {
340
        if let Some(out) = self.outputs.get_mut(o) {
341
            out.area = area;
342
        }
343
    }
344
345
    /// Returns whether anything changed.
346
    pub fn set_reserved(&mut self, o: OutputId, reserved: Insets) -> bool {
347
        match self.outputs.get_mut(o) {
348
            Some(out) if out.reserved != reserved => {
349
                out.reserved = reserved;
350
                true
351
            }
352
            _ => false,
353
        }
354
    }
355
356
    pub fn is_viewed(&self, o: OutputId, tag: TagId) -> bool {
357
        tag == self.homeless
358
            || self
359
                .outputs
360
                .get(o)
361
                .is_some_and(|out| out.viewed.contains(&tag))
362
    }
363
364
    /// Views `tag` alone, besides the homeless tag.
365
    pub fn view(&mut self, o: OutputId, tag: TagId) {
366
        let Some(out) = self.outputs.get_mut(o) else {
367
            return;
368
        };
369
        out.viewed.clear();
370
        if tag != self.homeless {
371
            out.viewed.push(tag);
372
        }
373
        self.tags[tag].urgent = false;
374
    }
375
376
    pub fn toggle_view(&mut self, o: OutputId, tag: TagId) {
377
        if tag == self.homeless {
378
            return;
379
        }
380
        let Some(out) = self.outputs.get_mut(o) else {
381
            return;
382
        };
383
        if let Some(i) = out.viewed.iter().position(|&t| t == tag) {
384
            out.viewed.remove(i);
385
        } else {
386
            out.viewed.push(tag);
387
            self.tags[tag].urgent = false;
388
        }
389
    }
390
391
    /// The tag a launch from `o` belongs to. The focused window's tag, else
392
    /// the first viewed tag, else homeless. Homeless is always on screen, so
393
    /// focusing one of its windows does not mean the user left their tag.
394
    pub fn launch_tag(&self, o: OutputId) -> TagId {
395
        self.focused_on(o)
396
            .map(|w| &self.windows[w])
397
            .filter(|w| w.scratchpad.is_none() && !w.sticky && w.tag != self.homeless)
398
            .map(|w| w.tag)
399
            .or_else(|| self.outputs.get(o)?.viewed.first().copied())
400
            .unwrap_or(self.homeless)
401
    }
402
403
    pub fn view_everywhere(&mut self, tag: TagId) {
404
        for o in self.outputs.keys().collect::<Vec<_>>() {
405
            self.view(o, tag);
406
        }
407
    }
408
409
    pub fn visible(&self, w: WindowId) -> bool {
410
        let Some(win) = self.windows.get(w) else {
411
            return false;
412
        };
413
        let Some(o) = win.output else {
414
            return false;
415
        };
416
        match win.scratchpad {
417
            Some(s) => {
418
                let sp = &self.scratchpads[s];
419
                sp.output == Some(o) && sp.shown.map(|i| sp.windows[i]) == Some(w)
420
            }
421
            None => win.sticky || self.is_viewed(o, win.tag),
422
        }
423
    }
424
425
    /// Adds a window on `output` (default the active one) and `tag` (default
426
    /// homeless). A window landing on a tag its output is not viewing marks
427
    /// the tag urgent.
428
    pub fn add_window(
429
        &mut self,
430
        output: Option<OutputId>,
431
        tag: Option<TagId>,
432
        floating: Option<Rect>,
433
    ) -> WindowId {
434
        let output = output
435
            .filter(|&o| self.outputs.contains_key(o))
436
            .or(self.active);
437
        let tag = tag
438
            .filter(|&t| self.tags.contains_key(t))
439
            .unwrap_or(self.homeless);
440
        let w = self.windows.insert(Window {
441
            output,
442
            tag,
443
            floating: floating.is_some(),
444
            float_rect: floating,
445
            fullscreen: false,
446
            sticky: false,
447
            scratchpad: None,
448
            evicted_from: None,
449
        });
450
        if let Some(o) = output {
451
            if !self.is_viewed(o, tag) {
452
                self.tags[tag].urgent = true;
453
            }
454
            self.place(w);
455
        }
456
        w
457
    }
458
459
    pub fn remove_window(&mut self, w: WindowId) {
460
        if !self.windows.contains_key(w) {
461
            return;
462
        }
463
        self.unplace(w);
464
        self.history.retain(|&h| h != w);
465
        if let Some(s) = self.windows[w].scratchpad {
466
            self.leave_scratchpad(s, w);
467
        }
468
        self.windows.remove(w);
469
    }
470
471
    /// Puts a window into its output's tree or floating stack. Tiled windows
472
    /// go next to the focused window if both are visible, else next to the
473
    /// latest window of their tag, else into a new row.
474
    fn place(&mut self, w: WindowId) {
475
        let win = &self.windows[w];
476
        let Some(o) = win.output else { return };
477
        if win.scratchpad.is_some() {
478
            return;
479
        }
480
        if win.floating {
481
            if win.float_rect.is_none() {
482
                let rect = centred(self.outputs[o].area, 0.6);
483
                self.windows[w].float_rect = Some(rect);
484
            }
485
            self.outputs[o].floating.push(w);
486
            return;
487
        }
488
        let tag = win.tag;
489
        let tree = &self.outputs[o].tree;
490
        let focused = self
491
            .focused()
492
            .filter(|&f| tree.contains(f) && self.is_viewed(o, tag));
493
        let anchor = focused.or_else(|| {
494
            self.history
495
                .iter()
496
                .rev()
497
                .copied()
498
                .find(|&h| tree.contains(h) && self.windows[h].tag == tag)
499
        });
500
        let tree = &mut self.outputs[o].tree;
501
        match anchor {
502
            Some(a) => tree.insert_after(w, a),
503
            None => tree.insert_row(w),
504
        }
505
    }
506
507
    fn unplace(&mut self, w: WindowId) {
508
        let Some(o) = self.windows[w].output else {
509
            return;
510
        };
511
        let out = &mut self.outputs[o];
512
        out.tree.remove(w);
513
        out.floating.retain(|&f| f != w);
514
    }
515
516
    pub fn focus(&mut self, w: WindowId) {
517
        let Some(win) = self.windows.get(w) else {
518
            return;
519
        };
520
        self.active = win.output.or(self.active);
521
        if let Some(o) = win.output {
522
            let stack = &mut self.outputs[o].floating;
523
            if let Some(i) = stack.iter().position(|&f| f == w) {
524
                stack.remove(i);
525
                stack.push(w);
526
            }
527
        }
528
        self.history.retain(|&h| h != w);
529
        self.history.push(w);
530
    }
531
532
    /// The most recently focused visible window.
533
    pub fn focused(&self) -> Option<WindowId> {
534
        self.history
535
            .iter()
536
            .rev()
537
            .copied()
538
            .find(|&w| self.visible(w))
539
    }
540
541
    /// The most recently focused visible window on `o`.
542
    pub fn focused_on(&self, o: OutputId) -> Option<WindowId> {
543
        self.history
544
            .iter()
545
            .rev()
546
            .copied()
547
            .find(|&w| self.windows[w].output == Some(o) && self.visible(w))
548
    }
549
550
    /// Focuses the nearest visible window in `dir`, crossing to the next
551
    /// output when there is none on this one.
552
    pub fn focus_dir(&mut self, dir: Dir) -> Option<WindowId> {
553
        let o = self.active?;
554
        let from = self.focused_on(o).and_then(|f| self.rect_of(f));
555
        let target = from.and_then(|from| {
556
            let arr = self.arrange(o);
557
            let candidates = arr
558
                .tiled
559
                .iter()
560
                .filter(|p| p.visible)
561
                .map(|p| (p.window, p.rect))
562
                .chain(arr.floating.iter().copied())
563
                .filter(|&(w, _)| Some(w) != self.focused_on(o));
564
            nearest(from, dir, candidates)
565
        });
566
        if let Some(w) = target {
567
            self.focus(w);
568
            return Some(w);
569
        }
570
        let next = self.output_towards(o, dir)?;
571
        self.active = Some(next);
572
        let w = self.focused_on(next);
573
        if let Some(w) = w {
574
            self.focus(w);
575
        }
576
        w
577
    }
578
579
    pub fn output_towards(&self, from: OutputId, dir: Dir) -> Option<OutputId> {
580
        let here = self.outputs.get(from)?.area;
581
        let others = self
582
            .outputs
583
            .iter()
584
            .filter(|&(id, _)| id != from)
585
            .map(|(id, out)| (id, out.area));
586
        nearest(here, dir, others)
587
    }
588
589
    fn rect_of(&self, w: WindowId) -> Option<Rect> {
590
        let o = self.windows.get(w)?.output?;
591
        let arr = self.arrange(o);
592
        arr.tiled
593
            .iter()
594
            .find(|p| p.window == w)
595
            .map(|p| p.rect)
596
            .or_else(|| arr.floating.iter().find(|f| f.0 == w).map(|f| f.1))
597
    }
598
599
    fn vis_on(&self, o: OutputId) -> impl Fn(WindowId) -> bool + '_ {
600
        move |w| self.windows[w].output == Some(o) && self.visible(w)
601
    }
602
603
    fn focused_tiled(&self) -> Option<(WindowId, OutputId)> {
604
        let w = self.focused()?;
605
        let o = self.windows[w].output?;
606
        self.outputs[o].tree.contains(w).then_some((w, o))
607
    }
608
609
    pub fn move_window(&mut self, dir: Dir) -> bool {
610
        let Some((w, o)) = self.focused_tiled() else {
611
            return false;
612
        };
613
        let mut tree = std::mem::take(&mut self.outputs[o].tree);
614
        let moved = tree.move_window(w, dir, self.vis_on(o));
615
        self.outputs[o].tree = tree;
616
        moved
617
    }
618
619
    pub fn move_container(&mut self, dir: Dir) -> bool {
620
        let Some((w, o)) = self.focused_tiled() else {
621
            return false;
622
        };
623
        let mut tree = std::mem::take(&mut self.outputs[o].tree);
624
        let moved = tree.move_container(w, dir, self.vis_on(o));
625
        self.outputs[o].tree = tree;
626
        moved
627
    }
628
629
    pub fn merge_window(&mut self, dir: Dir) -> bool {
630
        let Some((w, o)) = self.focused_tiled() else {
631
            return false;
632
        };
633
        let mut tree = std::mem::take(&mut self.outputs[o].tree);
634
        let merged = tree.merge_window(w, dir, self.vis_on(o));
635
        self.outputs[o].tree = tree;
636
        merged
637
    }
638
639
    /// Whether a tiled window fills its output through expansion.
640
    pub fn maximized(&self, w: WindowId) -> bool {
641
        let Some(o) = self.windows.get(w).and_then(|w| w.output) else {
642
            return false;
643
        };
644
        self.outputs[o].tree.maximized(w)
645
    }
646
647
    pub fn resize(&mut self, axis: Axis, delta: f64) -> bool {
648
        let Some((w, o)) = self.focused_tiled() else {
649
            return false;
650
        };
651
        let mut tree = std::mem::take(&mut self.outputs[o].tree);
652
        let done = tree.resize(w, axis, delta, self.vis_on(o));
653
        self.outputs[o].tree = tree;
654
        done
655
    }
656
657
    pub fn expand(&mut self, scope: ExpandScope) -> bool {
658
        let Some((w, o)) = self.focused_tiled() else {
659
            return false;
660
        };
661
        let (skip, count) = match scope {
662
            ExpandScope::Window => (0, 1),
663
            ExpandScope::Container => (1, 1),
664
            ExpandScope::All => (0, usize::MAX),
665
        };
666
        self.outputs[o].tree.toggle_expand(w, skip, count)
667
    }
668
669
    pub fn flip(&mut self, o: OutputId) {
670
        if let Some(out) = self.outputs.get_mut(o) {
671
            out.tree.flip();
672
        }
673
    }
674
675
    /// Retags a window. It keeps its slot and disappears if the new tag is
676
    /// not viewed.
677
    pub fn set_tag(&mut self, w: WindowId, tag: TagId) {
678
        if self.tags.contains_key(tag)
679
            && let Some(win) = self.windows.get_mut(w)
680
        {
681
            win.tag = tag;
682
        }
683
    }
684
685
    pub fn send_to_output(&mut self, w: WindowId, o: OutputId) {
686
        let Some(win) = self.windows.get(w) else {
687
            return;
688
        };
689
        if !self.outputs.contains_key(o) || win.output == Some(o) || win.scratchpad.is_some() {
690
            return;
691
        }
692
        let from = win.output.map(|f| self.outputs[f].area);
693
        self.unplace(w);
694
        let win = &mut self.windows[w];
695
        if let (Some(from), Some(rect)) = (from, win.float_rect) {
696
            win.float_rect = Some(translate(rect, from, self.outputs[o].area));
697
        }
698
        win.output = Some(o);
699
        win.evicted_from = None;
700
        self.place(w);
701
    }
702
703
    /// Switches a window between tiled and floating. A scratchpad window
704
    /// leaves its scratchpad and tiles on the tag being viewed, since its own
705
    /// tag may well be hidden.
706
    pub fn toggle_floating(&mut self, w: WindowId) {
707
        let Some(win) = self.windows.get(w) else {
708
            return;
709
        };
710
        if let Some(s) = win.scratchpad {
711
            let o = win.output.or(self.active);
712
            let tag = o.map_or(self.homeless, |o| self.launch_tag(o));
713
            self.leave_scratchpad(s, w);
714
            let win = &mut self.windows[w];
715
            win.output = o;
716
            win.tag = tag;
717
            win.floating = false;
718
            self.place(w);
719
            return;
720
        }
721
        self.unplace(w);
722
        let win = &mut self.windows[w];
723
        win.floating = !win.floating;
724
        self.place(w);
725
    }
726
727
    pub fn set_float_rect(&mut self, w: WindowId, rect: Rect) {
728
        if let Some(win) = self.windows.get_mut(w) {
729
            win.float_rect = Some(rect);
730
        }
731
    }
732
733
    pub fn toggle_fullscreen(&mut self, w: WindowId) {
734
        if let Some(win) = self.windows.get_mut(w) {
735
            win.fullscreen = !win.fullscreen;
736
        }
737
    }
738
739
    pub fn toggle_sticky(&mut self, w: WindowId) {
740
        if let Some(win) = self.windows.get_mut(w) {
741
            win.sticky = !win.sticky;
742
        }
743
    }
744
745
    /// Adds a window to a named scratchpad, creating it. Scratchpad windows
746
    /// float and are shown only by summoning.
747
    pub fn scratchpad_add(&mut self, w: WindowId, name: &str) {
748
        let Some(win) = self.windows.get(w) else {
749
            return;
750
        };
751
        if let Some(s) = win.scratchpad {
752
            self.leave_scratchpad(s, w);
753
        }
754
        self.unplace(w);
755
        let s = match self.scratchpads.iter().position(|sp| sp.name == name) {
756
            Some(s) => s,
757
            None => {
758
                self.scratchpads.push(Scratchpad {
759
                    name: name.into(),
760
                    windows: Vec::new(),
761
                    shown: None,
762
                    output: None,
763
                });
764
                self.scratchpads.len() - 1
765
            }
766
        };
767
        self.scratchpads[s].windows.push(w);
768
        let area = self.windows[w].output.map(|o| self.outputs[o].area);
769
        let win = &mut self.windows[w];
770
        win.scratchpad = Some(s);
771
        win.floating = true;
772
        if win.float_rect.is_none() {
773
            win.float_rect = area.map(|a| centred(a, 0.6));
774
        }
775
    }
776
777
    /// Cycles a scratchpad through off, each window, and off again, on the
778
    /// active output.
779
    pub fn summon(&mut self, name: &str) -> Option<WindowId> {
780
        let s = self.scratchpads.iter().position(|sp| sp.name == name)?;
781
        let o = self.active?;
782
        let sp = &mut self.scratchpads[s];
783
        let next = match sp.shown {
784
            None => 0,
785
            Some(i) => i + 1,
786
        };
787
        if next >= sp.windows.len() {
788
            sp.shown = None;
789
            return None;
790
        }
791
        let from = sp.output.map(|f| self.outputs[f].area);
792
        sp.shown = Some(next);
793
        sp.output = Some(o);
794
        let w = sp.windows[next];
795
        self.last_scratchpad = Some(s);
796
        let area = self.outputs[o].area;
797
        let win = &mut self.windows[w];
798
        if win.output != Some(o) {
799
            win.float_rect = match (from, win.float_rect) {
800
                (Some(from), Some(r)) => Some(translate(r, from, area)),
801
                _ => Some(centred(area, 0.6)),
802
            };
803
            win.output = Some(o);
804
        }
805
        self.focus(w);
806
        Some(w)
807
    }
808
809
    /// Hides a scratchpad, by default the last one summoned.
810
    pub fn dismiss(&mut self, name: Option<&str>) {
811
        let s = match name {
812
            Some(name) => self.scratchpads.iter().position(|sp| sp.name == name),
813
            None => self.last_scratchpad,
814
        };
815
        if let Some(sp) = s.and_then(|s| self.scratchpads.get_mut(s)) {
816
            sp.shown = None;
817
        }
818
    }
819
820
    fn leave_scratchpad(&mut self, s: usize, w: WindowId) {
821
        let sp = &mut self.scratchpads[s];
822
        let Some(i) = sp.windows.iter().position(|&x| x == w) else {
823
            return;
824
        };
825
        sp.windows.remove(i);
826
        sp.shown = match sp.shown {
827
            Some(j) if j == i => None,
828
            Some(j) if j > i => Some(j - 1),
829
            other => other,
830
        };
831
        self.windows[w].scratchpad = None;
832
    }
833
834
    /// Target geometry for everything on `o`, in output-local logical
835
    /// coordinates offset by the output's area.
836
    pub fn arrange(&self, o: OutputId) -> Arrangement {
837
        self.arrange_hiding(o, &[])
838
    }
839
840
    /// Like `arrange` with `hide` treated as hidden, which places them on the
841
    /// seeds they would grow from.
842
    pub fn arrange_hiding(&self, o: OutputId, hide: &[WindowId]) -> Arrangement {
843
        let Some(out) = self.outputs.get(o) else {
844
            return Arrangement::default();
845
        };
846
        let r = out.reserved;
847
        let usable = Rect::new(
848
            out.area.x + r.left,
849
            out.area.y + r.top,
850
            (out.area.w - r.left - r.right).max(0.0),
851
            (out.area.h - r.top - r.bottom).max(0.0),
852
        );
853
        let area = inset(usable, self.gaps.outer);
854
        let shown = self.vis_on(o);
855
        let vis = &|w: WindowId| shown(w) && !hide.contains(&w);
856
        let tiled = out
857
            .tree
858
            .arrange(area, self.gaps.inner, vis)
859
            .into_iter()
860
            .map(|(window, rect)| Placed {
861
                window,
862
                rect,
863
                visible: vis(window),
864
            })
865
            .collect();
866
        let mut floating: Vec<(WindowId, Rect)> = out
867
            .floating
868
            .iter()
869
            .copied()
870
            .filter(|&w| vis(w))
871
            .filter_map(|w| Some((w, self.windows[w].float_rect?)))
872
            .collect();
873
        for sp in &self.scratchpads {
874
            if let Some(i) = sp.shown
875
                && sp.output == Some(o)
876
                && let Some(r) = self.windows[sp.windows[i]].float_rect
877
            {
878
                floating.push((sp.windows[i], r));
879
            }
880
        }
881
        let fullscreen = self
882
            .history
883
            .iter()
884
            .rev()
885
            .copied()
886
            .find(|&w| self.windows[w].fullscreen && vis(w));
887
        Arrangement {
888
            tiled,
889
            floating,
890
            fullscreen,
891
        }
892
    }
893
894
    #[cfg(test)]
895
    pub(crate) fn check(&self) {
896
        for (o, out) in &self.outputs {
897
            out.tree.check();
898
            for w in out.tree.windows() {
899
                let win = &self.windows[w];
900
                assert_eq!(win.output, Some(o));
901
                assert!(!win.floating && win.scratchpad.is_none());
902
            }
903
            for &w in &out.floating {
904
                assert_eq!(self.windows[w].output, Some(o));
905
                assert!(self.windows[w].floating);
906
            }
907
        }
908
        for (w, win) in &self.windows {
909
            let homes = self
910
                .outputs
911
                .values()
912
                .filter(|out| out.tree.contains(w) || out.floating.contains(&w))
913
                .count();
914
            let expected = usize::from(win.output.is_some() && win.scratchpad.is_none());
915
            assert_eq!(homes, expected, "window placed {homes} times");
916
        }
917
    }
918
}
919
920
fn nearest<K: Copy>(
921
    from: Rect,
922
    dir: Dir,
923
    candidates: impl Iterator<Item = (K, Rect)>,
924
) -> Option<K> {
925
    const TOL: f64 = 0.5;
926
    let score = |r: Rect| -> Option<(bool, f64, f64)> {
927
        let (gap, overlap, offset) = match dir {
928
            Dir::Right => (
929
                r.x - from.right(),
930
                span_overlap(from.y, from.h, r.y, r.h),
931
                r.center_y() - from.center_y(),
932
            ),
933
            Dir::Left => (
934
                from.x - r.right(),
935
                span_overlap(from.y, from.h, r.y, r.h),
936
                r.center_y() - from.center_y(),
937
            ),
938
            Dir::Down => (
939
                r.y - from.bottom(),
940
                span_overlap(from.x, from.w, r.x, r.w),
941
                r.center_x() - from.center_x(),
942
            ),
943
            Dir::Up => (
944
                from.y - r.bottom(),
945
                span_overlap(from.x, from.w, r.x, r.w),
946
                r.center_x() - from.center_x(),
947
            ),
948
        };
949
        (gap > -TOL).then_some((overlap <= 0.0, gap.max(0.0), offset.abs()))
950
    };
951
    candidates
952
        .filter_map(|(k, r)| score(r).map(|s| (k, s)))
953
        .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
954
        .map(|(k, _)| k)
955
}
956
957
fn span_overlap(a: f64, al: f64, b: f64, bl: f64) -> f64 {
958
    (a + al).min(b + bl) - a.max(b)
959
}
960
961
fn inset(r: Rect, by: f64) -> Rect {
962
    Rect::new(
963
        r.x + by,
964
        r.y + by,
965
        (r.w - 2.0 * by).max(0.0),
966
        (r.h - 2.0 * by).max(0.0),
967
    )
968
}
969
970
fn centred(area: Rect, frac: f64) -> Rect {
971
    let (w, h) = (area.w * frac, area.h * frac);
972
    Rect::new(
973
        area.x + (area.w - w) / 2.0,
974
        area.y + (area.h - h) / 2.0,
975
        w,
976
        h,
977
    )
978
}
979
980
/// Keeps a rect at the same relative spot when it changes output.
981
fn translate(r: Rect, from: Rect, to: Rect) -> Rect {
982
    let sx = if from.w > 0.0 { to.w / from.w } else { 1.0 };
983
    let sy = if from.h > 0.0 { to.h / from.h } else { 1.0 };
984
    Rect::new(
985
        to.x + (r.x - from.x) * sx,
986
        to.y + (r.y - from.y) * sy,
987
        r.w.min(to.w),
988
        r.h.min(to.h),
989
    )
990
}