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
//! Layout engine type definitions.
//!
//! Core data types for the 2-layer layout pipeline. The key types are:
//!
//! - [`Column`] — a vertical container of windows with a pixel width
//! - [`VirtualLayout`] — the infinite horizontal canvas (widths in pixels,
//! no absolute x-positions stored)
//! - [`ActualLayout`] — projected screen coordinates (pixel rects)
//! - [`AppliedLayout`] — the result of a mutation (new virtual + actual layouts)
//!
//! # Width representation
//!
//! Column widths are stored directly in **pixels** (`width_px`). Earlier
//! revisions stored widths as eighths of the base `column_width` to stay
//! resolution-independent, but that quantization discarded the inter-column
//! `window_gap` during expand/shrink (the gap was smaller than one eighth and
//! rounded away), so a single expand step moved by exactly `column_width`
//! instead of `column_width + window_gap`. Pixel widths make the gap
//! observable and let expand/shrink advance by the true
//! `column_shift = column_width + window_gap`. The cost — dependence on the
//! configured `column_width` and monitor resolution — is accepted; the
//! `window_gap` is already pixel-based everywhere else.
use crate;
/// A single row (window slot) inside a [`Column`].
///
/// `height` is the **source of truth** for the row's pixel height — the
/// projection layer ([`super::projection::project`]) consumes it verbatim
/// when computing on-screen rectangles. Distribution of equal heights across
/// the rows of a column happens at the **mutation layer** (see
/// `distribute_heights` in `mutations.rs`) whenever row membership changes
/// (`merge-column`, `promote`, `add_window_to_column`, `remove_window`).
///
/// Storing the height per row (rather than recomputing it during projection)
/// is what unlocks future per-row resizing — drag-resize or IPC continuous
/// height adjustment will simply overwrite `height` on the affected rows.
/// A column on the virtual canvas containing one or more stacked windows.
///
/// Columns are the **vertical containers** in the layout. Each row holds a
/// single window with its own explicit pixel [`Row::height`] — windows within
/// a column need not share the same height once drag/IPC row-resize arrives.
///
/// The column does **not** store pixel position — that is *implicit* from its
/// index in [`VirtualLayout::columns`] plus the cumulative widths of preceding
/// columns. Pixel coordinates are computed by [`super::projection::project`].
///
/// # Container Model
///
/// ```text
/// VirtualLayout (horizontal)
/// ├── Column 0 (vertical)
/// │ ├── Row 0: Row { window_id: WindowId(1), height: 540 }
/// │ └── Row 1: Row { window_id: WindowId(2), height: 540 }
/// ├── Column 1
/// │ └── Row 0: Row { window_id: WindowId(3), height: 1080 }
/// └── ...
/// ```
/// The complete virtual layout — all tiling columns on the infinite horizontal canvas.
///
/// This is the "source of truth" for the layout engine. It describes **what exists**
/// (columns, windows, their pixel widths) and **where the camera is**
/// (`viewport_offset`), but contains no absolute x-positions for individual windows.
///
/// # Camera model
///
/// `viewport_offset` acts as a camera position sliding along the infinite canvas.
/// A value of `0` means the camera is aligned with the left edge of the first column.
/// Increasing it scrolls the viewport rightward across the canvas.
///
/// Many operations (scrolling, focus-to-offscreen, swap) are
/// implemented by adjusting `viewport_offset` rather than moving individual windows.
/// The projection layer then computes actual pixel positions from this combined state.
///
/// # Immutability
///
/// Mutations never modify a `VirtualLayout` in place — they return a new one.
/// A single window's computed on-screen rectangle — what Windows OS actually sees.
///
/// Every window in the [`VirtualLayout`] has a corresponding `ActualEntry`, whether
/// it is visible on-screen or parked off-screen. The `rect` field contains the real
/// pixel coordinates that will be passed to `SetWindowPos`.
/// The on-screen projection of the virtual layout — what Windows OS must render.
///
/// This is produced by [`super::projection::project()`] and contains pixel-accurate
/// rectangles for every window. Windows visible in the viewport receive on-screen
/// coordinates; windows outside the viewport are **parked** at deterministic
/// off-screen positions (one column-width beyond the nearest viewport edge).
///
/// # Why parking matters
///
/// Windows OS does not gracefully ignore windows placed at extreme off-screen
/// coordinates. Rather than leaving off-screen windows at their unreachable virtual
/// positions, we park them just beyond the viewport edge. This ensures:
/// - Animation transitions (scroll in/out) are smooth and short-distance.
/// - The OS window manager is never confused by far-off-screen windows.
/// - There are two parking zones: **left** (beyond the left edge) and **right**
/// (beyond the right edge), chosen based on which side the column exited.
/// Monitor geometry used for layout projection.
///
/// Contains the work area [`Rect`] (excluding taskbar). The layout engine uses
/// this to determine how many columns fit on screen and where to place windows.
/// Gap and margin configuration for the layout engine.
///
/// This mirrors [`config::types::Padding`](crate::config::types::Padding) but lives
/// in the layout module to avoid a circular dependency. The daemon converts
/// between the two when constructing [`MutationConfig`](crate::layout::mutations::MutationConfig).
///
/// # Gap Model (Uniform Spacing)
///
/// `window_gap` creates **uniform spacing** everywhere — the same gap appears
/// between the screen edge and a window, and between two adjacent windows.
///
/// ```text
/// Edge | window_gap | [Window 1] | window_gap | [Window 2] | window_gap | Edge
/// ```
///
/// This is achieved via the slot-based canvas model:
/// - Each column occupies a "slot" = content + right gap
/// - `slot_width = base_content_width + window_gap`
/// - Canvas starts at `window_gap` (initial left-edge gap)
/// - The last column's embedded right gap serves as the right-edge gap
///
/// Vertically, each window is inset by `window_gap` on top and bottom within
/// its row cell, producing `2 * window_gap` gap between adjacent rows and
/// `window_gap` gap at the top/bottom edges of the tiling area.
///
/// See the [crate-level documentation](crate#padding-strategy) for a visual diagram.
/// Result of a layout mutation — the new layout state after applying a change.
///
/// Produced by [`ScrollingSpace`](crate::workspace::ScrollingSpace) for each
/// mutation. Contains the new [`VirtualLayout`] (infinite canvas state) and
/// the new [`ActualLayout`] (pixel-accurate on-screen positions).
///
/// The animation layer compares each window's target rect (from
/// `actual_layout`) against its real on-screen position and animates only
/// the windows that differ. This means every window is represented — even
/// ones whose target didn't change — so a rapid second mutation during an
/// in-flight animation will correctly retarget windows that are still
/// mid-flight (the animator samples their real current position, sees it
/// differs from the target, and builds a tween).
///
/// ## Design decision: why no `moves` field
///
/// Previously this struct carried a list of window-move instructions
/// computed by diffing the previous and new actual layouts. That diff
/// only included windows whose *target* changed, which meant windows
/// that were still physically animating toward their old target were
/// absent from the diff. On a rapid second mutation, those windows
/// were dropped from the animator's batch and stranded mid-screen.
///
/// By returning the full `ActualLayout` instead, the animation layer
/// always sees every window and can make its own no-op filtering decision
/// based on real current positions (see `build_tweens` in the animation
/// module).