flow-wm 0.2.0

A scrolling, infinite-horizontal-canvas tiling window manager for Windows
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! Floating window space — ordered list of windows in literal pixel coordinates.
//!
//! [`FloatingSpace`] holds non-tiled windows as an ordered list of on-screen
//! rectangles ([`ActualEntry`](crate::layout::types::ActualEntry)). It lives
//! directly in pixel/actual space — it does NOT run windows through the
//! virtual/actual projection pipeline.
//!
//! # Coordinate model
//!
//! Each floating window keeps the on-screen rectangle the user dragged it to (or
//! the centered default when first floated). The space just remembers those
//! rectangles so it can hide/show them on workspace switch and merge them into
//! an [`ActualLayout`](crate::layout::types::ActualLayout) for the animation
//! batch.
//!
//! See (`docs/src/dev-guide/workspace.md`) for how `FloatingSpace` fits within
//! the two-coordinate-space model alongside
//! [`ScrollingSpace`](super::ScrollingSpace).

use crate::common::{Rect, Size, WindowId};
use crate::layout::types::{ActualEntry, ActualLayout};

/// Space for floating (non-tiled) windows within a [`Workspace`](super::Workspace).
///
/// An ordered list of windows with their on-screen pixel rectangles. Later
/// entries render on top (z-order). Pure data + math — no Win32, no side
/// effects.
///
/// See (`docs/src/dev-guide/floating-space.md`) for the architecture overview.
pub struct FloatingSpace {
    /// Ordered list of floating windows; later entries render on top.
    windows: Vec<ActualEntry>,
}

impl FloatingSpace {
    /// Create a new empty floating space.
    #[must_use]
    pub fn new() -> Self {
        Self {
            windows: Vec::new(),
        }
    }

    /// Add or update a floating window.
    ///
    /// Appends to the end (new window on top of z-order). If `window_id`
    /// already exists, updates its rect in place without changing z-order.
    pub fn add(&mut self, window_id: WindowId, rect: Rect) {
        if let Some(entry) = self.windows.iter_mut().find(|e| e.window_id == window_id) {
            entry.rect = rect;
        } else {
            self.windows.push(ActualEntry { window_id, rect });
        }
    }

    /// Remove a floating window, returning its old rect if it existed.
    pub fn remove(&mut self, window_id: WindowId) -> Option<Rect> {
        let idx = self.windows.iter().position(|e| e.window_id == window_id)?;
        let entry = self.windows.remove(idx);
        Some(entry.rect)
    }

    /// Returns `true` if the given window is in this floating space.
    #[must_use]
    pub fn contains(&self, window_id: WindowId) -> bool {
        self.windows.iter().any(|e| e.window_id == window_id)
    }

    /// Returns `true` if no floating windows are present.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.windows.is_empty()
    }

    /// Returns the number of floating windows.
    #[must_use]
    pub fn len(&self) -> usize {
        self.windows.len()
    }

    /// Read-only view of all floating window entries.
    #[must_use]
    pub fn windows(&self) -> &[ActualEntry] {
        &self.windows
    }

    /// Replace the entire floating window set with `entries`, preserving input
    /// order as z-order (earlier = deeper, later = on top — same convention as
    /// [`add`](Self::add)).
    ///
    /// Used by the loadout load path to restore saved floating window rects in
    /// one operation. Any windows currently in the space are discarded.
    pub fn replace_all(&mut self, entries: Vec<(WindowId, Rect)>) {
        self.windows = entries
            .into_iter()
            .map(|(window_id, rect)| ActualEntry { window_id, rect })
            .collect();
    }

    /// Build an [`ActualLayout`] from this space's entries.
    ///
    /// The daemon merges this into the animation batch alongside the scrolling
    /// space's `ActualLayout`.
    #[must_use]
    pub fn to_actual_layout(&self) -> ActualLayout {
        ActualLayout {
            entries: self.windows.clone(),
        }
    }

    /// Center a rect of `preferred` size within `work_area`.
    ///
    /// Clamps `preferred` to `work_area` dimensions if larger in either axis.
    /// Non-positive dimensions are clamped to zero.
    #[must_use]
    pub fn centered_rect(preferred: Size, work_area: Rect) -> Rect {
        let w = preferred.w.clamp(0, work_area.width);
        let h = preferred.h.clamp(0, work_area.height);
        let x = work_area.x + (work_area.width - w) / 2;
        let y = work_area.y + (work_area.height - h) / 2;
        Rect {
            x,
            y,
            width: w,
            height: h,
        }
    }
}

impl Default for FloatingSpace {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // --- centered_rect tests ---

    #[test]
    fn centered_rect_exact_center() {
        let preferred = Size { w: 400, h: 300 };
        let work_area = Rect {
            x: 100,
            y: 200,
            width: 1000,
            height: 800,
        };
        let result = FloatingSpace::centered_rect(preferred, work_area);
        assert_eq!(
            result,
            Rect {
                x: 400,
                y: 450,
                width: 400,
                height: 300
            }
        );
    }

    #[test]
    fn centered_rect_clamp_too_large() {
        let preferred = Size { w: 2000, h: 1500 };
        let work_area = Rect {
            x: 0,
            y: 0,
            width: 1000,
            height: 800,
        };
        let result = FloatingSpace::centered_rect(preferred, work_area);
        assert_eq!(
            result,
            Rect {
                x: 0,
                y: 0,
                width: 1000,
                height: 800
            }
        );
    }

    #[test]
    fn centered_rect_one_axis_oversized() {
        let preferred = Size { w: 1500, h: 300 };
        let work_area = Rect {
            x: 0,
            y: 0,
            width: 1000,
            height: 800,
        };
        let result = FloatingSpace::centered_rect(preferred, work_area);
        // Width clamped to 1000 (x=0), height centered: y = (800-300)/2 = 250
        assert_eq!(
            result,
            Rect {
                x: 0,
                y: 250,
                width: 1000,
                height: 300
            }
        );
    }

    #[test]
    fn centered_rect_zero_preferred() {
        let preferred = Size { w: 0, h: 0 };
        let work_area = Rect {
            x: 50,
            y: 50,
            width: 1000,
            height: 800,
        };
        let result = FloatingSpace::centered_rect(preferred, work_area);
        // x = 50 + (1000 - 0) / 2 = 550; y = 50 + (800 - 0) / 2 = 450
        assert_eq!(
            result,
            Rect {
                x: 550,
                y: 450,
                width: 0,
                height: 0
            }
        );
    }

    #[test]
    fn centered_rect_negative_preferred() {
        let preferred = Size { w: -100, h: -200 };
        let work_area = Rect {
            x: 0,
            y: 0,
            width: 1920,
            height: 1080,
        };
        let result = FloatingSpace::centered_rect(preferred, work_area);
        // Negative → clamped to 0, positioned at center
        assert_eq!(
            result,
            Rect {
                x: 960,
                y: 540,
                width: 0,
                height: 0
            }
        );
    }

    // --- add / contains / len round-trip ---

    #[test]
    fn add_then_contains_and_len() {
        let mut fs = FloatingSpace::new();
        assert!(fs.is_empty());
        assert_eq!(fs.len(), 0);

        let id = WindowId(1);
        let rect = Rect {
            x: 100,
            y: 100,
            width: 400,
            height: 300,
        };
        fs.add(id, rect);
        assert!(!fs.is_empty());
        assert_eq!(fs.len(), 1);
        assert!(fs.contains(id));
        assert!(!fs.contains(WindowId(99)));
    }

    #[test]
    fn add_duplicate_updates_rect_no_duplicate() {
        let mut fs = FloatingSpace::new();
        let id = WindowId(1);
        let rect1 = Rect {
            x: 100,
            y: 100,
            width: 400,
            height: 300,
        };
        let rect2 = Rect {
            x: 500,
            y: 500,
            width: 200,
            height: 200,
        };
        fs.add(id, rect1);
        assert_eq!(fs.len(), 1);
        fs.add(id, rect2);
        assert_eq!(fs.len(), 1);
        assert_eq!(fs.windows()[0].rect, rect2);
    }

    // --- remove tests ---

    #[test]
    fn remove_existing_returns_old_rect() {
        let mut fs = FloatingSpace::new();
        let id = WindowId(1);
        let rect = Rect {
            x: 100,
            y: 100,
            width: 400,
            height: 300,
        };
        fs.add(id, rect);
        let old = fs.remove(id);
        assert_eq!(old, Some(rect));
        assert!(!fs.contains(id));
        assert!(fs.is_empty());
    }

    #[test]
    fn remove_absent_returns_none() {
        let mut fs = FloatingSpace::new();
        let result = fs.remove(WindowId(99));
        assert_eq!(result, None);
        assert!(fs.is_empty());
    }

    // --- to_actual_layout round-trip ---

    #[test]
    fn to_actual_layout_preserves_entries() {
        let mut fs = FloatingSpace::new();
        let id1 = WindowId(1);
        let id2 = WindowId(2);
        let r1 = Rect {
            x: 100,
            y: 100,
            width: 400,
            height: 300,
        };
        let r2 = Rect {
            x: 200,
            y: 200,
            width: 500,
            height: 400,
        };
        fs.add(id1, r1);
        fs.add(id2, r2);
        let layout = fs.to_actual_layout();
        assert_eq!(layout.entries.len(), 2);
        assert_eq!(
            layout.entries[0],
            ActualEntry {
                window_id: id1,
                rect: r1
            }
        );
        assert_eq!(
            layout.entries[1],
            ActualEntry {
                window_id: id2,
                rect: r2
            }
        );
    }

    // --- z-order ---

    #[test]
    fn z_order_newest_on_top() {
        let mut fs = FloatingSpace::new();
        fs.add(
            WindowId(1),
            Rect {
                x: 0,
                y: 0,
                width: 100,
                height: 100,
            },
        );
        fs.add(
            WindowId(2),
            Rect {
                x: 0,
                y: 0,
                width: 200,
                height: 200,
            },
        );
        fs.add(
            WindowId(3),
            Rect {
                x: 0,
                y: 0,
                width: 300,
                height: 300,
            },
        );
        let entries = fs.windows();
        assert_eq!(entries[0].window_id, WindowId(1));
        assert_eq!(entries[1].window_id, WindowId(2));
        assert_eq!(entries[2].window_id, WindowId(3));
    }

    // --- replace_all tests ---

    #[test]
    fn replace_all_from_empty() {
        // Positive: empty → replace_all with 2 entries → len 2, order preserved, contains both.
        let mut fs = FloatingSpace::new();
        let r1 = Rect {
            x: 10,
            y: 10,
            width: 100,
            height: 100,
        };
        let r2 = Rect {
            x: 20,
            y: 20,
            width: 200,
            height: 200,
        };
        fs.replace_all(vec![(WindowId(1), r1), (WindowId(2), r2)]);

        assert_eq!(fs.len(), 2);
        assert!(fs.contains(WindowId(1)));
        assert!(fs.contains(WindowId(2)));
        // Order preserved (first = deepest in z-order).
        assert_eq!(fs.windows()[0].window_id, WindowId(1));
        assert_eq!(fs.windows()[1].window_id, WindowId(2));
        assert_eq!(fs.windows()[0].rect, r1);
        assert_eq!(fs.windows()[1].rect, r2);
    }

    #[test]
    fn replace_all_discards_existing() {
        // Positive: existing windows discarded — add some, replace_all with
        // different set, assert only the new set remains.
        let mut fs = FloatingSpace::new();
        fs.add(
            WindowId(1),
            Rect {
                x: 0,
                y: 0,
                width: 100,
                height: 100,
            },
        );
        fs.add(
            WindowId(2),
            Rect {
                x: 0,
                y: 0,
                width: 200,
                height: 200,
            },
        );
        assert_eq!(fs.len(), 2);

        let r_new = Rect {
            x: 50,
            y: 50,
            width: 300,
            height: 300,
        };
        fs.replace_all(vec![(WindowId(3), r_new)]);

        assert_eq!(fs.len(), 1);
        assert!(!fs.contains(WindowId(1)));
        assert!(!fs.contains(WindowId(2)));
        assert!(fs.contains(WindowId(3)));
        assert_eq!(fs.windows()[0].rect, r_new);
    }

    #[test]
    fn replace_all_preserves_z_order_from_input() {
        // Positive: z-order preserved from input order (first entry deepest).
        let mut fs = FloatingSpace::new();
        fs.replace_all(vec![
            (
                WindowId(10),
                Rect {
                    x: 0,
                    y: 0,
                    width: 100,
                    height: 100,
                },
            ),
            (
                WindowId(20),
                Rect {
                    x: 0,
                    y: 0,
                    width: 200,
                    height: 200,
                },
            ),
            (
                WindowId(30),
                Rect {
                    x: 0,
                    y: 0,
                    width: 300,
                    height: 300,
                },
            ),
        ]);
        let entries = fs.windows();
        assert_eq!(entries[0].window_id, WindowId(10));
        assert_eq!(entries[1].window_id, WindowId(20));
        assert_eq!(entries[2].window_id, WindowId(30));
    }

    #[test]
    fn replace_all_empty_clears_everything() {
        // Negative: empty input → clears everything (len 0, is_empty true).
        let mut fs = FloatingSpace::new();
        fs.add(
            WindowId(1),
            Rect {
                x: 0,
                y: 0,
                width: 100,
                height: 100,
            },
        );
        fs.add(
            WindowId(2),
            Rect {
                x: 0,
                y: 0,
                width: 200,
                height: 200,
            },
        );
        assert_eq!(fs.len(), 2);

        fs.replace_all(vec![]);

        assert!(fs.is_empty());
        assert_eq!(fs.len(), 0);
    }
}