Skip to main content

euv_ui/component/mermaid/
impl.rs

1use super::*;
2
3/// Fluent builder helpers for `EuvMermaid`.
4impl EuvMermaid {
5    /// Registers a callback invoked when the user clicks Copy on the Mermaid diagram header.
6    ///
7    /// # Arguments
8    ///
9    /// - `Self` - Source Mermaid component.
10    /// - `CallbackFn` - Callback receiving the raw Mermaid source text.
11    ///
12    /// # Returns
13    ///
14    /// - `Self` - Updated Mermaid component.
15    #[must_use]
16    pub fn on_copy<CallbackFn>(mut self, handler: CallbackFn) -> Self
17    where
18        CallbackFn: Fn(&str) + 'static,
19    {
20        self.set_on_copy(Some(Rc::new(handler)));
21        self
22    }
23}
24
25/// Declarative `#[component]` implementation for `EuvMermaid`.
26#[component]
27/// Inherent implementation of [`EuvMermaid`].
28impl EuvMermaid {
29    /// Renders a native vector Mermaid diagram with scrollable viewport, Copy button, and interactive Fullscreen modal.
30    ///
31    /// # Arguments
32    ///
33    /// - `&Self` - Mermaid component descriptor reference.
34    ///
35    /// # Returns
36    ///
37    /// - `NodeId` - Mounted Mermaid diagram card handle.
38    fn render(&self) -> NodeId {
39        let ctx: Context = current_context();
40        bind_fullscreen_context(&ctx);
41        let base_tokens: ThemeTokens = self.get_tokens();
42        let tokens: ThemeTokens = live_tokens_untracked(base_tokens);
43        let geometry: ThemeGeometry = tokens.get_geometry();
44        let ctrl_sm: f32 = geometry.get_control_sm();
45        let eva: CodeThemePalette = CodeThemePalette::resolve(tokens.get_mode());
46        let Self {
47            source,
48            style,
49            on_copy,
50            ..
51        } = self;
52        let diagram: Rc<MermaidDiagram> = Rc::new(parse_mermaid_diagram(source));
53        let nat_sz: Size = measure_mermaid_diagram(&diagram);
54        let badge: &'static str = mermaid_badge_label(&diagram);
55        let title_raw: String = mermaid_diagram_title(&diagram);
56        let header_title_rc: Option<Rc<str>> = if title_raw.is_empty() {
57            None
58        } else {
59            Some(Rc::from(title_raw))
60        };
61        let inline_max_h: f32 =
62            match style.map(|computed_style: Style| computed_style.get_layout().get_max_height()) {
63                Some(Length::Px(px)) if px > 0.0 => px,
64                _ => MERMAID_DEFAULT_MAX_HEIGHT,
65            };
66        let copied_sig: Signal<bool> = Signal::new(false);
67        let fs_open: Signal<bool> = Signal::new(false);
68        let source_rc: Rc<str> = Rc::from(source.as_str());
69        let copy_src: Rc<str> = Rc::clone(&source_rc);
70        let copy_cb: Option<MarkdownStrCallback> = on_copy.clone();
71        let ctx_copy: Context = ctx.clone();
72        let on_copy_click = move |_: &UiEvent| {
73            let _: bool = ctx_copy.copy_to_clipboard(&copy_src);
74            let _: bool = copied_sig.set(true);
75            if let Some(callback_fn) = copy_cb.as_ref() {
76                callback_fn(&copy_src);
77            }
78        };
79        let paint_inline_copy_icon = move |bounds: Rect, display_list: &mut DisplayList| {
80            let cur_tok: ThemeTokens = live_tokens_untracked(base_tokens);
81            let cur_eva: CodeThemePalette = CodeThemePalette::resolve(cur_tok.get_mode());
82            if copied_sig.get_untracked() {
83                draw_euv_icon(
84                    EuvIconName::Check,
85                    bounds,
86                    display_list,
87                    cur_tok.get_palette().get_success(),
88                );
89            } else {
90                draw_euv_icon(
91                    EuvIconName::Copy,
92                    bounds,
93                    display_list,
94                    cur_eva.get_function(),
95                );
96            }
97        };
98        let on_open_fs = move |_: &UiEvent| {
99            let _: bool = fs_open.set_if_changed(true);
100        };
101        let diag_inline: Rc<MermaidDiagram> = Rc::clone(&diagram);
102        let inline_cache: RefCell<Option<MermaidInlinePaintEntry>> = RefCell::new(None);
103        let paint_inline = move |bounds: Rect, draw_commands: &mut DisplayList| {
104            let live_tok: ThemeTokens = live_tokens_untracked(base_tokens);
105            let cur_scale: f32 = clamp_viewport_scale(draw_commands.get_scale_factor());
106            let scale_bits: u32 = cur_scale.to_bits();
107            if let Ok(guard) = inline_cache.try_borrow()
108                && let Some(cached) = guard.as_ref()
109                && cached.bounds == bounds
110                && cached.tokens == live_tok
111                && cached.scale_bits == scale_bits
112            {
113                draw_commands
114                    .get_mut_commands()
115                    .extend_from_slice(cached.commands.as_slice());
116                return;
117            }
118            let mut scratch: DisplayList = DisplayList::default();
119            scratch.reset(
120                Size::new(bounds.get_width(), bounds.get_height()),
121                cur_scale,
122            );
123            draw_mermaid_diagram(&diag_inline, bounds, &mut scratch, live_tok);
124            let cmds: Vec<DrawCommand> = take(scratch.get_mut_commands());
125            draw_commands
126                .get_mut_commands()
127                .extend_from_slice(cmds.as_slice());
128            if let Ok(mut guard) = inline_cache.try_borrow_mut() {
129                *guard = Some(MermaidInlinePaintEntry {
130                    bounds,
131                    tokens: live_tok,
132                    scale_bits,
133                    commands: cmds,
134                });
135            }
136        };
137        let paint_fs_icon = move |bounds: Rect, command_list: &mut DisplayList| {
138            let live_eva: CodeThemePalette =
139                CodeThemePalette::resolve(live_tokens_untracked(base_tokens).get_mode());
140            draw_code_fullscreen_icon(bounds, command_list, live_eva.get_function());
141        };
142        let diag_fs: Rc<MermaidDiagram> = Rc::clone(&diagram);
143        let fs_src_copy: Rc<str> = Rc::clone(&source_rc);
144        let fs_cb_copy: Option<MarkdownStrCallback> = on_copy.clone();
145        let fs_title_rc: Option<Rc<str>> = header_title_rc.clone();
146        let ctx_fs_copy: Context = ctx.clone();
147        let build_fs_panel = move || -> NodeId {
148            let live_tok: ThemeTokens = live_tokens_untracked(base_tokens);
149            let live_p: ThemePalette = live_tok.get_palette();
150            let live_g: ThemeGeometry = live_tok.get_geometry();
151            let modal_ctrl_h: f32 = live_g.get_control_sm();
152            let close_surface: SurfaceColors = live_p.surface(SurfaceRole::Primary);
153            let live_eva: CodeThemePalette = CodeThemePalette::resolve(live_tok.get_mode());
154            let diag_modal: Rc<MermaidDiagram> = Rc::clone(&diag_fs);
155            let modal_base_w: f32 = nat_sz.get_width().max(640.0);
156            let modal_base_h: f32 = nat_sz.get_height().max(320.0);
157            let zoom_sig: Signal<f32> = Signal::new(1.0);
158            let modal_cache: RefCell<Option<MermaidModalPaintEntry>> = RefCell::new(None);
159            let paint_modal = move |bounds: Rect, display_list_primary: &mut DisplayList| {
160                let cur_tok: ThemeTokens = live_tokens_untracked(base_tokens);
161                let cur_zoom: f32 = zoom_sig.get_untracked();
162                let zoom_bits: u32 = cur_zoom.to_bits();
163                let cur_scale: f32 = clamp_viewport_scale(display_list_primary.get_scale_factor());
164                let scale_bits: u32 = cur_scale.to_bits();
165                if let Ok(guard) = modal_cache.try_borrow()
166                    && let Some(cached) = guard.as_ref()
167                    && cached.bounds == bounds
168                    && cached.tokens == cur_tok
169                    && cached.zoom_bits == zoom_bits
170                    && cached.scale_bits == scale_bits
171                {
172                    display_list_primary
173                        .get_mut_commands()
174                        .extend_from_slice(cached.commands.as_slice());
175                    return;
176                }
177                let mut scratch: DisplayList = DisplayList::default();
178                scratch.reset(Size::new(modal_base_w, modal_base_h), cur_scale);
179                let base_rect: Rect =
180                    Rect::new(bounds.get_x(), bounds.get_y(), modal_base_w, modal_base_h);
181                draw_mermaid_diagram(&diag_modal, base_rect, &mut scratch, cur_tok);
182                scale_mermaid_display_list(
183                    scratch.get_mut_commands(),
184                    Point::new(bounds.get_x(), bounds.get_y()),
185                    cur_zoom,
186                );
187                let cmds: Vec<DrawCommand> = take(scratch.get_mut_commands());
188                display_list_primary
189                    .get_mut_commands()
190                    .extend_from_slice(cmds.as_slice());
191                if let Ok(mut guard) = modal_cache.try_borrow_mut() {
192                    *guard = Some(MermaidModalPaintEntry {
193                        bounds,
194                        tokens: cur_tok,
195                        zoom_bits,
196                        scale_bits,
197                        commands: cmds,
198                    });
199                }
200            };
201            let modal_src: Rc<str> = Rc::clone(&fs_src_copy);
202            let modal_cb: Option<MarkdownStrCallback> = fs_cb_copy.clone();
203            let ctx_m: Context = ctx_fs_copy.clone();
204            let modal_copy_icon_cell: Rc<Cell<NodeId>> = Rc::new(Cell::new(NodeId::default()));
205            let modal_copy_icon_cb: Rc<Cell<NodeId>> = Rc::clone(&modal_copy_icon_cell);
206            let modal_copy_btn_cell: Rc<Cell<NodeId>> = Rc::new(Cell::new(NodeId::default()));
207            let modal_copy_btn_cb: Rc<Cell<NodeId>> = Rc::clone(&modal_copy_btn_cell);
208            let on_modal_copy = move |_: &UiEvent| {
209                let _: bool = ctx_m.copy_to_clipboard(&modal_src);
210                let _: bool = copied_sig.set(true);
211                let m_icon: NodeId = modal_copy_icon_cb.get();
212                let m_btn: NodeId = modal_copy_btn_cb.get();
213                ctx_m.get_scene().with_write(
214                    |tree: &mut SceneTree| {
215                        if let Some(item_count) = tree.get_node_mut(m_btn) {
216                            *item_count.get_mut_a11y() = Some(A11yProps::new(
217                                A11yRole::Button,
218                                MERMAID_BUTTON_COPIED.to_string(),
219                            ));
220                        }
221                        tree.mark_dirty(m_icon, DIRTY_PAINT);
222                    },
223                    (),
224                );
225                if let Some(handler_cb) = modal_cb.as_ref() {
226                    handler_cb(&modal_src);
227                }
228            };
229            let paint_modal_copy_icon = move |bounds: Rect, draw_list: &mut DisplayList| {
230                let cur_tok: ThemeTokens = live_tokens_untracked(base_tokens);
231                let cur_eva: CodeThemePalette = CodeThemePalette::resolve(cur_tok.get_mode());
232                if copied_sig.get_untracked() {
233                    draw_euv_icon(
234                        EuvIconName::Check,
235                        bounds,
236                        draw_list,
237                        cur_tok.get_palette().get_success(),
238                    );
239                } else {
240                    draw_euv_icon(EuvIconName::Copy, bounds, draw_list, cur_eva.get_function());
241                }
242            };
243            let paint_close_icon = move |bounds: Rect, display_list_next: &mut DisplayList| {
244                let cur_p: ThemePalette = live_tokens_untracked(base_tokens).get_palette();
245                let close_fg: Color = cur_p.surface(SurfaceRole::Primary).get_fg();
246                draw_euv_icon(EuvIconName::Minimize, bounds, display_list_next, close_fg);
247            };
248            let on_close_fs = move |_: &UiEvent| {
249                let _: bool = fs_open.set_if_changed(false);
250            };
251            let on_zoom_out = move |_: &UiEvent| {
252                let _: bool = {
253                    if let Some(mut updated_value) = zoom_sig.try_get_untracked() {
254                        {
255                            let pos_z: &mut f32 = &mut updated_value;
256                            *pos_z = (*pos_z - MERMAID_ZOOM_STEP)
257                                .clamp(MERMAID_ZOOM_MIN, MERMAID_ZOOM_MAX);
258                        }
259                        zoom_sig.set(updated_value)
260                    } else {
261                        false
262                    }
263                };
264            };
265            let on_zoom_in = move |_: &UiEvent| {
266                let _: bool = {
267                    if let Some(mut updated_value) = zoom_sig.try_get_untracked() {
268                        {
269                            let coord_z: &mut f32 = &mut updated_value;
270                            *coord_z = (*coord_z + MERMAID_ZOOM_STEP)
271                                .clamp(MERMAID_ZOOM_MIN, MERMAID_ZOOM_MAX);
272                        }
273                        zoom_sig.set(updated_value)
274                    } else {
275                        false
276                    }
277                };
278            };
279            let body_scroll_cell: Rc<Cell<NodeId>> = Rc::new(Cell::new(NodeId::default()));
280            let body_scroll_reset: Rc<Cell<NodeId>> = Rc::clone(&body_scroll_cell);
281            let scene_reset: SceneHandle = ctx_fs_copy.get_scene().clone();
282            let on_zoom_reset = move |_: &UiEvent| {
283                let _: bool = zoom_sig.set_if_changed(1.0);
284                let node_id: NodeId = body_scroll_reset.get();
285                scene_reset.with_write(
286                    |tree: &mut SceneTree| {
287                        tree.set_scroll_offset(node_id, Point::default());
288                    },
289                    (),
290                );
291            };
292            let init_copy_a11y: String = if copied_sig.get_untracked() {
293                MERMAID_BUTTON_COPIED.to_string()
294            } else {
295                MERMAID_BUTTON_COPY.to_string()
296            };
297            let modal_title_str: &str = fs_title_rc.as_deref().unwrap_or(badge);
298            let safe_sig: Signal<SafeAreaInsets> = ctx_fs_copy.get_safe_area();
299            let vp_sig: Signal<Size> = ctx_fs_copy.get_viewport_size();
300            let init_fs: FullscreenSafeLayout =
301                resolve_fullscreen_safe_layout(vp_sig.get_untracked(), safe_sig.get_untracked());
302            let init_body: Edges = init_fs.get_body_padding();
303            let mut modal_scroll_st: Style = Style::default();
304            modal_scroll_st
305                .get_mut_layout()
306                .set_flex_direction(FlexDirection::Column);
307            let mut top_bar_node: NodeId = NodeId::default();
308            let mut body_scroll_node: NodeId = NodeId::default();
309            let mut modal_canvas_node: NodeId = NodeId::default();
310            let mut zoom_reset_txt: NodeId = NodeId::default();
311            let mut modal_copy_btn: NodeId = NodeId::default();
312            let mut modal_copy_icon: NodeId = NodeId::default();
313            let column_secondary_style: Style = style! {
314                Width(Length::Percent(100.0))
315                Height(Length::Percent(100.0))
316                FlexGrow(1.0)
317                Padding(Edges::all(0.0))
318                Margin(Edges::all(0.0))
319                Background(live_eva.get_background())
320                Border::new(0.0, COLOR_TRANSPARENT)
321                Radius(0.0)
322                Overflow::Hidden
323                StopEventPropagation(true)
324            };
325            let row_sixth_style: Style = style! {
326                Width(Length::Percent(100.0))
327                FlexShrink(0.0)
328                Padding(init_fs.get_header_padding())
329                JustifyContent::SpaceBetween
330                AlignItems::Center
331                Gap(SPACE_SM)
332                Overflow::Hidden
333                Background(live_eva.get_header_bg())
334            };
335            let row_seventh_style: Style = style! {
336                FlexGrow(1.0)
337                FlexShrink(1.0)
338                MinWidth(Length::Px(0.0))
339                AlignItems::Center
340                Gap(SPACE_SM)
341                Overflow::Hidden
342            };
343            let row_eighth_style: Style = style! {
344                Height(Length::Px(modal_ctrl_h))
345                Padding(Edges::symmetric(0.0, SPACE_SM))
346                JustifyContent::Center
347                AlignItems::Center
348                Radius(live_g.get_radius_sm())
349                Overflow::Hidden
350                Background(live_eva.get_background())
351                Border::new(1.0, live_eva.get_border())
352            };
353            let text_tertiary_style: Style = style! {
354                FontSize(10.5)
355                FontWeight::Bold
356                TextColor(live_eva.get_keyword())
357                TextOverflow::Clip
358            };
359            let text_quaternary_style: Style = style! {
360                FlexShrink(1.0)
361                MinWidth(Length::Px(0.0))
362                Overflow::Hidden
363                TextOverflow::Ellipsis
364                FontSize(13.0)
365                FontWeight::SemiBold
366                TextColor(live_p.get_foreground())
367            };
368            let row_ninth_style: Style = style! {
369                FlexShrink(0.0)
370                AlignItems::Center
371                Gap(SPACE_XS)
372            };
373            let row_tenth_style: Style = style! {
374                Width(Length::Px(modal_ctrl_h))
375                Height(Length::Px(modal_ctrl_h))
376                JustifyContent::Center
377                AlignItems::Center
378                Radius(live_g.get_radius_sm())
379                Overflow::Hidden
380                CursorIcon::Pointer
381                Background(live_eva.get_background())
382                Border::new(1.0, live_eva.get_border())
383            };
384            let text_fifth_style: Style = style! {
385                FontSize(14.0)
386                FontWeight::Bold
387                TextColor(live_p.get_foreground())
388                TextOverflow::Clip
389            };
390            let row_eleventh_style: Style = style! {
391                Height(Length::Px(modal_ctrl_h))
392                Padding(Edges::symmetric(0.0, SPACE_SM))
393                JustifyContent::Center
394                AlignItems::Center
395                Radius(live_g.get_radius_sm())
396                Overflow::Hidden
397                CursorIcon::Pointer
398                Background(live_eva.get_background())
399                Border::new(1.0, live_eva.get_border())
400            };
401            let text_sixth_style: Style = style! {
402                FontSize(11.0)
403                FontFamily::Monospace
404                FontWeight::SemiBold
405                TextColor(live_p.get_foreground())
406                TextOverflow::Clip
407            };
408            let canvas_tertiary_style: Style = style! {
409                Width(Length::Px(14.0))
410                Height(Length::Px(14.0))
411            };
412            let row_twelfth_style: Style = style! {
413                Width(Length::Px(modal_ctrl_h))
414                Height(Length::Px(modal_ctrl_h))
415                JustifyContent::Center
416                AlignItems::Center
417                Radius(live_g.get_radius_sm())
418                Overflow::Hidden
419                CursorIcon::Pointer
420                Background(close_surface.get_bg())
421            };
422            let view_secondary_style: Style = style! {
423                Width(Length::Percent(100.0))
424                Height(Length::Px(1.0))
425                Background(live_eva.get_border())
426            };
427            let scroll_secondary_style: Style = style! {
428                modal_scroll_st
429                FlexGrow(1.0)
430                FlexShrink(1.0)
431                MinHeight(Length::Px(0.0))
432                Width(Length::Percent(100.0))
433                AlignItems::Start
434                Padding(Edges::new(
435                    SPACE_MD,
436                    SPACE_MD + init_body.get_right(),
437                    SPACE_MD + init_body.get_bottom(),
438                    SPACE_MD + init_body.get_left()
439                ))
440                StopScrollPropagation(true)
441            };
442            let canvas_quaternary_style: Style = style! {
443                Width(Length::Px(modal_base_w))
444                Height(Length::Px(modal_base_h))
445                FlexShrink(0.0)
446                CursorIcon::Grab
447            };
448            let modal_panel: NodeId = view! {
449                column {
450                    style: column_secondary_style
451                    row {
452                        node_ref: top_bar_node
453                        style: row_sixth_style
454                        row {
455                            style: row_seventh_style
456                            row {
457                                style: row_eighth_style
458                                text {
459                                    style: text_tertiary_style
460                                    { badge }
461                                }
462                            }
463                            text {
464                                style: text_quaternary_style
465                                { modal_title_str }
466                            }
467                        }
468                        row {
469                            style: row_ninth_style
470                            row {
471                                style: row_tenth_style
472                                role: A11yRole::Button
473                                a11y_label: MERMAID_BUTTON_ZOOM_OUT.to_string()
474                                on_click: on_zoom_out
475                                text {
476                                    style: text_fifth_style
477                                    { MERMAID_ZOOM_MINUS }
478                                }
479                            }
480                            row {
481                                style: row_eleventh_style
482                                role: A11yRole::Button
483                                a11y_label: MERMAID_BUTTON_ZOOM_RESET.to_string()
484                                on_click: on_zoom_reset
485                                text {
486                                    node_ref: zoom_reset_txt
487                                    style: text_sixth_style
488                                    { MERMAID_ZOOM_INIT_LABEL }
489                                }
490                            }
491                            row {
492                                style: row_tenth_style
493                                role: A11yRole::Button
494                                a11y_label: MERMAID_BUTTON_ZOOM_IN.to_string()
495                                on_click: on_zoom_in
496                                text {
497                                    style: text_fifth_style
498                                    { MERMAID_ZOOM_PLUS }
499                                }
500                            }
501                            row {
502                                node_ref: modal_copy_btn
503                                style: row_tenth_style
504                                role: A11yRole::Button
505                                a11y_label: init_copy_a11y
506                                on_click: on_modal_copy
507                                canvas {
508                                    node_ref: modal_copy_icon
509                                    style: canvas_tertiary_style
510                                    painter: paint_modal_copy_icon
511                                }
512                            }
513                            row {
514                                style: row_twelfth_style
515                                role: A11yRole::Button
516                                a11y_label: MERMAID_BUTTON_CLOSE.to_string()
517                                on_click: on_close_fs
518                                canvas {
519                                    style: canvas_tertiary_style
520                                    painter: paint_close_icon
521                                }
522                            }
523                        }
524                    }
525                    view {
526                        style: view_secondary_style
527                    }
528                    scroll {
529                        node_ref: body_scroll_node
530                        style: scroll_secondary_style
531                        canvas {
532                            node_ref: modal_canvas_node
533                            style: canvas_quaternary_style
534                            painter: paint_modal
535                        }
536                    }
537                }
538            };
539            body_scroll_cell.set(body_scroll_node);
540            let handle_safe: SceneHandle = ctx_fs_copy.get_scene().clone();
541            let handle_zoom: SceneHandle = handle_safe.clone();
542            let (safe_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
543                let _: Effect = Effect::new(move || {
544                    let depth_z: f32 = zoom_sig.get();
545                    let next_w: f32 = (modal_base_w * depth_z).max(240.0);
546                    let next_h: f32 = (modal_base_h * depth_z).max(160.0);
547                    if !fs_open.get_untracked() {
548                        return;
549                    }
550                    let pct_label: String = format!("{}%", (depth_z * 100.0).round() as u32);
551                    handle_zoom.with_write(
552                        |tree: &mut SceneTree| {
553                            let dirty_l: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
554                            if let Some(node_entry) = tree.get_node_mut(modal_canvas_node) {
555                                let lay: &mut LayoutStyle =
556                                    node_entry.get_mut_style().get_mut_layout();
557                                lay.set_width(Length::Px(next_w))
558                                    .set_height(Length::Px(next_h));
559                                tree.mark_dirty(modal_canvas_node, dirty_l | DIRTY_PAINT);
560                            }
561                            tree.set_text(zoom_reset_txt, pct_label);
562                        },
563                        (),
564                    );
565                });
566                Effect::new(move || {
567                    let fs_lay: FullscreenSafeLayout =
568                        resolve_fullscreen_safe_layout(vp_sig.get(), safe_sig.get());
569                    let fs_body: Edges = fs_lay.get_body_padding();
570                    handle_safe.with_write(
571                        |tree: &mut SceneTree| {
572                            let dirty_l: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
573                            if let Some(normal_vec) = tree.get_node_mut(top_bar_node) {
574                                normal_vec
575                                    .get_mut_style()
576                                    .get_mut_layout()
577                                    .set_padding(fs_lay.get_header_padding());
578                                tree.mark_dirty(top_bar_node, dirty_l | DIRTY_PAINT);
579                            }
580                            if let Some(total_len) = tree.get_node_mut(body_scroll_node) {
581                                total_len
582                                    .get_mut_style()
583                                    .get_mut_layout()
584                                    .set_padding(Edges::new(
585                                        SPACE_MD,
586                                        SPACE_MD + fs_body.get_right(),
587                                        SPACE_MD + fs_body.get_bottom(),
588                                        SPACE_MD + fs_body.get_left(),
589                                    ));
590                                tree.mark_dirty(body_scroll_node, dirty_l | DIRTY_PAINT);
591                            }
592                        },
593                        (),
594                    );
595                })
596            });
597            ctx_fs_copy.get_scene().with_write(
598                |tree: &mut SceneTree| {
599                    tree.attach_scope(modal_panel, safe_scope);
600                },
601                (),
602            );
603            modal_copy_btn_cell.set(modal_copy_btn);
604            modal_copy_icon_cell.set(modal_copy_icon);
605            modal_panel
606        };
607        let mut fs_portal: NodeId = NodeId::default();
608        let mut inline_copy_btn: NodeId = NodeId::default();
609        let mut inline_copy_icon: NodeId = NodeId::default();
610        let mut inline_scroll_st: Style = Style::default();
611        inline_scroll_st
612            .get_mut_layout()
613            .set_flex_direction(FlexDirection::Column);
614        let column_style: Style = style! {
615            Width(Length::Percent(100.0))
616            Background(eva.get_background())
617            Border::new(1.0, eva.get_border())
618            Radius(geometry.get_radius_lg())
619            Overflow::Hidden
620        };
621        let row_style: Style = style! {
622            Width(Length::Percent(100.0))
623            Padding(Edges::symmetric(SPACE_SM, SPACE_MD))
624            JustifyContent::SpaceBetween
625            AlignItems::Center
626            Gap(SPACE_SM)
627            Overflow::Hidden
628            Background(eva.get_header_bg())
629        };
630        let row_secondary_style: Style = style! {
631            FlexGrow(1.0)
632            FlexShrink(1.0)
633            MinWidth(Length::Px(0.0))
634            AlignItems::Center
635            Gap(SPACE_SM)
636            Overflow::Hidden
637        };
638        let row_tertiary_style: Style = style! {
639            FlexShrink(0.0)
640            Height(Length::Px(ctrl_sm))
641            Padding(Edges::symmetric(0.0, SPACE_SM))
642            JustifyContent::Center
643            AlignItems::Center
644            Radius(geometry.get_radius_sm())
645            Overflow::Hidden
646            Background(eva.get_background())
647            Border::new(1.0, eva.get_border())
648        };
649        let text_style: Style = style! {
650            FontSize(10.5)
651            FontWeight::Bold
652            TextColor(eva.get_keyword())
653            TextOverflow::Clip
654        };
655        let text_secondary_style: Style = style! {
656            FlexGrow(1.0)
657            FlexShrink(1.0)
658            MinWidth(Length::Px(0.0))
659            Overflow::Hidden
660            TextOverflow::Ellipsis
661            FontSize(12.0)
662            FontWeight::SemiBold
663            TextColor(eva.get_foreground())
664        };
665        let row_quaternary_style: Style = style! {
666            FlexShrink(0.0)
667            AlignItems::Center
668            Gap(SPACE_XS)
669        };
670        let row_fifth_style: Style = style! {
671            Width(Length::Px(ctrl_sm))
672            Height(Length::Px(ctrl_sm))
673            Radius(geometry.get_radius_sm())
674            Overflow::Hidden
675            JustifyContent::Center
676            AlignItems::Center
677            CursorIcon::Pointer
678            Background(eva.get_background())
679            Border::new(1.0, eva.get_border())
680        };
681        let canvas_style: Style = style! {
682            Width(Length::Px(13.0))
683            Height(Length::Px(13.0))
684        };
685        let view_style: Style = style! {
686            Width(Length::Percent(100.0))
687            Height(Length::Px(1.0))
688            Background(eva.get_border())
689        };
690        let scroll_style: Style = style! {
691            inline_scroll_st
692            Width(Length::Percent(100.0))
693            MaxHeight(Length::Px(inline_max_h))
694            AlignItems::Start
695        };
696        let canvas_secondary_style: Style = style! {
697            Width(Length::Px(nat_sz.get_width()))
698            Height(Length::Px(nat_sz.get_height()))
699            FlexShrink(0.0)
700            CursorIcon::Grab
701        };
702        let portal_style: Style = style! {
703            FlexDirection::Column
704            Display::None
705            Position::Fixed
706            Inset(Some(0.0), Some(0.0), Some(0.0), Some(0.0))
707            Width(Length::Percent(100.0))
708            Height(Length::Percent(100.0))
709            Padding(Edges::all(0.0))
710            Margin(Edges::all(0.0))
711            JustifyContent::Start
712            AlignItems::Stretch
713            Background(eva.get_background())
714            Border::new(0.0, COLOR_TRANSPARENT)
715            Radius(0.0)
716            Overflow::Hidden
717            PointerEvents::None
718            StopScrollPropagation(true)
719            StopEventPropagation(true)
720            ZIndex(Z_INDEX_FULLSCREEN)
721        };
722        let root: NodeId = view! {
723            column {
724                style: column_style
725                row {
726                    style: row_style
727                    row {
728                        style: row_secondary_style
729                        row {
730                            style: row_tertiary_style
731                            text {
732                                style: text_style
733                                { badge }
734                            }
735                        }
736                        if let Some(title_text) = header_title_rc.as_deref() {
737                            text {
738                                style: text_secondary_style
739                                { title_text }
740                            }
741                        }
742                    }
743                    row {
744                        style: row_quaternary_style
745                        row {
746                            style: row_fifth_style
747                            role: A11yRole::Button
748                            a11y_label: MERMAID_BUTTON_FULLSCREEN.to_string()
749                            on_click: on_open_fs
750                            canvas {
751                                style: canvas_style
752                                painter: paint_fs_icon
753                            }
754                        }
755                        row {
756                            node_ref: inline_copy_btn
757                            style: row_fifth_style
758                            role: A11yRole::Button
759                            a11y_label: MERMAID_BUTTON_COPY.to_string()
760                            on_click: on_copy_click
761                            canvas {
762                                node_ref: inline_copy_icon
763                                style: canvas_style
764                                painter: paint_inline_copy_icon
765                            }
766                        }
767                    }
768                }
769                view {
770                    style: view_style
771                }
772                scroll {
773                    style: scroll_style
774                    canvas {
775                        style: canvas_secondary_style
776                        painter: paint_inline
777                    }
778                }
779                portal {
780                    node_ref: fs_portal
781                    style: portal_style
782                }
783            }
784        };
785        let handle: SceneHandle = ctx.get_scene().clone();
786        let handle_copy: SceneHandle = handle.clone();
787        let ctx_build: Context = ctx.clone();
788        let prev_open: Rc<Cell<bool>> = Rc::new(Cell::new(false));
789        let fs_acquired: Rc<Cell<bool>> = Rc::new(Cell::new(false));
790        let fs_cleanup: Rc<Cell<bool>> = Rc::clone(&fs_acquired);
791        let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
792            on_cleanup(move || {
793                if fs_cleanup.replace(false) {
794                    exit_fullscreen();
795                }
796            });
797            let _: Effect = Effect::new(move || {
798                let is_copied: bool = copied_sig.get();
799                let next_label: &str = if is_copied {
800                    MERMAID_BUTTON_COPIED
801                } else {
802                    MERMAID_BUTTON_COPY
803                };
804                handle_copy.with_write(
805                    |tree: &mut SceneTree| {
806                        if let Some(item_count_primary) = tree.get_node_mut(inline_copy_btn) {
807                            *item_count_primary.get_mut_a11y() =
808                                Some(A11yProps::new(A11yRole::Button, next_label.to_string()));
809                        }
810                        tree.mark_dirty(inline_copy_icon, DIRTY_PAINT);
811                    },
812                    (),
813                );
814            });
815            let _: Effect = EuvOverlayStack::bind_signal(
816                fs_open,
817                MERMAID_FULLSCREEN_OVERLAY_NAME,
818                Z_INDEX_FULLSCREEN,
819            );
820            let vp_sig: Signal<Size> = ctx_build.get_viewport_size();
821            let handle_vp: SceneHandle = handle.clone();
822            let _: Effect = Effect::new(move || {
823                let box_size: Size = vp_sig.get();
824                if !fs_open.get() {
825                    return;
826                }
827                let vw: f32 = box_size.get_width().max(1.0);
828                let vh: f32 = box_size.get_height().max(1.0);
829                handle_vp.with_write(
830                    |scene_tree: &mut SceneTree| {
831                        if let Some(item_count_secondary) = scene_tree.get_node_mut(fs_portal) {
832                            item_count_secondary
833                                .get_mut_style()
834                                .get_mut_layout()
835                                .set_width(Length::Px(vw))
836                                .set_height(Length::Px(vh));
837                            let dirty_l: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
838                            scene_tree.mark_dirty(fs_portal, dirty_l | DIRTY_PAINT);
839                            notify_frame_needed();
840                        }
841                    },
842                    (),
843                );
844            });
845            let in_enter_guard: Rc<Cell<bool>> = Rc::new(Cell::new(false));
846            let global_fs_sig: Signal<bool> = use_fullscreen_active();
847            let ctx_fs_sig: Signal<bool> = ctx_build.immersive_fullscreen_signal();
848            let saw_global_fs: Rc<Cell<bool>> = Rc::new(Cell::new(false));
849            let saw_ctx_fs: Rc<Cell<bool>> = Rc::new(Cell::new(false));
850            let saw_global_reset: Rc<Cell<bool>> = Rc::clone(&saw_global_fs);
851            let saw_ctx_reset: Rc<Cell<bool>> = Rc::clone(&saw_ctx_fs);
852            let fs_acquired_sync: Rc<Cell<bool>> = Rc::clone(&fs_acquired);
853            let enter_guard_sync: Rc<Cell<bool>> = Rc::clone(&in_enter_guard);
854            let _: Effect = Effect::new(move || {
855                let g_active: bool = global_fs_sig.get();
856                let c_active: bool = ctx_fs_sig.get();
857                if enter_guard_sync.get() {
858                    if g_active {
859                        saw_global_fs.set(true);
860                    }
861                    if c_active {
862                        saw_ctx_fs.set(true);
863                    }
864                    return;
865                }
866                if !fs_acquired_sync.get() {
867                    saw_global_fs.set(false);
868                    saw_ctx_fs.set(false);
869                    return;
870                }
871                if g_active {
872                    saw_global_fs.set(true);
873                }
874                if c_active {
875                    saw_ctx_fs.set(true);
876                }
877                let external_exit: bool =
878                    (saw_global_fs.get() && !g_active) || (saw_ctx_fs.get() && !c_active);
879                if external_exit {
880                    let _: bool = fs_open.set_if_changed(false);
881                }
882            });
883            Effect::new(move || {
884                let is_open: bool = fs_open.get();
885                if is_open == prev_open.replace(is_open) {
886                    return;
887                }
888                if is_open {
889                    in_enter_guard.set(true);
890                    saw_global_reset.set(false);
891                    saw_ctx_reset.set(false);
892                    let live_eva: CodeThemePalette =
893                        CodeThemePalette::resolve(live_tokens_untracked(base_tokens).get_mode());
894                    let vp_now: Size = vp_sig.get_untracked();
895                    let vw: f32 = vp_now.get_width().max(1.0);
896                    let vh: f32 = vp_now.get_height().max(1.0);
897                    let modal_panel: NodeId = with_context(ctx_build.clone(), &build_fs_panel);
898                    let portal_shown: bool = modal_panel != NodeId::default()
899                        && handle.with_write(
900                            |tree: &mut SceneTree| {
901                                if tree.get_node(fs_portal).is_none()
902                                    || tree.get_node(modal_panel).is_none()
903                                {
904                                    return false;
905                                }
906                                tree.cancel_subtree_transitions(fs_portal);
907                                tree.replace_children(fs_portal, vec![modal_panel]);
908                                if let Some(item_count_next) = tree.get_node_mut(fs_portal) {
909                                    let element_style: &mut Style = item_count_next.get_mut_style();
910                                    element_style
911                                        .get_mut_layout()
912                                        .set_display(Display::Flex)
913                                        .set_width(Length::Px(vw))
914                                        .set_height(Length::Px(vh));
915                                    element_style
916                                        .get_mut_paint()
917                                        .set_background(Some(live_eva.get_background()))
918                                        .set_pointer_events(PointerEvents::Auto)
919                                        .set_opacity(1.0)
920                                        .set_transform(Transform2D::default());
921                                }
922                                let dirty_l: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
923                                tree.mark_dirty(fs_portal, dirty_l | DIRTY_PAINT);
924                                notify_frame_needed();
925                                true
926                            },
927                            false,
928                        );
929                    let entered: bool = portal_shown
930                        && (fs_acquired.get()
931                            || enter_fullscreen_for_portal(&ctx_build, fs_portal));
932                    if entered {
933                        fs_acquired.set(true);
934                    } else {
935                        handle.dismiss_portal_immediate(fs_portal);
936                        if fs_acquired.replace(false) {
937                            exit_fullscreen();
938                        }
939                        prev_open.set(false);
940                        let _: bool = fs_open.set_if_changed(false);
941                    }
942                    in_enter_guard.set(false);
943                    notify_frame_needed();
944                } else {
945                    in_enter_guard.set(false);
946                    if fs_acquired.replace(false) {
947                        exit_fullscreen();
948                    }
949                    handle.dismiss_portal_immediate(fs_portal);
950                }
951            })
952        });
953        ctx.get_scene().with_write(
954            |tree: &mut SceneTree| {
955                tree.attach_scope(root, scope);
956            },
957            (),
958        );
959        root
960    }
961}