Skip to main content

ferroui_diagnostics/diagnostics/
dev_tools_options.rs

1//! Port of `Diagnostics/DevToolsOptions.cs`.
2
3use super::{Conventions, DevToolsViewKind, HotKeyConfiguration, IScreenshotHandler};
4use ferroui_base::input::{Key, KeyGesture};
5use ferroui_base::media::IBrush;
6use ferroui_base::styling::ThemeVariant;
7use ferroui_base::Size;
8use std::cell::{Cell, RefCell};
9use std::rc::Rc;
10
11/// Describes options used to customize DevTools.
12///
13/// A class upstream: a shared handle (`Rc<DevToolsOptions>`) whose properties are read with
14/// `foo()`, set with `set_foo(v)` and given with the chaining `with_foo(v)` while the options
15/// are built; the members that upstream sets only when the object is created (`init`) have no
16/// `set_foo`.
17pub struct DevToolsOptions {
18    gesture: Cell<KeyGesture>,
19    show_as_child_window: Cell<bool>,
20    size: Cell<Size>,
21    startup_screen_index: Cell<Option<i32>>,
22    show_implemented_interfaces: Cell<bool>,
23    screenshot_handler: RefCell<Rc<dyn IScreenshotHandler>>,
24    theme_variant: RefCell<Option<ThemeVariant>>,
25    focus_highlighter_brush: RefCell<Option<Rc<dyn IBrush>>>,
26    launch_view: Cell<DevToolsViewKind>,
27    hot_keys: RefCell<Rc<HotKeyConfiguration>>,
28}
29
30impl PartialEq for DevToolsOptions {
31    fn eq(&self, other: &Self) -> bool {
32        std::ptr::eq(self, other)
33    }
34}
35
36impl Default for DevToolsOptions {
37    fn default() -> Self {
38        Self {
39            gesture: Cell::new(KeyGesture::from_key(Key::F12)),
40            show_as_child_window: Cell::new(true),
41            size: Cell::new(Size::new(1280.0, 720.0)),
42            startup_screen_index: Cell::new(None),
43            show_implemented_interfaces: Cell::new(true),
44            screenshot_handler: RefCell::new(Conventions::default_screenshot_handler()),
45            theme_variant: RefCell::new(None),
46            focus_highlighter_brush: RefCell::new(None),
47            launch_view: Cell::new(DevToolsViewKind::default()),
48            hot_keys: RefCell::new(HotKeyConfiguration::new()),
49        }
50    }
51}
52
53impl DevToolsOptions {
54    /// C# `new DevToolsOptions()`: the default options.
55    pub fn new() -> Rc<Self> {
56        Rc::new(Self::default())
57    }
58
59    /// Gets or sets the key gesture used to open DevTools.
60    pub fn gesture(&self) -> KeyGesture {
61        self.gesture.get()
62    }
63
64    pub fn set_gesture(&self, value: KeyGesture) {
65        self.gesture.set(value);
66    }
67
68    pub fn with_gesture(self: Rc<Self>, value: KeyGesture) -> Rc<Self> {
69        self.set_gesture(value);
70        self
71    }
72
73    /// Gets or sets a value indicating whether DevTools should be displayed as a child window
74    /// of the window being inspected. The default value is true.
75    ///
76    /// This setting is ignored if DevTools is attached to an application.
77    pub fn show_as_child_window(&self) -> bool {
78        self.show_as_child_window.get()
79    }
80
81    pub fn set_show_as_child_window(&self, value: bool) {
82        self.show_as_child_window.set(value);
83    }
84
85    pub fn with_show_as_child_window(self: Rc<Self>, value: bool) -> Rc<Self> {
86        self.set_show_as_child_window(value);
87        self
88    }
89
90    /// Gets or sets the initial size of the DevTools window. The default value is 1280x720.
91    pub fn size(&self) -> Size {
92        self.size.get()
93    }
94
95    pub fn set_size(&self, value: Size) {
96        self.size.set(value);
97    }
98
99    pub fn with_size(self: Rc<Self>, value: Size) -> Rc<Self> {
100        self.set_size(value);
101        self
102    }
103
104    /// Get or set the startup screen index where the DevTools window will be displayed.
105    pub fn startup_screen_index(&self) -> Option<i32> {
106        self.startup_screen_index.get()
107    }
108
109    pub fn set_startup_screen_index(&self, value: Option<i32>) {
110        self.startup_screen_index.set(value);
111    }
112
113    pub fn with_startup_screen_index(self: Rc<Self>, value: Option<i32>) -> Rc<Self> {
114        self.set_startup_screen_index(value);
115        self
116    }
117
118    /// Gets or sets a value indicating whether DevTools should be displayed implemented interfaces on Control details. The default value is true.
119    pub fn show_implemented_interfaces(&self) -> bool {
120        self.show_implemented_interfaces.get()
121    }
122
123    pub fn set_show_implemented_interfaces(&self, value: bool) {
124        self.show_implemented_interfaces.set(value);
125    }
126
127    pub fn with_show_implemented_interfaces(self: Rc<Self>, value: bool) -> Rc<Self> {
128        self.set_show_implemented_interfaces(value);
129        self
130    }
131
132    /// Allow to customize SreenshotHandler
133    ///
134    /// Default handler is [`FilePickerHandler`](super::screenshots::FilePickerHandler)
135    pub fn screenshot_handler(&self) -> Rc<dyn IScreenshotHandler> {
136        self.screenshot_handler.borrow().clone()
137    }
138
139    pub fn set_screenshot_handler(&self, value: Rc<dyn IScreenshotHandler>) {
140        *self.screenshot_handler.borrow_mut() = value;
141    }
142
143    pub fn with_screenshot_handler(self: Rc<Self>, value: Rc<dyn IScreenshotHandler>) -> Rc<Self> {
144        self.set_screenshot_handler(value);
145        self
146    }
147
148    /// Gets or sets whether DevTools theme.
149    pub fn theme_variant(&self) -> Option<ThemeVariant> {
150        self.theme_variant.borrow().clone()
151    }
152
153    pub fn set_theme_variant(&self, value: Option<ThemeVariant>) {
154        *self.theme_variant.borrow_mut() = value;
155    }
156
157    pub fn with_theme_variant(self: Rc<Self>, value: Option<ThemeVariant>) -> Rc<Self> {
158        self.set_theme_variant(value);
159        self
160    }
161
162    /// Get or set Focus Highlighter brush
163    pub fn focus_highlighter_brush(&self) -> Option<Rc<dyn IBrush>> {
164        self.focus_highlighter_brush.borrow().clone()
165    }
166
167    pub fn set_focus_highlighter_brush(&self, value: Option<Rc<dyn IBrush>>) {
168        *self.focus_highlighter_brush.borrow_mut() = value;
169    }
170
171    pub fn with_focus_highlighter_brush(self: Rc<Self>, value: Option<Rc<dyn IBrush>>) -> Rc<Self> {
172        self.set_focus_highlighter_brush(value);
173        self
174    }
175
176    /// Set the [kind](DevToolsViewKind) of diagnostic view that show at launch of DevTools
177    pub fn launch_view(&self) -> DevToolsViewKind {
178        self.launch_view.get()
179    }
180
181    /// Initializes [`launch_view`](Self::launch_view) (`init` upstream).
182    pub fn with_launch_view(self: Rc<Self>, value: DevToolsViewKind) -> Rc<Self> {
183        self.launch_view.set(value);
184        self
185    }
186
187    /// Gets or inits the [`HotKeyConfiguration`] used to activate DevTools features
188    pub fn hot_keys(&self) -> Rc<HotKeyConfiguration> {
189        self.hot_keys.borrow().clone()
190    }
191
192    /// Initializes [`hot_keys`](Self::hot_keys) (`init` upstream).
193    pub fn with_hot_keys(self: Rc<Self>, value: Rc<HotKeyConfiguration>) -> Rc<Self> {
194        *self.hot_keys.borrow_mut() = value;
195        self
196    }
197}