michiu_ui 0.0.3

The michiu library handles layout and rendering.
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
use crate::{
    ActiveInteractionStates, ActiveMasksSecondary, ActiveTransitionsSparse,
    BaseBasicLayoutsSecondary, BaseVisualPropertiesSecondary, BasicLayoutsSecondary,
    CapacityConfig, DEFAULT_BASIC, DEFAULT_FLEX, DebugStore, DirtyLayoutEntitiesVec,
    DirtyRenderEntitiesVec, Display, EntityId, FlexLayoutsSecondary, GridLayoutsSparse,
    InteractionPropertiesSecondary, LayoutPoint, LayoutSize, LayoutStore, Length, MichiuSoA,
    ParentsSecondary, Rect, RectsSecondary, RenderStore, ResolvedBasicSecondary,
    ResolvedFlexSecondary, ResolvedGeometry, ResolvedGridSparse, ScrollOffsetsSecondary,
    ScrollSizesSecondary, ScrollStore, Size, TaffyNodesSecondary, TaffyResultTraceExt,
    TaffyTreeEntityId, ThisStyle, Val, VisualPropertiesSecondary, WindowStore,
    define_sparse_secondary,
};
use slotmap::SparseSecondaryMap;
use smallvec::SmallVec;
use std::time::{Duration, Instant};

/// スクロールバーを表示する配置モード
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ScrollbarMode {
    /// コンテンツの横/下にレイアウト領域を確保して配置(コンテンツが狭まる)
    Layout,
    /// コンテンツの最前面に重ねて配置(コンテンツ領域を侵食しない)
    #[default]
    Overlay,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ScrollbarDisplay {
    None,
    Always,
    #[default]
    Auto,
    Transient,
}

/// スクロールコンテナが保持するスタイリング設定
#[derive(Debug, Clone)]
pub struct ScrollbarStyle {
    /// スクロールバーの太さ(縦スクロールバー時は幅、横スクロールバー時は高さ)
    pub width: f32,
    /// 表示
    pub display: ScrollbarDisplay,
    /// スクロールバーの表示モード
    pub mode: ScrollbarMode,
    /// 縦スクロールバー(Y軸)のトラック(レール)部分のスタイル
    pub v_track: Option<ThisStyle>,
    /// 縦スクロールバー(Y軸)のサム(つまみ)部分のスタイル
    pub v_thumb: Option<ThisStyle>,
    /// 横スクロールバー(X軸)のトラック(レール)部分のスタイル
    pub h_track: Option<ThisStyle>,
    /// 横スクロールバー(X軸)のサム(つまみ)部分のスタイル
    pub h_thumb: Option<ThisStyle>,
}

impl Default for ScrollbarStyle {
    fn default() -> Self {
        Self {
            width: 10.0,
            display: ScrollbarDisplay::Auto,
            mode: ScrollbarMode::Overlay,
            v_track: None,
            v_thumb: None,
            h_track: None,
            h_thumb: None,
        }
    }
}

impl ScrollbarStyle {
    #[must_use]
    pub fn new(width: f32) -> Self {
        Self {
            width,
            display: ScrollbarDisplay::Auto,
            mode: ScrollbarMode::Overlay,
            v_track: None,
            v_thumb: None,
            h_track: None,
            h_thumb: None,
        }
    }

    #[must_use]
    pub fn mode(mut self, mode: ScrollbarMode) -> Self {
        self.mode = mode;
        self
    }

    #[must_use]
    pub fn display(mut self, display: ScrollbarDisplay) -> Self {
        self.display = display;
        self
    }

    #[must_use]
    pub fn v_track(mut self, style: ThisStyle) -> Self {
        self.v_track = Some(style);
        self
    }

    #[must_use]
    pub fn v_thumb(mut self, style: ThisStyle) -> Self {
        self.v_thumb = Some(style);
        self
    }

    #[must_use]
    pub fn h_track(mut self, style: ThisStyle) -> Self {
        self.h_track = Some(style);
        self
    }

    #[must_use]
    pub fn h_thumb(mut self, style: ThisStyle) -> Self {
        self.h_thumb = Some(style);
        self
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum ScrollbarComponent {
    VThumb,
    HThumb,
    VTrack,
    HTrack,
}

#[derive(Debug, Clone, Default)]
pub struct ScrollBarState {
    pub style: ScrollbarStyle,

    // レイアウトツリーに動的挿入される Element の EntityId
    pub v_track_id: Option<EntityId>,
    pub v_thumb_id: Option<EntityId>,
    pub h_track_id: Option<EntityId>,
    pub h_thumb_id: Option<EntityId>,

    // ホバー・ドラッグのランタイム状態
    pub v_thumb_hovered: bool,
    pub v_thumb_dragged: bool,
    pub h_thumb_hovered: bool,
    pub h_thumb_dragged: bool,

    pub drag_start_mouse: LayoutPoint,
    pub drag_start_offset: LayoutPoint,

    // 一時表示(Transient)モードの表示制御用
    pub last_scroll_time: Option<std::time::Instant>,
}

define_sparse_secondary!(pub struct ScrollbarStylesSparse(ScrollBarState));

pub(crate) struct ScrollbarStore {
    pub(crate) bar_styles: ScrollbarStylesSparse,
}

impl Default for ScrollbarStore {
    fn default() -> Self {
        Self::new()
    }
}

impl ScrollbarStore {
    #[must_use]
    #[inline]
    pub(crate) fn new() -> Self {
        Self {
            bar_styles: ScrollbarStylesSparse(SparseSecondaryMap::new()),
        }
    }

    #[inline]
    #[must_use]
    pub(crate) fn with_capacity(c: &CapacityConfig) -> Self {
        Self {
            bar_styles: ScrollbarStylesSparse(SparseSecondaryMap::with_capacity(c.bar_styles)),
        }
    }

    #[inline]
    pub(crate) fn clear(&mut self) {
        self.bar_styles.clear();
    }

    #[inline]
    pub(crate) fn despawn(&mut self, id: EntityId) {
        self.bar_styles.remove(id);
    }
}

impl ScrollbarStore {
    #[inline]
    pub(crate) fn get_scrollbar_dirty_ids(
        bar_styles: &mut ScrollbarStylesSparse,
    ) -> SmallVec<[EntityId; 4]> {
        let mut dirty_ids = SmallVec::<[EntityId; 4]>::new();

        for (id, state) in bar_styles.iter_mut() {
            if state.v_thumb_dragged || state.h_thumb_dragged {
                state.v_thumb_dragged = false;
                state.h_thumb_dragged = false;
                dirty_ids.push(id);
            }
        }
        dirty_ids
    }

    #[track_caller]
    pub(crate) fn hit_decision_element_scrollbar(
        target_id: EntityId,
        pointer_pos: LayoutPoint,
        win_last_size: Option<LayoutSize>,
        evt_interaction_states: &mut ActiveInteractionStates,
        topo_active_masks: &mut ActiveMasksSecondary,
        topo_parents: &ParentsSecondary,
        lay_dirty_entities: &mut DirtyLayoutEntitiesVec,
        lay_taffy_tree: &mut TaffyTreeEntityId,
        bar_styles: &mut ScrollbarStylesSparse,
        lay_taffy_nodes: &TaffyNodesSecondary,
        lay_resolved_basic: &ResolvedBasicSecondary,
        rnd_dirty_entities: &mut DirtyRenderEntitiesVec,
        sc_offsets: &mut ScrollOffsetsSecondary,
        out_rects: &RectsSecondary,
        sc_sizes: &ScrollSizesSecondary,
        debug: &mut DebugStore,
    ) -> bool {
        let Some((c_id, component)) = bar_styles.iter().find_map(|(c_id, sb_state)| {
            if sb_state.v_thumb_id == Some(target_id) {
                Some((c_id, ScrollbarComponent::VThumb))
            } else if sb_state.h_thumb_id == Some(target_id) {
                Some((c_id, ScrollbarComponent::HThumb))
            } else if sb_state.v_track_id == Some(target_id) {
                Some((c_id, ScrollbarComponent::VTrack))
            } else if sb_state.h_track_id == Some(target_id) {
                Some((c_id, ScrollbarComponent::HTrack))
            } else {
                None
            }
        }) else {
            return false;
        };

        // 親スクロールコンテナ
        let sb_state = bar_styles.at(c_id);
        let container_rect = out_rects.find_or_default(c_id, debug);
        let scroll_size = sc_sizes.find_or_default(c_id, debug);
        let offset = sc_offsets.find_or_default(c_id, debug);

        match component {
            ScrollbarComponent::VThumb | ScrollbarComponent::HThumb => {
                // サムをクリックした場合:ドラッグを開始
                if let Some(st) = bar_styles.find_mut(c_id) {
                    if component == ScrollbarComponent::VThumb {
                        st.v_thumb_dragged = true;
                    } else {
                        st.h_thumb_dragged = true;
                    }
                    st.drag_start_mouse = pointer_pos;
                    st.drag_start_offset = offset;
                }
                evt_interaction_states.pressed = Some(target_id);
                RenderStore::mark_render_dirty(target_id, topo_active_masks, rnd_dirty_entities);
            }
            ScrollbarComponent::VTrack | ScrollbarComponent::HTrack => {
                // レールをクリックした場合:ダイレクトジャンプスクロールを実行
                let is_vertical = component == ScrollbarComponent::VTrack;
                let thumb_id = if is_vertical {
                    sb_state.v_thumb_id
                } else {
                    sb_state.h_thumb_id
                };

                // ヒット先があるなら Some のはず
                let track_rect = out_rects.find_or_default(target_id, debug);
                let thumb_rect = thumb_id
                    .and_then(|i| out_rects.find(i).copied())
                    .unwrap_or_default();
                let visible_size = WindowStore::calc_visible_size(container_rect, win_last_size);

                // 縦・横の計算用パラメータ
                let (pointer_coord, track_coord, track_len, thumb_len, scroll_total, visible_total) =
                    if is_vertical {
                        (
                            pointer_pos.y,
                            track_rect.y,
                            track_rect.height,
                            thumb_rect.height,
                            scroll_size.height,
                            visible_size.height,
                        )
                    } else {
                        (
                            pointer_pos.x,
                            track_rect.x,
                            track_rect.width,
                            thumb_rect.width,
                            scroll_size.width,
                            visible_size.width,
                        )
                    };

                let relative_pos = pointer_coord - track_coord;
                let track_range = track_len - thumb_len;
                let scroll_ratio = if track_range > 0.0 {
                    ((relative_pos - thumb_len * 0.5) / track_range).clamp(0.0, 1.0)
                } else {
                    0.0
                };

                let target_val = scroll_ratio * (scroll_total - visible_total);
                let (target_x, target_y) = if is_vertical {
                    (offset.x, target_val)
                } else {
                    (target_val, offset.y)
                };

                ScrollStore::scroll_to(
                    c_id,
                    target_x,
                    target_y,
                    win_last_size,
                    topo_active_masks,
                    topo_parents,
                    lay_dirty_entities,
                    lay_taffy_tree,
                    bar_styles,
                    lay_taffy_nodes,
                    lay_resolved_basic,
                    sc_offsets,
                    out_rects,
                    sc_sizes,
                    debug,
                );

                let new_offset = sc_offsets.find_or_default(c_id, debug);
                if let Some(st) = bar_styles.find_mut(c_id) {
                    if is_vertical {
                        st.v_thumb_dragged = true;
                    } else {
                        st.h_thumb_dragged = true;
                    }
                    st.drag_start_mouse = pointer_pos;
                    st.drag_start_offset = new_offset;
                }

                evt_interaction_states.pressed = thumb_id;
                if let Some(tid) = thumb_id {
                    RenderStore::mark_render_dirty(tid, topo_active_masks, rnd_dirty_entities);
                }
            }
        }
        true
    }

    // 表示状態と不透明度の計算
    #[inline]
    fn calculate_visibility_opacity(
        display: ScrollbarDisplay,
        last_scroll_time: Option<Instant>,
        show_bar: bool,
    ) -> (bool, f32) {
        if !show_bar || display == ScrollbarDisplay::None {
            return (false, 1.0);
        }
        match display {
            ScrollbarDisplay::Always | ScrollbarDisplay::Auto => (true, 1.0),
            ScrollbarDisplay::Transient => {
                let Some(last) = last_scroll_time else {
                    return (false, 1.0);
                };

                let elapsed = last.elapsed();
                if elapsed < Duration::from_secs(1) {
                    (true, 1.0)
                } else if elapsed < Duration::from_millis(1500) {
                    let opacity = 1.0 - (elapsed.as_secs_f32() - 1.0) / 0.5;
                    (true, opacity)
                } else {
                    (false, 1.0)
                }
            }
            ScrollbarDisplay::None => (false, 1.0),
        }
    }

    // トラック有効長の計算
    #[inline]
    fn calculate_track_len(
        visible_dim: f32,
        border_start: f32,
        border_end: f32,
        show_other: bool,
        scrollbar_width: f32,
    ) -> f32 {
        let extra = if show_other { scrollbar_width } else { 0.0 };
        (visible_dim - border_start - border_end - extra).max(0.0)
    }

    // つまみの物理サイズと位置の計算
    #[inline]
    fn calculate_thumb_geometry(
        track_len: f32,
        visible_len: f32,
        scroll_len: f32,
        current_scroll_val: f32,
        scrollbar_width: f32,
        ext: &ExtractedThumb,
    ) -> (f32, f32, f32, f32) {
        let view_ratio = if scroll_len > 0.0 {
            (visible_len / scroll_len).min(1.0)
        } else {
            1.0
        };

        let calculated_len = ext.initial_len * view_ratio;
        let thumb_len = calculated_len
            .max(ext.min_len)
            .min(ext.max_len)
            .min(track_len);

        let scroll_ratio = if scroll_len > visible_len {
            (current_scroll_val / (scroll_len - visible_len)).clamp(0.0, 1.0)
        } else {
            0.0
        };

        let max_thumb_pos = (track_len - thumb_len - ext.margin_start - ext.margin_end).max(0.0);
        let thumb_main_pos = max_thumb_pos * scroll_ratio;

        let thumb_cross_len = if let Some(w) = ext.cross_size_override {
            w.min(scrollbar_width)
        } else {
            scrollbar_width
        };

        let thumb_cross_pos = if ext.pad_end > 0.0 {
            scrollbar_width - thumb_cross_len - ext.pad_end
        } else if ext.pad_start > 0.0 {
            ext.pad_start
        } else {
            (scrollbar_width - thumb_cross_len) * 0.5
        };

        (thumb_len, thumb_main_pos, thumb_cross_len, thumb_cross_pos)
    }

    #[track_caller]
    pub(crate) fn sync_bar_styles(
        win_last_size: Option<LayoutSize>,
        topo_active_masks: &ActiveMasksSecondary,
        topo_parents: &ParentsSecondary,
        lay_taffy_tree: &mut TaffyTreeEntityId,
        lay_basic: &mut BasicLayoutsSecondary,
        lay_base_basic: &mut BaseBasicLayoutsSecondary,
        lay_resolved_basic: &mut ResolvedBasicSecondary,
        lay_resolved_flex: &mut ResolvedFlexSecondary,
        lay_resolved_grid: &mut ResolvedGridSparse,
        lay_taffy_nodes: &TaffyNodesSecondary,
        lay_flex: &FlexLayoutsSecondary,
        lay_grid: &GridLayoutsSparse,
        bar_styles: &ScrollbarStylesSparse,
        rnd_visual: &mut VisualPropertiesSecondary,
        rnd_base_visual: &mut BaseVisualPropertiesSecondary,
        rnd_interaction: &InteractionPropertiesSecondary,
        rnd_active_transitions: &ActiveTransitionsSparse,
        out_rects: &RectsSecondary,
        sc_offsets: &ScrollOffsetsSecondary,
        sc_sizes: &ScrollSizesSecondary,
        debug: &mut DebugStore,
    ) {
        let scrollbar_ids: Vec<EntityId> = bar_styles.keys().collect();

        for id in scrollbar_ids {
            let sb_state = bar_styles.at(id);
            let rect = out_rects.find_or_default(id, debug);
            let scroll_size = sc_sizes.find_or_default(id, debug);
            let current_scroll = sc_offsets.find_or_default(id, debug);

            let resolved_geom =
                ResolvedGeometry::resolved(id, sc_offsets, lay_resolved_basic, out_rects, debug);
            let border = resolved_geom.border;

            let visible_size = WindowStore::calc_visible_size(rect, win_last_size);
            let content_size = resolved_geom.inner_content_size(visible_size);

            let show_v = scroll_size.height > content_size.height;
            let show_h = scroll_size.width > content_size.width;

            // トラックデフォルト長の計算
            let track_h_default = ScrollbarStore::calculate_track_len(
                visible_size.height,
                border.top,
                border.bottom,
                show_h,
                sb_state.style.width,
            );
            let track_w = ScrollbarStore::calculate_track_len(
                visible_size.width,
                border.left,
                border.right,
                show_v,
                sb_state.style.width,
            );

            let mut ctx = ScrollbarSyncContext {
                topo_active_masks,
                topo_parents,
                lay_taffy_tree,
                lay_basic,
                lay_base_basic,
                lay_resolved_basic,
                lay_resolved_flex,
                lay_resolved_grid,
                lay_taffy_nodes,
                lay_flex,
                lay_grid,
                bar_styles,
                rnd_visual,
                rnd_base_visual,
                rnd_interaction,
                rnd_active_transitions,
                debug,
            };

            // 縦トラック (V-Track) の同期
            if let Some(v_track) = sb_state.v_track_id {
                let (visible, opacity) = ScrollbarStore::calculate_visibility_opacity(
                    sb_state.style.display,
                    sb_state.last_scroll_time,
                    show_v,
                );
                if visible {
                    let mut track_h = track_h_default;
                    if let Some(ref track_style) = sb_state.style.v_track
                        && let Val::Px(val) = track_style.inner.basic_layout.size.height
                    {
                        track_h = val;
                    }
                    ctx.update_el(
                        v_track,
                        Size::new(Val::Px(sb_state.style.width), Val::Px(track_h)),
                        Rect::new(Val::Px(0.0), Val::Px(0.0), Val::Auto, Val::Auto),
                        opacity,
                    );
                } else {
                    ctx.hide_el(v_track);
                }
            }

            // 縦つまみ (V-Thumb) の同期
            if let Some(v_thumb) = sb_state.v_thumb_id {
                let (visible, opacity) = ScrollbarStore::calculate_visibility_opacity(
                    sb_state.style.display,
                    sb_state.last_scroll_time,
                    show_v,
                );
                if visible {
                    let mut ext = ExtractedThumb {
                        initial_len: track_h_default,
                        min_len: 24.0,
                        max_len: track_h_default,
                        margin_start: 0.0,
                        margin_end: 0.0,
                        pad_start: 0.0,
                        pad_end: 0.0,
                        cross_size_override: None,
                    };

                    if let Some(ref thumb_style) = sb_state.style.v_thumb {
                        let layout = &thumb_style.inner.basic_layout;
                        if let Val::Px(val) = layout.size.height {
                            ext.initial_len = val;
                        }
                        if let Val::Px(val) = layout.min_size.height {
                            ext.min_len = val;
                        }
                        if let Val::Px(val) = layout.max_size.height {
                            ext.max_len = val;
                        }
                        if let Val::Px(val) = layout.margin.top {
                            ext.margin_start = val;
                        }
                        if let Val::Px(val) = layout.margin.bottom {
                            ext.margin_end = val;
                        }
                        if let Length::Px(val) = layout.padding.left {
                            ext.pad_start = val;
                        }
                        if let Length::Px(val) = layout.padding.right {
                            ext.pad_end = val;
                        }
                        if let Val::Px(val) = layout.size.width {
                            ext.cross_size_override = Some(val);
                        }
                    }

                    let (h, y, w, x) = ScrollbarStore::calculate_thumb_geometry(
                        track_h_default,
                        visible_size.height,
                        scroll_size.height,
                        current_scroll.y,
                        sb_state.style.width,
                        &ext,
                    );

                    ctx.update_el(
                        v_thumb,
                        Size::new(Val::Px(w), Val::Px(h)),
                        Rect::new(Val::Px(y), Val::Auto, Val::Auto, Val::Px(x)),
                        opacity,
                    );
                } else {
                    ctx.hide_el(v_thumb);
                }
            }

            // 横トラック (H-Track) の同期
            if let Some(h_track) = sb_state.h_track_id {
                let (visible, opacity) = ScrollbarStore::calculate_visibility_opacity(
                    sb_state.style.display,
                    sb_state.last_scroll_time,
                    show_h,
                );
                if visible {
                    ctx.update_el(
                        h_track,
                        Size::new(Val::Px(track_w), Val::Px(sb_state.style.width)),
                        Rect::new(Val::Auto, Val::Auto, Val::Px(0.0), Val::Px(0.0)),
                        opacity,
                    );
                } else {
                    ctx.hide_el(h_track);
                }
            }

            // 横つまみ (H-Thumb) の同期
            if let Some(h_thumb) = sb_state.h_thumb_id {
                let (visible, opacity) = ScrollbarStore::calculate_visibility_opacity(
                    sb_state.style.display,
                    sb_state.last_scroll_time,
                    show_h,
                );
                if visible {
                    let mut ext = ExtractedThumb {
                        initial_len: track_w,
                        min_len: 24.0,
                        max_len: track_w,
                        margin_start: 0.0,
                        margin_end: 0.0,
                        pad_start: 0.0,
                        pad_end: 0.0,
                        cross_size_override: None,
                    };

                    if let Some(ref thumb_style) = sb_state.style.h_thumb {
                        let layout = &thumb_style.inner.basic_layout;
                        if let Val::Px(val) = layout.size.width {
                            ext.initial_len = val;
                        }
                        if let Val::Px(val) = layout.min_size.width {
                            ext.min_len = val;
                        }
                        if let Val::Px(val) = layout.max_size.width {
                            ext.max_len = val;
                        }
                        if let Val::Px(val) = layout.margin.left {
                            ext.margin_start = val;
                        }
                        if let Val::Px(val) = layout.margin.right {
                            ext.margin_end = val;
                        }
                        if let Length::Px(val) = layout.padding.top {
                            ext.pad_start = val;
                        }
                        if let Length::Px(val) = layout.padding.bottom {
                            ext.pad_end = val;
                        }
                        if let Val::Px(val) = layout.size.height {
                            ext.cross_size_override = Some(val);
                        }
                    }

                    let (w, x, h, y) = ScrollbarStore::calculate_thumb_geometry(
                        track_w,
                        visible_size.width,
                        scroll_size.width,
                        current_scroll.x,
                        sb_state.style.width,
                        &ext,
                    );

                    ctx.update_el(
                        h_thumb,
                        Size::new(Val::Px(w), Val::Px(h)),
                        Rect::new(Val::Px(y), Val::Auto, Val::Auto, Val::Px(x)),
                        opacity,
                    );
                } else {
                    ctx.hide_el(h_thumb);
                }
            }
        }
    }
}

// つまみ(Thumb)計算用の入力パラメータ
pub(crate) struct ExtractedThumb {
    initial_len: f32,
    min_len: f32,
    max_len: f32,
    margin_start: f32,
    margin_end: f32,
    pad_start: f32,
    pad_end: f32,
    cross_size_override: Option<f32>,
}

pub(crate) struct ScrollbarSyncContext<'a> {
    pub topo_active_masks: &'a ActiveMasksSecondary,
    pub topo_parents: &'a ParentsSecondary,
    pub lay_taffy_tree: &'a mut TaffyTreeEntityId,
    pub lay_basic: &'a mut BasicLayoutsSecondary,
    pub lay_base_basic: &'a mut BaseBasicLayoutsSecondary,
    pub lay_resolved_basic: &'a mut ResolvedBasicSecondary,
    pub lay_resolved_flex: &'a mut ResolvedFlexSecondary,
    pub lay_resolved_grid: &'a mut ResolvedGridSparse,
    pub lay_taffy_nodes: &'a TaffyNodesSecondary,
    pub lay_flex: &'a FlexLayoutsSecondary,
    pub lay_grid: &'a GridLayoutsSparse,
    pub bar_styles: &'a ScrollbarStylesSparse,
    pub rnd_visual: &'a mut VisualPropertiesSecondary,
    pub rnd_base_visual: &'a mut BaseVisualPropertiesSecondary,
    pub rnd_interaction: &'a InteractionPropertiesSecondary,
    pub rnd_active_transitions: &'a ActiveTransitionsSparse,
    pub debug: &'a mut DebugStore,
}

impl ScrollbarSyncContext<'_> {
    #[inline]
    pub fn update_el(&mut self, el_id: EntityId, size: Size<Val>, rect: Rect<Val>, opacity: f32) {
        ScrollbarStore::update_scrollbar_element(
            el_id,
            size,
            rect,
            opacity,
            self.topo_active_masks,
            self.topo_parents,
            self.lay_taffy_tree,
            self.lay_basic,
            self.lay_base_basic,
            self.lay_resolved_basic,
            self.lay_resolved_flex,
            self.lay_resolved_grid,
            self.lay_taffy_nodes,
            self.lay_flex,
            self.lay_grid,
            self.bar_styles,
            self.rnd_visual,
            self.rnd_base_visual,
            self.rnd_interaction,
            self.rnd_active_transitions,
            self.debug,
        );
    }
    #[inline]
    pub fn hide_el(&mut self, el_id: EntityId) {
        ScrollbarStore::hide_scrollbar_element(
            el_id,
            self.lay_taffy_tree,
            self.lay_basic,
            self.lay_base_basic,
            self.lay_taffy_nodes,
            self.debug,
        );
    }
}

impl ScrollbarStore {
    /// スクロールバー用要素のレイアウト情報を同期して更新。
    fn update_scrollbar_element_layout(
        id: EntityId,
        size: Size<Val>,
        inset: Rect<Val>,
        lay_basic: &mut BasicLayoutsSecondary,
        lay_base_basic: &mut BaseBasicLayoutsSecondary,
    ) {
        let layouts = [lay_basic.find_mut(id), lay_base_basic.find_mut(id)];

        for layout in layouts.into_iter().flatten() {
            layout.display = Display::Flex;
            layout.size = size;
            layout.inset = inset;
        }
    }

    /// スクロールバー用要素の不透明度(解決値と静的ベース値)を同時同期して更新します。
    #[inline]
    fn update_scrollbar_element_opacity(
        id: EntityId,
        opacity: f32,
        rnd_visual: &mut VisualPropertiesSecondary,
        rnd_base_visual: &mut BaseVisualPropertiesSecondary,
    ) {
        let visuals = [rnd_visual.find_mut(id), rnd_base_visual.find_mut(id)];

        for vis in visuals.into_iter().flatten() {
            vis.opacity = Some(opacity);
        }
    }

    /// スクロールバー用要素(TrackやThumb)のレイアウト、不透明度、Taffyスタイルへの反映を一括して同期更新します。
    #[inline]
    fn update_scrollbar_element(
        id: EntityId,
        size: Size<Val>,
        inset: Rect<Val>,
        opacity: f32,
        topo_active_masks: &ActiveMasksSecondary,
        topo_parents: &ParentsSecondary,
        lay_taffy_tree: &mut TaffyTreeEntityId,
        lay_basic: &mut BasicLayoutsSecondary,
        lay_base_basic: &mut BaseBasicLayoutsSecondary,
        lay_resolved_basic: &mut ResolvedBasicSecondary,
        lay_resolved_flex: &mut ResolvedFlexSecondary,
        lay_resolved_grid: &mut ResolvedGridSparse,
        lay_taffy_nodes: &TaffyNodesSecondary,
        lay_flex: &FlexLayoutsSecondary,
        lay_grid: &GridLayoutsSparse,
        bar_styles: &ScrollbarStylesSparse,
        rnd_visual: &mut VisualPropertiesSecondary,
        rnd_base_visual: &mut BaseVisualPropertiesSecondary,
        rnd_interaction: &InteractionPropertiesSecondary,
        rnd_active_transitions: &ActiveTransitionsSparse,
        debug: &mut DebugStore,
    ) {
        ScrollbarStore::update_scrollbar_element_layout(id, size, inset, lay_basic, lay_base_basic);
        ScrollbarStore::update_scrollbar_element_opacity(id, opacity, rnd_visual, rnd_base_visual);

        // 変更されたスクロールバー要素のキャッシュを更新
        LayoutStore::update_resolved_active_layout_cache(
            id,
            topo_active_masks,
            topo_parents,
            lay_resolved_basic,
            lay_resolved_flex,
            lay_resolved_grid,
            lay_basic,
            lay_flex,
            lay_grid,
            rnd_visual,
            rnd_interaction,
            rnd_active_transitions,
            debug,
        );

        let basic = lay_resolved_basic.find_or(id, &DEFAULT_BASIC, debug);
        let flex = lay_resolved_flex.find_or(id, &DEFAULT_FLEX, debug);
        let grid = lay_resolved_grid.find(id); // Grid実装時用

        LayoutStore::set_taffy_style(
            id,
            basic,
            flex,
            grid,
            lay_taffy_tree,
            lay_taffy_nodes,
            bar_styles,
            debug,
        );
    }

    /// スクロールバー用要素をレイアウト上から安全に隠します。
    #[track_caller]
    #[inline]
    fn hide_scrollbar_element(
        id: EntityId,
        lay_taffy_tree: &mut TaffyTreeEntityId,
        lay_basic: &mut BasicLayoutsSecondary,
        lay_base_basic: &mut BaseBasicLayoutsSecondary,
        lay_taffy_nodes: &TaffyNodesSecondary,
        debug: &mut DebugStore,
    ) {
        let layouts = [lay_basic.find_mut(id), lay_base_basic.find_mut(id)];

        for layout in layouts.into_iter().flatten() {
            layout.display = Display::None;
        }

        // 非表示パスで、Taffy側ノードスタイルを確実に Display::None にして同期する
        let node_id = *lay_taffy_nodes.at(id);
        lay_taffy_tree
            .set_style(
                node_id,
                taffy::Style {
                    display: taffy::Display::None,
                    ..Default::default()
                },
            )
            .unwrap_or_trace(Some(id), debug);
    }
}