envision 0.16.0

A ratatui framework for collaborative TUI development with headless testing support
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
//! An interactive flame graph component for profiling data visualization.
//!
//! [`FlameGraph`] renders stack frames as horizontal bars where width is
//! proportional to time or samples. Supports zoom into subtrees and
//! search/highlight. Root is rendered at the top (icicle graph style).
//!
//! State is stored in [`FlameGraphState`], updated via [`FlameGraphMessage`],
//! and produces [`FlameGraphOutput`].
//!
//!
//! # Example
//!
//! ```rust
//! use envision::component::{
//!     Component, FlameGraph, FlameGraphState, FlameGraphMessage, FlameNode,
//! };
//! use ratatui::style::Color;
//!
//! let root = FlameNode::new("main()", 500)
//!     .with_color(Color::Red)
//!     .with_child(
//!         FlameNode::new("compute()", 300)
//!             .with_child(FlameNode::new("sort()", 200))
//!     )
//!     .with_child(FlameNode::new("io()", 100));
//! let mut state = FlameGraphState::with_root(root);
//!
//! // Navigate down into children
//! FlameGraph::update(&mut state, FlameGraphMessage::SelectDown);
//! assert_eq!(state.selected_depth(), 1);
//! ```

use super::{Component, EventContext, RenderContext};
use crate::input::{Event, Key};

mod node;
mod render;

pub use node::FlameNode;

/// Messages that can be sent to a FlameGraph component.
///
/// # Example
///
/// ```rust
/// use envision::component::{
///     Component, FlameGraph, FlameGraphState, FlameGraphMessage, FlameNode,
/// };
///
/// let root = FlameNode::new("main()", 500)
///     .with_child(FlameNode::new("compute()", 300));
/// let mut state = FlameGraphState::with_root(root);
/// FlameGraph::update(&mut state, FlameGraphMessage::SelectDown);
/// ```
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum FlameGraphMessage {
    /// Set the root frame.
    SetRoot(FlameNode),
    /// Clear the graph.
    Clear,
    /// Zoom into the selected frame.
    ZoomIn,
    /// Zoom back to parent.
    ZoomOut,
    /// Reset zoom to original root.
    ResetZoom,
    /// Move selection to parent depth.
    SelectUp,
    /// Move selection to child depth.
    SelectDown,
    /// Move selection to previous sibling at current depth.
    SelectLeft,
    /// Move selection to next sibling at current depth.
    SelectRight,
    /// Set search query for highlighting.
    SetSearch(String),
    /// Clear search query.
    ClearSearch,
}

/// Output messages from a FlameGraph component.
///
/// # Example
///
/// ```rust
/// use envision::component::{
///     Component, FlameGraph, FlameGraphState, FlameGraphMessage, FlameGraphOutput, FlameNode,
/// };
///
/// let root = FlameNode::new("main()", 500)
///     .with_child(FlameNode::new("compute()", 300));
/// let mut state = FlameGraphState::with_root(root);
///
/// let output = FlameGraph::update(&mut state, FlameGraphMessage::SelectDown);
/// assert!(matches!(output, Some(FlameGraphOutput::FrameSelected { .. })));
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum FlameGraphOutput {
    /// A frame was selected.
    FrameSelected {
        /// The label of the selected frame.
        label: String,
        /// The total value of the selected frame.
        value: u64,
        /// The self value of the selected frame (excluding children).
        self_value: u64,
    },
    /// Zoomed into a frame.
    ZoomedIn(String),
    /// Zoomed out from a frame.
    ZoomedOut,
}

/// State for a FlameGraph component.
///
/// Contains the root flame node, zoom stack, selection state, and search
/// configuration.
///
/// # Example
///
/// ```rust
/// use envision::component::{FlameGraphState, FlameNode};
///
/// let root = FlameNode::new("main()", 500)
///     .with_child(FlameNode::new("compute()", 300));
/// let state = FlameGraphState::with_root(root);
///
/// assert!(state.root().is_some());
/// assert_eq!(state.selected_depth(), 0);
/// assert_eq!(state.selected_index(), 0);
/// ```
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct FlameGraphState {
    /// The root frame.
    root: Option<FlameNode>,
    /// Stack of zoomed-into node labels (for zoom navigation).
    zoom_stack: Vec<String>,
    /// Selected depth level.
    selected_depth: usize,
    /// Selected frame index at current depth.
    selected_index: usize,
    /// Search query for highlighting.
    search_query: String,
    /// Optional title.
    title: Option<String>,
}

impl FlameGraphState {
    /// Creates an empty flame graph state.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::FlameGraphState;
    ///
    /// let state = FlameGraphState::new();
    /// assert!(state.root().is_none());
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a flame graph state with the given root frame.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let root = FlameNode::new("main()", 500);
    /// let state = FlameGraphState::with_root(root);
    /// assert!(state.root().is_some());
    /// assert_eq!(state.root().unwrap().label(), "main()");
    /// ```
    pub fn with_root(root: FlameNode) -> Self {
        Self {
            root: Some(root),
            ..Self::default()
        }
    }

    /// Sets the title (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let state = FlameGraphState::with_root(FlameNode::new("main()", 500))
    ///     .with_title("CPU Profile");
    /// assert_eq!(state.title(), Some("CPU Profile"));
    /// ```
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    // ---- Accessors ----

    /// Returns the root frame, if any.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let state = FlameGraphState::with_root(FlameNode::new("main()", 500));
    /// assert_eq!(state.root().unwrap().label(), "main()");
    /// ```
    pub fn root(&self) -> Option<&FlameNode> {
        self.root.as_ref()
    }

    /// Returns a mutable reference to the root frame, if any.
    ///
    /// This is safe because the flame node tree is simple data.
    /// The zoom stack references nodes by label, so mutating node
    /// values or children does not corrupt navigation state.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    /// use ratatui::style::Color;
    ///
    /// let root = FlameNode::new("main()", 500);
    /// let mut state = FlameGraphState::with_root(root);
    /// if let Some(r) = state.root_mut() {
    ///     *r = r.clone().with_color(Color::Red);
    /// }
    /// assert!(state.root().is_some());
    /// ```
    pub fn root_mut(&mut self) -> Option<&mut FlameNode> {
        self.root.as_mut()
    }

    /// Sets the root frame.
    ///
    /// Resets zoom and selection state.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let mut state = FlameGraphState::new();
    /// state.set_root(FlameNode::new("main()", 500));
    /// assert!(state.root().is_some());
    /// ```
    pub fn set_root(&mut self, root: FlameNode) {
        self.root = Some(root);
        self.zoom_stack.clear();
        self.selected_depth = 0;
        self.selected_index = 0;
    }

    /// Clears the flame graph.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let mut state = FlameGraphState::with_root(FlameNode::new("main()", 500));
    /// state.clear();
    /// assert!(state.root().is_none());
    /// ```
    pub fn clear(&mut self) {
        self.root = None;
        self.zoom_stack.clear();
        self.selected_depth = 0;
        self.selected_index = 0;
        self.search_query.clear();
    }

    /// Returns the currently visible root (after zoom).
    ///
    /// Follows the zoom stack to find the currently displayed subtree root.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let root = FlameNode::new("main()", 500)
    ///     .with_child(FlameNode::new("compute()", 300));
    /// let state = FlameGraphState::with_root(root);
    /// assert_eq!(state.current_view_root().unwrap().label(), "main()");
    /// ```
    pub fn current_view_root(&self) -> Option<&FlameNode> {
        let mut current = self.root.as_ref()?;
        for label in &self.zoom_stack {
            let found = current.children.iter().find(|c| &c.label == label);
            match found {
                Some(child) => current = child,
                None => return Some(current),
            }
        }
        Some(current)
    }

    /// Returns the currently selected frame.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let root = FlameNode::new("main()", 500)
    ///     .with_child(FlameNode::new("compute()", 300));
    /// let state = FlameGraphState::with_root(root);
    /// assert_eq!(state.selected_frame().unwrap().label(), "main()");
    /// ```
    pub fn selected_frame(&self) -> Option<&FlameNode> {
        let view_root = self.current_view_root()?;
        Self::find_at_depth(view_root, self.selected_depth, self.selected_index)
    }

    /// Returns the zoom stack.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::FlameGraphState;
    ///
    /// let state = FlameGraphState::new();
    /// assert!(state.zoom_stack().is_empty());
    /// ```
    pub fn zoom_stack(&self) -> &[String] {
        &self.zoom_stack
    }

    /// Returns the selected depth level.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::FlameGraphState;
    ///
    /// let state = FlameGraphState::new();
    /// assert_eq!(state.selected_depth(), 0);
    /// ```
    pub fn selected_depth(&self) -> usize {
        self.selected_depth
    }

    /// Returns the selected frame index at the current depth.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::FlameGraphState;
    ///
    /// let state = FlameGraphState::new();
    /// assert_eq!(state.selected_index(), 0);
    /// ```
    pub fn selected_index(&self) -> usize {
        self.selected_index
    }

    /// Returns the search query.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::FlameGraphState;
    ///
    /// let state = FlameGraphState::new();
    /// assert_eq!(state.search_query(), "");
    /// ```
    pub fn search_query(&self) -> &str {
        &self.search_query
    }

    /// Returns the search query as an `Option<&str>`, or `None` if empty.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let state = FlameGraphState::new();
    /// assert_eq!(state.search(), None);
    ///
    /// let mut state = FlameGraphState::with_root(FlameNode::new("main()", 500));
    /// state.set_search("compute".to_string());
    /// assert_eq!(state.search(), Some("compute"));
    /// ```
    pub fn search(&self) -> Option<&str> {
        if self.search_query.is_empty() {
            None
        } else {
            Some(&self.search_query)
        }
    }

    /// Sets the search query.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let mut state = FlameGraphState::with_root(FlameNode::new("main()", 500));
    /// state.set_search("compute".to_string());
    /// assert_eq!(state.search_query(), "compute");
    /// ```
    pub fn set_search(&mut self, query: String) {
        self.search_query = query;
    }

    /// Returns the title, if set.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::FlameGraphState;
    ///
    /// let state = FlameGraphState::new();
    /// assert_eq!(state.title(), None);
    /// ```
    pub fn title(&self) -> Option<&str> {
        self.title.as_deref()
    }

    /// Sets the title.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::FlameGraphState;
    ///
    /// let mut state = FlameGraphState::new();
    /// state.set_title("CPU Profile");
    /// assert_eq!(state.title(), Some("CPU Profile"));
    /// ```
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.title = Some(title.into());
    }

    // ---- Zoom ----

    /// Zooms into the currently selected frame, making it the new view root.
    ///
    /// Only zooms if the selected frame has children.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let root = FlameNode::new("main()", 500)
    ///     .with_child(FlameNode::new("compute()", 300)
    ///         .with_child(FlameNode::new("sort()", 200)));
    /// let mut state = FlameGraphState::with_root(root);
    ///
    /// // Select compute() (depth 1, index 0)
    /// state.select_down();
    /// // Zoom into compute()
    /// let zoomed = state.zoom_in();
    /// assert!(zoomed);
    /// assert_eq!(state.current_view_root().unwrap().label(), "compute()");
    /// ```
    pub fn zoom_in(&mut self) -> bool {
        if let Some(frame) = self.selected_frame() {
            if !frame.children.is_empty() {
                let label = frame.label.clone();
                self.zoom_stack.push(label);
                self.selected_depth = 0;
                self.selected_index = 0;
                return true;
            }
        }
        false
    }

    /// Zooms out one level.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let root = FlameNode::new("main()", 500)
    ///     .with_child(FlameNode::new("compute()", 300)
    ///         .with_child(FlameNode::new("sort()", 200)));
    /// let mut state = FlameGraphState::with_root(root);
    /// state.select_down();
    /// state.zoom_in();
    /// assert!(state.zoom_out());
    /// assert_eq!(state.current_view_root().unwrap().label(), "main()");
    /// ```
    pub fn zoom_out(&mut self) -> bool {
        if self.zoom_stack.pop().is_some() {
            self.selected_depth = 0;
            self.selected_index = 0;
            true
        } else {
            false
        }
    }

    /// Resets zoom to the original root.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let root = FlameNode::new("main()", 500)
    ///     .with_child(FlameNode::new("compute()", 300)
    ///         .with_child(FlameNode::new("sort()", 200)));
    /// let mut state = FlameGraphState::with_root(root);
    /// state.select_down();
    /// state.zoom_in();
    /// state.reset_zoom();
    /// assert!(state.zoom_stack().is_empty());
    /// assert_eq!(state.current_view_root().unwrap().label(), "main()");
    /// ```
    pub fn reset_zoom(&mut self) {
        self.zoom_stack.clear();
        self.selected_depth = 0;
        self.selected_index = 0;
    }

    // ---- Navigation ----

    /// Moves selection up to parent depth.
    ///
    /// Returns true if the selection changed.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let root = FlameNode::new("main()", 500)
    ///     .with_child(FlameNode::new("compute()", 300));
    /// let mut state = FlameGraphState::with_root(root);
    /// state.select_down();
    /// assert_eq!(state.selected_depth(), 1);
    /// assert!(state.select_up());
    /// assert_eq!(state.selected_depth(), 0);
    /// ```
    pub fn select_up(&mut self) -> bool {
        if self.root.is_none() {
            return false;
        }
        if self.selected_depth > 0 {
            // Find which parent contains the currently selected node
            let parent_index = self.find_parent_index();
            self.selected_depth -= 1;
            self.selected_index = parent_index;
            true
        } else {
            false
        }
    }

    /// Moves selection down to child depth.
    ///
    /// Selects the first child of the nearest ancestor that has children
    /// at the next depth level.
    ///
    /// Returns true if the selection changed.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let root = FlameNode::new("main()", 500)
    ///     .with_child(FlameNode::new("compute()", 300));
    /// let mut state = FlameGraphState::with_root(root);
    /// assert!(state.select_down());
    /// assert_eq!(state.selected_depth(), 1);
    /// ```
    pub fn select_down(&mut self) -> bool {
        let view_root = match self.current_view_root() {
            Some(r) => r,
            None => return false,
        };
        let frames_at_next = Self::frames_at_depth(view_root, self.selected_depth + 1);
        if frames_at_next.is_empty() {
            return false;
        }

        // Find the first child of the currently selected frame
        if let Some(selected) = self.selected_frame() {
            if !selected.children.is_empty() {
                // Find the index of the first child in the next depth level
                let first_child_label = &selected.children[0].label;
                let child_idx = frames_at_next
                    .iter()
                    .position(|f| &f.label == first_child_label)
                    .unwrap_or(0);
                self.selected_depth += 1;
                self.selected_index = child_idx;
                return true;
            }
        }

        // If selected frame has no children, go to first frame at next depth
        self.selected_depth += 1;
        self.selected_index = 0;
        true
    }

    /// Moves selection to the previous sibling at the current depth.
    ///
    /// Returns true if the selection changed.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let root = FlameNode::new("main()", 500)
    ///     .with_child(FlameNode::new("a()", 200))
    ///     .with_child(FlameNode::new("b()", 300));
    /// let mut state = FlameGraphState::with_root(root);
    /// state.select_down();
    /// state.select_right();
    /// assert_eq!(state.selected_index(), 1);
    /// assert!(state.select_left());
    /// assert_eq!(state.selected_index(), 0);
    /// ```
    pub fn select_left(&mut self) -> bool {
        if self.root.is_none() {
            return false;
        }
        if self.selected_index > 0 {
            self.selected_index -= 1;
            true
        } else {
            false
        }
    }

    /// Moves selection to the next sibling at the current depth.
    ///
    /// Returns true if the selection changed.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphState, FlameNode};
    ///
    /// let root = FlameNode::new("main()", 500)
    ///     .with_child(FlameNode::new("a()", 200))
    ///     .with_child(FlameNode::new("b()", 300));
    /// let mut state = FlameGraphState::with_root(root);
    /// state.select_down();
    /// assert_eq!(state.selected_index(), 0);
    /// assert!(state.select_right());
    /// assert_eq!(state.selected_index(), 1);
    /// ```
    pub fn select_right(&mut self) -> bool {
        let view_root = match self.current_view_root() {
            Some(r) => r,
            None => return false,
        };
        let frames_at_depth = Self::frames_at_depth(view_root, self.selected_depth);
        if self.selected_index + 1 < frames_at_depth.len() {
            self.selected_index += 1;
            true
        } else {
            false
        }
    }

    // ---- Instance methods ----

    /// Updates the state with a message, returning any output.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{FlameGraphMessage, FlameGraphOutput, FlameGraphState, FlameNode};
    ///
    /// let root = FlameNode::new("main()", 500)
    ///     .with_child(FlameNode::new("compute()", 300));
    /// let mut state = FlameGraphState::with_root(root);
    /// let output = state.update(FlameGraphMessage::SelectDown);
    /// assert!(matches!(output, Some(FlameGraphOutput::FrameSelected { .. })));
    /// ```
    pub fn update(&mut self, msg: FlameGraphMessage) -> Option<FlameGraphOutput> {
        FlameGraph::update(self, msg)
    }

    // ---- Private helpers ----

    /// Finds the parent index of the currently selected node.
    fn find_parent_index(&self) -> usize {
        if self.selected_depth == 0 {
            return 0;
        }
        let view_root = match self.current_view_root() {
            Some(r) => r,
            None => return 0,
        };
        let parent_frames = Self::frames_at_depth(view_root, self.selected_depth - 1);
        let child_frames = Self::frames_at_depth(view_root, self.selected_depth);

        // Find which parent owns the currently selected child
        if let Some(selected_child) = child_frames.get(self.selected_index) {
            for (pi, parent) in parent_frames.iter().enumerate() {
                if parent
                    .children
                    .iter()
                    .any(|c| c.label == selected_child.label && c.value == selected_child.value)
                {
                    return pi;
                }
            }
        }
        0
    }

    /// Returns all frames at a given depth level under a root.
    fn frames_at_depth(root: &FlameNode, depth: usize) -> Vec<&FlameNode> {
        let mut result = Vec::new();
        Self::collect_at_depth(root, 0, depth, &mut result);
        result
    }

    /// Recursively collects frames at a specific depth.
    fn collect_at_depth<'a>(
        node: &'a FlameNode,
        current_depth: usize,
        target_depth: usize,
        result: &mut Vec<&'a FlameNode>,
    ) {
        if current_depth == target_depth {
            result.push(node);
            return;
        }
        for child in &node.children {
            Self::collect_at_depth(child, current_depth + 1, target_depth, result);
        }
    }

    /// Finds the node at a given depth and index.
    fn find_at_depth(root: &FlameNode, depth: usize, index: usize) -> Option<&FlameNode> {
        let frames = Self::frames_at_depth(root, depth);
        frames.into_iter().nth(index)
    }

    /// Returns the maximum depth of the tree rooted at the current view root.
    pub(crate) fn max_depth(&self) -> usize {
        match self.current_view_root() {
            Some(root) => Self::compute_max_depth(root, 0),
            None => 0,
        }
    }

    /// Recursively computes the maximum depth.
    fn compute_max_depth(node: &FlameNode, current: usize) -> usize {
        if node.children.is_empty() {
            return current;
        }
        node.children
            .iter()
            .map(|c| Self::compute_max_depth(c, current + 1))
            .max()
            .unwrap_or(current)
    }
}

/// An interactive flame graph component for profiling data visualization.
///
/// Renders stack frames as horizontal bars where width is proportional
/// to time or samples. Root is displayed at the top (icicle graph style).
///
/// # Visual Format
///
/// ```text
/// +- Flame Graph -----------------------------------------+
/// | ████████████████████████████████████████ main()       |  depth 0
/// | ████████████████████████████ compute()  ██████ io()   |  depth 1
/// | ██████████████ sort()  ████████ hash()                |  depth 2
/// | ████ merge()                                          |  depth 3
/// |-------------------------------------------------------|
/// | Selected: sort()  150 samples (30%)  self: 50         |
/// +-------------------------------------------------------+
/// ```
///
/// # Keyboard Navigation
///
/// - `Up/k`: Select parent depth
/// - `Down/j`: Select child depth
/// - `Left/h`: Select previous sibling
/// - `Right/l`: Select next sibling
/// - `Enter`: Zoom into selected frame
/// - `Escape/Backspace`: Zoom out
/// - `Home`: Reset zoom to root
/// - `/`: Set search query
///
/// # Example
///
/// ```rust
/// use envision::component::{
///     Component, FlameGraph, FlameGraphState, FlameGraphMessage, FlameNode,
/// };
/// use ratatui::style::Color;
///
/// let root = FlameNode::new("main()", 500)
///     .with_color(Color::Red)
///     .with_child(
///         FlameNode::new("compute()", 300)
///             .with_child(FlameNode::new("sort()", 200))
///     )
///     .with_child(FlameNode::new("io()", 100));
///
/// let mut state = FlameGraphState::with_root(root);
///
/// // Navigate and zoom
/// FlameGraph::update(&mut state, FlameGraphMessage::SelectDown);
/// FlameGraph::update(&mut state, FlameGraphMessage::ZoomIn);
/// ```
pub struct FlameGraph;

impl Component for FlameGraph {
    type State = FlameGraphState;
    type Message = FlameGraphMessage;
    type Output = FlameGraphOutput;

    fn init() -> Self::State {
        FlameGraphState::default()
    }

    fn update(state: &mut Self::State, msg: Self::Message) -> Option<Self::Output> {
        match msg {
            FlameGraphMessage::SetRoot(root) => {
                state.set_root(root);
                None
            }
            FlameGraphMessage::Clear => {
                state.clear();
                None
            }
            FlameGraphMessage::SetSearch(query) => {
                state.search_query = query;
                None
            }
            FlameGraphMessage::ClearSearch => {
                state.search_query.clear();
                None
            }
            FlameGraphMessage::ResetZoom => {
                state.reset_zoom();
                None
            }
            _ => {
                state.root.as_ref()?;
                match msg {
                    FlameGraphMessage::SelectUp => {
                        if state.select_up() {
                            make_selected_output(state)
                        } else {
                            None
                        }
                    }
                    FlameGraphMessage::SelectDown => {
                        if state.select_down() {
                            make_selected_output(state)
                        } else {
                            None
                        }
                    }
                    FlameGraphMessage::SelectLeft => {
                        if state.select_left() {
                            make_selected_output(state)
                        } else {
                            None
                        }
                    }
                    FlameGraphMessage::SelectRight => {
                        if state.select_right() {
                            make_selected_output(state)
                        } else {
                            None
                        }
                    }
                    FlameGraphMessage::ZoomIn => {
                        if let Some(frame) = state.selected_frame() {
                            let label = frame.label.clone();
                            if state.zoom_in() {
                                Some(FlameGraphOutput::ZoomedIn(label))
                            } else {
                                None
                            }
                        } else {
                            None
                        }
                    }
                    FlameGraphMessage::ZoomOut => {
                        if state.zoom_out() {
                            Some(FlameGraphOutput::ZoomedOut)
                        } else {
                            None
                        }
                    }
                    // Already handled above
                    FlameGraphMessage::SetRoot(_)
                    | FlameGraphMessage::Clear
                    | FlameGraphMessage::SetSearch(_)
                    | FlameGraphMessage::ClearSearch
                    | FlameGraphMessage::ResetZoom => unreachable!(),
                }
            }
        }
    }

    fn handle_event(
        _state: &Self::State,
        event: &Event,
        ctx: &EventContext,
    ) -> Option<Self::Message> {
        if !ctx.focused || ctx.disabled {
            return None;
        }
        if let Some(key) = event.as_key() {
            match key.code {
                Key::Up | Key::Char('k') => Some(FlameGraphMessage::SelectUp),
                Key::Down | Key::Char('j') => Some(FlameGraphMessage::SelectDown),
                Key::Left | Key::Char('h') => Some(FlameGraphMessage::SelectLeft),
                Key::Right | Key::Char('l') => Some(FlameGraphMessage::SelectRight),
                Key::Enter => Some(FlameGraphMessage::ZoomIn),
                Key::Esc | Key::Backspace => Some(FlameGraphMessage::ZoomOut),
                Key::Home => Some(FlameGraphMessage::ResetZoom),
                Key::Char('/') => Some(FlameGraphMessage::SetSearch(String::new())),
                _ => None,
            }
        } else {
            None
        }
    }

    fn view(state: &Self::State, ctx: &mut RenderContext<'_, '_>) {
        render::render_flame_graph(
            state,
            ctx.frame,
            ctx.area,
            ctx.theme,
            ctx.focused,
            ctx.disabled,
        );
    }
}

/// Creates a `FrameSelected` output from the current state.
fn make_selected_output(state: &FlameGraphState) -> Option<FlameGraphOutput> {
    state
        .selected_frame()
        .map(|frame| FlameGraphOutput::FrameSelected {
            label: frame.label.clone(),
            value: frame.total_value(),
            self_value: frame.self_value(),
        })
}

#[cfg(test)]
mod snapshot_tests;
#[cfg(test)]
mod tests;