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
901
902
903
904
905
906
907
908
909
910
911
912
913
use super::*;
/// Fluent interactive selection and theme helpers for `EuvBreadcrumb`.
impl EuvBreadcrumb {
/// Binds a reactive signal capturing the selected breadcrumb segment index when clicked.
///
/// # Arguments
///
/// - `Self` - Source breadcrumb.
/// - `Signal<usize>` - Active segment index signal.
///
/// # Returns
///
/// - `Self` - Interactive breadcrumb trail.
#[must_use]
pub fn active(mut self, active: Signal<usize>) -> Self {
self.set_active(Some(active));
self
}
/// Resolves `(row_bg, text_fg)` for an interactive breadcrumb segment.
///
/// # Arguments
///
/// - `ThemePalette` - Live theme palette.
/// - `bool` - Whether this segment is active.
/// - `bool` - Whether the pointer is currently hovering this segment.
///
/// # Returns
///
/// - `(Color, Color)` - Paired segment background and readable foreground color.
#[must_use]
pub fn segment_colors(palette: ThemePalette, active: bool, hovered: bool) -> (Color, Color) {
if active {
let base: SurfaceColors = palette.surface(SurfaceRole::Background);
(COLOR_TRANSPARENT, base.get_fg())
} else if hovered {
let accent: SurfaceColors = palette.surface(SurfaceRole::Accent);
(accent.get_bg(), accent.get_fg())
} else {
(COLOR_TRANSPARENT, palette.get_muted_foreground())
}
}
}
/// Declarative `#[component]` implementation for `EuvBreadcrumb`.
#[component]
/// Inherent implementation of [`EuvBreadcrumb`].
impl EuvBreadcrumb {
/// Renders `EuvBreadcrumb` into the ambient framework-managed `Context`.
///
/// # Arguments
///
/// - `&Self` - Component descriptor reference.
///
/// # Returns
///
/// - `NodeId` - Mounted root scene node handle.
fn render(&self) -> NodeId {
let mut ctx: Context = current_context();
let base_tokens: ThemeTokens = self.get_tokens();
let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
let p: ThemePalette = init_tok.get_palette();
let g: ThemeGeometry = init_tok.get_geometry();
let Self {
segments, active, ..
} = self;
let active: Option<Signal<usize>> = *active;
let hovered_idx: Signal<Option<usize>> = Signal::new(None);
let last_idx: usize = segments.len().saturating_sub(1);
let fg_active: Color = p.surface(SurfaceRole::Background).get_fg();
let fg_muted: Color = p.get_muted_foreground();
let mut seg_nodes: Vec<(NodeId, NodeId)> = Vec::with_capacity(segments.len());
let mut owned_nodes: Vec<NodeId> = Vec::with_capacity(segments.len() * 2);
let mut row_items: Vec<NodeId> = Vec::with_capacity(segments.len() * 2);
let init_active: usize = active
.map(|s: Signal<usize>| s.get_untracked())
.unwrap_or(0);
for (idx, seg) in segments.iter().enumerate() {
if let Some(active_sig) = active {
let is_cur: bool = init_active == idx;
let (init_bg, init_fg): (Color, Color) = Self::segment_colors(p, is_cur, false);
let mut row_id: NodeId = NodeId::default();
let mut txt_id: NodeId = NodeId::default();
let on_pick = move |_: &UiEvent| {
let _: bool = active_sig.set_if_changed(idx);
};
let on_enter = move |_: &UiEvent| {
let _: bool = hovered_idx.set_if_changed(Some(idx));
};
let on_leave = move |_: &UiEvent| {
if hovered_idx.get_untracked() == Some(idx) {
let _: bool = hovered_idx.set_if_changed(None);
}
};
let item: NodeId = view! {
row {
node_ref: row_id
style: {
padding: Edges::symmetric(2.0, 6.0)
radius: g.get_radius_sm()
background: init_bg
overflow: Overflow::Hidden
cursor: CursorIcon::Pointer
}
on_click: on_pick
on_pointer_enter: on_enter
on_pointer_leave: on_leave
text {
node_ref: txt_id
style: {
font_size: 13.0
font_weight: if is_cur { FontWeight::SemiBold } else { FontWeight::Normal }
color: init_fg
}
{ seg.as_str() }
}
}
};
owned_nodes.push(row_id);
owned_nodes.push(txt_id);
seg_nodes.push((row_id, txt_id));
row_items.push(item);
} else {
row_items.push(view! {
text {
style: {
font_size: 13.0
color: if idx == last_idx { fg_active } else { fg_muted }
}
{ seg.as_str() }
}
});
}
if idx < last_idx {
row_items.push(view! {
text {
style: {
font_size: 12.0
color: fg_muted
}
{ BREADCRUMB_SEP }
}
});
}
}
let root: NodeId = view! {
row {
style: {
flex_wrap: FlexWrap::Wrap
width: Length::Percent(100.0)
align: AlignItems::Center
gap: SPACE_XS
}
}
};
if !row_items.is_empty() {
ctx.append_many(root, row_items.as_slice());
}
if let Some(active_sig) = active {
let handle: SceneHandle = ctx.get_scene().clone();
let prev_state: Cell<(usize, Option<usize>, ThemeKey)> =
Cell::new((init_active, None, init_tok.key()));
let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
let next_idx: usize = active_sig.get();
let next_hover: Option<usize> = hovered_idx.get();
let live_tok: ThemeTokens = use_live_tokens(base_tokens);
let (old_idx, old_hover, old_key): (usize, Option<usize>, ThemeKey) =
prev_state.replace((next_idx, next_hover, live_tok.key()));
let theme_changed: bool = old_key != live_tok.key();
if !theme_changed && old_idx == next_idx && old_hover == next_hover {
return;
}
let live_p: ThemePalette = live_tok.get_palette();
handle.with_write(
|tree: &mut SceneTree| {
let dirty_l: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
let dirty_t: u16 = DIRTY_TEXT | DIRTY_PAINT;
for (idx, &(row_id, txt_id)) in seg_nodes.iter().enumerate() {
let affected: bool = theme_changed
|| idx == old_idx
|| idx == next_idx
|| old_hover == Some(idx)
|| next_hover == Some(idx);
if !affected {
continue;
}
let is_cur: bool = idx == next_idx;
let is_hov: bool = !is_cur && next_hover == Some(idx);
let (bg, fg): (Color, Color) =
Self::segment_colors(live_p, is_cur, is_hov);
if let Some(n) = tree.get_node_mut(row_id) {
n.get_mut_style().get_mut_paint().set_background(Some(bg));
tree.mark_dirty(row_id, DIRTY_PAINT);
}
if let Some(n) = tree.get_node_mut(txt_id) {
let weight_changed: bool = idx == old_idx || idx == next_idx;
n.get_mut_style()
.get_mut_text()
.set_weight(if is_cur {
FontWeight::SemiBold
} else {
FontWeight::Normal
})
.set_color(fg);
let mask: u16 = if weight_changed {
dirty_l | dirty_t
} else {
DIRTY_PAINT
};
tree.mark_dirty(txt_id, mask);
}
}
},
(),
);
})
});
ctx.get_scene().with_write(
|tree: &mut SceneTree| {
mark_theme_owned(tree, owned_nodes.as_slice());
tree.attach_scope(root, scope);
},
(),
);
}
root
}
}
/// Declarative `#[component]` implementation for `EuvPagination`.
#[component]
/// Inherent implementation of [`EuvPagination`].
impl EuvPagination {
/// Renders `EuvPagination` into the ambient framework-managed `Context`.
///
/// # Arguments
///
/// - `&Self` - Component descriptor reference.
///
/// # Returns
///
/// - `NodeId` - Mounted root scene node handle.
fn render(&self) -> NodeId {
let base_tokens: ThemeTokens = self.get_tokens();
let tokens: ThemeTokens = live_tokens_untracked(base_tokens);
let p: ThemePalette = tokens.get_palette();
let g: ThemeGeometry = tokens.get_geometry();
let current: Signal<usize> = self.get_current();
let total: usize = self.get_total_pages().max(1);
let bar_h: f32 = g.get_control_sm();
let on_prev = move |_: &UiEvent| {
let _: bool = current.update(|v: &mut usize| *v = v.saturating_sub(1).max(1));
};
let on_next = move |_: &UiEvent| {
let _: bool = current.update(|v: &mut usize| *v = v.saturating_add(1).min(total));
};
let page_text = move || -> String { format!("{} / {total}", current.get()) };
view! {
row {
style: {
flex_wrap: FlexWrap::Wrap
align: AlignItems::Center
gap: SPACE_SM
}
EuvButton {
label: PAGE_PREV_LABEL.to_string()
tokens: tokens
outline()
sm()
on_click: on_prev
}
row {
style: {
height: Length::Px(bar_h)
padding: Edges::symmetric(0.0, SPACE_SM)
justify: JustifyContent::Center
align: AlignItems::Center
}
text {
style: {
font_size: 13.0
font_weight: FontWeight::Medium
color: p.get_foreground()
}
{ page_text }
}
}
EuvButton {
label: PAGE_NEXT_LABEL.to_string()
tokens: tokens
outline()
sm()
on_click: on_next
}
}
}
}
}
/// Theme-aware contrast-safe paint helpers for `EuvSteps`.
impl EuvSteps {
/// Resolves `(circle_bg, circle_border, number_fg)` for a step indicator circle.
///
/// # Arguments
///
/// - `ThemeTokens` - Live theme tokens.
/// - `bool` - Whether the step is active or completed.
///
/// # Returns
///
/// - `(Color, Border, Color)` - Paired circle fill, border, and readable step number foreground.
#[must_use]
pub fn step_colors(tokens: ThemeTokens, done: bool) -> (Color, Border, Color) {
let p: ThemePalette = tokens.get_palette();
let g: ThemeGeometry = tokens.get_geometry();
if done {
let primary: SurfaceColors = p.surface(SurfaceRole::Primary);
(
primary.get_bg(),
Border::new(0.0, primary.get_border()),
primary.get_fg(),
)
} else {
let secondary: SurfaceColors = p.surface(SurfaceRole::Secondary);
(
secondary.get_bg(),
Border::new(g.get_border_width(), secondary.get_border()),
secondary.get_fg(),
)
}
}
}
/// Declarative `#[component]` implementation for `EuvSteps`.
#[component]
/// Inherent implementation of [`EuvSteps`].
impl EuvSteps {
/// Renders `EuvSteps` into the ambient framework-managed `Context`.
///
/// # Arguments
///
/// - `&Self` - Component descriptor reference.
///
/// # Returns
///
/// - `NodeId` - Mounted root scene node handle.
fn render(&self) -> NodeId {
let mut ctx: Context = current_context();
let base_tokens: ThemeTokens = self.get_tokens();
let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
let p: ThemePalette = init_tok.get_palette();
let active: Signal<usize> = self.get_active();
let Self { labels, .. } = self;
let init_active: usize = active.get_untracked();
let mut step_nodes: Vec<(NodeId, NodeId)> = Vec::with_capacity(labels.len());
let mut owned_nodes: Vec<NodeId> = Vec::with_capacity(labels.len() * 2);
let mut row_items: Vec<NodeId> = Vec::with_capacity(labels.len());
for (idx, lbl) in labels.iter().enumerate() {
let done: bool = init_active >= idx;
let (init_bg, init_border, init_fg): (Color, Border, Color) =
Self::step_colors(init_tok, done);
let mut circle_id: NodeId = NodeId::default();
let mut num_id: NodeId = NodeId::default();
let on_pick = move |_: &UiEvent| {
let _: bool = active.set_if_changed(idx);
};
let item: NodeId = view! {
row {
style: {
align: AlignItems::Center
gap: SPACE_SM
cursor: CursorIcon::Pointer
}
on_click: on_pick
row {
node_ref: circle_id
style: {
width: Length::Px(24.0)
height: Length::Px(24.0)
flex_shrink: 0.0
justify: JustifyContent::Center
align: AlignItems::Center
radius: RADIUS_PILL
overflow: Overflow::Hidden
border: (init_border.get_width(), init_border.get_color())
background: init_bg
}
text {
node_ref: num_id
style: {
font_size: 12.0
font_weight: FontWeight::Bold
color: init_fg
}
{ (idx + 1).to_string() }
}
}
text {
style: {
font_size: 13.0
font_weight: FontWeight::Medium
color: p.get_foreground()
}
{ lbl.as_str() }
}
}
};
owned_nodes.push(circle_id);
owned_nodes.push(num_id);
step_nodes.push((circle_id, num_id));
row_items.push(item);
}
let root: NodeId = view! {
row {
style: {
flex_wrap: FlexWrap::Wrap
width: Length::Percent(100.0)
gap: SPACE_MD
}
}
};
if !row_items.is_empty() {
ctx.append_many(root, row_items.as_slice());
}
let handle: SceneHandle = ctx.get_scene().clone();
let prev_state: Cell<(usize, ThemeKey)> = Cell::new((init_active, init_tok.key()));
let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
let next_active: usize = active.get();
let live_tok: ThemeTokens = use_live_tokens(base_tokens);
let (old_active, old_key): (usize, ThemeKey) =
prev_state.replace((next_active, live_tok.key()));
let theme_changed: bool = old_key != live_tok.key();
if !theme_changed && old_active == next_active {
return;
}
let lo: usize = if theme_changed {
0
} else {
old_active.min(next_active)
};
let hi: usize = if theme_changed {
step_nodes.len()
} else {
old_active.max(next_active).min(step_nodes.len())
};
handle.with_write(
|tree: &mut SceneTree| {
for (idx, &(circle_id, num_id)) in step_nodes
.iter()
.enumerate()
.skip(lo)
.take(hi.saturating_sub(lo).saturating_add(1))
{
let done: bool = next_active >= idx;
let (bg, border, fg): (Color, Border, Color) =
Self::step_colors(live_tok, done);
if let Some(n) = tree.get_node_mut(circle_id) {
n.get_mut_style()
.get_mut_paint()
.set_border(border)
.set_background(Some(bg));
tree.mark_dirty(circle_id, DIRTY_PAINT);
}
if let Some(n) = tree.get_node_mut(num_id) {
n.get_mut_style().get_mut_text().set_color(fg);
tree.mark_dirty(num_id, DIRTY_PAINT);
}
}
},
(),
);
})
});
ctx.get_scene().with_write(
|tree: &mut SceneTree| {
mark_theme_owned(tree, owned_nodes.as_slice());
tree.attach_scope(root, scope);
},
(),
);
root
}
}
/// Theme-aware contrast-safe header helpers for `EuvAccordion`.
impl EuvAccordion {
/// Resolves `(header_bg, title_fg)` for an accordion item header.
///
/// # Arguments
///
/// - `ThemePalette` - Live theme palette.
/// - `bool` - Whether the pointer is currently hovering the header.
///
/// # Returns
///
/// - `(Color, Color)` - Paired header background and readable title color.
#[must_use]
pub fn header_colors(palette: ThemePalette, hovered: bool) -> (Color, Color) {
if hovered {
let accent: SurfaceColors = palette.surface(SurfaceRole::Accent);
(accent.get_bg(), accent.get_fg())
} else {
let card: SurfaceColors = palette.surface(SurfaceRole::Card);
(COLOR_TRANSPARENT, card.get_fg())
}
}
}
/// Declarative `#[component]` implementation for `EuvAccordion`.
#[component]
/// Inherent implementation of [`EuvAccordion`].
impl EuvAccordion {
/// Renders `EuvAccordion` into the ambient framework-managed `Context`.
///
/// # Arguments
///
/// - `&Self` - Component descriptor reference.
///
/// # Returns
///
/// - `NodeId` - Mounted root scene node handle.
fn render(&self) -> NodeId {
let mut ctx: Context = current_context();
let base_tokens: ThemeTokens = self.get_tokens();
let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
let p: ThemePalette = init_tok.get_palette();
let g: ThemeGeometry = init_tok.get_geometry();
let open_index: Signal<Option<usize>> = self.get_open_index();
let hovered_idx: Signal<Option<usize>> = Signal::new(None);
let Self { items, .. } = self;
let init_open: Option<usize> = open_index.get_untracked();
let mut entries: Vec<(NodeId, NodeId, NodeId, NodeId)> = Vec::with_capacity(items.len());
let mut owned_nodes: Vec<NodeId> = Vec::with_capacity(items.len() * 3);
let mut cards: Vec<NodeId> = Vec::with_capacity(items.len());
for (idx, item) in items.iter().enumerate() {
let is_open: bool = init_open == Some(idx);
let (init_hdr_bg, init_title_fg): (Color, Color) = Self::header_colors(p, false);
let mut hdr_id: NodeId = NodeId::default();
let mut title_id: NodeId = NodeId::default();
let mut chev_id: NodeId = NodeId::default();
let mut body_id: NodeId = NodeId::default();
let on_toggle = move |_: &UiEvent| {
let _: bool = open_index.update(|cur: &mut Option<usize>| {
*cur = if *cur == Some(idx) { None } else { Some(idx) };
});
};
let on_enter = move |_: &UiEvent| {
let _: bool = hovered_idx.set_if_changed(Some(idx));
};
let on_leave = move |_: &UiEvent| {
if hovered_idx.get_untracked() == Some(idx) {
let _: bool = hovered_idx.set_if_changed(None);
}
};
let paint_chev = move |bounds: Rect, dl: &mut DisplayList| {
let live_p: ThemePalette = live_tokens_untracked(base_tokens).get_palette();
let icon: EuvIconName = if open_index.get_untracked() == Some(idx) {
EuvIconName::ChevronDown
} else {
EuvIconName::ArrowRight
};
draw_euv_icon(icon, bounds, dl, live_p.get_muted_foreground());
};
let card: NodeId = view! {
column {
style: {
width: Length::Percent(100.0)
background: p.get_card()
border: (g.get_border_width(), p.get_border())
radius: g.get_radius_md()
overflow: Overflow::Hidden
}
row {
node_ref: hdr_id
style: {
width: Length::Percent(100.0)
justify: JustifyContent::SpaceBetween
align: AlignItems::Center
gap: SPACE_SM
padding: Edges::symmetric(SPACE_SM, SPACE_MD)
background: init_hdr_bg
cursor: CursorIcon::Pointer
overflow: Overflow::Hidden
}
role: A11yRole::Button
a11y_label: item.get_title().clone()
on_click: on_toggle
on_pointer_enter: on_enter
on_pointer_leave: on_leave
text {
node_ref: title_id
style: {
flex_grow: 1.0
flex_shrink: 1.0
min_width: Length::Px(0.0)
font_size: 14.0
font_weight: FontWeight::SemiBold
color: init_title_fg
}
{ item.get_title().as_str() }
}
canvas {
node_ref: chev_id
style: {
width: Length::Px(12.0)
height: Length::Px(12.0)
flex_shrink: 0.0
}
painter: paint_chev
}
}
column {
node_ref: body_id
style: {
display: if is_open { Display::Flex } else { Display::None }
width: Length::Percent(100.0)
padding: Edges::symmetric(SPACE_SM, SPACE_MD)
}
text {
style: {
width: Length::Percent(100.0)
font_size: 13.0
color: p.get_muted_foreground()
}
{ item.get_content().as_str() }
}
}
}
};
owned_nodes.push(hdr_id);
owned_nodes.push(title_id);
owned_nodes.push(chev_id);
entries.push((hdr_id, title_id, chev_id, body_id));
cards.push(card);
}
let root: NodeId = view! {
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_XS
}
}
};
if !cards.is_empty() {
ctx.append_many(root, cards.as_slice());
}
let handle: SceneHandle = ctx.get_scene().clone();
let prev_state: Cell<(Option<usize>, Option<usize>, ThemeKey)> =
Cell::new((init_open, None, init_tok.key()));
let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
let next_open: Option<usize> = open_index.get();
let next_hover: Option<usize> = hovered_idx.get();
let live_tok: ThemeTokens = use_live_tokens(base_tokens);
let (old_open, old_hover, old_key): (Option<usize>, Option<usize>, ThemeKey) =
prev_state.replace((next_open, next_hover, live_tok.key()));
let theme_changed: bool = old_key != live_tok.key();
if !theme_changed && old_open == next_open && old_hover == next_hover {
return;
}
let live_p: ThemePalette = live_tok.get_palette();
handle.with_write(
|tree: &mut SceneTree| {
let dirty_l: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
if old_open != next_open {
if let Some(old_idx) = old_open
&& let Some(&(_, _, old_chev, old_body)) = entries.get(old_idx)
{
if let Some(n) = tree.get_node_mut(old_body) {
n.get_mut_style()
.get_mut_layout()
.set_display(Display::None);
tree.mark_dirty(old_body, dirty_l | DIRTY_PAINT);
}
tree.mark_dirty(old_chev, DIRTY_PAINT);
}
if let Some(new_idx) = next_open
&& let Some(&(_, _, new_chev, new_body)) = entries.get(new_idx)
{
if let Some(n) = tree.get_node_mut(new_body) {
n.get_mut_style()
.get_mut_layout()
.set_display(Display::Flex);
tree.mark_dirty(new_body, dirty_l | DIRTY_PAINT);
}
tree.mark_dirty(new_chev, DIRTY_PAINT);
}
}
for (idx, &(hdr_id, title_id, chev_id, _)) in entries.iter().enumerate() {
let paint_affected: bool =
theme_changed || old_hover == Some(idx) || next_hover == Some(idx);
if !paint_affected {
continue;
}
let (hdr_bg, title_fg): (Color, Color) =
Self::header_colors(live_p, next_hover == Some(idx));
if let Some(n) = tree.get_node_mut(hdr_id) {
n.get_mut_style()
.get_mut_paint()
.set_background(Some(hdr_bg));
tree.mark_dirty(hdr_id, DIRTY_PAINT);
}
if let Some(n) = tree.get_node_mut(title_id) {
n.get_mut_style().get_mut_text().set_color(title_fg);
tree.mark_dirty(title_id, DIRTY_PAINT);
}
if theme_changed {
tree.mark_dirty(chev_id, DIRTY_PAINT);
}
}
},
(),
);
})
});
ctx.get_scene().with_write(
|tree: &mut SceneTree| {
mark_theme_owned(tree, owned_nodes.as_slice());
tree.attach_scope(root, scope);
},
(),
);
root
}
}
/// Theme-aware contrast-safe row helpers for `EuvToc`.
impl EuvToc {
/// Resolves `(row_bg, label_fg)` for a table-of-contents row state.
///
/// # Arguments
///
/// - `ThemePalette` - Live theme palette.
/// - `bool` - Whether this heading is active.
/// - `bool` - Whether the pointer is currently hovering this row.
///
/// # Returns
///
/// - `(Color, Color)` - Paired row background and readable label color.
#[must_use]
pub fn row_colors(palette: ThemePalette, active: bool, hovered: bool) -> (Color, Color) {
if active {
let accent: SurfaceColors = palette.surface(SurfaceRole::Accent);
(accent.get_bg(), accent.get_fg())
} else if hovered {
let card: SurfaceColors = palette.surface(SurfaceRole::Card);
(card.get_bg(), card.get_fg())
} else {
let muted: SurfaceColors = palette.surface(SurfaceRole::Muted);
(COLOR_TRANSPARENT, muted.get_fg())
}
}
}
/// Declarative `#[component]` implementation for `EuvToc`.
#[component]
/// Inherent implementation of [`EuvToc`].
impl EuvToc {
/// Renders `EuvToc` into the ambient framework-managed `Context`.
///
/// # Arguments
///
/// - `&Self` - Component descriptor reference.
///
/// # Returns
///
/// - `NodeId` - Mounted root scene node handle.
fn render(&self) -> NodeId {
let mut ctx: Context = current_context();
let base_tokens: ThemeTokens = self.get_tokens();
let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
let p: ThemePalette = init_tok.get_palette();
let g: ThemeGeometry = init_tok.get_geometry();
let active: Signal<usize> = self.get_active();
let hovered_idx: Signal<Option<usize>> = Signal::new(None);
let Self { headings, .. } = self;
let init_active: usize = active.get_untracked();
let outer_radius: f32 = g.get_radius_lg();
let row_radius: f32 = concentric_radius(outer_radius, CONTROL_INSET);
let mut row_entries: Vec<(NodeId, NodeId)> = Vec::with_capacity(headings.len());
let mut owned_nodes: Vec<NodeId> = Vec::with_capacity(headings.len() * 2);
let mut row_nodes: Vec<NodeId> = Vec::with_capacity(headings.len());
for (idx, h) in headings.iter().enumerate() {
let is_cur: bool = init_active == idx;
let (init_bg, init_fg): (Color, Color) = Self::row_colors(p, is_cur, false);
let mut row_id: NodeId = NodeId::default();
let mut lbl_id: NodeId = NodeId::default();
let on_pick = move |_: &UiEvent| {
let _: bool = active.set_if_changed(idx);
};
let on_enter = move |_: &UiEvent| {
let _: bool = hovered_idx.set_if_changed(Some(idx));
};
let on_leave = move |_: &UiEvent| {
if hovered_idx.get_untracked() == Some(idx) {
let _: bool = hovered_idx.set_if_changed(None);
}
};
let row_node: NodeId = view! {
row {
node_ref: row_id
style: {
width: Length::Percent(100.0)
padding: Edges::symmetric(SPACE_XS, SPACE_SM)
radius: row_radius
background: init_bg
overflow: Overflow::Hidden
cursor: CursorIcon::Pointer
}
on_click: on_pick
on_pointer_enter: on_enter
on_pointer_leave: on_leave
text {
node_ref: lbl_id
style: {
flex_grow: 1.0
flex_shrink: 1.0
min_width: Length::Px(0.0)
font_size: 13.0
color: init_fg
overflow: Overflow::Hidden
text_overflow: TextOverflow::Ellipsis
}
{ h.as_str() }
}
}
};
owned_nodes.push(row_id);
owned_nodes.push(lbl_id);
row_entries.push((row_id, lbl_id));
row_nodes.push(row_node);
}
let root: NodeId = view! {
column {
style: {
width: Length::Percent(100.0)
padding: Edges::all(SPACE_MD)
gap: CONTROL_INSET
background: p.get_muted()
border: (g.get_border_width(), p.get_border())
radius: outer_radius
overflow: Overflow::Hidden
}
text {
style: {
width: Length::Percent(100.0)
font_size: 12.0
font_weight: FontWeight::Bold
color: p.get_muted_foreground()
}
{ TOC_HEADING_DEFAULT }
}
}
};
if !row_nodes.is_empty() {
ctx.append_many(root, row_nodes.as_slice());
}
let handle: SceneHandle = ctx.get_scene().clone();
let prev_state: Cell<(usize, Option<usize>, ThemeKey)> =
Cell::new((init_active, None, init_tok.key()));
let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
let next_active: usize = active.get();
let next_hover: Option<usize> = hovered_idx.get();
let live_tok: ThemeTokens = use_live_tokens(base_tokens);
let (old_active, old_hover, old_key): (usize, Option<usize>, ThemeKey) =
prev_state.replace((next_active, next_hover, live_tok.key()));
let theme_changed: bool = old_key != live_tok.key();
if !theme_changed && old_active == next_active && old_hover == next_hover {
return;
}
let live_p: ThemePalette = live_tok.get_palette();
handle.with_write(
|tree: &mut SceneTree| {
for (idx, &(row_id, lbl_id)) in row_entries.iter().enumerate() {
let affected: bool = theme_changed
|| idx == old_active
|| idx == next_active
|| old_hover == Some(idx)
|| next_hover == Some(idx);
if !affected {
continue;
}
let is_cur: bool = idx == next_active;
let is_hov: bool = !is_cur && next_hover == Some(idx);
let (bg, fg): (Color, Color) = Self::row_colors(live_p, is_cur, is_hov);
if let Some(n) = tree.get_node_mut(row_id) {
n.get_mut_style().get_mut_paint().set_background(Some(bg));
tree.mark_dirty(row_id, DIRTY_PAINT);
}
if let Some(n) = tree.get_node_mut(lbl_id) {
n.get_mut_style().get_mut_text().set_color(fg);
tree.mark_dirty(lbl_id, DIRTY_PAINT);
}
}
},
(),
);
})
});
ctx.get_scene().with_write(
|tree: &mut SceneTree| {
mark_theme_owned(tree, owned_nodes.as_slice());
tree.attach_scope(root, scope);
},
(),
);
root
}
}