pispas_tray_app/
tray_utils.rs

1/*
2 * Authors: Jorge.A Duran & Mario Gónzalez
3 * Company: pispas Technologies SL
4 * Date: April 23, 2023
5 * Description: Utils Tray-icon.
6 */
7pub const ICON_ON_BYTES: &[u8] = include_bytes!("../../../resources/img/icon.ico");
8pub const ICON_OFF_BYTES: &[u8] = include_bytes!("../../../resources/img/iconOFF.ico");
9pub const ICON_ALERT_BYTES: &[u8] = include_bytes!("../../../resources/img/iconwarning.ico");
10
11pub mod utils {
12    use image::io::Reader as ImageReader;
13    use crate::pispas_tray::{TrayIconResult};
14
15    use std::io::Cursor;
16    use msgbox::common::MsgBoxError;
17    use msgbox::common::IconType;
18    #[allow(unreachable_patterns)]
19    pub fn open_window(title: &str, content: &str) {
20        match msgbox::create(title, content, IconType::None) {
21            Ok(()) => (),
22            Err(MsgBoxError::Create(_)) => {
23                easy_trace::instruments::tracing::error!(
24                    "Creation error: (type: {}, title: \"{}\", content: \"{}\")",
25                    IconType::None, title, content
26                );
27            }
28            _ => {}
29        }
30    }
31
32    pub fn load_icon(icon: &'static [u8]) -> TrayIconResult<tray_icon::Icon> {
33        let (icon_rgba, icon_width, icon_height) = {
34            let image = ImageReader::new(Cursor::new(icon)).with_guessed_format()?.decode()?.into_rgba8();
35
36            // let image = image::open(path)
37            //     .expect("Failed to open icon path")
38            //     .into_rgba8();
39            let (width, height) = image.dimensions();
40            let rgba = image.into_raw();
41            (rgba, width, height)
42        };
43        Ok(tray_icon::Icon::from_rgba(icon_rgba, icon_width, icon_height)?)
44    }
45
46}
47
48
49pub use utils::*;