Skip to main content

libsurfer/
state_util.rs

1//! Utility functions, typically inlined, for more readable code
2
3use ecolor::Color32;
4use egui::Modifiers;
5
6use crate::{
7    SystemState,
8    clock_highlighting::ClockHighlightType,
9    config::{ArrowKeyBindings, AutoLoad, FocusHighlight, PrimaryMouseDrag, TransitionValue},
10    displayed_item::DisplayedItem,
11    hierarchy::{HierarchyStyle, ParameterDisplayLocation},
12    trace_style::TraceStyle,
13};
14
15impl SystemState {
16    #[inline]
17    pub fn get_item_text_color(&self, item: &DisplayedItem) -> Color32 {
18        item.color()
19            .and_then(|color| self.user.config.theme.get_color(color))
20            .unwrap_or(self.user.config.theme.primary_ui_color.foreground)
21    }
22
23    #[inline]
24    pub fn show_statusbar(&self) -> bool {
25        self.user.show_statusbar.unwrap_or_else(|| {
26            (self.user.waves.is_some() || self.progress_tracker.is_some())
27                && self.user.config.layout.show_statusbar()
28        })
29    }
30
31    #[inline]
32    pub fn show_toolbar(&self) -> bool {
33        self.user
34            .show_toolbar
35            .unwrap_or_else(|| self.user.config.layout.show_toolbar())
36    }
37
38    #[inline]
39    pub fn show_overview(&self) -> bool {
40        self.user
41            .show_overview
42            .unwrap_or_else(|| self.user.config.layout.show_overview())
43    }
44
45    #[inline]
46    pub fn show_hierarchy(&self) -> bool {
47        self.user
48            .show_hierarchy
49            .unwrap_or_else(|| self.user.config.layout.show_hierarchy())
50    }
51
52    #[inline]
53    pub fn show_tooltip(&self) -> bool {
54        self.user
55            .show_tooltip
56            .unwrap_or_else(|| self.user.config.layout.show_tooltip())
57    }
58
59    #[inline]
60    pub fn show_scope_tooltip(&self) -> bool {
61        self.user
62            .show_scope_tooltip
63            .unwrap_or_else(|| self.user.config.layout.show_scope_tooltip())
64    }
65
66    #[inline]
67    pub fn show_ticks(&self) -> bool {
68        self.user
69            .show_ticks
70            .unwrap_or_else(|| self.user.config.layout.show_ticks())
71    }
72
73    #[inline]
74    pub fn show_menu(&self) -> bool {
75        self.user
76            .show_menu
77            .unwrap_or_else(|| self.user.config.layout.show_menu())
78    }
79
80    #[inline]
81    pub fn show_variable_indices(&self) -> bool {
82        self.user
83            .show_variable_indices
84            .unwrap_or_else(|| self.user.config.layout.show_variable_indices())
85    }
86
87    #[inline]
88    pub fn show_variable_direction(&self) -> bool {
89        self.user
90            .show_variable_direction
91            .unwrap_or_else(|| self.user.config.layout.show_variable_direction())
92    }
93
94    #[inline]
95    pub fn ui_zoom_factor(&self) -> f32 {
96        self.user
97            .ui_zoom_factor
98            .unwrap_or_else(|| self.user.config.layout.default_zoom_factor())
99    }
100
101    #[inline]
102    pub fn show_empty_scopes(&self) -> bool {
103        self.user
104            .show_empty_scopes
105            .unwrap_or_else(|| self.user.config.layout.show_empty_scopes())
106    }
107
108    #[inline]
109    pub fn show_hierarchy_icons(&self) -> bool {
110        self.user
111            .show_hierarchy_icons
112            .unwrap_or_else(|| self.user.config.layout.show_hierarchy_icons())
113    }
114
115    #[inline]
116    pub fn show_default_timeline(&self) -> bool {
117        self.user
118            .show_default_timeline
119            .unwrap_or_else(|| self.user.config.layout.show_default_timeline())
120    }
121
122    #[inline]
123    pub fn focus_highlight(&self) -> FocusHighlight {
124        self.user
125            .focus_highlight
126            .unwrap_or_else(|| self.user.config.layout.focus_highlight())
127    }
128
129    #[inline]
130    pub fn fill_high_values(&self) -> bool {
131        self.user
132            .fill_high_values
133            .unwrap_or_else(|| self.user.config.layout.fill_high_values())
134    }
135
136    #[inline]
137    pub fn animation_enabled(&self) -> bool {
138        self.user
139            .animation_enabled
140            .unwrap_or_else(|| self.user.config.animation_enabled())
141    }
142
143    #[inline]
144    pub fn show_divider_text(&self) -> bool {
145        self.user.config.show_divider_text()
146    }
147
148    #[inline]
149    pub fn primary_button_drag_behavior(&self) -> PrimaryMouseDrag {
150        self.user
151            .primary_button_drag_behavior
152            .unwrap_or_else(|| self.user.config.behavior.primary_button_drag_behavior())
153    }
154
155    #[inline]
156    /// Return true if the combination of `primary_button_drag_behavior` and
157    /// `modifiers` results in a measure, false otherwise.
158    pub fn do_measure(&self, modifiers: &Modifiers) -> bool {
159        let drag_behavior = self.primary_button_drag_behavior();
160        (drag_behavior == PrimaryMouseDrag::Measure && !modifiers.shift)
161            || (drag_behavior == PrimaryMouseDrag::Cursor && modifiers.shift)
162    }
163
164    #[inline]
165    pub fn arrow_key_bindings(&self) -> ArrowKeyBindings {
166        self.user
167            .arrow_key_bindings
168            .unwrap_or_else(|| self.user.config.behavior.arrow_key_bindings())
169    }
170
171    #[inline]
172    pub fn clock_highlight_type(&self) -> ClockHighlightType {
173        self.user
174            .clock_highlight_type
175            .unwrap_or_else(|| self.user.config.default_clock_highlight_type())
176    }
177
178    #[inline]
179    pub fn hierarchy_style(&self) -> HierarchyStyle {
180        self.user
181            .hierarchy_style
182            .unwrap_or_else(|| self.user.config.layout.hierarchy_style())
183    }
184
185    #[inline]
186    pub fn autoreload_files(&self) -> AutoLoad {
187        self.user
188            .autoreload_files
189            .unwrap_or_else(|| self.user.config.autoreload_files())
190    }
191
192    #[inline]
193    pub fn autoload_sibling_state_files(&self) -> AutoLoad {
194        self.user
195            .autoload_sibling_state_files
196            .unwrap_or_else(|| self.user.config.autoload_sibling_state_files())
197    }
198
199    #[inline]
200    pub fn parameter_display_location(&self) -> ParameterDisplayLocation {
201        self.user
202            .parameter_display_location
203            .unwrap_or_else(|| self.user.config.layout.parameter_display_location())
204    }
205
206    #[inline]
207    /// Return the trace style to use, taking into account both the explicit trace style and the deprecated dinotrace style for backwards compatibility.
208    pub fn trace_style(&self) -> TraceStyle {
209        self.user.trace_style.unwrap_or_else(|| {
210            if self.user.use_dinotrace_style == Some(true) {
211                TraceStyle::Dinotrace
212            } else {
213                self.user.config.layout.trace_style()
214            }
215        })
216    }
217
218    #[inline]
219    pub fn transition_value(&self) -> TransitionValue {
220        self.user
221            .transition_value
222            .unwrap_or_else(|| self.user.config.layout.transition_value())
223    }
224
225    #[inline]
226    pub fn align_names_right(&self) -> bool {
227        self.user
228            .align_names_right
229            .unwrap_or_else(|| self.user.config.layout.align_names_right())
230    }
231
232    #[inline]
233    pub fn draw_vector_unknowns_as_line(&self) -> bool {
234        self.user
235            .draw_vector_unknowns_as_line
236            .unwrap_or_else(|| self.user.config.layout.draw_vector_unknowns_as_line())
237    }
238
239    #[inline]
240    pub fn enable_time_offset(&self) -> bool {
241        self.user
242            .enable_time_offset
243            .unwrap_or_else(|| self.user.config.layout.enable_time_offset())
244    }
245}