demo_default/demo_mod/
listeners.rs

1use std::cell::RefCell;
2use std::rc::Rc;
3
4use neutrino::utils::event::Key;
5use neutrino::widgets::button::{ButtonListener, ButtonState};
6use neutrino::widgets::checkbox::{CheckBoxListener, CheckBoxState};
7use neutrino::widgets::combo::{ComboListener, ComboState};
8use neutrino::widgets::label::{LabelListener, LabelState};
9use neutrino::widgets::menubar::{MenuBarListener, MenuBarState};
10use neutrino::widgets::progressbar::{ProgressBarListener, ProgressBarState};
11use neutrino::widgets::radio::{RadioListener, RadioState};
12use neutrino::widgets::range::{RangeListener, RangeState};
13use neutrino::widgets::tabs::{TabsListener, TabsState};
14use neutrino::widgets::textinput::{TextInputListener, TextInputState};
15use neutrino::WindowListener;
16
17use super::models::{Panes, State};
18
19/*
20 Window listener: waits for menu shortcuts
21*/
22pub struct MyWindowListener {
23    panes: Rc<RefCell<Panes>>,
24}
25
26impl MyWindowListener {
27    pub fn new(panes: Rc<RefCell<Panes>>) -> Self {
28        Self { panes }
29    }
30}
31
32impl WindowListener for MyWindowListener {
33    fn on_key(&self, key: Key) {
34        match key {
35            Key::Num1 => self.panes.borrow_mut().set_value(0),
36            Key::Num2 => self.panes.borrow_mut().set_value(1),
37            Key::Num3 => self.panes.borrow_mut().set_value(2),
38            Key::Q => std::process::exit(0),
39            _ => (),
40        }
41    }
42
43    fn on_tick(&self) {}
44}
45
46/*
47 Tabs Listener: change current tab when user clicks on a tab label,
48 on a menu item or uses a shortcut
49*/
50pub struct MyTabsListener {
51    panes: Rc<RefCell<Panes>>,
52}
53
54impl MyTabsListener {
55    pub fn new(panes: Rc<RefCell<Panes>>) -> Self {
56        Self { panes }
57    }
58}
59
60impl TabsListener for MyTabsListener {
61    fn on_update(&self, state: &mut TabsState) {
62        state.set_selected(u32::from(self.panes.borrow().value()));
63    }
64
65    fn on_change(&self, state: &TabsState) {
66        self.panes.borrow_mut().set_value(state.selected() as u8);
67    }
68}
69
70/* Menu Bar Listener: waits for the user to select a menu item */
71pub struct MyMenuBarListener {
72    panes: Rc<RefCell<Panes>>,
73}
74
75impl MyMenuBarListener {
76    pub fn new(panes: Rc<RefCell<Panes>>) -> Self {
77        Self { panes }
78    }
79}
80
81impl MenuBarListener for MyMenuBarListener {
82    fn on_change(&self, state: &MenuBarState) {
83        match state.selected_item() {
84            None => (),
85            Some(selected_item) => {
86                if selected_item == 0 {
87                    std::process::exit(0);
88                } else if selected_item == 1 {
89                    match state.selected_function() {
90                        None => (),
91                        Some(selected_function) => {
92                            self.panes
93                                .borrow_mut()
94                                .set_value(selected_function as u8);
95                        }
96                    }
97                }
98            }
99        }
100    }
101}
102
103/* Range Listener: update State when the user scroll the Range widget */
104pub struct MyRangeListener {
105    state: Rc<RefCell<State>>,
106}
107
108impl MyRangeListener {
109    pub fn new(state: Rc<RefCell<State>>) -> Self {
110        Self { state }
111    }
112}
113
114impl RangeListener for MyRangeListener {
115    fn on_update(&self, state: &mut RangeState) {
116        state.set_value(self.state.borrow().range());
117        state.set_disabled(self.state.borrow().disabled());
118    }
119    fn on_change(&self, state: &RangeState) {
120        self.state.borrow_mut().set_range(state.value());
121    }
122}
123
124/* Progress Bar Listener: update the Progress Bar value to the current
125State value*/
126pub struct MyProgressBarListener {
127    state: Rc<RefCell<State>>,
128}
129
130impl MyProgressBarListener {
131    pub fn new(state: Rc<RefCell<State>>) -> Self {
132        Self { state }
133    }
134}
135
136impl ProgressBarListener for MyProgressBarListener {
137    fn on_update(&self, state: &mut ProgressBarState) {
138        state.set_value(self.state.borrow().range());
139    }
140}
141
142/* Label Listenr: update the Label text to show the current State value,
143formatted as a percent */
144pub struct MyLabelListener {
145    state: Rc<RefCell<State>>,
146}
147
148impl MyLabelListener {
149    pub fn new(state: Rc<RefCell<State>>) -> Self {
150        Self { state }
151    }
152}
153
154impl LabelListener for MyLabelListener {
155    fn on_update(&self, state: &mut LabelState) {
156        let text = format!("{}%", self.state.borrow().range());
157        state.set_text(&text);
158    }
159}
160
161/* Text Input Listener: update the TextInput value to the
162current State value or set the State when the user
163changes the TextInput value */
164pub struct MyTextInputListener {
165    state: Rc<RefCell<State>>,
166}
167
168impl MyTextInputListener {
169    pub fn new(state: Rc<RefCell<State>>) -> Self {
170        Self { state }
171    }
172}
173
174impl TextInputListener for MyTextInputListener {
175    fn on_update(&self, state: &mut TextInputState) {
176        state.set_value(&self.state.borrow().range().to_string());
177        state.set_disabled(self.state.borrow().disabled());
178    }
179    fn on_change(&self, state: &TextInputState) {
180        self.state
181            .borrow_mut()
182            .set_range(state.value().parse().unwrap_or(0));
183    }
184}
185
186pub struct MyButtonListener {
187    state: Rc<RefCell<State>>,
188}
189
190impl MyButtonListener {
191    pub fn new(state: Rc<RefCell<State>>) -> Self {
192        Self { state }
193    }
194}
195
196impl ButtonListener for MyButtonListener {
197    fn on_update(&self, state: &mut ButtonState) {
198        state.set_disabled(self.state.borrow().disabled());
199    }
200
201    fn on_change(&self, _state: &ButtonState) {}
202}
203
204pub struct MyComboListener {
205    state: Rc<RefCell<State>>,
206}
207
208impl MyComboListener {
209    pub fn new(state: Rc<RefCell<State>>) -> Self {
210        Self { state }
211    }
212}
213
214impl ComboListener for MyComboListener {
215    fn on_update(&self, state: &mut ComboState) {
216        state.set_disabled(self.state.borrow().disabled());
217    }
218
219    fn on_change(&self, _state: &ComboState) {}
220}
221
222pub struct MyRadioListener {
223    state: Rc<RefCell<State>>,
224}
225
226impl MyRadioListener {
227    pub fn new(state: Rc<RefCell<State>>) -> Self {
228        Self { state }
229    }
230}
231
232impl RadioListener for MyRadioListener {
233    fn on_update(&self, state: &mut RadioState) {
234        state.set_disabled(self.state.borrow().disabled());
235    }
236
237    fn on_change(&self, _state: &RadioState) {}
238}
239
240pub struct MyCheckBoxListener {
241    state: Rc<RefCell<State>>,
242}
243
244impl MyCheckBoxListener {
245    pub fn new(state: Rc<RefCell<State>>) -> Self {
246        Self { state }
247    }
248}
249
250impl CheckBoxListener for MyCheckBoxListener {
251    fn on_update(&self, state: &mut CheckBoxState) {
252        state.set_disabled(self.state.borrow().disabled());
253    }
254
255    fn on_change(&self, _state: &CheckBoxState) {}
256}
257
258pub struct MyCheckBoxDisabledListener {
259    state: Rc<RefCell<State>>,
260}
261
262impl MyCheckBoxDisabledListener {
263    pub fn new(state: Rc<RefCell<State>>) -> Self {
264        Self { state }
265    }
266}
267
268impl CheckBoxListener for MyCheckBoxDisabledListener {
269    fn on_update(&self, state: &mut CheckBoxState) {
270        state.set_checked(self.state.borrow().disabled());
271    }
272
273    fn on_change(&self, state: &CheckBoxState) {
274        self.state.borrow_mut().set_disabled(state.checked());
275    }
276}