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
292
293
294
295
296
297
298
299
300
301
302
303
use crate::utils::event::Event;
use crate::utils::icon::Icon;
use crate::utils::pixmap::Pixmap;
use crate::utils::style::{inline_style, scss_to_css};
use crate::widgets::widget::Widget;

/// # The state of an Image
///
/// ## Fields
///
/// ```text
/// data: String
/// extension: String
/// background: String
/// keep_ratio_aspect: bool
/// stretched: bool
/// style: String
/// ```
pub struct ImageState {
    data: String,
    extension: String,
    background: String,
    keep_ratio_aspect: bool,
    stretched: bool,
    style: String,
}

impl ImageState {
    /// Get the base64 encoded image data
    pub fn data(&self) -> &str {
        &self.data
    }

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

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

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

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

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

    /// Set the base64 encoded image data
    pub fn set_data(&mut self, data: &str) {
        self.data = data.to_string();
    }

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

    /// Set the background color
    pub fn set_background(&mut self, background: &str) {
        self.background = background.to_string();
    }

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

    /// Set the stretched 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 for an Image
pub trait ImageListener {
    /// Function triggered on update event
    fn on_update(&self, state: &mut ImageState);
}

/// # An element able to display images from icons and path
///
/// ## Fields
///
/// ```text
/// name: String
/// state: ImageState
/// listener: Option<Box<dyn ImageListener>>
/// ```
///
/// ## Default values
///
/// The variable `pixmap` is built in both constructors from the given Icon or
/// path.
///
/// ```text
/// name: name.to_string()
/// state:
///     data: pixmap.data().to_string()
///     extension: pixmap.extension().to_string()
///     background: "black".to_string()
///     keep_ratio_aspect: false
///     stretched: false
///     style: "".to_string()
/// listener: None
/// ```
/// 
/// ## Style
///
/// ```text
/// div.image
///     img
/// ```
/// 
/// ## Example
///
/// ```
/// use std::cell::RefCell;
/// use std::rc::Rc;
///
/// use neutrino::widgets::image::{Image, ImageListener, ImageState};
/// use neutrino::utils::theme::Theme;
/// use neutrino::utils::pixmap::Pixmap;
/// use neutrino::{App, Window};
///
///
/// struct Painting {
///     path: String,
/// }
///
/// impl Painting {
///     fn new(path: &str) -> Self {
///         Self { path: path.to_string() }
///     }
///
///     fn path(&self) -> &str {
///         &self.path
///     }
/// }
///
///
/// struct MyImageListener {
///     painting: Rc<RefCell<Painting>>,
/// }
///
/// impl MyImageListener {
///    pub fn new(painting: Rc<RefCell<Painting>>) -> Self {
///        Self { painting }
///    }
/// }
///
/// impl ImageListener for MyImageListener {
///     fn on_update(&self, state: &mut ImageState) {
///         let pixmap = Pixmap::from_path(self.painting.borrow().path());
///         state.set_data(pixmap.data());
///         state.set_extension(pixmap.extension());
///     }
/// }
///
///
/// fn main() {
///     let painting = Rc::new(RefCell::new(Painting::new("/home/neutrino/le_radeau_de_la_meduse.jpg")));
///
///     let my_listener = MyImageListener::new(Rc::clone(&painting));
///
///     let mut my_image = Image::from_path("my_image", "/home/neutrino/le_radeau_de_la_meduse.jpg");
///     my_image.set_listener(Box::new(my_listener));
/// }
/// ```
pub struct Image {
    name: String,
    state: ImageState,
    listener: Option<Box<dyn ImageListener>>,
}

impl Image {
    /// Create an image from a path
    pub fn from_path(name: &str, path: &str) -> Self {
        let pixmap = Pixmap::from_path(path);
        Self {
            name: name.to_string(),
            state: ImageState {
                data: pixmap.data().to_string(),
                extension: pixmap.extension().to_string(),
                background: "black".to_string(),
                keep_ratio_aspect: false,
                stretched: false,
                style: "".to_string(),
            },
            listener: None,
        }
    }

    /// Create an image from an icon
    pub fn from_icon(name: &str, icon: Box<dyn Icon>) -> Self {
        let pixmap = Pixmap::from_icon(icon);
        Self {
            name: name.to_string(),
            state: ImageState {
                data: pixmap.data().to_string(),
                extension: pixmap.extension().to_string(),
                background: "black".to_string(),
                keep_ratio_aspect: false,
                stretched: false,
                style: "".to_string(),
            },
            listener: None,
        }
    }

    /// Set the background color
    pub fn set_background(&mut self, background: &str) {
        self.state.set_background(background);
    }

    /// Set the keep_ratio_aspect flag to true
    pub fn set_keep_ratio_aspect(&mut self) {
        self.state.set_keep_ratio_aspect(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 ImageListener>) {
        self.listener = Some(listener);
    }

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

impl Widget for Image {
    fn eval(&self) -> String {
        let ratio = if self.state.keep_ratio_aspect() {
            ""
        } else {
            r#"width="100%" height="100%""#
        };
        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="image {}" style="background:{};"><img {} src="data:image/{};base64,{}" /></div>"#, 
            self.name,
            stretched,
            self.state.background(),
            ratio,
            self.state.extension(),
            self.state.data(),
        );
        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) {}
}