ratatui_toolkit/widgets/markdown_widget/extensions/toc/constructors/new.rs
1//! Constructor for Toc widget.
2
3use crate::widgets::markdown_widget::extensions::toc::enums::TocConfig;
4use crate::widgets::markdown_widget::extensions::toc::Toc;
5use crate::widgets::markdown_widget::state::toc_state::TocState;
6
7impl<'a> Toc<'a> {
8 /// Create a new TOC widget from TocState.
9 ///
10 /// The Toc widget is a UI-only component that receives state via reference.
11 /// State mutations happen through TocState methods, not the Toc widget.
12 ///
13 /// # Arguments
14 ///
15 /// * `toc_state` - Reference to the TocState containing entries, scroll, and hover state.
16 ///
17 /// # Returns
18 ///
19 /// A new `Toc` instance ready for rendering.
20 ///
21 /// # Example
22 ///
23 /// ```rust,no_run
24 /// use ratatui_toolkit::markdown_widget::extensions::toc::Toc;
25 /// use ratatui_toolkit::markdown_widget::state::toc_state::TocState;
26 ///
27 /// let toc_state = TocState::new();
28 /// let toc = Toc::new(&toc_state);
29 /// ```
30 pub fn new(toc_state: &'a TocState) -> Self {
31 Self {
32 toc_state,
33 config: TocConfig::default(),
34 expanded: false,
35 }
36 }
37
38 /// Set whether the TOC is expanded.
39 ///
40 /// # Arguments
41 ///
42 /// * `expanded` - True for expanded mode (full text), false for compact mode (lines).
43 ///
44 /// # Returns
45 ///
46 /// Self for method chaining.
47 pub fn expanded(mut self, expanded: bool) -> Self {
48 self.expanded = expanded;
49 self
50 }
51
52 /// Set the TOC configuration.
53 ///
54 /// # Arguments
55 ///
56 /// * `config` - The TOC configuration.
57 ///
58 /// # Returns
59 ///
60 /// Self for method chaining.
61 pub fn config(mut self, config: TocConfig) -> Self {
62 self.config = config;
63 self
64 }
65}