Skip to main content

ferroui_diagnostics/
dev_tools_extensions.rs

1//! Port of `DevToolsExtensions.cs`: the extension methods that attach DevTools to a top level
2//! or an application.
3
4use crate::diagnostics::{DevTools, DevToolsOptions};
5use ferroui_base::input::KeyGesture;
6use ferroui_controls::{Application, TopLevel};
7use std::rc::Rc;
8
9/// Extension methods for attaching DevTools..
10///
11/// Implemented by top levels (windows) and by the application.
12pub trait DevToolsExtensions {
13    /// Attaches DevTools, to be opened with the F12 key.
14    fn attach_dev_tools(&self);
15
16    /// Attaches DevTools, to be opened with the specified options.
17    ///
18    /// `options` are the additional settings of DevTools.
19    ///
20    /// For an application, attach DevTools should only be called after application
21    /// initialization is complete. A good point is `on_framework_initialization_completed`:
22    ///
23    /// ```ignore
24    /// impl ApplicationImpl for App {
25    ///     fn on_framework_initialization_completed(this: &Self) {
26    ///         // Create the main window of the lifetime of the application...
27    ///         Self::parent_on_framework_initialization_completed(this);
28    ///         this.attach_dev_tools_with_options(DevToolsOptions::new().with_startup_screen_index(Some(1)));
29    ///     }
30    /// }
31    /// ```
32    fn attach_dev_tools_with_options(&self, options: Rc<DevToolsOptions>);
33}
34
35/// The extension method of top levels for attaching DevTools with a key gesture (upstream has it
36/// for top levels only).
37pub trait DevToolsTopLevelExtensions {
38    /// Attaches DevTools to a window, to be opened with the specified key gesture.
39    ///
40    /// `gesture` is the key gesture to open DevTools.
41    fn attach_dev_tools_with_gesture(&self, gesture: KeyGesture);
42}
43
44impl DevToolsExtensions for TopLevel {
45    /// Attaches DevTools to a window, to be opened with the F12 key.
46    fn attach_dev_tools(&self) {
47        DevTools::attach(self, DevToolsOptions::new());
48    }
49
50    /// Attaches DevTools to a window, to be opened with the specified options.
51    fn attach_dev_tools_with_options(&self, options: Rc<DevToolsOptions>) {
52        DevTools::attach(self, options);
53    }
54}
55
56impl DevToolsTopLevelExtensions for TopLevel {
57    fn attach_dev_tools_with_gesture(&self, gesture: KeyGesture) {
58        DevTools::attach_with_gesture(self, gesture);
59    }
60}
61
62impl DevToolsExtensions for Application {
63    /// Attaches DevTools to an application, to be opened with the F12 key.
64    fn attach_dev_tools(&self) {
65        DevTools::attach_application(self, DevToolsOptions::new());
66    }
67
68    /// Attaches DevTools to an application, to be opened with the specified options.
69    fn attach_dev_tools_with_options(&self, options: Rc<DevToolsOptions>) {
70        DevTools::attach_application(self, options);
71    }
72}