Skip to main content

slt/context/widgets_display/
status.rs

1use super::*;
2
3impl Context {
4    /// Render an alert banner with icon and level-based coloring.
5    ///
6    /// Argument order is `(message, level)` — message first, then the
7    /// [`AlertLevel`](crate::widgets::AlertLevel). This is the executable
8    /// proof that [API_DESIGN.md](https://github.com/subinium/superlighttui/blob/main/docs/API_DESIGN.md)
9    /// Rule 3 matches the shipped signature.
10    ///
11    /// # Example
12    ///
13    /// ```no_run
14    /// # use slt::AlertLevel;
15    /// # slt::run(|ui: &mut slt::Context| {
16    /// ui.alert("Disk full", AlertLevel::Error);
17    /// ui.alert("Saved", AlertLevel::Success);
18    /// # });
19    /// ```
20    pub fn alert(&mut self, message: &str, level: crate::widgets::AlertLevel) -> Response {
21        use crate::widgets::AlertLevel;
22
23        let theme = self.theme;
24        let (icon, color) = match level {
25            AlertLevel::Info => ("ℹ", theme.accent),
26            AlertLevel::Success => ("✓", theme.success),
27            AlertLevel::Warning => ("⚠", theme.warning),
28            AlertLevel::Error => ("✕", theme.error),
29        };
30
31        let focused = self.register_focusable();
32        let key_dismiss = if focused {
33            let consumed: Vec<usize> = self
34                .available_key_presses()
35                .filter_map(|(i, key)| {
36                    if matches!(key.code, KeyCode::Enter | KeyCode::Char('x')) {
37                        Some(i)
38                    } else {
39                        None
40                    }
41                })
42                .collect();
43            let dismissed = !consumed.is_empty();
44            self.consume_indices(consumed);
45            dismissed
46        } else {
47            false
48        };
49
50        let mut response = self.container().col(|ui| {
51            ui.line(|ui| {
52                let mut icon_text = String::with_capacity(icon.len() + 2);
53                icon_text.push(' ');
54                icon_text.push_str(icon);
55                icon_text.push(' ');
56                ui.text(icon_text).fg(color).bold();
57                ui.text(message).grow(1);
58                ui.text(" [×] ").dim();
59            });
60        });
61        response.focused = focused;
62        if key_dismiss {
63            response.clicked = true;
64        }
65
66        response
67    }
68
69    /// Yes/No confirmation dialog. Returns Response with .clicked=true when answered.
70    ///
71    /// `result` is set to true for Yes, false for No.
72    ///
73    /// # Examples
74    /// ```
75    /// # use slt::*;
76    /// # TestBackend::new(80, 24).render(|ui| {
77    /// let mut answer = false;
78    /// let r = ui.confirm("Delete this file?", &mut answer);
79    /// if r.clicked && answer { /* user confirmed */ }
80    /// # });
81    /// ```
82    pub fn confirm(&mut self, question: &str, result: &mut bool) -> Response {
83        let focused = self.register_focusable();
84        let mut is_yes = *result;
85        let mut clicked = false;
86
87        // 1) Keyboard hit-test runs first so it can mutate `is_yes`.
88        if focused {
89            let mut consumed_indices = Vec::new();
90            for (i, key) in self.available_key_presses() {
91                match key.code {
92                    KeyCode::Char('y') => {
93                        is_yes = true;
94                        *result = true;
95                        clicked = true;
96                        consumed_indices.push(i);
97                    }
98                    KeyCode::Char('n') => {
99                        is_yes = false;
100                        *result = false;
101                        clicked = true;
102                        consumed_indices.push(i);
103                    }
104                    KeyCode::Tab | KeyCode::BackTab | KeyCode::Left | KeyCode::Right => {
105                        is_yes = !is_yes;
106                        *result = is_yes;
107                        consumed_indices.push(i);
108                    }
109                    KeyCode::Enter => {
110                        *result = is_yes;
111                        clicked = true;
112                        consumed_indices.push(i);
113                    }
114                    _ => {}
115                }
116            }
117            self.consume_indices(consumed_indices);
118        }
119
120        // 2) Mouse hit-test runs *before* style computation and rendering so
121        // the visual feedback for `[Yes]` / `[No]` reflects the click in the
122        // same frame the click happened. Predict the row's interaction id
123        // (the next slot the row will allocate) and look up the previous
124        // frame's rect from `prev_hit_map`. On the first frame the row has
125        // no entry yet, so we fall back to assuming the row starts at (0,0)
126        // — same behaviour as the prior implementation.
127        let q_width = UnicodeWidthStr::width(question) as u32;
128        if !clicked && let Some((mx, my)) = self.click_pos {
129            let next_id = self.rollback.interaction_count;
130            let prev_rect = self.prev_hit_map.get(next_id).copied();
131            let row_x = prev_rect.map(|r| r.x).unwrap_or(0);
132            let in_row_y = match prev_rect {
133                Some(r) if r.height > 0 => my >= r.y && my < r.bottom(),
134                _ => true,
135            };
136            if in_row_y {
137                let yes_start = row_x + q_width + 1;
138                let yes_end = yes_start + 5;
139                let no_start = yes_end + 1;
140                let no_end = no_start + 4; // "[No]" = 4 display columns
141                if mx >= yes_start && mx < yes_end {
142                    is_yes = true;
143                    *result = true;
144                    clicked = true;
145                } else if mx >= no_start && mx < no_end {
146                    is_yes = false;
147                    *result = false;
148                    clicked = true;
149                }
150            }
151        }
152
153        // 3) Style computation reads the now-mutated `is_yes`.
154        let yes_style = if is_yes {
155            if focused {
156                Style::new().fg(self.theme.bg).bg(self.theme.success).bold()
157            } else {
158                Style::new().fg(self.theme.success).bold()
159            }
160        } else {
161            Style::new().fg(self.theme.text_dim)
162        };
163        let no_style = if !is_yes {
164            if focused {
165                Style::new().fg(self.theme.bg).bg(self.theme.error).bold()
166            } else {
167                Style::new().fg(self.theme.error).bold()
168            }
169        } else {
170            Style::new().fg(self.theme.text_dim)
171        };
172
173        // 4) Render with the post-hit-test styles.
174        let mut response = self.row(|ui| {
175            ui.text(question);
176            ui.text(" ");
177            ui.styled("[Yes]", yes_style);
178            ui.text(" ");
179            ui.styled("[No]", no_style);
180        });
181
182        response.focused = focused;
183        response.clicked = clicked;
184        response.changed = clicked;
185        response
186    }
187
188    /// Begin building a breadcrumb navigation bar with the default separator
189    /// (` › `).
190    ///
191    /// Returns a [`Breadcrumb`] builder that auto-renders on `Drop`. Chain
192    /// `.separator(s)` for a custom separator and `.color(c)` for a custom
193    /// link color. Call `.show()` to render and obtain a
194    /// [`BreadcrumbResponse`] carrying `clicked_segment` and `Deref<Response>`.
195    ///
196    /// # Example
197    ///
198    /// ```no_run
199    /// # slt::run(|ui: &mut slt::Context| {
200    /// // simple
201    /// ui.breadcrumb(&["Home", "Settings", "Profile"]);
202    ///
203    /// // with custom separator + color, capturing the response
204    /// let r = ui
205    ///     .breadcrumb(&["Home", "src", "lib.rs"])
206    ///     .separator(" > ")
207    ///     .show();
208    /// if let Some(i) = r.clicked_segment {
209    ///     // navigate to segment `i`
210    /// }
211    /// # });
212    /// ```
213    pub fn breadcrumb<'a>(&'a mut self, segments: &'a [&'a str]) -> Breadcrumb<'a> {
214        Breadcrumb::new(self, segments)
215    }
216
217    /// Collapsible section that toggles on click, Enter, or Space.
218    pub fn accordion(
219        &mut self,
220        title: &str,
221        open: &mut bool,
222        f: impl FnOnce(&mut Context),
223    ) -> Response {
224        let theme = self.theme;
225        let focused = self.register_focusable();
226        let old_open = *open;
227        let toggled_from_key = self.consume_activation_keys(focused);
228        if toggled_from_key {
229            *open = !*open;
230        }
231
232        let icon = if *open { "▾" } else { "▸" };
233        let title_color = if focused { theme.primary } else { theme.text };
234
235        let mut response = self.container().col(|ui| {
236            ui.line(|ui| {
237                ui.text(icon).fg(title_color);
238                let mut title_text = String::with_capacity(1 + title.len());
239                title_text.push(' ');
240                title_text.push_str(title);
241                ui.text(title_text).bold().fg(title_color);
242            });
243        });
244
245        if response.clicked {
246            *open = !*open;
247        }
248
249        if *open {
250            let indent = self.theme.spacing.sm();
251            let _ = self.container().pl(indent).col(f);
252        }
253
254        response.focused = focused;
255        response.changed = *open != old_open;
256        response
257    }
258
259    /// Render a key-value definition list with aligned columns.
260    ///
261    /// Keys are right-padded to the widest key so the value column lines up.
262    ///
263    /// # Example
264    ///
265    /// ```no_run
266    /// # slt::run(|ui: &mut slt::Context| {
267    /// ui.definition_list(&[
268    ///     ("Name", "SuperLightTUI"),
269    ///     ("Version", "0.21.1"),
270    ///     ("License", "MIT"),
271    /// ]);
272    /// # });
273    /// ```
274    pub fn definition_list(&mut self, items: &[(&str, &str)]) -> Response {
275        let max_key_width = items
276            .iter()
277            .map(|(k, _)| UnicodeWidthStr::width(*k))
278            .max()
279            .unwrap_or(0);
280
281        let _ = self.col(|ui| {
282            for (key, value) in items {
283                ui.line(|ui| {
284                    let key_display_w = UnicodeWidthStr::width(*key);
285                    let pad = max_key_width.saturating_sub(key_display_w);
286                    let mut padded = String::with_capacity(key.len() + pad);
287                    padded.extend(std::iter::repeat_n(' ', pad));
288                    padded.push_str(key);
289                    ui.text(padded).dim();
290                    ui.text("  ");
291                    ui.text(*value);
292                });
293            }
294        });
295
296        Response::none()
297    }
298
299    /// Render a horizontal divider with a centered text label.
300    ///
301    /// The label is padded with one space on each side and centered between
302    /// two `─` separator runs spanning the available width.
303    ///
304    /// # Example
305    ///
306    /// ```no_run
307    /// # slt::run(|ui: &mut slt::Context| {
308    /// ui.divider_text("Settings");
309    /// # });
310    /// ```
311    pub fn divider_text(&mut self, label: &str) -> Response {
312        let w = self.width();
313        let label_len = UnicodeWidthStr::width(label) as u32;
314        // Reserve `label_len + 2` for the label and its single-space padding on
315        // each side, then split the remaining width evenly. On odd widths the
316        // right separator is one cell longer (no asymmetry that's visible).
317        let total_separator = w.saturating_sub(label_len + 2);
318        let left_len = total_separator / 2;
319        let right_len = total_separator - left_len;
320        let left: String = "─".repeat(left_len as usize);
321        let right: String = "─".repeat(right_len as usize);
322        let theme = self.theme;
323        self.line(|ui| {
324            ui.text(&left).fg(theme.border);
325            let mut label_text = String::with_capacity(label.len() + 2);
326            label_text.push(' ');
327            label_text.push_str(label);
328            label_text.push(' ');
329            ui.text(label_text).fg(theme.text);
330            ui.text(&right).fg(theme.border);
331        });
332
333        Response::none()
334    }
335
336    /// Render a badge with the theme's primary color.
337    ///
338    /// Returns a [`Response`] carrying real `hovered` / `right_clicked` state
339    /// for the badge's rect, so callers can attach `.on_hover(...)` tooltips.
340    /// Prior to v0.21.0 this always returned [`Response::none()`]; statement-form
341    /// callers (`ui.badge("NEW");`) compile unchanged.
342    ///
343    /// # Example
344    ///
345    /// ```no_run
346    /// # slt::run(|ui: &mut slt::Context| {
347    /// let r = ui.badge("NEW");
348    /// if r.hovered { /* attach a tooltip */ }
349    /// # });
350    /// ```
351    pub fn badge(&mut self, label: &str) -> Response {
352        let theme = self.theme;
353        self.badge_colored(label, theme.primary)
354    }
355
356    /// Render a badge with a custom background color.
357    ///
358    /// Foreground is auto-selected for contrast via [`Color::contrast_fg`].
359    ///
360    /// Returns a [`Response`] carrying real `hovered` / `right_clicked` state
361    /// for the badge's rect, so callers can attach `.on_hover(...)` tooltips.
362    /// Prior to v0.21.0 this always returned [`Response::none()`]; statement-form
363    /// callers compile unchanged.
364    ///
365    /// # Example
366    ///
367    /// ```no_run
368    /// # use slt::Color;
369    /// # slt::run(|ui: &mut slt::Context| {
370    /// let r = ui.badge_colored("ALPHA", Color::Magenta);
371    /// if r.hovered { /* attach a tooltip */ }
372    /// # });
373    /// ```
374    pub fn badge_colored(&mut self, label: &str, color: Color) -> Response {
375        let fg = Color::contrast_fg(color);
376        let mut label_text = String::with_capacity(label.len() + 2);
377        label_text.push(' ');
378        label_text.push_str(label);
379        label_text.push(' ');
380        // Reserve the interaction slot *before* the text so the marker
381        // attaches to the badge's rect (same pattern as `spinner` / `gauge`).
382        let response = self.interaction();
383        self.text(label_text).fg(fg).bg(color);
384
385        response
386    }
387
388    /// Render a keyboard shortcut hint with reversed styling.
389    ///
390    /// Returns a [`Response`] carrying real `hovered` / `right_clicked` state
391    /// for the hint's rect, so callers can attach `.on_hover(...)` tooltips.
392    /// Prior to v0.21.0 this always returned [`Response::none()`]; statement-form
393    /// callers compile unchanged.
394    ///
395    /// # Example
396    ///
397    /// ```no_run
398    /// # slt::run(|ui: &mut slt::Context| {
399    /// ui.line(|ui| {
400    ///     ui.text("Quit: ");
401    ///     let r = ui.key_hint("Ctrl+Q");
402    ///     if r.hovered { /* attach a tooltip */ }
403    /// });
404    /// # });
405    /// ```
406    pub fn key_hint(&mut self, key: &str) -> Response {
407        let theme = self.theme;
408        let mut key_text = String::with_capacity(key.len() + 2);
409        key_text.push(' ');
410        key_text.push_str(key);
411        key_text.push(' ');
412        // Reserve the interaction slot *before* the text so the marker
413        // attaches to the hint's rect.
414        let response = self.interaction();
415        self.text(key_text).reversed().fg(theme.text_dim);
416
417        response
418    }
419
420    /// Render a label-value stat pair.
421    ///
422    /// Renders as a column: a dim label above a bold value. Pair multiple
423    /// stats in a [`row`](Self::row) for a compact dashboard strip.
424    ///
425    /// Returns a [`Response`] carrying real `hovered` / `clicked` /
426    /// `right_clicked` state for the stat's column rect, so callers can attach
427    /// `.on_hover(...)` tooltips. Prior to v0.21.0 this always returned
428    /// [`Response::none()`]; statement-form callers compile unchanged.
429    ///
430    /// # Example
431    ///
432    /// ```no_run
433    /// # slt::run(|ui: &mut slt::Context| {
434    /// ui.row(|ui| {
435    ///     let r = ui.stat("Users", "1.2k");
436    ///     if r.hovered { /* attach a tooltip */ }
437    ///     ui.stat("Revenue", "$8,420");
438    /// });
439    /// # });
440    /// ```
441    pub fn stat(&mut self, label: &str, value: &str) -> Response {
442        self.col(|ui| {
443            ui.text(label).dim();
444            ui.text(value).bold();
445        })
446    }
447
448    /// Render a stat pair with a custom value color.
449    ///
450    /// Returns a [`Response`] carrying real `hovered` / `clicked` /
451    /// `right_clicked` state for the stat's column rect, so callers can attach
452    /// `.on_hover(...)` tooltips. Prior to v0.21.0 this always returned
453    /// [`Response::none()`]; statement-form callers compile unchanged.
454    ///
455    /// # Example
456    ///
457    /// ```no_run
458    /// # use slt::Color;
459    /// # slt::run(|ui: &mut slt::Context| {
460    /// let r = ui.stat_colored("Errors", "0", Color::Green);
461    /// if r.hovered { /* attach a tooltip */ }
462    /// # });
463    /// ```
464    pub fn stat_colored(&mut self, label: &str, value: &str, color: Color) -> Response {
465        self.col(|ui| {
466            ui.text(label).dim();
467            ui.text(value).bold().fg(color);
468        })
469    }
470
471    /// Render a stat pair with an up/down trend arrow.
472    ///
473    /// The arrow color follows the theme: `success` for [`Trend::Up`],
474    /// `error` for [`Trend::Down`].
475    ///
476    /// Returns a [`Response`] carrying real `hovered` / `clicked` /
477    /// `right_clicked` state for the stat's column rect, so callers can attach
478    /// `.on_hover(...)` tooltips. Prior to v0.21.0 this always returned
479    /// [`Response::none()`]; statement-form callers compile unchanged.
480    ///
481    /// [`Trend::Up`]: crate::widgets::Trend::Up
482    /// [`Trend::Down`]: crate::widgets::Trend::Down
483    ///
484    /// # Example
485    ///
486    /// ```no_run
487    /// # use slt::widgets::Trend;
488    /// # slt::run(|ui: &mut slt::Context| {
489    /// let r = ui.stat_trend("MRR", "$24.5k", Trend::Up);
490    /// if r.hovered { /* attach a tooltip */ }
491    /// ui.stat_trend("Churn", "1.8%", Trend::Down);
492    /// # });
493    /// ```
494    pub fn stat_trend(
495        &mut self,
496        label: &str,
497        value: &str,
498        trend: crate::widgets::Trend,
499    ) -> Response {
500        let theme = self.theme;
501        let (arrow, color) = match trend {
502            crate::widgets::Trend::Up => ("↑", theme.success),
503            crate::widgets::Trend::Down => ("↓", theme.error),
504        };
505        self.col(|ui| {
506            ui.text(label).dim();
507            ui.line(|ui| {
508                ui.text(value).bold();
509                let mut arrow_text = String::with_capacity(1 + arrow.len());
510                arrow_text.push(' ');
511                arrow_text.push_str(arrow);
512                ui.text(arrow_text).fg(color);
513            });
514        })
515    }
516
517    /// Render a centered empty-state placeholder.
518    ///
519    /// Title is rendered prominently; description is dimmed below. Both are
520    /// centered horizontally and vertically inside the available space.
521    ///
522    /// Returns a [`Response`] carrying real `hovered` / `clicked` /
523    /// `right_clicked` state for the placeholder rect, so callers can attach
524    /// `.on_hover(...)` tooltips. Prior to v0.21.0 this always returned
525    /// [`Response::none()`]; statement-form callers compile unchanged.
526    ///
527    /// # Example
528    ///
529    /// ```no_run
530    /// # let items: Vec<&str> = vec![];
531    /// # slt::run(|ui: &mut slt::Context| {
532    /// if items.is_empty() {
533    ///     ui.empty_state("No items yet", "Press 'a' to add one");
534    /// }
535    /// # });
536    /// ```
537    pub fn empty_state(&mut self, title: &str, description: &str) -> Response {
538        self.container().center().col(|ui| {
539            ui.text(title).align(Align::Center);
540            ui.text(description).dim().align(Align::Center);
541        })
542    }
543
544    /// Render a centered empty-state placeholder with an action button.
545    ///
546    /// Returns a [`Response`] whose `clicked` field is `true` on the frame
547    /// the action button is activated. As of v0.21.0 the response also carries
548    /// real `hovered` / `right_clicked` state (and the laid-out `rect`) for the
549    /// placeholder area, so callers can attach `.on_hover(...)` tooltips. The
550    /// `clicked` / `changed` fields still track the action button specifically,
551    /// not the whole placeholder.
552    ///
553    /// # Example
554    ///
555    /// ```no_run
556    /// # let items: Vec<&str> = vec![];
557    /// # slt::run(|ui: &mut slt::Context| {
558    /// if items.is_empty() {
559    ///     let r = ui.empty_state_action("No items yet", "Get started", "Add first item");
560    ///     if r.clicked {
561    ///         // open create flow
562    ///     }
563    /// }
564    /// # });
565    /// ```
566    pub fn empty_state_action(
567        &mut self,
568        title: &str,
569        description: &str,
570        action_label: &str,
571    ) -> Response {
572        let mut clicked = false;
573        // The container response carries hover / right-click / rect for the
574        // whole placeholder area; `clicked` still tracks the action button.
575        let mut response = self.container().center().col(|ui| {
576            ui.text(title).align(Align::Center);
577            ui.text(description).dim().align(Align::Center);
578            if ui.button(action_label).clicked {
579                clicked = true;
580            }
581        });
582
583        response.clicked = clicked;
584        response.changed = clicked;
585        response
586    }
587
588    /// Begin building a syntax-highlighted code block.
589    ///
590    /// Chain `.lang(...)` for language-aware highlighting and `.numbered()`
591    /// for a line-number gutter. The returned [`CodeBlock`] auto-renders when
592    /// dropped, so a bare `ui.code_block(code);` produces a default block.
593    /// Call `.show()` (instead of dropping) to capture the [`Response`].
594    ///
595    /// This is the consuming-builder shape shared with [`Context::gauge`] /
596    /// [`Context::breadcrumb`] — see [API_DESIGN.md](https://github.com/subinium/superlighttui/blob/main/docs/API_DESIGN.md) Rule 1.
597    ///
598    /// # Example
599    ///
600    /// ```no_run
601    /// # slt::run(|ui: &mut slt::Context| {
602    /// ui.code_block("let x = 1;");
603    /// let r = ui.code_block("fn main() {}").lang("rust").numbered().show();
604    /// if r.hovered { /* attach tooltip */ }
605    /// # });
606    /// ```
607    pub fn code_block<'a>(&'a mut self, code: &'a str) -> CodeBlock<'a> {
608        CodeBlock::new(self, code)
609    }
610
611    /// Render a code block with language-aware syntax highlighting.
612    #[deprecated(since = "0.21.0", note = "use `code_block(code).lang(lang)`")]
613    pub fn code_block_lang(&mut self, code: &str, lang: &str) -> Response {
614        render_code_block(self, code, lang, false)
615    }
616
617    /// Render a code block with line numbers and keyword highlighting.
618    #[deprecated(since = "0.21.0", note = "use `code_block(code).numbered()`")]
619    pub fn code_block_numbered(&mut self, code: &str) -> Response {
620        render_code_block(self, code, "", true)
621    }
622
623    /// Render a code block with line numbers and language-aware highlighting.
624    #[deprecated(
625        since = "0.21.0",
626        note = "use `code_block(code).lang(lang).numbered()`"
627    )]
628    pub fn code_block_numbered_lang(&mut self, code: &str, lang: &str) -> Response {
629        render_code_block(self, code, lang, true)
630    }
631}
632
633/// Syntax-highlighted code block builder. Auto-renders on `Drop`.
634///
635/// Constructed via [`Context::code_block`]. Chain `.lang(...)` for
636/// language-aware highlighting and `.numbered()` for a line-number gutter.
637/// Drop the value to render without capturing a response, or call
638/// [`Self::show`] to render and obtain a [`Response`].
639///
640/// Consuming-builder shape, mirroring [`Gauge`](super::Gauge) /
641/// [`Breadcrumb`]: `Drop` is intentional so `ui.code_block(code);` is the
642/// idiomatic form when the response isn't needed (egui's `ui.add(...)` idiom).
643pub struct CodeBlock<'a> {
644    ctx: Option<&'a mut Context>,
645    code: &'a str,
646    lang: &'a str,
647    numbered: bool,
648}
649
650impl<'a> CodeBlock<'a> {
651    fn new(ctx: &'a mut Context, code: &'a str) -> Self {
652        Self {
653            ctx: Some(ctx),
654            code,
655            lang: "",
656            numbered: false,
657        }
658    }
659
660    /// Set the language for syntax highlighting (e.g. `"rust"`). Empty string
661    /// (the default) falls back to keyword-based highlighting.
662    pub fn lang(mut self, lang: &'a str) -> Self {
663        self.lang = lang;
664        self
665    }
666
667    /// Enable the line-number gutter.
668    pub fn numbered(mut self) -> Self {
669        self.numbered = true;
670        self
671    }
672
673    /// Render now and return the [`Response`].
674    pub fn show(mut self) -> Response {
675        // SAFETY: ctx is Some until Drop runs; show consumes self before Drop.
676        let ctx = self.ctx.take().expect("CodeBlock::show called twice");
677        render_code_block(ctx, self.code, self.lang, self.numbered)
678    }
679}
680
681impl Drop for CodeBlock<'_> {
682    fn drop(&mut self) {
683        if let Some(ctx) = self.ctx.take() {
684            let _ = render_code_block(ctx, self.code, self.lang, self.numbered);
685        }
686    }
687}
688
689/// Internal code-block rendering shared by the [`CodeBlock`] builder and the
690/// deprecated `code_block_*` aliases. Folds the language-aware and
691/// line-numbered paths on the `numbered` flag — no behavior change versus the
692/// previous separate `code_block_lang` / `code_block_numbered_lang` bodies.
693fn render_code_block(ctx: &mut Context, code: &str, lang: &str, numbered: bool) -> Response {
694    let theme = ctx.theme;
695    let pad = theme.spacing.xs();
696    let highlighted: Option<Vec<Vec<(String, Style)>>> =
697        crate::syntax::highlight_code(code, lang, &theme);
698
699    if numbered {
700        let lines: Vec<&str> = code.lines().collect();
701        let gutter_w = (lines.len().max(1).ilog10() + 1) as usize;
702        let _ = ctx
703            .bordered(Border::Rounded)
704            .bg(theme.surface)
705            .p(pad)
706            .col(|ui| {
707                if let Some(ref hl_lines) = highlighted {
708                    for (i, segs) in hl_lines.iter().enumerate() {
709                        ui.line(|ui| {
710                            ui.text(format!("{:>gutter_w$} │ ", i + 1))
711                                .fg(theme.text_dim);
712                            for (text, style) in segs {
713                                ui.styled(text, *style);
714                            }
715                        });
716                    }
717                } else {
718                    for (i, line) in lines.iter().enumerate() {
719                        ui.line(|ui| {
720                            ui.text(format!("{:>gutter_w$} │ ", i + 1))
721                                .fg(theme.text_dim);
722                            render_highlighted_line(ui, line);
723                        });
724                    }
725                }
726            });
727    } else {
728        let _ = ctx
729            .bordered(Border::Rounded)
730            .bg(theme.surface)
731            .p(pad)
732            .col(|ui| {
733                if let Some(ref lines) = highlighted {
734                    render_tree_sitter_lines(ui, lines);
735                } else {
736                    for line in code.lines() {
737                        ui.line(|ui| render_highlighted_line(ui, line));
738                    }
739                }
740            });
741    }
742
743    Response::none()
744}
745
746/// Breadcrumb navigation bar builder. Auto-renders on `Drop`.
747///
748/// Constructed via [`Context::breadcrumb`]. Chain `.separator(s)` to override
749/// the default ` › ` separator and `.color(c)` to override the link color.
750/// Drop the value to render without capturing a response, or call
751/// [`Self::show`] to render and obtain a [`BreadcrumbResponse`].
752///
753/// `Drop` is intentional: `ui.breadcrumb(&["Home", "src"]).separator(" > ");`
754/// is the idiomatic form when the response isn't needed.
755pub struct Breadcrumb<'a> {
756    ctx: Option<&'a mut Context>,
757    segments: &'a [&'a str],
758    separator: &'a str,
759    color: Option<Color>,
760}
761
762impl<'a> Breadcrumb<'a> {
763    pub(super) fn new(ctx: &'a mut Context, segments: &'a [&'a str]) -> Self {
764        Self {
765            ctx: Some(ctx),
766            segments,
767            separator: " › ",
768            color: None,
769        }
770    }
771
772    /// Set the separator string between segments (default: ` › `).
773    pub fn separator(mut self, sep: &'a str) -> Self {
774        self.separator = sep;
775        self
776    }
777
778    /// Override the link (clickable segment) color. Defaults to `theme.primary`.
779    pub fn color(mut self, color: Color) -> Self {
780        self.color = Some(color);
781        self
782    }
783
784    /// Render now and return the [`BreadcrumbResponse`].
785    pub fn show(mut self) -> BreadcrumbResponse {
786        let ctx = self.ctx.take().expect("Breadcrumb::show called twice");
787        render_breadcrumb(ctx, self.segments, self.separator, self.color)
788    }
789}
790
791impl Drop for Breadcrumb<'_> {
792    fn drop(&mut self) {
793        if let Some(ctx) = self.ctx.take() {
794            let _ = render_breadcrumb(ctx, self.segments, self.separator, self.color);
795        }
796    }
797}
798
799fn render_breadcrumb(
800    ctx: &mut Context,
801    segments: &[&str],
802    separator: &str,
803    color_override: Option<Color>,
804) -> BreadcrumbResponse {
805    let theme = ctx.theme;
806    let last_idx = segments.len().saturating_sub(1);
807    let mut clicked_segment: Option<usize> = None;
808    let link_color = color_override.unwrap_or(theme.primary);
809
810    let response = ctx.row(|ui| {
811        for (i, segment) in segments.iter().enumerate() {
812            let is_last = i == last_idx;
813            if is_last {
814                ui.text(*segment).bold();
815            } else {
816                let focused = ui.register_focusable();
817                let resp = ui.interaction();
818                let activated = resp.clicked || ui.consume_activation_keys(focused);
819                let color = if resp.hovered || focused {
820                    theme.accent
821                } else {
822                    link_color
823                };
824                ui.text(*segment).fg(color).underline();
825                if activated {
826                    clicked_segment = Some(i);
827                }
828                ui.text(separator).dim();
829            }
830        }
831    });
832
833    BreadcrumbResponse {
834        response,
835        clicked_segment,
836    }
837}
838
839#[cfg(test)]
840mod code_block_tests {
841    use crate::test_utils::TestBackend;
842    use crate::widgets::AlertLevel;
843
844    #[test]
845    fn code_block_builder_renders_lang_and_gutter() {
846        let mut tb = TestBackend::new(40, 8);
847        tb.render(|ui| {
848            let _ = ui.code_block("let x = 1;").lang("rust").numbered().show();
849        });
850        tb.assert_contains("let");
851        // Line-number gutter from the numbered path (`status.rs` render).
852        tb.assert_contains("1 │");
853    }
854
855    #[test]
856    fn code_block_default_drop_renders() {
857        // Bare drop-render (no chain) must produce the same content as `.show()`.
858        let mut tb_drop = TestBackend::new(40, 8);
859        tb_drop.render(|ui| {
860            ui.code_block("a\nb");
861        });
862        let mut tb_show = TestBackend::new(40, 8);
863        tb_show.render(|ui| {
864            let _ = ui.code_block("a\nb").show();
865        });
866        assert_eq!(tb_drop.to_string(), tb_show.to_string());
867    }
868
869    #[test]
870    fn code_block_deprecated_alias_byte_identical() {
871        let code = "fn main() {}\nlet y = 2;";
872        let mut tb_builder = TestBackend::new(40, 8);
873        tb_builder.render(|ui| {
874            let _ = ui.code_block(code).lang("rust").numbered().show();
875        });
876        let mut tb_alias = TestBackend::new(40, 8);
877        tb_alias.render(|ui| {
878            #[allow(deprecated)]
879            let _ = ui.code_block_numbered_lang(code, "rust");
880        });
881        assert_eq!(
882            tb_builder.to_string(),
883            tb_alias.to_string(),
884            "deprecated alias must be behavior-preserving"
885        );
886    }
887
888    #[test]
889    fn alert_message_first_then_level() {
890        // Regression guard for the API_DESIGN.md arg-order drift: `(message,
891        // level)` is the shipped order. Compiles == doc order matches code.
892        let mut tb = TestBackend::new(40, 5);
893        tb.render(|ui| {
894            let _ = ui.alert("Disk full", AlertLevel::Error);
895        });
896        tb.assert_contains("Disk full");
897    }
898}