egui_arbor/
style.rs

1//! Style configuration for the outliner widget.
2//!
3//! This module provides types for customizing the visual appearance of the outliner,
4//! including colors, spacing, and icon styles.
5
6/// Style configuration for the outliner widget.
7///
8/// Controls the visual appearance including spacing, colors, and icon sizes.
9/// Use the builder pattern methods for convenient construction:
10///
11/// ```rust
12/// use egui_arbor::Style;
13/// use egui::Color32;
14///
15/// let style = Style::default()
16///     .with_indent(20.0)
17///     .with_selection_color(Color32::from_rgb(100, 150, 200));
18/// ```
19#[derive(Debug, Clone)]
20pub struct Style {
21    /// Indentation per hierarchy level in logical pixels.
22    ///
23    /// Default: 16.0
24    pub indent: f32,
25
26    /// Spacing between icon and text in logical pixels.
27    ///
28    /// Default: 4.0
29    pub icon_spacing: f32,
30
31    /// Height of each row in logical pixels.
32    ///
33    /// Default: 20.0
34    pub row_height: f32,
35
36    /// Size of expand/collapse arrow in logical pixels.
37    ///
38    /// Default: 12.0
39    pub expand_icon_size: f32,
40
41    /// Size of action icons in logical pixels.
42    ///
43    /// Default: 16.0
44    pub action_icon_size: f32,
45
46    /// Optional selection highlight color.
47    ///
48    /// If `None`, uses egui's default selection color.
49    pub selection_color: Option<egui::Color32>,
50
51    /// Optional hover highlight color.
52    ///
53    /// If `None`, uses egui's default hover color.
54    pub hover_color: Option<egui::Color32>,
55
56    /// Style of the expand/collapse icon.
57    ///
58    /// Default: `ExpandIconStyle::Arrow`
59    pub expand_icon_style: ExpandIconStyle,
60}
61
62impl Default for Style {
63    fn default() -> Self {
64        Self {
65            indent: 16.0,
66            icon_spacing: 4.0,
67            row_height: 20.0,
68            expand_icon_size: 12.0,
69            action_icon_size: 16.0,
70            selection_color: Some(egui::Color32::from_rgba_unmultiplied(100, 150, 200, 100)),
71            hover_color: Some(egui::Color32::from_rgba_unmultiplied(100, 150, 200, 50)),
72            expand_icon_style: ExpandIconStyle::Arrow,
73        }
74    }
75}
76
77impl Style {
78    /// Set the indentation per hierarchy level.
79    ///
80    /// # Arguments
81    /// * `indent` - Indentation in logical pixels
82    ///
83    /// # Example
84    /// ```rust
85    /// use egui_arbor::Style;
86    ///
87    /// let style = Style::default().with_indent(20.0);
88    /// ```
89    pub fn with_indent(mut self, indent: f32) -> Self {
90        self.indent = indent;
91        self
92    }
93
94    /// Set the spacing between icon and text.
95    ///
96    /// # Arguments
97    /// * `spacing` - Spacing in logical pixels
98    ///
99    /// # Example
100    /// ```rust
101    /// use egui_arbor::Style;
102    ///
103    /// let style = Style::default().with_icon_spacing(6.0);
104    /// ```
105    pub fn with_icon_spacing(mut self, spacing: f32) -> Self {
106        self.icon_spacing = spacing;
107        self
108    }
109
110    /// Set the height of each row.
111    ///
112    /// # Arguments
113    /// * `height` - Row height in logical pixels
114    ///
115    /// # Example
116    /// ```rust
117    /// use egui_arbor::Style;
118    ///
119    /// let style = Style::default().with_row_height(24.0);
120    /// ```
121    pub fn with_row_height(mut self, height: f32) -> Self {
122        self.row_height = height;
123        self
124    }
125
126    /// Set the size of expand/collapse arrows.
127    ///
128    /// # Arguments
129    /// * `size` - Icon size in logical pixels
130    ///
131    /// # Example
132    /// ```rust
133    /// use egui_arbor::Style;
134    ///
135    /// let style = Style::default().with_expand_icon_size(14.0);
136    /// ```
137    pub fn with_expand_icon_size(mut self, size: f32) -> Self {
138        self.expand_icon_size = size;
139        self
140    }
141
142    /// Set the size of action icons.
143    ///
144    /// # Arguments
145    /// * `size` - Icon size in logical pixels
146    ///
147    /// # Example
148    /// ```rust
149    /// use egui_arbor::Style;
150    ///
151    /// let style = Style::default().with_action_icon_size(18.0);
152    /// ```
153    pub fn with_action_icon_size(mut self, size: f32) -> Self {
154        self.action_icon_size = size;
155        self
156    }
157
158    /// Set the selection highlight color.
159    ///
160    /// # Arguments
161    /// * `color` - The color to use for selection highlighting
162    ///
163    /// # Example
164    /// ```rust
165    /// use egui_arbor::Style;
166    /// use egui::Color32;
167    ///
168    /// let style = Style::default()
169    ///     .with_selection_color(Color32::from_rgb(100, 150, 200));
170    /// ```
171    pub fn with_selection_color(mut self, color: egui::Color32) -> Self {
172        self.selection_color = Some(color);
173        self
174    }
175
176    /// Set the hover highlight color.
177    ///
178    /// # Arguments
179    /// * `color` - The color to use for hover highlighting
180    ///
181    /// # Example
182    /// ```rust
183    /// use egui_arbor::Style;
184    /// use egui::Color32;
185    ///
186    /// let style = Style::default()
187    ///     .with_hover_color(Color32::from_rgb(150, 180, 210));
188    /// ```
189    pub fn with_hover_color(mut self, color: egui::Color32) -> Self {
190        self.hover_color = Some(color);
191        self
192    }
193
194    /// Set the expand/collapse icon style.
195    ///
196    /// # Arguments
197    /// * `style` - The icon style to use
198    ///
199    /// # Example
200    /// ```rust
201    /// use egui_arbor::{Style, ExpandIconStyle};
202    ///
203    /// let style = Style::default()
204    ///     .with_expand_icon_style(ExpandIconStyle::PlusMinus);
205    /// ```
206    pub fn with_expand_icon_style(mut self, style: ExpandIconStyle) -> Self {
207        self.expand_icon_style = style;
208        self
209    }
210}
211
212/// Style of the expand/collapse icon.
213///
214/// Determines the visual appearance of the icon used to expand and collapse
215/// tree nodes in the outliner.
216#[derive(Debug, Clone, PartialEq, Eq)]
217pub enum ExpandIconStyle {
218    /// Simple arrow style (▶ when collapsed, ▼ when expanded).
219    Arrow,
220
221    /// Plus/minus signs (+ when collapsed, - when expanded).
222    PlusMinus,
223
224    /// Chevron style (› when collapsed, ⌄ when expanded).
225    ChevronRight,
226
227    /// Custom strings for collapsed and expanded states.
228    ///
229    /// # Example
230    /// ```rust
231    /// use egui_arbor::ExpandIconStyle;
232    ///
233    /// let style = ExpandIconStyle::Custom {
234    ///     collapsed: "→".to_string(),
235    ///     expanded: "↓".to_string(),
236    /// };
237    /// ```
238    Custom {
239        /// String to display when the node is collapsed.
240        collapsed: String,
241        /// String to display when the node is expanded.
242        expanded: String,
243    },
244}
245
246impl ExpandIconStyle {
247    /// Get the string representation for the collapsed state.
248    ///
249    /// # Returns
250    /// The string to display when a node is collapsed.
251    pub fn collapsed_str(&self) -> &str {
252        match self {
253            ExpandIconStyle::Arrow => "▶",
254            ExpandIconStyle::PlusMinus => "+",
255            ExpandIconStyle::ChevronRight => "›",
256            ExpandIconStyle::Custom { collapsed, .. } => collapsed,
257        }
258    }
259
260    /// Get the string representation for the expanded state.
261    ///
262    /// # Returns
263    /// The string to display when a node is expanded.
264    pub fn expanded_str(&self) -> &str {
265        match self {
266            ExpandIconStyle::Arrow => "▼",
267            ExpandIconStyle::PlusMinus => "-",
268            ExpandIconStyle::ChevronRight => "⌄",
269            ExpandIconStyle::Custom { expanded, .. } => expanded,
270        }
271    }
272}
273
274impl Default for ExpandIconStyle {
275    fn default() -> Self {
276        Self::Arrow
277    }
278}