term-wm-layout-engine 0.8.8-alpha

Pure-math tiling window layout engine with BSP/N-ary tree support for term-wm.
Documentation
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
use crate::rect::{LayoutRect, rect_contains};

/// Minimum width for a floating window (in cells).
pub const FLOATING_MIN_WIDTH: u16 = 6;

/// Minimum height for a floating window (in cells).
pub const FLOATING_MIN_HEIGHT: u16 = 3;

/// Identifies which edge(s) of a floating window are being dragged.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResizeEdge {
    Left,
    Right,
    Top,
    Bottom,
    TopLeft,
    TopRight,
    BottomLeft,
    BottomRight,
}

/// A single-cell hit-test handle at a corner or along an edge of a floating window.
#[derive(Debug, Clone, Copy)]
pub struct ResizeHandle<R: Copy + Eq + Ord> {
    pub id: R,
    pub rect: LayoutRect,
    pub edge: ResizeEdge,
}

/// State of an in-progress resize drag.
#[derive(Debug, Clone, Copy)]
pub struct ResizeDrag<R: Copy + Eq + Ord> {
    pub id: R,
    pub edge: ResizeEdge,
    pub start_col: u16,
    pub start_row: u16,
    pub start_x: i32,
    pub start_y: i32,
    pub start_width: u16,
    pub start_height: u16,
}

/// State of an in-progress header drag (move).
#[derive(Debug, Clone, Copy)]
pub struct HeaderDrag<R: Copy + Eq + Ord> {
    pub id: R,
    pub initial_x: i32,
    pub initial_y: i32,
    pub start_x: u16,
    pub start_y: u16,
}

/// A hit-test region for the title bar of a floating window.
#[derive(Debug, Clone, Copy)]
pub struct DragHandle<R: Copy + Eq + Ord> {
    pub id: R,
    pub rect: LayoutRect,
}

/// Generate all 8 resize handles (4 corners + 4 edges) for a floating region.
pub fn resize_handles_for_region<R: Copy + Eq + Ord>(
    id: R,
    rect: LayoutRect,
    _bounds: LayoutRect,
) -> Vec<ResizeHandle<R>> {
    if rect.width == 0 || rect.height == 0 {
        return Vec::new();
    }

    let x1 = rect.x;
    let y1 = rect.y;
    let x2 = rect
        .x
        .saturating_add(i32::from(rect.width.saturating_sub(1)));
    let y2 = rect
        .y
        .saturating_add(i32::from(rect.height.saturating_sub(1)));

    let mut handles = Vec::with_capacity(8);

    // Corners (1×1)
    handles.push(ResizeHandle {
        id,
        rect: LayoutRect {
            x: x1,
            y: y1,
            width: 1,
            height: 1,
        },
        edge: ResizeEdge::TopLeft,
    });
    handles.push(ResizeHandle {
        id,
        rect: LayoutRect {
            x: x2,
            y: y1,
            width: 1,
            height: 1,
        },
        edge: ResizeEdge::TopRight,
    });
    handles.push(ResizeHandle {
        id,
        rect: LayoutRect {
            x: x1,
            y: y2,
            width: 1,
            height: 1,
        },
        edge: ResizeEdge::BottomLeft,
    });
    handles.push(ResizeHandle {
        id,
        rect: LayoutRect {
            x: x2,
            y: y2,
            width: 1,
            height: 1,
        },
        edge: ResizeEdge::BottomRight,
    });

    // Edge handles (span the full dimension minus corners)
    if rect.height > 2 {
        let inner_h = rect.height.saturating_sub(2);
        handles.push(ResizeHandle {
            id,
            rect: LayoutRect {
                x: x1,
                y: y1.saturating_add(1),
                width: 1,
                height: inner_h,
            },
            edge: ResizeEdge::Left,
        });
        handles.push(ResizeHandle {
            id,
            rect: LayoutRect {
                x: x2,
                y: y1.saturating_add(1),
                width: 1,
                height: inner_h,
            },
            edge: ResizeEdge::Right,
        });
    }
    if rect.width > 2 {
        let inner_w = rect.width.saturating_sub(2);
        handles.push(ResizeHandle {
            id,
            rect: LayoutRect {
                x: x1.saturating_add(1),
                y: y1,
                width: inner_w,
                height: 1,
            },
            edge: ResizeEdge::Top,
        });
        handles.push(ResizeHandle {
            id,
            rect: LayoutRect {
                x: x1.saturating_add(1),
                y: y2,
                width: inner_w,
                height: 1,
            },
            edge: ResizeEdge::Bottom,
        });
    }

    handles
}

/// Generate a drag handle for the title bar of a floating window.
pub fn floating_header_for_region<R: Copy + Eq + Ord>(
    id: R,
    rect: LayoutRect,
    bounds: LayoutRect,
) -> Option<DragHandle<R>> {
    if rect.width < 3 || rect.height < 3 {
        return None;
    }
    let header_rect = LayoutRect {
        x: rect.x.saturating_add(1),
        y: rect.y.saturating_add(1),
        width: rect.width.saturating_sub(2),
        height: 1,
    };
    if !rect_contains(&bounds, header_rect.x as u16, header_rect.y as u16) {
        return None;
    }
    Some(DragHandle {
        id,
        rect: header_rect,
    })
}

/// Apply a resize drag delta to a floating window's geometry.
///
/// Returns the new [`LayoutRect`] after applying the delta, enforcing
/// minimum size constraints and bounds clamping.
#[allow(clippy::too_many_arguments)]
pub fn apply_resize_drag_signed(
    start_x: i32,
    start_y: i32,
    start_width: u16,
    start_height: u16,
    edge: ResizeEdge,
    column: u16,
    row: u16,
    start_col: u16,
    start_row: u16,
    bounds: LayoutRect,
    allow_offscreen: bool,
) -> LayoutRect {
    let dx = i32::from(column).saturating_sub(i32::from(start_col));
    let dy = i32::from(row).saturating_sub(i32::from(start_row));

    let mut x = start_x;
    let mut y = start_y;
    let mut w = i32::from(start_width);
    let mut h = i32::from(start_height);

    // Apply delta to edges
    match edge {
        ResizeEdge::Left | ResizeEdge::TopLeft | ResizeEdge::BottomLeft => {
            x = x.saturating_add(dx);
            w = w.saturating_sub(dx);
        }
        ResizeEdge::Right | ResizeEdge::TopRight | ResizeEdge::BottomRight => {
            w = w.saturating_add(dx);
        }
        _ => {}
    }
    match edge {
        ResizeEdge::Top | ResizeEdge::TopLeft | ResizeEdge::TopRight => {
            y = y.saturating_add(dy);
            h = h.saturating_sub(dy);
        }
        ResizeEdge::Bottom | ResizeEdge::BottomLeft | ResizeEdge::BottomRight => {
            h = h.saturating_add(dy);
        }
        _ => {}
    }

    // Enforce minimum size
    let min_w = i32::from(FLOATING_MIN_WIDTH);
    let min_h = i32::from(FLOATING_MIN_HEIGHT);

    if w < min_w {
        match edge {
            ResizeEdge::Left | ResizeEdge::TopLeft | ResizeEdge::BottomLeft => {
                x = x.saturating_sub(min_w.saturating_sub(w));
            }
            _ => {}
        }
        w = min_w;
    }
    if h < min_h {
        match edge {
            ResizeEdge::Top | ResizeEdge::TopLeft | ResizeEdge::TopRight => {
                y = y.saturating_sub(min_h.saturating_sub(h));
            }
            _ => {}
        }
        h = min_h;
    }

    // Convert to u16 with safety clamp
    let mut width = w.max(1).min(i32::from(u16::MAX)) as u16;
    let mut height = h.max(1).min(i32::from(u16::MAX)) as u16;

    // Bounds clamping
    if !allow_offscreen {
        width = width.min(bounds.width);
        height = height.min(bounds.height);

        let bounds_x1 = bounds.x.saturating_add(i32::from(bounds.width));
        let bounds_y1 = bounds.y.saturating_add(i32::from(bounds.height));
        let max_x = bounds_x1.saturating_sub(i32::from(width));
        let max_y = bounds_y1.saturating_sub(i32::from(height));

        x = x.max(bounds.x).min(max_x);
        y = y.max(bounds.y).min(max_y);
    }

    LayoutRect {
        x,
        y,
        width,
        height,
    }
}

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

    fn area() -> LayoutRect {
        LayoutRect {
            x: 0,
            y: 0,
            width: 80,
            height: 24,
        }
    }

    #[test]
    fn resize_handles_count() {
        let rect = LayoutRect {
            x: 10,
            y: 10,
            width: 20,
            height: 15,
        };
        let handles = resize_handles_for_region(1u8, rect, area());
        assert_eq!(handles.len(), 8);
    }

    #[test]
    fn resize_handles_small_rect() {
        let rect = LayoutRect {
            x: 10,
            y: 10,
            width: 1,
            height: 1,
        };
        let handles = resize_handles_for_region(1u8, rect, area());
        // Only corners (4), no edge handles since dims <= 2
        assert_eq!(handles.len(), 4);
    }

    #[test]
    fn floating_header_normal() {
        let rect = LayoutRect {
            x: 10,
            y: 10,
            width: 20,
            height: 20,
        };
        let header = floating_header_for_region(1u8, rect, area());
        assert!(header.is_some());
        let h = header.unwrap();
        assert_eq!(h.rect.width, 18);
        assert_eq!(h.rect.height, 1);
        assert_eq!(h.rect.y, 11);
    }

    #[test]
    fn floating_header_too_small() {
        let rect = LayoutRect {
            x: 10,
            y: 10,
            width: 2,
            height: 2,
        };
        assert!(floating_header_for_region(1u8, rect, area()).is_none());
    }

    #[test]
    fn apply_resize_drag_right_edge() {
        let result = apply_resize_drag_signed(
            10,
            10,
            20,
            15,
            ResizeEdge::Right,
            50,
            10,
            30,
            10,
            area(),
            false,
        );
        assert_eq!(result.width, 40); // 20 + (50-30)
        assert_eq!(result.x, 10);
    }

    #[test]
    fn apply_resize_drag_left_edge() {
        let result = apply_resize_drag_signed(
            20,
            10,
            20,
            15,
            ResizeEdge::Left,
            10,
            10,
            30,
            10,
            area(),
            false,
        );
        assert_eq!(result.x, 0); // 20 + (10-30) = 0
        assert_eq!(result.width, 40); // 20 - (10-30) = 40
    }

    #[test]
    fn apply_resize_drag_enforces_min_size() {
        // Drag left edge rightward to shrink width below minimum
        let result = apply_resize_drag_signed(
            10,
            10,
            10,
            10,
            ResizeEdge::Left,
            20,
            10,
            5,
            10,
            area(),
            false,
        );
        assert_eq!(result.width, FLOATING_MIN_WIDTH);
    }

    #[test]
    fn apply_resize_drag_offscreen_allowed() {
        let result =
            apply_resize_drag_signed(0, 0, 80, 24, ResizeEdge::Left, 40, 0, 0, 0, area(), true);
        assert_eq!(result.x, 40);
        assert_eq!(result.width, 40);
    }
}