hadrone-core 0.1.0

Core layout engine: compaction, collisions, and layout validation
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
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
546
547
548
549
550
551
//! # Grid Layout Core
//!
//! The spatial engine for a cross-framework grid layout system.
//! This crate contains the deterministic math for grid compaction, collision detection,
//! and interaction sessions. It is designed to be headless and framework-agnostic.

pub mod collision;
pub mod events;
pub mod interaction;
pub mod responsive;
pub mod validate;

pub use collision::{CollisionResolver, CollisionStrategy, NoopCollisionResolver, PushDownResolver};
pub use events::{InteractionPhase, LayoutEvent};
pub use responsive::{BreakpointSpec, scale_layout_cols, select_breakpoint};
pub use validate::{LayoutIssue, repair_layout, validate_layout};

use serde::{Deserialize, Serialize};
use std::collections::HashSet;

/// Represents a handle for resizing a grid item.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ResizeHandle {
    North,
    South,
    East,
    West,
    NorthEast,
    NorthWest,
    SouthEast,
    SouthWest,
}

/// Human-readable label for assistive tech (e.g. `aria-label` on resize controls).
pub fn resize_handle_aria_label(handle: ResizeHandle) -> &'static str {
    match handle {
        ResizeHandle::North => "Resize top edge",
        ResizeHandle::South => "Resize bottom edge",
        ResizeHandle::East => "Resize right edge",
        ResizeHandle::West => "Resize left edge",
        ResizeHandle::NorthEast => "Resize top-right corner",
        ResizeHandle::NorthWest => "Resize top-left corner",
        ResizeHandle::SouthEast => "Resize bottom-right corner",
        ResizeHandle::SouthWest => "Resize bottom-left corner",
    }
}

/// A single item within the grid layout.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct LayoutItem {
    /// Unique identifier for the item.
    pub id: String,
    /// X coordinate in grid units.
    pub x: i32,
    /// Y coordinate in grid units.
    pub y: i32,
    /// Width in grid units.
    pub w: i32,
    /// Height in grid units.
    pub h: i32,
    /// Minimum allowed width.
    pub min_w: Option<i32>,
    /// Maximum allowed width.
    pub max_w: Option<i32>,
    /// Minimum allowed height.
    pub min_h: Option<i32>,
    /// Maximum allowed height.
    pub max_h: Option<i32>,
    /// Optional fixed aspect ratio **width / height** in grid units.
    pub aspect_ratio: Option<f32>,
    /// If true, the item cannot be dragged or resized and is fixed for compaction.
    pub is_static: bool,
    /// When `is_static` is false: allow dragging (subject to compaction).
    pub is_draggable: bool,
    /// When `is_static` is false: allow resizing.
    pub is_resizable: bool,
    /// Enabled resize handles for this item.
    pub resize_handles: HashSet<ResizeHandle>,
}

impl LayoutItem {
    /// User-driven drag allowed (keyboard / pointer).
    #[inline]
    pub fn can_drag(&self) -> bool {
        !self.is_static && self.is_draggable
    }

    /// User-driven resize allowed.
    #[inline]
    pub fn can_resize(&self) -> bool {
        !self.is_static && self.is_resizable
    }
}

impl Default for LayoutItem {
    fn default() -> Self {
        let mut handles = HashSet::new();
        handles.insert(ResizeHandle::SouthEast);
        Self {
            id: "".into(),
            x: 0,
            y: 0,
            w: 1,
            h: 1,
            min_w: None,
            max_w: None,
            min_h: None,
            max_h: None,
            aspect_ratio: None,
            is_static: false,
            is_draggable: true,
            is_resizable: true,
            resize_handles: handles,
        }
    }
}

/// Trait for grid compaction strategies.
/// Compaction is the process of resolving overlaps and settling items into a stable layout.
pub trait Compactor {
    /// Compacts the layout by resolving collisions and moving items.
    fn compact(&self, layout: &mut Vec<LayoutItem>, cols: i32);
}

/// Supported compaction strategies.
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub enum CompactionType {
    /// Items settle towards the top of the grid (Y=0).
    #[default]
    Gravity,
    /// Items stay at their requested positions unless they collide.
    FreePlacement,
}

/// O(N * Cols) implementation using the "waterline" approach
pub struct RisingTideCompactor;

impl Compactor for RisingTideCompactor {
    fn compact(&self, layout: &mut Vec<LayoutItem>, cols: i32) {
        // Sort items by Y, then X
        layout.sort_by(|a, b| a.y.cmp(&b.y).then(a.x.cmp(&b.x)));

        let mut waterline = vec![0; cols as usize];
        let mut new_layout = Vec::with_capacity(layout.len());

        for mut item in layout.drain(..) {
            if !item.is_static {
                // Find max waterline in the range [x, x+w)
                let start_col = item.x.max(0) as usize;
                let end_col = (item.x + item.w).min(cols) as usize;

                let max_y = waterline[start_col..end_col]
                    .iter()
                    .max()
                    .copied()
                    .unwrap_or(0);

                // Set Y to the max waterline found
                item.y = max_y;

                // Update waterline for the range
                let new_y_plus_h = item.y + item.h;
                waterline[start_col..end_col].fill(new_y_plus_h);
            } else {
                // Static items update the waterline at their fixed position
                let start_col = item.x.max(0) as usize;
                let end_col = (item.x + item.w).min(cols) as usize;
                let new_y_plus_h = item.y + item.h;
                for val in &mut waterline[start_col..end_col] {
                    *val = (*val).max(new_y_plus_h);
                }
            }
            new_layout.push(item);
        }

        *layout = new_layout;
    }
}

pub struct FreePlacementCompactor;

impl Compactor for FreePlacementCompactor {
    fn compact(&self, layout: &mut Vec<LayoutItem>, _cols: i32) {
        // Sort items by Y, then X
        layout.sort_by(|a, b| a.y.cmp(&b.y).then(a.x.cmp(&b.x)));

        let mut processed: Vec<LayoutItem> = Vec::with_capacity(layout.len());

        for mut item in layout.drain(..) {
            // Keep bumping y if there's a collision with any already processed item
            // This preserves the item's requested Y as much as possible without overlaps
            while processed.iter().any(|other| collides(&item, other)) {
                item.y += 1;
            }
            processed.push(item);
        }

        *layout = processed;
    }
}

/// The main orchestration point for grid operations.
pub struct LayoutEngine {
    /// The compaction strategy to use.
    pub compactor: Box<dyn Compactor>,
    /// Displacement policy after overlaps.
    pub collision: Box<dyn CollisionResolver>,
    /// The number of columns in the grid.
    pub cols: i32,
}

impl LayoutEngine {
    pub fn new(
        compactor: Box<dyn Compactor>,
        collision: Box<dyn CollisionResolver>,
        cols: i32,
    ) -> Self {
        Self {
            compactor,
            collision,
            cols,
        }
    }

    /// Gravity compaction with push-down collision resolution.
    pub fn with_default_collision(compactor: Box<dyn Compactor>, cols: i32) -> Self {
        Self::new(compactor, CollisionStrategy::PushDown.build(), cols)
    }

    pub fn compact(&self, layout: &mut Vec<LayoutItem>) {
        self.compactor.compact(layout, self.cols);
    }

    pub fn move_element(&self, layout: &mut Vec<LayoutItem>, id: &str, x: i32, y: i32) {
        if let Some(index) = layout.iter().position(|i| i.id == id) {
            let mut item = layout[index].clone();
            if !item.can_drag() {
                return;
            }

            item.x = x.max(0).min(self.cols - item.w);
            item.y = y.max(0);

            layout[index] = item;
            self.collision.resolve_collisions(layout, id);
            self.compact(layout);
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub fn resize_element(
        &self,
        layout: &mut Vec<LayoutItem>,
        id: &str,
        x: i32,
        y: i32,
        w: i32,
        h: i32,
        handle: Option<ResizeHandle>,
    ) {
        if let Some(index) = layout.iter().position(|i| i.id == id) {
            let mut item = layout[index].clone();
            if !item.can_resize() {
                return;
            }

            let mut final_w = w;
            let mut final_h = h;
            if let Some(min) = item.min_w {
                final_w = final_w.max(min);
            }
            if let Some(max) = item.max_w {
                final_w = final_w.min(max);
            }
            if let Some(min) = item.min_h {
                final_h = final_h.max(min);
            }
            if let Some(max) = item.max_h {
                final_h = final_h.min(max);
            }

            let final_x = x.max(0).min(self.cols - final_w);
            let final_y = y.max(0);

            item.x = final_x;
            item.y = final_y;
            item.w = final_w.max(1);
            item.h = final_h.max(1);

            apply_aspect_and_clamp(&mut item, self.cols, handle);

            layout[index] = item;
            self.collision.resolve_collisions(layout, id);
            self.compact(layout);
        }
    }
}

/// Applies `aspect_ratio` (w/h) plus min/max; re-clamps `x`/`w` to `cols`.
pub fn apply_aspect_and_clamp(item: &mut LayoutItem, cols: i32, handle: Option<ResizeHandle>) {
    let mut w = item.w.max(1);
    let mut h = item.h.max(1);

    if let Some(ar) = item.aspect_ratio.filter(|a| a.is_finite() && *a > 0.0) {
        let prefer_width = handle.map(aspect_prefers_width).unwrap_or(true);
        for _ in 0..6 {
            if prefer_width {
                h = (w as f32 / ar).round() as i32;
            } else {
                w = (h as f32 * ar).round() as i32;
            }
            h = h.max(1);
            w = w.max(1);
            if let Some(min) = item.min_h {
                h = h.max(min);
            }
            if let Some(max) = item.max_h {
                h = h.min(max);
            }
            if let Some(min) = item.min_w {
                w = w.max(min);
            }
            if let Some(max) = item.max_w {
                w = w.min(max);
            }
            if prefer_width {
                w = (h as f32 * ar).round() as i32;
            } else {
                h = (w as f32 / ar).round() as i32;
            }
            w = w.max(1);
            h = h.max(1);
        }
    }

    item.w = w.min(cols).max(1);
    item.h = h.max(1);
    item.x = item.x.max(0).min((cols - item.w).max(0));
    item.y = item.y.max(0);
}

fn aspect_prefers_width(handle: ResizeHandle) -> bool {
    match handle {
        ResizeHandle::East
        | ResizeHandle::West
        | ResizeHandle::NorthEast
        | ResizeHandle::SouthEast => true,
        ResizeHandle::North | ResizeHandle::South => false,
        ResizeHandle::NorthWest | ResizeHandle::SouthWest => false,
    }
}

/// Checks if two layout items overlap.
pub fn collides(a: &LayoutItem, b: &LayoutItem) -> bool {
    if a.id == b.id {
        return false;
    }
    !(a.x + a.w <= b.x || a.x >= b.x + b.w || a.y + a.h <= b.y || a.y >= b.y + b.h)
}

/// Build an engine from compaction + collision strategy (common in interaction code).
pub fn layout_engine(
    compaction: CompactionType,
    collision: CollisionStrategy,
    cols: i32,
) -> LayoutEngine {
    let compactor: Box<dyn Compactor> = match compaction {
        CompactionType::Gravity => Box::new(RisingTideCompactor),
        CompactionType::FreePlacement => Box::new(FreePlacementCompactor),
    };
    LayoutEngine::new(compactor, collision.build(), cols)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::interaction::{InteractionSession, InteractionType};
    use crate::validate::LayoutIssue;
    use std::collections::HashSet;

    fn item(id: &str, x: i32, y: i32, w: i32, h: i32) -> LayoutItem {
        LayoutItem {
            id: id.into(),
            x,
            y,
            w,
            h,
            ..Default::default()
        }
    }

    #[test]
    fn collides_false_for_same_id() {
        let a = item("a", 0, 0, 2, 2);
        assert!(!collides(&a, &a));
    }

    #[test]
    fn collides_true_when_overlapping() {
        let a = item("a", 0, 0, 2, 2);
        let b = item("b", 1, 1, 2, 2);
        assert!(collides(&a, &b));
    }

    #[test]
    fn collides_false_when_adjacent() {
        let a = item("a", 0, 0, 2, 2);
        let b = item("b", 2, 0, 2, 2);
        assert!(!collides(&a, &b));
    }

    #[test]
    fn rising_tide_stacks_vertically() {
        let mut layout = vec![item("a", 0, 5, 4, 2), item("b", 0, 0, 4, 2)];
        RisingTideCompactor.compact(&mut layout, 12);
        let a = layout.iter().find(|i| i.id == "a").unwrap();
        let b = layout.iter().find(|i| i.id == "b").unwrap();
        assert_eq!(b.y, 0);
        assert_eq!(a.y, 2);
        assert!(!collides(a, b));
    }

    #[test]
    fn free_placement_pushes_second_item_down() {
        let mut layout = vec![item("a", 0, 0, 4, 4), item("b", 1, 1, 2, 2)];
        FreePlacementCompactor.compact(&mut layout, 12);
        let b = layout.iter().find(|i| i.id == "b").unwrap();
        assert_eq!(b.y, 4);
    }

    #[test]
    fn move_element_clamps_to_grid_width() {
        let engine = LayoutEngine::with_default_collision(Box::new(RisingTideCompactor), 6);
        let mut layout = vec![item("w", 0, 0, 4, 1)];
        engine.move_element(&mut layout, "w", 10, 0);
        let w = layout.iter().find(|i| i.id == "w").unwrap();
        assert_eq!(w.x, 2);
    }

    #[test]
    fn static_item_does_not_move_in_compactor() {
        let mut layout = vec![LayoutItem {
            id: "s".into(),
            x: 0,
            y: 3,
            w: 2,
            h: 1,
            is_static: true,
            ..Default::default()
        }];
        RisingTideCompactor.compact(&mut layout, 12);
        assert_eq!(layout[0].y, 3);
    }

    #[test]
    fn resize_element_applies_min_width() {
        let engine = LayoutEngine::with_default_collision(Box::new(RisingTideCompactor), 12);
        let mut handles = HashSet::new();
        handles.insert(ResizeHandle::SouthEast);
        let mut layout = vec![LayoutItem {
            id: "x".into(),
            x: 0,
            y: 0,
            w: 4,
            h: 2,
            min_w: Some(3),
            resize_handles: handles,
            ..Default::default()
        }];
        engine.resize_element(&mut layout, "x", 0, 0, 1, 2, Some(ResizeHandle::East));
        let x = layout.iter().find(|i| i.id == "x").unwrap();
        assert_eq!(x.w, 3);
    }

    #[test]
    fn interaction_drag_updates_position() {
        let mut handles = HashSet::new();
        handles.insert(ResizeHandle::SouthEast);
        let mut layout = vec![LayoutItem {
            id: "d".into(),
            x: 0,
            y: 0,
            w: 2,
            h: 2,
            resize_handles: handles,
            ..Default::default()
        }];
        let session = InteractionSession {
            id: "d".into(),
            interaction_type: InteractionType::Drag,
            start_mouse: (0.0, 0.0),
            start_rect: (0, 0, 2, 2),
            handle: ResizeHandle::SouthEast,
            col_width_px: 100.0,
            row_height_px: 50.0,
            margin: (0, 10),
            container_padding: (0, 0),
            compaction: CompactionType::Gravity,
            collision: CollisionStrategy::PushDown,
        };
        session.update((200.0, 0.0), &mut layout, 12);
        let d = layout.iter().find(|i| i.id == "d").unwrap();
        assert_eq!(d.x, 2);
        assert_eq!(d.y, 0);
    }

    #[test]
    fn scale_layout_cols_halves_positions() {
        let items = vec![item("a", 4, 1, 4, 2)];
        let out = scale_layout_cols(&items, 12, 6);
        let a = out.iter().find(|i| i.id == "a").unwrap();
        assert_eq!(a.x, 2);
        assert_eq!(a.w, 2);
    }

    #[test]
    fn aspect_ratio_enforced_on_resize() {
        let engine = LayoutEngine::with_default_collision(Box::new(RisingTideCompactor), 12);
        let mut layout = vec![LayoutItem {
            id: "ar".into(),
            x: 0,
            y: 0,
            w: 4,
            h: 2,
            aspect_ratio: Some(2.0),
            ..Default::default()
        }];
        engine.resize_element(&mut layout, "ar", 0, 0, 2, 2, Some(ResizeHandle::East));
        let it = layout.iter().find(|i| i.id == "ar").unwrap();
        assert_eq!(it.w, 2);
        assert_eq!(it.h, 1);
    }

    #[test]
    fn validate_layout_detects_duplicate_ids() {
        let layout = vec![
            item("dup", 0, 0, 2, 2),
            LayoutItem {
                id: "dup".into(),
                x: 2,
                y: 0,
                w: 2,
                h: 2,
                ..Default::default()
            },
        ];
        let err = validate_layout(&layout, 12).unwrap_err();
        assert!(err.iter().any(|e| matches!(e, LayoutIssue::DuplicateId { .. })));
    }
}