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
use crate::utils::event::Event;
use crate::utils::style::{inline_style, scss_to_css};
use crate::widgets::widget::Widget;

/// # The state of a ProgressBar
///
/// ## Fields
///
/// ```text
/// min: i32
/// max: i32
/// value: i32
/// stretched: bool
/// style: String
/// ```
pub struct ProgressBarState {
    min: i32,
    max: i32,
    value: i32,
    stretched: bool,
    style: String,
}

impl ProgressBarState {
    /// Get the min
    pub fn min(&self) -> i32 {
        self.min
    }

    /// Get the max
    pub fn max(&self) -> i32 {
        self.max
    }

    /// Get the value
    pub fn value(&self) -> i32 {
        self.value
    }

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

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

    /// Set the min
    pub fn set_min(&mut self, min: i32) {
        self.min = min;
    }

    /// Set the max
    pub fn set_max(&mut self, max: i32) {
        self.max = max;
    }

    /// Set the value
    pub fn set_value(&mut self, value: i32) {
        self.value = if value > self.max {
            self.max
        } else if value < self.min {
            self.min
        } else {
            value
        };
    }

    /// Set the stretched flqg
    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 ProgressBar
pub trait ProgressBarListener {
    /// Function triggered on update event
    fn on_update(&self, state: &mut ProgressBarState);
}

/// # A progress bar
///
/// ## Fields
///
/// ```text
/// name: String
/// state: ProgressBarState
/// listener: Option<Box<dyn ProgressBarListener>>
/// ```
///
/// ## Default values
///
/// ```text
/// name: name.to_string()
/// state:
///     min: 0
///     max: 100
///     value: 0
///     stretched: false
///     style: "".to_string()
/// listener: None
/// ```
///
/// ## Style
///
/// ```text
/// div.progressbar
///     div.background
///     div.foreground
/// ```
/// 
/// ## Example
///
/// ```
/// use std::cell::RefCell;
/// use std::rc::Rc;
///
/// use neutrino::widgets::progressbar::{ProgressBar, ProgressBarListener, ProgressBarState};
/// use neutrino::utils::theme::Theme;
/// use neutrino::{App, Window};
///
///
/// struct Counter {
///     value: i32,
/// }
///
/// impl Counter {
///     fn new() -> Self {
///         Self { value: 0 }
///     }
///
///     fn value(&self) -> i32 {
///         self.value
///     }
/// }
///
///
/// struct MyProgressBarListener {
///     counter: Rc<RefCell<Counter>>,
/// }
///
/// impl MyProgressBarListener {
///    pub fn new(counter: Rc<RefCell<Counter>>) -> Self {
///        Self { counter }
///    }
/// }
///
/// impl ProgressBarListener for MyProgressBarListener {
///     fn on_update(&self, state: &mut ProgressBarState) {
///         state.set_value(self.counter.borrow().value());
///     }
/// }
///
///
/// fn main() {
///     let counter = Rc::new(RefCell::new(Counter::new()));
///
///     let my_listener = MyProgressBarListener::new(Rc::clone(&counter));
///
///     let mut my_progressbar = ProgressBar::new("my_progressbar");
///     my_progressbar.set_listener(Box::new(my_listener));
/// }
/// ```
pub struct ProgressBar {
    name: String,
    state: ProgressBarState,
    listener: Option<Box<dyn ProgressBarListener>>,
}

impl ProgressBar {
    /// Create a ProgressBar
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            state: ProgressBarState {
                min: 0,
                max: 100,
                value: 0,
                stretched: false,
                style: "".to_string(),
            },
            listener: None,
        }
    }

    // Set the min
    pub fn set_min(&mut self, min: i32) {
        self.state.set_min(min);
    }

    // Set the max
    pub fn set_max(&mut self, max: i32) {
        self.state.set_max(max);
    }

    // Set the value
    pub fn set_value(&mut self, value: i32) {
        self.state.set_value(value);
    }

    // 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 ProgressBarListener>) {
        self.listener = Some(listener);
    }

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

impl Widget for ProgressBar {
    fn eval(&self) -> String {
        let stretched = if self.state.stretched() {
            "stretched"
        } else {
            ""
        };
        let style = inline_style(&scss_to_css(&format!(
            r##"#{}{{{}}}"##,
            self.name,
            self.state.style(),
        )));
        let html = format!(
            r#"<div id="{}" class="progressbar {}"><div class="background"></div><div class="foreground" style="width: {}%;"></div></div>"#, 
            self.name,
            stretched,
            f64::from(self.state.value() - self.state.min()) /
            f64::from(self.state.max() - self.state.min()) *
            100.0,
        );
        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.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) {}
}