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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use crate::utils::event::Event;
use crate::utils::style::{inline_style, scss_to_css};
use crate::widgets::widget::Widget;

/// # The state of a CheckBox
///
/// ## Fields
///
/// ```text
/// text: String
/// checked: bool
/// disabled: bool
/// stretched: bool
/// style: String
/// ```
pub struct CheckBoxState {
    text: String,
    checked: bool,
    disabled: bool,
    stretched: bool,
    style: String,
}

impl CheckBoxState {
    /// Get the text
    pub fn text(&self) -> &str {
        &self.text
    }

    /// Get the checked flag
    pub fn checked(&self) -> bool {
        self.checked
    }

    /// Get the disabled flag
    pub fn disabled(&self) -> bool {
        self.disabled
    }

    /// Get the stretched flag
    pub fn stretched(&self) -> bool {
        self.stretched
    }

    /// Get the style
    pub fn style(&self) -> &str {
        &self.style
    }

    /// Set the text
    pub fn set_text(&mut self, text: &str) {
        self.text = text.to_string();
    }

    /// Set the checked flag
    pub fn set_checked(&mut self, checked: bool) {
        self.checked = checked;
    }

    /// Set the disabled flag
    pub fn set_disabled(&mut self, disabled: bool) {
        self.disabled = disabled;
    }

    /// Set the streched flag
    pub fn set_stretched(&mut self, stretched: bool) {
        self.stretched = stretched;
    }

    /// Set the style
    pub fn set_style(&mut self, style: &str) {
        self.style = style.to_string();
    }
}

/// # The listener of a Checkbox
pub trait CheckBoxListener {
    /// Function triggered on change event
    fn on_change(&self, state: &CheckBoxState);

    /// Function triggered on update event
    fn on_update(&self, state: &mut CheckBoxState);
}

/// # A togglable checkbox with a label
///
/// ## Fields
///
/// ```text
/// name: String
/// state: CheckBoxState
/// listener: Option<Box<dyn CheckBoxListener>>
/// ```
///
/// ## Default values
///
/// ```text
/// name: name.to_string()
/// state:
///     text: "CheckBox".to_string()
///     checked: false
///     disabled: false
///     stretched: false
///     style: "".to_string()
/// listener: None
/// ```
///
/// ## Style
///
/// ```text
/// div.checkbox[.disabled][.checked]
///     div.checkbox-outer
///         div.checkbox-inner
///     label
/// ```
///
/// ## Example
///
/// ```
/// use std::cell::RefCell;
/// use std::rc::Rc;
///
/// use neutrino::widgets::checkbox::{CheckBox, CheckBoxListener, CheckBoxState};
/// use neutrino::utils::theme::Theme;
/// use neutrino::{App, Window};
///
///
/// struct Switch {
///     value: bool,
/// }
///
/// impl Switch {
///     fn new() -> Self {
///         Self { value: false }
///     }
///
///     fn value(&self) -> bool {
///         self.value
///     }
///
///     fn toggle(&mut self) {
///         self.value = !self.value;
///     }
/// }
///
///
/// struct MyCheckBoxListener {
///     switch: Rc<RefCell<Switch>>,
/// }
///
/// impl MyCheckBoxListener {
///    pub fn new(switch: Rc<RefCell<Switch>>) -> Self {
///        Self { switch }
///    }
/// }
///
/// impl CheckBoxListener for MyCheckBoxListener {
///     fn on_change(&self, _state: &CheckBoxState) {
///         self.switch.borrow_mut().toggle();
///     }
///
///     fn on_update(&self, state: &mut CheckBoxState) {
///         state.set_checked(self.switch.borrow().value());
///     }
/// }
///
///
/// fn main() {
///     let switch = Rc::new(RefCell::new(Switch::new()));
///
///     let my_listener = MyCheckBoxListener::new(Rc::clone(&switch));
///
///     let mut my_checkbox = CheckBox::new("my_checkbox");
///     my_checkbox.set_text("Toggle me !");
///     my_checkbox.set_listener(Box::new(my_listener));
/// }
/// ```
pub struct CheckBox {
    name: String,
    state: CheckBoxState,
    listener: Option<Box<dyn CheckBoxListener>>,
}

impl CheckBox {
    /// Create a CheckBox
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            state: CheckBoxState {
                text: "CheckBox".to_string(),
                checked: false,
                disabled: false,
                stretched: false,
                style: "".to_string(),
            },
            listener: None,
        }
    }

    /// Set the text
    pub fn set_text(&mut self, text: &str) {
        self.state.set_text(text);
    }

    /// Set the checked flag to true
    pub fn set_checked(&mut self) {
        self.state.set_checked(true);
    }

    /// Set the disabled flag to true
    pub fn set_disabled(&mut self) {
        self.state.set_disabled(true);
    }

    /// Set the stretched flag to true
    pub fn set_stretched(&mut self) {
        self.state.set_stretched(true);
    }

    /// Set the listener
    pub fn set_listener(&mut self, listener: Box<dyn CheckBoxListener>) {
        self.listener = Some(listener);
    }

    /// Set the style
    pub fn set_style(&mut self, style: &str) {
        self.state.set_style(style);
    }
}

impl Widget for CheckBox {
    fn eval(&self) -> String {
        let checked = if self.state.checked() { "checked" } else { "" };
        let stretched = if self.state.stretched() {
            "stretched"
        } else {
            ""
        };
        let disabled = if self.state.disabled() {
            "disabled"
        } else {
            ""
        };
        let style = inline_style(&scss_to_css(&format!(
            r##"#{}{{{}}}"##,
            self.name,
            self.state.style(),
        )));
        let html = format!(
            r#"<div id="{}" class="checkbox {} {} {}" onmousedown="{}"><div class="checkbox-outer"><div class="checkbox-inner"></div></div><label>{}</label></div>"#, 
            self.name,
            disabled,
            checked,
            stretched,
            Event::change_js(&self.name, "''"), 
            self.state.text,
        );
        format!("{}{}", style, html)
    }

    fn trigger(&mut self, event: &Event) {
        match event {
            Event::Update => self.on_update(),
            Event::Change { source, value } => {
                if source == &self.name && !self.state.disabled() {
                    self.on_change(value)
                }
            }
            _ => (),
        }
    }

    fn on_update(&mut self) {
        match &self.listener {
            None => (),
            Some(listener) => {
                listener.on_update(&mut self.state);
            }
        }
    }

    fn on_change(&mut self, _value: &str) {
        self.state.set_checked(!self.state.checked());
        match &self.listener {
            None => (),
            Some(listener) => {
                listener.on_change(&self.state);
            }
        }
    }
}