azul-layout 0.0.7

Layout solver + font and image loader the Azul GUI framework
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Scroll-into-view implementation
//!
//! Provides W3C CSSOM View Module compliant scroll-into-view functionality.
//! This module contains the core primitive `scroll_rect_into_view` which all
//! higher-level scroll-into-view APIs build upon.
//!
//! # Architecture
//!
//! The core principle is that all scroll-into-view operations reduce to scrolling
//! a rectangle into the visible area of its scroll container ancestry:
//!
//! - `scroll_rect_into_view`: Core primitive - scroll any rect into view
//! - `scroll_node_into_view`: Scroll a DOM node's bounding rect into view
//! - `scroll_cursor_into_view`: Scroll a text cursor position into view
//!
//! # W3C Compliance
//!
//! This implementation follows the W3C CSSOM View Module specification:
//! - ScrollLogicalPosition: start, center, end, nearest
//! - ScrollBehavior: auto, instant, smooth
//! - Proper scroll ancestor chain traversal

use alloc::vec::Vec;

use azul_core::{
    dom::{DomId, DomNodeId, NodeId},
    geom::{LogicalPosition, LogicalRect, LogicalSize},
    styled_dom::NodeHierarchyItemId,
    task::{Duration, Instant},
};
use azul_css::props::layout::LayoutOverflow;

use crate::{
    managers::scroll_state::ScrollManager,
    solver3::getters::{get_overflow_x, get_overflow_y, MultiValue},
    window::DomLayoutResult,
};

// Re-export types from core for public API
pub use azul_core::events::{ScrollIntoViewBehavior, ScrollIntoViewOptions, ScrollLogicalPosition};

/// Calculated scroll adjustment for one scroll container
#[derive(Debug, Clone)]
pub struct ScrollAdjustment {
    /// The scroll container that needs adjustment
    pub scroll_container_dom_id: DomId,
    pub scroll_container_node_id: NodeId,
    /// The scroll delta to apply
    pub delta: LogicalPosition,
    /// The scroll behavior to use
    pub behavior: ScrollIntoViewBehavior,
}

/// Information about a scrollable ancestor
#[derive(Debug, Clone)]
struct ScrollableAncestor {
    dom_id: DomId,
    node_id: NodeId,
    /// The visible rect of the scroll container (content area)
    visible_rect: LogicalRect,
    /// Whether horizontal scroll is enabled
    scroll_x: bool,
    /// Whether vertical scroll is enabled
    scroll_y: bool,
}

// ============================================================================
// Core API: scroll_rect_into_view
// ============================================================================

/// Core function: scroll a rect into the visible area of its scroll containers
///
/// This is the ONLY scroll-into-view primitive. All higher-level APIs call this.
///
/// # Arguments
///
/// * `target_rect` - The rectangle to make visible (in absolute coordinates)
/// * `target_dom_id` - The DOM containing the target node
/// * `target_node_id` - The target node (used for finding scroll ancestors)
/// * `layout_results` - Layout data for all DOMs
/// * `scroll_manager` - Current scroll state
/// * `options` - How to scroll (alignment and animation)
/// * `now` - Current timestamp for animation
///
/// # Returns
///
/// A vector of scroll adjustments for each scroll container in the ancestry chain.
/// The adjustments are ordered from innermost (closest to target) to outermost.
pub fn scroll_rect_into_view(
    target_rect: LogicalRect,
    target_dom_id: DomId,
    target_node_id: NodeId,
    layout_results: &alloc::collections::BTreeMap<DomId, DomLayoutResult>,
    scroll_manager: &mut ScrollManager,
    options: ScrollIntoViewOptions,
    now: Instant,
) -> Vec<ScrollAdjustment> {
    let mut adjustments = Vec::new();
    
    // Find scrollable ancestors from target to root
    let scroll_ancestors = find_scrollable_ancestors(
        target_dom_id,
        target_node_id,
        layout_results,
        scroll_manager,
    );
    
    if scroll_ancestors.is_empty() {
        return adjustments;
    }
    
    // Transform target_rect relative to each scroll container and calculate deltas
    let mut current_rect = target_rect;
    
    for ancestor in scroll_ancestors {
        // Calculate the scroll delta based on options
        let delta = calculate_scroll_delta(
            current_rect,
            ancestor.visible_rect,
            options.block,
            options.inline_axis,
            ancestor.scroll_x,
            ancestor.scroll_y,
        );
        
        // Only add adjustment if there's actual scrolling to do
        if delta.x.abs() > 0.5 || delta.y.abs() > 0.5 {
            // Resolve scroll behavior
            let behavior = resolve_scroll_behavior(
                options.behavior,
                ancestor.dom_id,
                ancestor.node_id,
                layout_results,
            );
            
            // Apply the scroll adjustment
            apply_scroll_adjustment(
                scroll_manager,
                ancestor.dom_id,
                ancestor.node_id,
                delta,
                behavior,
                now.clone(),
            );
            
            adjustments.push(ScrollAdjustment {
                scroll_container_dom_id: ancestor.dom_id,
                scroll_container_node_id: ancestor.node_id,
                delta,
                behavior,
            });
            
            // Adjust current_rect for next iteration (relative to new scroll position)
            current_rect.origin.x -= delta.x;
            current_rect.origin.y -= delta.y;
        }
    }
    
    adjustments
}

// ============================================================================
// Higher-Level APIs
// ============================================================================

/// Scroll a DOM node's bounding rect into view
///
/// This is a convenience wrapper around `scroll_rect_into_view` that
/// automatically gets the node's bounding rect from layout results.
pub fn scroll_node_into_view(
    node_id: DomNodeId,
    layout_results: &alloc::collections::BTreeMap<DomId, DomLayoutResult>,
    scroll_manager: &mut ScrollManager,
    options: ScrollIntoViewOptions,
    now: Instant,
) -> Vec<ScrollAdjustment> {
    // Get node's bounding rect from layout
    let target_rect = match get_node_rect(node_id, layout_results) {
        Some(rect) => rect,
        None => return Vec::new(),
    };
    
    let internal_node_id = match node_id.node.into_crate_internal() {
        Some(nid) => nid,
        None => return Vec::new(),
    };
    
    // Call the core rect-based API
    scroll_rect_into_view(
        target_rect,
        node_id.dom,
        internal_node_id,
        layout_results,
        scroll_manager,
        options,
        now,
    )
}

/// Scroll a text cursor position into view
///
/// This requires the cursor's visual rect (from text layout) and transforms
/// it to absolute coordinates before scrolling.
///
/// # Arguments
///
/// * `cursor_rect` - The cursor's rect in node-local coordinates
/// * `node_id` - The contenteditable node containing the cursor
/// * `layout_results` - Layout data
/// * `scroll_manager` - Scroll state
/// * `options` - Scroll options
/// * `now` - Current timestamp
pub fn scroll_cursor_into_view(
    cursor_rect: LogicalRect,
    node_id: DomNodeId,
    layout_results: &alloc::collections::BTreeMap<DomId, DomLayoutResult>,
    scroll_manager: &mut ScrollManager,
    options: ScrollIntoViewOptions,
    now: Instant,
) -> Vec<ScrollAdjustment> {
    // Get node's position to transform cursor_rect to absolute coordinates
    let node_rect = match get_node_rect(node_id, layout_results) {
        Some(rect) => rect,
        None => return Vec::new(),
    };
    
    // Transform cursor rect to absolute coordinates
    let absolute_cursor_rect = LogicalRect {
        origin: LogicalPosition {
            x: node_rect.origin.x + cursor_rect.origin.x,
            y: node_rect.origin.y + cursor_rect.origin.y,
        },
        size: cursor_rect.size,
    };
    
    let internal_node_id = match node_id.node.into_crate_internal() {
        Some(nid) => nid,
        None => return Vec::new(),
    };
    
    // Call the core rect-based API
    scroll_rect_into_view(
        absolute_cursor_rect,
        node_id.dom,
        internal_node_id,
        layout_results,
        scroll_manager,
        options,
        now,
    )
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Find all scrollable ancestors from a node to the root
///
/// Returns ancestors ordered from innermost (closest to target) to outermost (root).
fn find_scrollable_ancestors(
    dom_id: DomId,
    node_id: NodeId,
    layout_results: &alloc::collections::BTreeMap<DomId, DomLayoutResult>,
    scroll_manager: &ScrollManager,
) -> Vec<ScrollableAncestor> {
    let mut ancestors = Vec::new();
    
    let layout_result = match layout_results.get(&dom_id) {
        Some(lr) => lr,
        None => return ancestors,
    };
    
    let node_hierarchy = layout_result.styled_dom.node_hierarchy.as_container();
    let styled_nodes = layout_result.styled_dom.styled_nodes.as_container();
    
    // Walk up the DOM tree from parent of target node
    let mut current = node_hierarchy.get(node_id).and_then(|h| h.parent_id());
    
    while let Some(current_node_id) = current {
        // Check if this node is scrollable
        if let Some(ancestor) = check_if_scrollable(
            dom_id,
            current_node_id,
            layout_result,
            scroll_manager,
        ) {
            ancestors.push(ancestor);
        }
        
        // Move to parent
        current = node_hierarchy.get(current_node_id).and_then(|h| h.parent_id());
    }
    
    ancestors
}

/// Check if a node is scrollable and return its scroll info
fn check_if_scrollable(
    dom_id: DomId,
    node_id: NodeId,
    layout_result: &DomLayoutResult,
    scroll_manager: &ScrollManager,
) -> Option<ScrollableAncestor> {
    let styled_nodes = layout_result.styled_dom.styled_nodes.as_container();
    let styled_node = styled_nodes.get(node_id)?;
    
    let overflow_x = get_overflow_x(
        &layout_result.styled_dom,
        node_id,
        &styled_node.styled_node_state,
    );
    let overflow_y = get_overflow_y(
        &layout_result.styled_dom,
        node_id,
        &styled_node.styled_node_state,
    );
    
    let scroll_x = overflow_x.is_scroll();
    let scroll_y = overflow_y.is_scroll();
    
    // If neither axis is scrollable, skip this node
    if !scroll_x && !scroll_y {
        return None;
    }
    
    // Check if the scroll manager has scroll state for this node
    // (which means it actually has overflowing content)
    let scroll_state = scroll_manager.get_scroll_state(dom_id, node_id)?;
    
    // Check if content actually overflows
    let has_overflow_x = scroll_state.content_rect.size.width > scroll_state.container_rect.size.width;
    let has_overflow_y = scroll_state.content_rect.size.height > scroll_state.container_rect.size.height;
    
    if !has_overflow_x && !has_overflow_y {
        return None;
    }
    
    // Get the visible rect (container rect minus current scroll offset)
    let visible_rect = LogicalRect {
        origin: LogicalPosition {
            x: scroll_state.container_rect.origin.x + scroll_state.current_offset.x,
            y: scroll_state.container_rect.origin.y + scroll_state.current_offset.y,
        },
        size: scroll_state.container_rect.size,
    };
    
    Some(ScrollableAncestor {
        dom_id,
        node_id,
        visible_rect,
        scroll_x: scroll_x && has_overflow_x,
        scroll_y: scroll_y && has_overflow_y,
    })
}

/// Calculate the scroll delta needed to bring target into view within container
fn calculate_scroll_delta(
    target: LogicalRect,
    container: LogicalRect,
    block: ScrollLogicalPosition,
    inline: ScrollLogicalPosition,
    scroll_x_enabled: bool,
    scroll_y_enabled: bool,
) -> LogicalPosition {
    LogicalPosition {
        x: if scroll_x_enabled {
            calculate_axis_delta(
                target.origin.x,
                target.size.width,
                container.origin.x,
                container.size.width,
                inline,
            )
        } else {
            0.0
        },
        y: if scroll_y_enabled {
            calculate_axis_delta(
                target.origin.y,
                target.size.height,
                container.origin.y,
                container.size.height,
                block,
            )
        } else {
            0.0
        },
    }
}

/// Calculate scroll delta for a single axis
fn calculate_axis_delta(
    target_start: f32,
    target_size: f32,
    container_start: f32,
    container_size: f32,
    position: ScrollLogicalPosition,
) -> f32 {
    let target_end = target_start + target_size;
    let container_end = container_start + container_size;
    
    match position {
        ScrollLogicalPosition::Start => {
            // Align target start with container start
            target_start - container_start
        }
        ScrollLogicalPosition::End => {
            // Align target end with container end
            target_end - container_end
        }
        ScrollLogicalPosition::Center => {
            // Center target in container
            let target_center = target_start + target_size / 2.0;
            let container_center = container_start + container_size / 2.0;
            target_center - container_center
        }
        ScrollLogicalPosition::Nearest => {
            // Minimum scroll to make target fully visible
            if target_start < container_start {
                // Target is above/left of visible area - scroll up/left
                target_start - container_start
            } else if target_end > container_end {
                // Target is below/right of visible area
                if target_size <= container_size {
                    // Target fits, align end with container end
                    target_end - container_end
                } else {
                    // Target doesn't fit, align start with container start
                    target_start - container_start
                }
            } else {
                // Target is already fully visible
                0.0
            }
        }
    }
}

/// Resolve scroll behavior based on options and CSS properties
fn resolve_scroll_behavior(
    requested: ScrollIntoViewBehavior,
    _dom_id: DomId,
    _node_id: NodeId,
    _layout_results: &alloc::collections::BTreeMap<DomId, DomLayoutResult>,
) -> ScrollIntoViewBehavior {
    match requested {
        ScrollIntoViewBehavior::Auto => {
            // TODO: Check CSS scroll-behavior property on the scroll container
            // For now, default to instant
            ScrollIntoViewBehavior::Instant
        }
        other => other,
    }
}

/// Apply a scroll adjustment to the scroll manager
fn apply_scroll_adjustment(
    scroll_manager: &mut ScrollManager,
    dom_id: DomId,
    node_id: NodeId,
    delta: LogicalPosition,
    behavior: ScrollIntoViewBehavior,
    now: Instant,
) {
    use azul_core::events::EasingFunction;
    use azul_core::task::SystemTimeDiff;
    
    let current = scroll_manager
        .get_current_offset(dom_id, node_id)
        .unwrap_or_default();
    
    let new_position = LogicalPosition {
        x: current.x + delta.x,
        y: current.y + delta.y,
    };
    
    match behavior {
        ScrollIntoViewBehavior::Instant | ScrollIntoViewBehavior::Auto => {
            scroll_manager.set_scroll_position(dom_id, node_id, new_position, now);
        }
        ScrollIntoViewBehavior::Smooth => {
            // Use smooth scroll with 300ms duration
            let duration = Duration::System(SystemTimeDiff::from_millis(300));
            scroll_manager.scroll_to(
                dom_id,
                node_id,
                new_position,
                duration,
                EasingFunction::EaseOut,
                now,
            );
        }
    }
}

/// Get a node's bounding rect from layout results
fn get_node_rect(
    node_id: DomNodeId,
    layout_results: &alloc::collections::BTreeMap<DomId, DomLayoutResult>,
) -> Option<LogicalRect> {
    let layout_result = layout_results.get(&node_id.dom)?;
    let nid = node_id.node.into_crate_internal()?;
    
    // Get position
    let layout_indices = layout_result.layout_tree.dom_to_layout.get(&nid)?;
    let layout_index = *layout_indices.first()?;
    let position = *layout_result.calculated_positions.get(layout_index)?;
    
    // Get size
    let layout_node = layout_result.layout_tree.get(layout_index)?;
    let size = layout_node.used_size?;
    
    Some(LogicalRect::new(position, size))
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_calculate_axis_delta_nearest_visible() {
        // Target already visible - no scroll needed
        let delta = calculate_axis_delta(
            100.0,  // target_start
            50.0,   // target_size
            50.0,   // container_start
            200.0,  // container_size
            ScrollLogicalPosition::Nearest,
        );
        assert_eq!(delta, 0.0);
    }
    
    #[test]
    fn test_calculate_axis_delta_nearest_above() {
        // Target above visible area
        let delta = calculate_axis_delta(
            20.0,   // target_start (above container)
            50.0,   // target_size
            100.0,  // container_start
            200.0,  // container_size
            ScrollLogicalPosition::Nearest,
        );
        assert_eq!(delta, -80.0); // scroll up by 80
    }
    
    #[test]
    fn test_calculate_axis_delta_nearest_below() {
        // Target below visible area
        let delta = calculate_axis_delta(
            280.0,  // target_start (past container end)
            50.0,   // target_size
            100.0,  // container_start
            200.0,  // container_size (ends at 300)
            ScrollLogicalPosition::Nearest,
        );
        assert_eq!(delta, 30.0); // scroll down by 30 (target_end=330, container_end=300)
    }
    
    #[test]
    fn test_calculate_axis_delta_center() {
        // Center target in container
        let delta = calculate_axis_delta(
            50.0,   // target_start
            20.0,   // target_size (center at 60)
            100.0,  // container_start
            200.0,  // container_size (center at 200)
            ScrollLogicalPosition::Center,
        );
        assert_eq!(delta, -140.0); // 60 - 200 = -140
    }
    
    #[test]
    fn test_calculate_axis_delta_start() {
        let delta = calculate_axis_delta(
            150.0,  // target_start
            50.0,   // target_size
            100.0,  // container_start
            200.0,  // container_size
            ScrollLogicalPosition::Start,
        );
        assert_eq!(delta, 50.0); // 150 - 100 = 50
    }
    
    #[test]
    fn test_calculate_axis_delta_end() {
        let delta = calculate_axis_delta(
            150.0,  // target_start
            50.0,   // target_size (end at 200)
            100.0,  // container_start
            200.0,  // container_size (end at 300)
            ScrollLogicalPosition::End,
        );
        assert_eq!(delta, -100.0); // 200 - 300 = -100
    }
}