1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::;
use ;
/// A button is a clickable toolbar item that can be positioned on the top or bottom part of a window.
///
/// Toolbar buttons display text and can trigger actions when clicked. They can also have hotkeys
/// defined using the '&' character in the button caption. For example, the caption "St&art" will
/// set 'Alt+A' as a hotkey for the button.
///
/// To intercept button clicks, implement the `ToolBarEvents` trait for the window containing the button
/// and implement the `on_button_clicked` method.
///
/// # Example
///
/// The following example creates a window with a counter that can be incremented or decremented
/// using toolbar buttons:
///
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// #[Window(events = ToolBarEvents)]
/// struct CounterWindow {
/// increase_button: Handle<toolbar::Button>,
/// decrease_button: Handle<toolbar::Button>,
/// counter_label: Handle<Label>,
/// counter: i32,
/// }
///
/// impl CounterWindow {
/// fn new() -> Self {
/// let mut win = CounterWindow {
/// base: window!("'Counter',a:c,w:40,h:6"),
/// increase_button: Handle::None,
/// decrease_button: Handle::None,
/// counter_label: Handle::None,
/// counter: 0,
/// };
///
/// // Create a toolbar group at the bottom right of the window
/// let group = win.toolbar().create_group(toolbar::GroupPosition::BottomRight);
///
/// // Add buttons to the toolbar group
/// let mut btn_minus = toolbar::Button::new("-");
/// btn_minus.set_tooltip("Decrease counter");
/// win.decrease_button = win.toolbar().add(group, btn_minus);
/// let mut btn_plus = toolbar::Button::new("+");
/// btn_plus.set_tooltip("Increase counter");
/// win.increase_button = win.toolbar().add(group, btn_plus);
///
/// // Add a label to display the counter value
/// win.counter_label = win.add(label!("0,a:c,w:10,h:1"));
///
/// win
/// }
///
/// // Update the counter label
/// fn update_counter(&mut self) {
/// let h = self.counter_label;
/// let text = format!("{}", self.counter);
/// if let Some(label) = self.control_mut(h) {
/// label.set_caption(&text);
/// }
/// }
/// }
///
/// impl ToolBarEvents for CounterWindow {
/// fn on_button_clicked(&mut self, handle: Handle<toolbar::Button>) -> EventProcessStatus {
/// if handle == self.increase_button {
/// self.counter += 1;
/// self.update_counter();
/// EventProcessStatus::Processed
/// } else if handle == self.decrease_button {
/// self.counter -= 1;
/// self.update_counter();
/// EventProcessStatus::Processed
/// } else {
/// EventProcessStatus::Ignored
/// }
/// }
/// }
///
/// fn main() -> Result<(), appcui::system::Error> {
/// let mut app = App::new().build()?;
/// app.add_window(CounterWindow::new());
/// app.run();
/// Ok(())
/// }
/// ```
add_to_toolbar_impl!;