quickgui 0.1.4

A damage-driven, GPU-accelerated desktop GUI foundation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
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
//! Retained native Markdown parsing and rendering.
//!
//! The parser keeps a settled top-level prefix and reparses only the last unstable groups when
//! text is appended. Rendering is ordinary QuickGUI elements and retained [`StyledText`] values;
//! there is no webview, HTML layout engine, or per-frame parser.

mod mend;
mod parser;

use std::{collections::HashMap, sync::Arc};

use crate::{
    AccessibilityRole, Color, Element, ElementId, FontFamily, FontWeight, HighlightStyle,
    IntoElement, MAX_TEXT_HIGHLIGHTS, StyledText, TextAlign, div, text,
};

use parser::{IncrementalParser, TopBlock};
pub use parser::{
    MarkdownBlock, MarkdownInlineRun, MarkdownInlineStyle, MarkdownListItem, MarkdownTableAlign,
};

/// Maximum UTF-8 source retained by one Markdown component.
pub const MAX_MARKDOWN_SOURCE_BYTES: usize = 4 * 1024 * 1024;
/// Maximum parsed top-level blocks retained by one Markdown component.
pub const MAX_MARKDOWN_BLOCKS: usize = 32_768;
/// Maximum recursive Markdown container depth.
pub const MAX_MARKDOWN_NESTING_DEPTH: usize = 64;
const MAX_MARKDOWN_TABLE_COLUMNS: usize = 64;

/// Semantic Markdown metrics and optional colors.
///
/// `None` colors inherit from the surrounding element. The default remains neutral across light
/// and dark application palettes while retaining heading hierarchy, spacing, code typography,
/// and list/table structure.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MarkdownStyle {
    pub text_color: Option<Color>,
    pub muted_color: Option<Color>,
    pub link_color: Option<Color>,
    pub code_text_color: Option<Color>,
    pub code_background: Option<Color>,
    pub border_color: Option<Color>,
    pub font_size: f32,
    pub line_height: f32,
    pub block_gap: f32,
    pub code_font_size: f32,
}

impl MarkdownStyle {
    pub fn text_color(mut self, color: Color) -> Self {
        self.text_color = Some(color);
        self
    }

    pub fn muted_color(mut self, color: Color) -> Self {
        self.muted_color = Some(color);
        self
    }

    pub fn link_color(mut self, color: Color) -> Self {
        self.link_color = Some(color);
        self
    }

    pub fn code_text_color(mut self, color: Color) -> Self {
        self.code_text_color = Some(color);
        self
    }

    pub fn code_background(mut self, color: Color) -> Self {
        self.code_background = Some(color);
        self
    }

    pub fn border_color(mut self, color: Color) -> Self {
        self.border_color = Some(color);
        self
    }

    pub fn font_size(mut self, size: f32) -> Self {
        self.font_size = finite_at_least(size, 1.0, 15.0);
        self
    }

    pub fn line_height(mut self, height: f32) -> Self {
        self.line_height = finite_at_least(height, 1.0, 23.0);
        self
    }

    pub fn block_gap(mut self, gap: f32) -> Self {
        self.block_gap = finite_at_least(gap, 0.0, 12.0);
        self
    }

    pub fn code_font_size(mut self, size: f32) -> Self {
        self.code_font_size = finite_at_least(size, 1.0, 13.0);
        self
    }

    fn sanitized(mut self) -> Self {
        self.font_size = finite_at_least(self.font_size, 1.0, 15.0);
        self.line_height = finite_at_least(self.line_height, 1.0, 23.0);
        self.block_gap = finite_at_least(self.block_gap, 0.0, 12.0);
        self.code_font_size = finite_at_least(self.code_font_size, 1.0, 13.0);
        self
    }
}

impl Default for MarkdownStyle {
    fn default() -> Self {
        Self {
            text_color: None,
            muted_color: None,
            link_color: None,
            code_text_color: None,
            code_background: Some(Color::rgba8(127, 127, 127, 32)),
            border_color: Some(Color::rgba8(127, 127, 127, 72)),
            font_size: 15.0,
            line_height: 23.0,
            block_gap: 12.0,
            code_font_size: 13.0,
        }
    }
}

/// Result of changing retained Markdown source.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct MarkdownUpdate {
    pub changed: bool,
    pub appended: bool,
    pub reparsed_from: usize,
    pub truncated: bool,
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
struct FlatKey {
    source_start: usize,
    top_index: usize,
    slot: u32,
}

/// Caller-owned retained Markdown document.
///
/// Keep one instance per logical document and call [`Self::set_text`] as streamed content grows.
/// [`Self::element`] rebuilds only the cheap declarative element shell; parsed blocks and shaped
/// inline run inputs remain retained across frames.
pub struct Markdown {
    parser: IncrementalParser,
    display_tail: Option<Vec<TopBlock>>,
    flat_cache: HashMap<FlatKey, StyledText>,
    style: MarkdownStyle,
    streaming: bool,
    truncated: bool,
}

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

impl Markdown {
    pub fn new() -> Self {
        Self {
            parser: IncrementalParser::new(),
            display_tail: None,
            flat_cache: HashMap::new(),
            style: MarkdownStyle::default(),
            streaming: false,
            truncated: false,
        }
    }

    pub fn with_text(text: &str) -> Self {
        let mut markdown = Self::new();
        markdown.set_text(text);
        markdown
    }

    pub fn text(&self) -> &str {
        self.parser.text()
    }

    pub fn block_count(&self) -> usize {
        self.canonical_display_prefix_len() + self.display_tail.as_ref().map_or(0, Vec::len)
    }

    pub const fn is_streaming(&self) -> bool {
        self.streaming
    }

    pub const fn is_truncated(&self) -> bool {
        self.truncated
    }

    pub const fn style(&self) -> MarkdownStyle {
        self.style
    }

    pub fn set_style(&mut self, style: MarkdownStyle) -> bool {
        let style = style.sanitized();
        if self.style == style {
            return false;
        }
        self.style = style;
        self.flat_cache.clear();
        true
    }

    pub fn set_streaming(&mut self, streaming: bool) -> bool {
        if self.streaming == streaming {
            return false;
        }
        self.streaming = streaming;
        self.invalidate_display_tail();
        true
    }

    /// Point the retained document at new controlled source.
    ///
    /// Append-only changes reuse the settled prefix. Rewrites and link reference definitions fall
    /// back to a bounded full parse. Input beyond [`MAX_MARKDOWN_SOURCE_BYTES`] is truncated at a
    /// UTF-8 boundary.
    pub fn set_text(&mut self, text: &str) -> MarkdownUpdate {
        let (text, truncated) = bounded_source(text);
        let previous = self.parser.text();
        let appended = text.len() >= previous.len() && text.starts_with(previous);
        let changed = self.parser.set_text(text);
        self.truncated = truncated;
        if !changed {
            return MarkdownUpdate {
                changed: false,
                appended,
                reparsed_from: self.parser.reparsed_from(),
                truncated,
            };
        }
        let reparsed_from = self.parser.reparsed_from();
        if appended {
            self.flat_cache
                .retain(|key, _| key.source_start < reparsed_from);
        } else {
            self.flat_cache.clear();
        }
        self.refresh_display_tail();
        MarkdownUpdate {
            changed: true,
            appended,
            reparsed_from,
            truncated,
        }
    }

    /// Render the document as ordinary native QuickGUI elements.
    pub fn element(&mut self, id: impl Into<ElementId>) -> Element {
        let root_id = id.into();
        let mut root = div()
            .id(root_id)
            .w_full()
            .min_w(0.0)
            .flex_col()
            .gap(self.style.block_gap)
            .user_select_text()
            .accessibility_role(AccessibilityRole::Group);
        if let Some(color) = self.style.text_color {
            root = root.text_color(color);
        }

        let style = self.style;
        let canonical_len = self.canonical_display_prefix_len();
        let cache = &mut self.flat_cache;
        let canonical = &self.parser.tree().blocks;
        let mut elements = Vec::new();
        for (index, block) in canonical[..canonical_len].iter().enumerate() {
            let mut slot = 0;
            elements.push(render_block(
                &block.block,
                block.range.start,
                &mut slot,
                root_id,
                index,
                0,
                style,
                cache,
            ));
        }
        if let Some(tail) = &self.display_tail {
            for (tail_index, block) in tail.iter().enumerate() {
                let mut slot = 0;
                elements.push(render_block(
                    &block.block,
                    block.range.start,
                    &mut slot,
                    root_id,
                    canonical_len + tail_index,
                    0,
                    style,
                    cache,
                ));
            }
        }
        root.children(elements)
    }

    fn refresh_display_tail(&mut self) {
        self.display_tail = self.streaming.then(|| self.parser.display_tail()).flatten();
    }

    fn canonical_display_prefix_len(&self) -> usize {
        let canonical = &self.parser.tree().blocks;
        let Some(tail_start) = self
            .display_tail
            .as_ref()
            .and_then(|tail| tail.first())
            .map(|block| block.range.start)
        else {
            return canonical.len();
        };
        canonical.partition_point(|block| block.range.start < tail_start)
    }

    fn invalidate_display_tail(&mut self) {
        if let Some(start) = self
            .parser
            .tree()
            .blocks
            .last()
            .map(|block| block.range.start)
        {
            self.flat_cache.retain(|key, _| key.source_start < start);
        }
        self.refresh_display_tail();
    }
}

#[allow(clippy::too_many_arguments)]
fn render_block(
    block: &MarkdownBlock,
    source_start: usize,
    slot: &mut u32,
    root_id: ElementId,
    top_index: usize,
    depth: usize,
    style: MarkdownStyle,
    cache: &mut HashMap<FlatKey, StyledText>,
) -> Element {
    let (id, block_slot) = allocate_id(root_id, top_index, depth, slot);
    match block {
        MarkdownBlock::Paragraph(runs) => inline_element(
            runs,
            source_start,
            top_index,
            block_slot,
            id,
            style,
            style.font_size,
            style.line_height,
            FontWeight::NORMAL,
            cache,
        ),
        MarkdownBlock::Heading { level, runs } => {
            let (font_size, line_height) = heading_metrics(*level, style);
            inline_element(
                runs,
                source_start,
                top_index,
                block_slot,
                id,
                style,
                font_size,
                line_height,
                FontWeight::BOLD,
                cache,
            )
            .accessibility_role(AccessibilityRole::Heading)
        }
        MarkdownBlock::CodeBlock { language, code } => {
            let (code_id, code_slot) = allocate_id(root_id, top_index, depth + 1, slot);
            let code_key = FlatKey {
                source_start,
                top_index,
                slot: code_slot,
            };
            let styled = cache
                .entry(code_key)
                .or_insert_with(|| StyledText::new(Arc::<str>::from(code.as_str())))
                .clone();
            let mut code_text = styled
                .into_element()
                .id(code_id)
                .w_full()
                .min_w(0.0)
                .wrap()
                .font_family(FontFamily::Monospace)
                .text_size(style.code_font_size)
                .line_height((style.code_font_size * 1.55).max(style.code_font_size + 2.0));
            if let Some(color) = style.code_text_color.or(style.text_color) {
                code_text = code_text.text_color(color);
            }
            let mut container = div()
                .id(id)
                .w_full()
                .min_w(0.0)
                .flex_col()
                .gap_2()
                .p_3()
                .rounded_lg();
            if let Some(background) = style.code_background {
                container = container.bg(background);
            }
            if let Some(border) = style.border_color {
                container = container.border(1.0, border);
            }
            let mut children = Vec::with_capacity(2);
            if let Some(language) = language.as_deref().filter(|language| !language.is_empty()) {
                let (label_id, _) = allocate_id(root_id, top_index, depth + 1, slot);
                let mut label = text(Arc::<str>::from(language))
                    .id(label_id)
                    .text_size(11.0)
                    .line_height(14.0)
                    .font_semibold();
                if let Some(color) = style.muted_color {
                    label = label.text_color(color);
                }
                children.push(label);
            }
            children.push(code_text);
            container.children(children)
        }
        MarkdownBlock::BlockQuote(children) => {
            let (marker_id, _) = allocate_id(root_id, top_index, depth + 1, slot);
            let mut marker = div().id(marker_id).w(3.0).self_stretch().rounded_sm();
            if let Some(color) = style.border_color {
                marker = marker.bg(color);
            }
            let content = render_blocks(
                children,
                source_start,
                slot,
                root_id,
                top_index,
                depth + 1,
                style,
                cache,
            )
            .flex_1()
            .min_w(0.0);
            let mut quote = div()
                .id(id)
                .w_full()
                .min_w(0.0)
                .flex_row()
                .items_stretch()
                .gap_3()
                .child(marker)
                .child(content);
            if let Some(color) = style.muted_color {
                quote = quote.text_color(color);
            }
            quote
        }
        MarkdownBlock::List {
            ordered_start,
            items,
        } => {
            let mut rows = Vec::with_capacity(items.len());
            for (item_index, item) in items.iter().enumerate() {
                let (marker_id, _) = allocate_id(root_id, top_index, depth + 1, slot);
                let marker = item.task.map_or_else(
                    || {
                        ordered_start.map_or_else(
                            || "•".to_owned(),
                            |start| format!("{}.", start.saturating_add(item_index as u64)),
                        )
                    },
                    |checked| if checked { "☑" } else { "☐" }.to_owned(),
                );
                let mut marker = text(marker)
                    .id(marker_id)
                    .w(26.0)
                    .flex_none()
                    .text_size(style.font_size)
                    .line_height(style.line_height)
                    .text_right();
                if let Some(color) = style.muted_color {
                    marker = marker.text_color(color);
                }
                let body = render_blocks(
                    &item.blocks,
                    source_start,
                    slot,
                    root_id,
                    top_index,
                    depth + 1,
                    style,
                    cache,
                )
                .flex_1()
                .min_w(0.0);
                let (row_id, _) = allocate_id(root_id, top_index, depth + 1, slot);
                rows.push(
                    div()
                        .id(row_id)
                        .w_full()
                        .min_w(0.0)
                        .flex_row()
                        .items_start()
                        .gap_2()
                        .child(marker)
                        .child(body),
                );
            }
            div()
                .id(id)
                .w_full()
                .min_w(0.0)
                .flex_col()
                .gap_2()
                .children(rows)
        }
        MarkdownBlock::Table {
            header,
            rows,
            align,
        } => {
            let columns = header
                .len()
                .max(rows.iter().map(Vec::len).max().unwrap_or(0))
                .clamp(1, MAX_MARKDOWN_TABLE_COLUMNS);
            let mut cells = Vec::new();
            for row in std::iter::once(header).chain(rows.iter()) {
                let is_header = std::ptr::eq(row, header);
                for column in 0..columns {
                    let runs = row.get(column).map(Vec::as_slice).unwrap_or(&[]);
                    let (cell_id, cell_slot) = allocate_id(root_id, top_index, depth + 1, slot);
                    let mut cell = inline_element(
                        runs,
                        source_start,
                        top_index,
                        cell_slot,
                        cell_id,
                        style,
                        (style.font_size - 1.0).max(11.0),
                        style.line_height,
                        if is_header {
                            FontWeight::SEMIBOLD
                        } else {
                            FontWeight::NORMAL
                        },
                        cache,
                    )
                    .p_2()
                    .text_align(
                        match align.get(column).copied().unwrap_or_default() {
                            MarkdownTableAlign::Left => TextAlign::Left,
                            MarkdownTableAlign::Center => TextAlign::Center,
                            MarkdownTableAlign::Right => TextAlign::Right,
                        },
                    );
                    if let Some(border) = style.border_color {
                        cell = cell.border(1.0, border);
                    }
                    cells.push(cell);
                }
            }
            div()
                .id(id)
                .w_full()
                .min_w(0.0)
                .grid()
                .grid_cols(columns as u16)
                .children(cells)
        }
        MarkdownBlock::Image { url, alt } => {
            let label = if alt.trim().is_empty() { "Image" } else { alt };
            let runs = [MarkdownInlineRun {
                text: format!("{label} ({url})"),
                style: MarkdownInlineStyle {
                    italic: true,
                    link: Some(url.clone()),
                    ..MarkdownInlineStyle::default()
                },
            }];
            inline_element(
                &runs,
                source_start,
                top_index,
                block_slot,
                id,
                style,
                style.font_size,
                style.line_height,
                FontWeight::NORMAL,
                cache,
            )
        }
        MarkdownBlock::Rule => {
            let mut rule = div().id(id).w_full().h(1.0);
            if let Some(color) = style.border_color {
                rule = rule.bg(color);
            }
            rule
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn render_blocks(
    blocks: &[MarkdownBlock],
    source_start: usize,
    slot: &mut u32,
    root_id: ElementId,
    top_index: usize,
    depth: usize,
    style: MarkdownStyle,
    cache: &mut HashMap<FlatKey, StyledText>,
) -> Element {
    let (id, _) = allocate_id(root_id, top_index, depth, slot);
    let children = blocks
        .iter()
        .map(|block| {
            render_block(
                block,
                source_start,
                slot,
                root_id,
                top_index,
                depth,
                style,
                cache,
            )
        })
        .collect::<Vec<_>>();
    div()
        .id(id)
        .w_full()
        .min_w(0.0)
        .flex_col()
        .gap(style.block_gap.min(8.0))
        .children(children)
}

#[allow(clippy::too_many_arguments)]
fn inline_element(
    runs: &[MarkdownInlineRun],
    source_start: usize,
    top_index: usize,
    cache_slot: u32,
    id: ElementId,
    style: MarkdownStyle,
    font_size: f32,
    line_height: f32,
    weight: FontWeight,
    cache: &mut HashMap<FlatKey, StyledText>,
) -> Element {
    let key = FlatKey {
        source_start,
        top_index,
        slot: cache_slot,
    };
    let styled = cache
        .entry(key)
        .or_insert_with(|| flatten_runs(runs, style))
        .clone();
    let mut element = styled
        .into_element()
        .id(id)
        .w_full()
        .min_w(0.0)
        .wrap()
        .text_size(font_size)
        .line_height(line_height)
        .font_weight(weight);
    if let Some(color) = style.text_color {
        element = element.text_color(color);
    }
    element
}

fn flatten_runs(runs: &[MarkdownInlineRun], style: MarkdownStyle) -> StyledText {
    let bytes = runs.iter().map(|run| run.text.len()).sum();
    let mut content = String::with_capacity(bytes);
    let mut highlights = Vec::with_capacity(runs.len().min(MAX_TEXT_HIGHLIGHTS));
    for run in runs {
        let start = content.len();
        content.push_str(&run.text);
        let end = content.len();
        if start == end || highlights.len() == MAX_TEXT_HIGHLIGHTS {
            continue;
        }
        let mut highlight = HighlightStyle::default();
        if run.style.bold {
            highlight = highlight.font_bold();
        }
        if run.style.italic {
            highlight = highlight.italic();
        }
        if run.style.strikethrough {
            highlight = highlight.strikethrough();
        }
        if run.style.code {
            highlight = highlight.font_family(FontFamily::Monospace);
            if let Some(color) = style.code_text_color {
                highlight = highlight.color(color);
            }
            if let Some(background) = style.code_background {
                highlight = highlight.background(background);
            }
        }
        if run.style.link.is_some() {
            highlight = highlight.underline();
            if let Some(color) = style.link_color {
                highlight = highlight.color(color).underline_color(color);
            }
        }
        highlights.push((start..end, highlight));
    }
    StyledText::new(Arc::<str>::from(content)).with_highlights(highlights)
}

fn heading_metrics(level: u8, style: MarkdownStyle) -> (f32, f32) {
    let scale = match level {
        1 => 1.75,
        2 => 1.45,
        3 => 1.25,
        4 => 1.12,
        5 => 1.0,
        _ => 0.92,
    };
    let size = (style.font_size * scale).max(11.0);
    (size, (size * 1.3).max(style.line_height))
}

fn derived_id(root: ElementId, top_index: usize, depth: usize, slot: u32) -> ElementId {
    let mut hash = root.as_u64()
        ^ (top_index as u64).wrapping_mul(0x9e37_79b9_7f4a_7c15)
        ^ (depth as u64).rotate_left(19)
        ^ u64::from(slot).rotate_left(37)
        ^ 0x6d61_726b_646f_776e;
    hash ^= hash >> 30;
    hash = hash.wrapping_mul(0xbf58_476d_1ce4_e5b9);
    hash ^= hash >> 27;
    hash = hash.wrapping_mul(0x94d0_49bb_1331_11eb);
    hash ^= hash >> 31;
    if hash == u64::MAX || hash == root.as_u64() {
        hash ^= 0xa5a5_5a5a_d3c4_b2e1;
    }
    ElementId::new(hash)
}

fn allocate_id(
    root: ElementId,
    top_index: usize,
    depth: usize,
    slot: &mut u32,
) -> (ElementId, u32) {
    let allocated = *slot;
    *slot = slot.saturating_add(1);
    (derived_id(root, top_index, depth, allocated), allocated)
}

fn bounded_source(source: &str) -> (&str, bool) {
    if source.len() <= MAX_MARKDOWN_SOURCE_BYTES {
        return (source, false);
    }
    let mut end = MAX_MARKDOWN_SOURCE_BYTES;
    while !source.is_char_boundary(end) {
        end -= 1;
    }
    (&source[..end], true)
}

fn finite_at_least(value: f32, minimum: f32, fallback: f32) -> f32 {
    if value.is_finite() {
        value.max(minimum)
    } else {
        fallback
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(target_os = "macos")]
    use crate::{Application, View, ViewContext, WindowOptions};

    #[cfg(target_os = "macos")]
    struct WrappedMarkdownView(Markdown);

    #[cfg(target_os = "macos")]
    impl View for WrappedMarkdownView {
        fn render(&mut self, _cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
            div()
                .size_full()
                .p_2()
                .child(self.0.element("wrapped-markdown"))
        }
    }

    #[test]
    fn retained_document_reuses_settled_flattened_text() {
        let mut markdown = Markdown::with_text("First.\n\nSecond.\n\nThird");
        let _ = markdown.element("markdown");
        let before = markdown.flat_cache.len();
        let update = markdown.set_text("First.\n\nSecond.\n\nThird grows.");
        assert!(update.appended);
        assert!(update.reparsed_from > 0);
        assert!(!markdown.flat_cache.is_empty());
        assert!(markdown.flat_cache.len() < before);
        let _ = markdown.element("markdown");
        assert!(markdown.flat_cache.len() >= before);
    }

    #[test]
    fn streaming_display_mends_then_settles() {
        let mut markdown = Markdown::new();
        markdown.set_streaming(true);
        markdown.set_text("Now **bold");
        assert!(markdown.display_tail.is_some());
        markdown.set_streaming(false);
        assert!(markdown.display_tail.is_none());
    }

    #[test]
    fn streaming_tail_replaces_every_block_from_one_source_group() {
        let mut markdown = Markdown::new();
        markdown.set_streaming(true);
        markdown.set_text("before ![alt](https://example.com/image.png) after **bold");
        let display_count = markdown.block_count();
        let canonical_prefix = markdown.canonical_display_prefix_len();
        let tail_count = markdown.display_tail.as_ref().map_or(0, Vec::len);
        assert_eq!(display_count, canonical_prefix + tail_count);
        assert_eq!(canonical_prefix, 0);
    }

    #[test]
    fn one_source_group_keeps_distinct_flattened_blocks() {
        let mut markdown =
            Markdown::with_text("before ![alt](https://example.com/image.png) after");
        let _ = markdown.element("markdown");
        let content = markdown
            .flat_cache
            .values()
            .map(|styled| styled.content().as_ref())
            .collect::<Vec<_>>();
        assert_eq!(content.len(), 3);
        assert!(content.contains(&"before "));
        assert!(content.contains(&"alt (https://example.com/image.png)"));
        assert!(content.contains(&" after"));
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn native_markdown_wraps_to_its_available_width() {
        let markdown = Markdown::with_text(
            "This is a deliberately long Markdown paragraph that must wrap across several lines.",
        );
        let (mut cx, view) = Application::new()
            .into_test_context(
                WindowOptions::default().size(180.0, 140.0),
                WrappedMarkdownView(markdown),
            )
            .unwrap();
        let bounds = cx
            .visual(view.window_handle())
            .unwrap()
            .element_bounds("wrapped-markdown")
            .unwrap();
        assert!(bounds.width <= 164.0);
        assert!(bounds.height > MarkdownStyle::default().line_height * 2.0);
    }

    #[test]
    fn source_limit_preserves_utf8_boundaries() {
        let source = format!("{}🙂", "a".repeat(MAX_MARKDOWN_SOURCE_BYTES - 1));
        let mut markdown = Markdown::new();
        let update = markdown.set_text(&source);
        assert!(update.truncated);
        assert!(markdown.text().is_char_boundary(markdown.text().len()));
        assert!(markdown.text().len() <= MAX_MARKDOWN_SOURCE_BYTES);
    }

    #[test]
    fn style_changes_invalidate_only_render_cache() {
        let mut markdown = Markdown::with_text("A **bold** paragraph.");
        let _ = markdown.element("markdown");
        assert!(!markdown.flat_cache.is_empty());
        assert!(markdown.set_style(MarkdownStyle::default().font_size(18.0)));
        assert!(markdown.flat_cache.is_empty());
        assert_eq!(markdown.block_count(), 1);
    }
}