flow-wm 0.1.1

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
538
539
540
541
542
543
544
545
//! Physical monitor owning a vertical stack of workspaces.
//!
//! A [`Monitor`] is the top of the workspace hierarchy under
//! [`FlowWM`](crate::daemon::FlowWM). It binds two
//! pieces of screen geometry to the [`Workspace`]s available on that display:
//! the full physical [`Rect`](crate::common::Rect) (for parking workspaces
//! off-screen) and the taskbar-excluded work area (for in-workspace tiling).
//! Only one workspace per monitor is on screen at a time — the
//! `active_workspace` — while the rest sit parked above and below, ready to
//! be scrolled into view.
//!
//! See the [module-level docs](super) for the full hierarchy diagram.

use super::{FloatingSpace, ScrollingSpace, Workspace, WorkspaceId};
use crate::common::{Rect, WindowId};

/// A physical monitor and the workspaces it can show.
///
/// The monitor remembers **two** screen rectangles for this display:
///
/// - **`screen_rect`** — the full physical bounds (taskbar included). Used
///   for *inter-workspace* geometry: parking a non-active workspace far
///   enough off-screen that none of it leaks past the taskbar strip (see
///   `workspace::workspace_y_offset`).
/// - **`work_area`** — the taskbar-excluded bounds. Used for *intra-workspace*
///   geometry: sizing each workspace so tiled windows never underlap the
///   taskbar or any shell appbar (e.g. `yasb`).
///
/// Each [`Workspace`]'s [`ScrollingSpace`] *also* carries a copy of the work
/// area (inside its `MonitorInfo`) for projection — the two are kept in sync
/// by the daemon at construction time. The daemon today creates exactly one
/// monitor, so the duplication is benign; multi-monitor support is future
/// work.
pub struct Monitor {
    /// Full physical screen geometry for this display, in screen
    /// coordinates. Taskbar and appbars are NOT excluded. Source of truth
    /// for parking workspaces off-screen.
    screen_rect: Rect,
    /// Work-area geometry (taskbar excluded) for this display, in screen
    /// coordinates. Source of truth for sizing workspaces.
    work_area: Rect,
    /// The vertical stack of workspaces on this monitor. Index `0` is the
    /// topmost (first created).
    workspaces: Vec<Workspace>,
    /// Index into [`workspaces`](Self::workspaces) of the currently visible
    /// workspace. Always a valid index while `workspaces` is non-empty.
    active_workspace: usize,
}

impl Monitor {
    /// Create a new monitor with the given geometry and workspace stack.
    ///
    /// `screen_rect` is the full physical monitor rect (taskbar included);
    /// `work_area` is the taskbar-excluded rect used for in-workspace
    /// tiling. The daemon populates both from a single
    /// `GetMonitorInfoW` query at construction time.
    ///
    /// `active_workspace` is clamped into range so a stale index can never
    /// panic a later accessor. If `workspaces` is empty the active index is
    /// forced to `0`; callers should push a workspace before relying on
    /// [`active_workspace`](Self::active_workspace).
    #[must_use]
    pub fn new(
        screen_rect: Rect,
        work_area: Rect,
        workspaces: Vec<Workspace>,
        active_workspace: usize,
    ) -> Self {
        let active_workspace = if workspaces.is_empty() {
            0
        } else {
            active_workspace.min(workspaces.len() - 1)
        };
        Self {
            screen_rect,
            work_area,
            workspaces,
            active_workspace,
        }
    }

    /// The full physical [`Rect`] for this monitor (taskbar included).
    ///
    /// Use this for inter-workspace math (parking workspaces off-screen).
    /// For in-workspace window placement use [`work_area`](Self::work_area).
    #[must_use]
    pub fn screen_rect(&self) -> Rect {
        self.screen_rect
    }

    /// The work-area [`Rect`] for this monitor (taskbar excluded).
    #[must_use]
    pub fn work_area(&self) -> Rect {
        self.work_area
    }

    /// Read access to every workspace on this monitor.
    #[must_use]
    pub fn workspaces(&self) -> &[Workspace] {
        &self.workspaces
    }

    /// Mutable access to every workspace on this monitor.
    ///
    /// Used by config hot-reload to reconfigure each workspace's layout in
    /// place without rebuilding the monitor; see
    /// (`docs/src/dev-guide/config-and-persistence.md`).
    pub fn workspaces_mut(&mut self) -> &mut [Workspace] {
        &mut self.workspaces
    }

    /// The index of the currently visible workspace.
    #[must_use]
    pub fn active_workspace_index(&self) -> usize {
        self.active_workspace
    }

    /// Borrow the active workspace (the one currently on screen).
    ///
    /// # Panics
    ///
    /// Panics if the monitor has no workspaces. The daemon always keeps at
    /// least one workspace per monitor, so this never fires in practice.
    #[must_use]
    pub fn active_workspace(&self) -> &Workspace {
        &self.workspaces[self.active_workspace]
    }

    /// Mutably borrow the active workspace.
    ///
    /// # Panics
    ///
    /// Panics if the monitor has no workspaces — see
    /// [`active_workspace`](Self::active_workspace).
    #[must_use]
    pub fn active_workspace_mut(&mut self) -> &mut Workspace {
        &mut self.workspaces[self.active_workspace]
    }

    /// Borrow the active workspace's scrolling space.
    ///
    /// Convenience for `self.active_workspace().scrolling` — the daemon routes
    /// every tiling mutation through this accessor.
    ///
    /// # Panics
    ///
    /// Panics if the monitor has no workspaces.
    #[must_use]
    pub fn active_scrolling(&self) -> &ScrollingSpace {
        &self.active_workspace().scrolling
    }

    /// Mutably borrow the active workspace's scrolling space.
    ///
    /// # Panics
    ///
    /// Panics if the monitor has no workspaces.
    #[must_use]
    pub fn active_scrolling_mut(&mut self) -> &mut ScrollingSpace {
        &mut self.active_workspace_mut().scrolling
    }

    /// Borrow the active workspace's floating space.
    ///
    /// Convenience for `self.active_workspace().floating` — the daemon routes
    /// float/tile transitions through this accessor.
    ///
    /// # Panics
    ///
    /// Panics if the monitor has no workspaces.
    #[must_use]
    pub fn active_floating(&self) -> &FloatingSpace {
        &self.active_workspace().floating
    }

    /// Mutably borrow the active workspace's floating space.
    ///
    /// # Panics
    ///
    /// Panics if the monitor has no workspaces.
    #[must_use]
    pub fn active_floating_mut(&mut self) -> &mut FloatingSpace {
        &mut self.active_workspace_mut().floating
    }

    // ---- Workspace lookup-by-id ------------------------------------------
    //
    // The accessors above all operate on the *active* workspace. The
    // workspace-op commands (SwitchWorkspace, MoveWindowToWorkspace) need to
    // reach *other* workspaces on the same monitor — to read their layouts for
    // animation, mutate their scrolling spaces on window-transport, or change
    // which workspace is active. The methods below give the daemon stable-id
    // access without exposing the underlying `Vec<Workspace>` indices, so the
    // storage representation can change (e.g. a future swap operation that
    // reorders the vec) without breaking callers.

    /// Find the storage index of a workspace by its stable [`WorkspaceId`].
    ///
    /// Linear scan over the workspace stack. Workspaces are not guaranteed to
    /// be sorted by id (a future `SwapWorkspace` operation may reorder them),
    /// so a `position` search is the only correct general lookup.
    ///
    /// Returns `None` if no workspace on this monitor has the given id. Use
    /// [`active_workspace_index`](Self::active_workspace_index) if you need
    /// the active workspace's position rather than its id.
    #[must_use]
    pub fn find_workspace_index(&self, id: WorkspaceId) -> Option<usize> {
        self.workspaces.iter().position(|ws| ws.id == id)
    }

    /// Borrow a workspace by stable [`WorkspaceId`].
    ///
    /// Returns `None` if no workspace on this monitor has the given id. For
    /// the on-screen workspace prefer the cheaper
    /// [`active_workspace`](Self::active_workspace) accessor.
    #[must_use]
    pub fn workspace(&self, id: WorkspaceId) -> Option<&Workspace> {
        // `find_workspace_index` returns an owned `Option<usize>` (no borrow
        // held), so NLL permits the indexing lookup immediately afterwards.
        self.find_workspace_index(id).map(|i| &self.workspaces[i])
    }

    /// Mutably borrow a workspace by stable [`WorkspaceId`].
    ///
    /// Returns `None` if no workspace on this monitor has the given id. For
    /// the on-screen workspace prefer
    /// [`active_workspace_mut`](Self::active_workspace_mut).
    ///
    /// Note: this is intentionally a single-target lookup. If a caller needs
    /// to mutate two workspaces at once (e.g. `MoveWindowToWorkspace`
    /// removes a window from the source and inserts into the dest), it must
    /// decompose the operation into single-workspace steps — Rust's borrow
    /// checker forbids two simultaneous `&mut` borrows of the same `Vec`.
    /// The daemon-side workspace-op handlers do exactly this.
    #[must_use]
    pub fn workspace_mut(&mut self, id: WorkspaceId) -> Option<&mut Workspace> {
        match self.find_workspace_index(id) {
            Some(i) => Some(&mut self.workspaces[i]),
            None => None,
        }
    }

    /// The stable [`WorkspaceId`] of the currently on-screen workspace.
    ///
    /// Convenience for `self.active_workspace().id`. The workspace-op dispatch
    /// handlers use this to capture the source workspace before calling
    /// [`set_active_workspace`](Self::set_active_workspace).
    #[must_use]
    pub fn active_workspace_id(&self) -> WorkspaceId {
        self.active_workspace().id
    }

    /// Change which workspace is on screen, addressed by stable [`WorkspaceId`].
    ///
    /// This is the dispatch-time mechanism for `SwitchWorkspace`: the daemon
    /// captures the current active id via
    /// [`active_workspace_id`](Self::active_workspace_id), calls this method
    /// to update the active index, then submits the animation batch (see the
    /// daemon dispatch module).
    ///
    /// Returns the **previous** active *index* (not id) on success so callers
    /// can report or restore it; returns `None` if no workspace on this
    /// monitor has the given id, in which case the active index is left
    /// untouched. Calling with the already-active id is a no-op but still
    /// returns `Some(previous_index)`.
    ///
    /// This method is deliberately pure bookkeeping — it does **not** animate
    /// or touch any window positions. The daemon is responsible for
    /// submitting animation targets for both the source and destination
    /// workspaces after this returns.
    #[must_use]
    pub fn set_active_workspace(&mut self, id: WorkspaceId) -> Option<usize> {
        match self.find_workspace_index(id) {
            Some(new_idx) => {
                let prev = self.active_workspace;
                self.active_workspace = new_idx;
                Some(prev)
            }
            None => None,
        }
    }

    /// Find which workspace owns a window, by [`WindowId`].
    ///
    /// Linear scan over every workspace, checking both the tiling
    /// ([`ScrollingSpace`] virtual layout) and floating ([`FloatingSpace`])
    /// rosters. Returns the stable [`WorkspaceId`] of the first workspace
    /// that contains the window, or `None` when the window is not managed by
    /// any workspace on this monitor (ignored, untracked, or off-monitor).
    ///
    /// Used by the foreground hook (`on_focus_changed`) to discover which
    /// workspace an externally-foregrounded window belongs to.
    #[must_use]
    pub fn find_workspace_containing(&self, window: WindowId) -> Option<WorkspaceId> {
        self.workspaces.iter().find_map(|ws| {
            let in_tiling = ws.scrolling.virtual_layout().find_window(window).is_some();
            let in_floating = ws
                .floating
                .windows()
                .iter()
                .any(|entry| entry.window_id == window);
            (in_tiling || in_floating).then_some(ws.id)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::Rect;
    use crate::layout::types::{MonitorInfo, Padding};

    /// Build a minimal [`ScrollingSpace`] for tests — parameters mirror the
    /// test setup in `scrolling_space.rs::tests` so behaviour matches the
    /// production engine. We never drive the scrolling space in these tests;
    /// it exists only to satisfy [`Workspace::new`]'s signature.
    fn make_scrolling() -> ScrollingSpace {
        ScrollingSpace::new(
            MonitorInfo {
                work_area: Rect {
                    x: 0,
                    y: 0,
                    width: 1920,
                    height: 1080,
                },
            },
            960,
            320,
            100,
            Padding {
                window_gap: 4,
                up: 0,
                down: 0,
            },
            4,
        )
    }

    /// Build a [`Monitor`] whose workspaces have the given ids (in order),
    /// with the given index active. Helper keeps the lookup-by-id tests
    /// self-contained.
    fn make_monitor_with_ids(ids: &[u32], active_idx: usize) -> Monitor {
        let workspaces: Vec<Workspace> = ids
            .iter()
            .map(|&id| Workspace::new(WorkspaceId(id), make_scrolling()))
            .collect();
        // Tests don't model a taskbar, so screen_rect and work_area are
        // identical — the two-rect distinction only matters for the parking
        // math exercised in y_offset.rs.
        let rect = Rect {
            x: 0,
            y: 0,
            width: 1920,
            height: 1080,
        };
        Monitor::new(rect, rect, workspaces, active_idx)
    }

    // ---- find_workspace_index --------------------------------------------

    // Positive: existing ids resolve to their storage position.
    #[test]
    fn find_workspace_index_returns_position_for_existing_id() {
        let monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        assert_eq!(monitor.find_workspace_index(WorkspaceId(1)), Some(0));
        assert_eq!(monitor.find_workspace_index(WorkspaceId(2)), Some(1));
        assert_eq!(monitor.find_workspace_index(WorkspaceId(3)), Some(2));
    }

    // Negative: missing id returns None rather than panicking.
    #[test]
    fn find_workspace_index_returns_none_for_missing_id() {
        let monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        assert_eq!(monitor.find_workspace_index(WorkspaceId(99)), None);
    }

    // Positive: lookup works even if workspaces are stored out of numeric
    // order — important for a future SwapWorkspace that reorders the vec.
    #[test]
    fn find_workspace_index_works_for_unordered_ids() {
        let monitor = make_monitor_with_ids(&[5, 1, 9], 0);
        assert_eq!(monitor.find_workspace_index(WorkspaceId(9)), Some(2));
        assert_eq!(monitor.find_workspace_index(WorkspaceId(5)), Some(0));
    }

    // ---- workspace / workspace_mut ---------------------------------------

    #[test]
    fn workspace_borrows_by_id() {
        let monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        assert_eq!(
            monitor.workspace(WorkspaceId(2)).map(|ws| ws.id),
            Some(WorkspaceId(2)),
        );
    }

    #[test]
    fn workspace_returns_none_for_missing_id() {
        let monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        assert!(monitor.workspace(WorkspaceId(99)).is_none());
    }

    #[test]
    fn workspace_mut_borrows_by_id() {
        let mut monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        let ws = monitor
            .workspace_mut(WorkspaceId(3))
            .expect("workspace 3 should exist");
        assert_eq!(ws.id, WorkspaceId(3));
    }

    #[test]
    fn workspace_mut_returns_none_for_missing_id() {
        let mut monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        assert!(monitor.workspace_mut(WorkspaceId(99)).is_none());
    }

    // ---- active_workspace_id ---------------------------------------------

    #[test]
    fn active_workspace_id_reflects_current_active() {
        // Active index 1 -> workspace with id 10.
        let monitor = make_monitor_with_ids(&[5, 10, 15], 1);
        assert_eq!(monitor.active_workspace_id(), WorkspaceId(10));
    }

    // ---- set_active_workspace --------------------------------------------

    #[test]
    fn set_active_workspace_updates_index_and_returns_previous() {
        let mut monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        let prev = monitor.set_active_workspace(WorkspaceId(3));
        assert_eq!(prev, Some(0));
        assert_eq!(monitor.active_workspace_index(), 2);
        assert_eq!(monitor.active_workspace_id(), WorkspaceId(3));
    }

    #[test]
    fn set_active_workspace_returns_none_for_missing_id_leaving_state_unchanged() {
        let mut monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        let prev = monitor.set_active_workspace(WorkspaceId(99));
        assert_eq!(prev, None);
        // Active index must be untouched.
        assert_eq!(monitor.active_workspace_index(), 0);
    }

    #[test]
    fn set_active_workspace_to_current_returns_previous_without_changing_state() {
        let mut monitor = make_monitor_with_ids(&[1, 2, 3], 1);
        let prev = monitor.set_active_workspace(WorkspaceId(2));
        assert_eq!(prev, Some(1));
        assert_eq!(monitor.active_workspace_index(), 1);
    }

    #[test]
    fn set_active_workspace_round_trip_restores_original_active() {
        let mut monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        let _ = monitor.set_active_workspace(WorkspaceId(3));
        let prev = monitor.set_active_workspace(WorkspaceId(1));
        assert_eq!(prev, Some(2));
        assert_eq!(monitor.active_workspace_index(), 0);
    }

    // ---- find_workspace_containing --------------------------------------

    #[test]
    fn find_workspace_containing_returns_none_for_empty_workspaces() {
        let monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        assert_eq!(monitor.find_workspace_containing(WindowId(100)), None);
    }

    #[test]
    fn find_workspace_containing_finds_tiling_window() {
        let mut monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        monitor
            .workspace_mut(WorkspaceId(2))
            .expect("workspace 2 exists")
            .scrolling
            .add_window(WindowId(100));
        assert_eq!(
            monitor.find_workspace_containing(WindowId(100)),
            Some(WorkspaceId(2))
        );
    }

    #[test]
    fn find_workspace_containing_returns_none_for_untracked_window() {
        let mut monitor = make_monitor_with_ids(&[1, 2, 3], 0);
        monitor
            .workspace_mut(WorkspaceId(1))
            .expect("workspace 1 exists")
            .scrolling
            .add_window(WindowId(100));
        assert_eq!(monitor.find_workspace_containing(WindowId(999)), None);
    }

    // ---- screen_rect / work_area storage --------------------------------
    //
    // Regression guard for the y_offset bug: a Monitor must carry the full
    // physical rect SEPARATELY from the taskbar-excluded work area, so the
    // parking math can use the (larger) physical height.

    #[test]
    fn screen_rect_returns_stored_value() {
        let screen = Rect {
            x: 0,
            y: 0,
            width: 1920,
            height: 1200,
        };
        let work = Rect {
            x: 0,
            y: 0,
            width: 1920,
            height: 1160,
        };
        let monitor = Monitor::new(screen, work, Vec::new(), 0);
        assert_eq!(monitor.screen_rect(), screen);
    }

    #[test]
    fn screen_rect_and_work_area_are_stored_independently() {
        // Physical 1200-tall screen with a 40px bottom taskbar → work area
        // is 1160 tall. The two must NOT collapse to the same value, or the
        // parking offset would under-travel by exactly the taskbar height.
        let screen = Rect {
            x: 0,
            y: 0,
            width: 1920,
            height: 1200,
        };
        let work = Rect {
            x: 0,
            y: 0,
            width: 1920,
            height: 1160,
        };
        let monitor = Monitor::new(screen, work, Vec::new(), 0);
        assert_ne!(monitor.screen_rect(), monitor.work_area());
        assert_eq!(
            monitor.screen_rect().height - monitor.work_area().height,
            40
        );
    }
}