appcui/ui/appbar/events.rs
1use crate::ui::AppBar;
2
3/// A trait that defines the event handlers for AppBar interation
4pub trait AppBarEvents {
5 /// Called when a button is clicked.
6 ///
7 /// # Parameters
8 /// * `button` - A handle to the button that was clicked.
9 ///
10 /// # Example
11 ///
12 /// ```rust, no_run
13 /// use appcui::prelude::*;
14 ///
15 /// #[Desktop(events = AppBarEvents+DesktopEvents)]
16 /// struct MyDesktop {
17 /// my_button: Handle<appbar::Button>,
18 /// }
19 ///
20 /// impl MyDesktop {
21 /// fn new() -> Self {
22 /// Self {
23 /// base: Desktop::new(),
24 /// my_button: Handle::None,
25 /// }
26 /// }
27 /// }
28 ///
29 /// impl DesktopEvents for MyDesktop {
30 /// fn on_start(&mut self) {
31 /// self.my_button = self.appbar().add(
32 /// appbar::Button::new("Button",
33 /// 0,
34 /// appbar::Side::Left));
35 /// }
36 /// }
37 ///
38 /// impl AppBarEvents for MyDesktop {
39 /// fn on_button_click(&mut self, button: Handle<appbar::Button>) {
40 /// // Do something when the button is clicked
41 /// }
42 /// }
43 /// ```
44 fn on_button_click(&mut self, _button: Handle<super::Button>) {}
45
46 /// Called when a toggle button's state changes.
47 ///
48 /// # Parameters
49 /// * `togglebutton` - A handle to the toggle button that had its state changed.
50 /// * `selected` - The new state of the toggle button.
51 ///
52 /// # Example
53 ///
54 /// ```rust, no_run
55 /// use appcui::prelude::*;
56 ///
57 /// #[Desktop(events = AppBarEvents+DesktopEvents)]
58 /// struct MyDesktop {
59 /// my_togglebutton: Handle<appbar::ToggleButton>,
60 /// }
61 ///
62 /// impl MyDesktop {
63 /// fn new() -> Self {
64 /// Self {
65 /// base: Desktop::new(),
66 /// my_togglebutton: Handle::None,
67 /// }
68 /// }
69 /// }
70 ///
71 /// impl DesktopEvents for MyDesktop {
72 /// fn on_start(&mut self) {
73 /// self.my_togglebutton = self.appbar().add(
74 /// appbar::ToggleButton::new("ToggleButton",
75 /// false,
76 /// 0,
77 /// appbar::Side::Left));
78 /// }
79 /// }
80 ///
81 /// impl AppBarEvents for MyDesktop {
82 /// fn on_togglebutton_state_changed(&mut self, togglebutton: Handle<appbar::ToggleButton>, selected: bool) {
83 /// // Do something when the toggle button's state changes
84 /// }
85 /// }
86 /// ```
87 fn on_togglebutton_state_changed(&mut self, _togglebutton: Handle<super::ToggleButton>, _selected: bool) {}
88
89 /// Called when a switch button's state changes.
90 ///
91 /// # Parameters
92 /// * `switchbutton` - A handle to the switch button that had its state changed.
93 /// * `selected` - The new state of the switch button.
94 ///
95 /// # Example
96 ///
97 /// ```rust, no_run
98 /// use appcui::prelude::*;
99 ///
100 /// #[Desktop(events = AppBarEvents+DesktopEvents)]
101 /// struct MyDesktop {
102 /// my_switchbutton: Handle<appbar::SwitchButton>,
103 /// }
104 ///
105 /// impl MyDesktop {
106 /// fn new() -> Self {
107 /// Self {
108 /// base: Desktop::new(),
109 /// my_switchbutton: Handle::None,
110 /// }
111 /// }
112 /// }
113 ///
114 /// impl DesktopEvents for MyDesktop {
115 /// fn on_start(&mut self) {
116 /// self.my_switchbutton = self.appbar().add(
117 /// appbar::SwitchButton::new(" State-1 ", " State-2 ",
118 /// false,
119 /// 0,
120 /// appbar::Side::Left));
121 /// }
122 /// }
123 ///
124 /// impl AppBarEvents for MyDesktop {
125 /// fn on_switchbutton_state_changed(&mut self, switchbutton: Handle<appbar::SwitchButton>, selected: bool) {
126 /// // Do something when the switch button's state changes
127 /// }
128 /// }
129 /// ```
130 fn on_switchbutton_state_changed(&mut self, _switchbutton: Handle<super::SwitchButton>, _selected: bool) {}
131
132
133
134 /// Called when the app bar needs to be updated. By default, all items are hidden and whenever the focus changes, the AppCUI framework hides all items and starts from the focus control and moves to its parent and calls this method. In this method you should call appbar.show(...) method to show the items you want to show.
135 ///
136 /// This method is used to add appbar items (e.g. usually a MenuButton)
137 /// It is called whenever the focus changes or when a control
138 /// explicitly requests a app bar update.
139 ///
140 /// # Parameters
141 /// * `appbar` - A mutable reference to the AppCUI AppBar object.
142 ///
143 /// # Example
144 ///
145 /// ```rust, no_run
146 /// use appcui::prelude::*;
147 ///
148 /// #[Desktop(events = AppBarEvents+DesktopEvents)]
149 /// struct MyDesktop {
150 /// my_button: Handle<appbar::Button>,
151 /// }
152 /// impl MyDesktop {
153 /// fn new() -> Self {
154 /// Self {
155 /// base: Desktop::new(),
156 /// my_button: Handle::None,
157 /// }
158 /// }
159 /// }
160 ///
161 /// impl DesktopEvents for MyDesktop {
162 /// fn on_start(&mut self) {
163 /// // Add the button to the app bar and store the handle
164 /// self.my_button = self.appbar().add(
165 /// appbar::Button::new("Button",
166 /// 0,
167 /// appbar::Side::Left));
168 /// }
169 /// }
170 ///
171 /// impl AppBarEvents for MyDesktop {
172 /// fn on_update(&self, appbar: &mut AppBar) {
173 /// // Show the button in the app bar
174 /// appbar.show(self.my_button);
175 /// }
176 /// }
177 /// ```
178 fn on_update(&self, _appbar: &mut AppBar) {}
179}
180
181use crate::system::Handle;
182
183#[derive(Copy, Clone)]
184pub(crate) struct ButtonClickEvent {
185 pub(crate) button_handle: Handle<super::Button>,
186 pub(crate) control_receiver_handle: Handle<()>,
187}
188
189#[derive(Copy, Clone)]
190pub(crate) struct ToggleButtonStatusChangedEvent {
191 pub(crate) button_handle: Handle<super::ToggleButton>,
192 pub(crate) control_receiver_handle: Handle<()>,
193 pub(crate) state: bool,
194}
195#[derive(Copy, Clone)]
196pub(crate) struct SwitchButtonStatusChangedEvent {
197 pub(crate) button_handle: Handle<super::SwitchButton>,
198 pub(crate) control_receiver_handle: Handle<()>,
199 pub(crate) state: bool,
200}
201
202#[derive(Copy, Clone)]
203pub(crate) enum AppBarEvent {
204 ButtonClick(ButtonClickEvent),
205 ToggleButtonStatusChanged(ToggleButtonStatusChangedEvent),
206 SwitchButtonStatusChanged(SwitchButtonStatusChangedEvent),
207}