|
| 1 | +use anyhow::Result; |
| 2 | +use image::load_from_memory; |
| 3 | +use image::GenericImageView; |
| 4 | +use image::ImageReader; |
| 5 | +use std::path::Path; |
| 6 | + |
| 7 | +/// Trait that creates icons for various types |
| 8 | +pub trait DioxusIconTrait { |
| 9 | + fn get_icon() -> Result<Self> |
| 10 | + where |
| 11 | + Self: Sized; |
| 12 | + fn from_memory(value: &[u8]) -> Result<Self> |
| 13 | + where |
| 14 | + Self: Sized; |
| 15 | + fn path<P: AsRef<Path>>(path: P, size: Option<(u32, u32)>) -> Result<Self> |
| 16 | + where |
| 17 | + Self: Sized; |
| 18 | +} |
| 19 | + |
| 20 | +// preferably this would have platform specific implementations, not just for windows |
| 21 | +#[cfg(not(target_os = "windows"))] |
| 22 | +static DEFAULT_ICON: &[u8] = include_bytes!("./assets/default_icon.png"); |
| 23 | + |
| 24 | +#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] |
| 25 | +use crate::trayicon::DioxusTrayIcon; |
| 26 | + |
| 27 | +fn load_image_from_memory(value: &[u8]) -> Result<(Vec<u8>, u32, u32)> { |
| 28 | + let img = load_from_memory(value)?; |
| 29 | + let rgba = img.to_rgba8(); |
| 30 | + let (width, height) = img.dimensions(); |
| 31 | + Ok((rgba.to_vec(), width, height)) |
| 32 | +} |
| 33 | + |
| 34 | +fn load_image_from_path<P: AsRef<Path>>(path: P) -> Result<(Vec<u8>, u32, u32)> { |
| 35 | + let img = ImageReader::open(path)?.decode()?; |
| 36 | + let rgba = img.to_rgba8(); |
| 37 | + let (width, height) = img.dimensions(); |
| 38 | + Ok((rgba.to_vec(), width, height)) |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] |
| 42 | +impl DioxusIconTrait for DioxusTrayIcon { |
| 43 | + fn get_icon() -> Result<Self> |
| 44 | + where |
| 45 | + Self: Sized, |
| 46 | + { |
| 47 | + #[cfg(target_os = "windows")] |
| 48 | + #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 49 | + { |
| 50 | + let (img, width, height) = load_image_from_memory(DEFAULT_ICON)?; |
| 51 | + DioxusTrayIcon::from_rgba(img, width, height).map_err(Into::into) |
| 52 | + } |
| 53 | + #[cfg(target_os = "windows")] |
| 54 | + DioxusTrayIcon::from_resource(32512, None).map_err(Into::into) |
| 55 | + } |
| 56 | + |
| 57 | + fn from_memory(value: &[u8]) -> Result<Self> |
| 58 | + where |
| 59 | + Self: Sized, |
| 60 | + { |
| 61 | + let (icon, width, height) = load_image_from_memory(value)?; |
| 62 | + DioxusTrayIcon::from_rgba(icon, width, height).map_err(Into::into) |
| 63 | + } |
| 64 | + |
| 65 | + fn path<P: AsRef<Path>>(path: P, size: Option<(u32, u32)>) -> Result<Self> |
| 66 | + where |
| 67 | + Self: Sized, |
| 68 | + { |
| 69 | + let (img, width, height) = load_image_from_path(path)?; |
| 70 | + if let Some((width, height)) = size { |
| 71 | + Ok(DioxusTrayIcon::from_rgba(img, width, height)?) |
| 72 | + } else { |
| 73 | + Ok(DioxusTrayIcon::from_rgba(img, width, height)?) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(not(any(target_os = "ios", target_os = "android")))] |
| 79 | +use crate::menubar::DioxusMenuIcon; |
| 80 | + |
| 81 | +#[cfg(not(any(target_os = "ios", target_os = "android")))] |
| 82 | +impl DioxusIconTrait for DioxusMenuIcon { |
| 83 | + fn get_icon() -> Result<Self> |
| 84 | + where |
| 85 | + Self: Sized, |
| 86 | + { |
| 87 | + #[cfg(not(target_os = "windows"))] |
| 88 | + { |
| 89 | + let (img, width, height) = load_image_from_memory(DEFAULT_ICON)?; |
| 90 | + DioxusMenuIcon::from_rgba(img, width, height).map_err(Into::into) |
| 91 | + } |
| 92 | + #[cfg(target_os = "windows")] |
| 93 | + DioxusMenuIcon::from_resource(32512, None).map_err(Into::into) |
| 94 | + } |
| 95 | + |
| 96 | + fn from_memory(value: &[u8]) -> Result<Self> |
| 97 | + where |
| 98 | + Self: Sized, |
| 99 | + { |
| 100 | + let (icon, width, height) = load_image_from_memory(value)?; |
| 101 | + DioxusMenuIcon::from_rgba(icon, width, height).map_err(Into::into) |
| 102 | + } |
| 103 | + |
| 104 | + fn path<P: AsRef<Path>>(path: P, size: Option<(u32, u32)>) -> Result<Self> |
| 105 | + where |
| 106 | + Self: Sized, |
| 107 | + { |
| 108 | + let (img, width, height) = load_image_from_path(path)?; |
| 109 | + if let Some((width, height)) = size { |
| 110 | + Ok(DioxusMenuIcon::from_rgba(img, width, height)?) |
| 111 | + } else { |
| 112 | + Ok(DioxusMenuIcon::from_rgba(img, width, height)?) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +use tao::window::Icon; |
| 118 | + |
| 119 | +#[cfg(target_os = "windows")] |
| 120 | +use tao::platform::windows::IconExtWindows; |
| 121 | + |
| 122 | +impl DioxusIconTrait for Icon { |
| 123 | + fn get_icon() -> Result<Self> |
| 124 | + where |
| 125 | + Self: Sized, |
| 126 | + { |
| 127 | + #[cfg(not(target_os = "windows"))] |
| 128 | + { |
| 129 | + let (img, width, height) = load_image_from_memory(DEFAULT_ICON)?; |
| 130 | + Icon::from_rgba(img, width, height).map_err(Into::into) |
| 131 | + } |
| 132 | + #[cfg(target_os = "windows")] |
| 133 | + Icon::from_resource(32512, None).map_err(Into::into) |
| 134 | + } |
| 135 | + |
| 136 | + fn from_memory(value: &[u8]) -> Result<Self> |
| 137 | + where |
| 138 | + Self: Sized, |
| 139 | + { |
| 140 | + let (icon, width, height) = load_image_from_memory(value)?; |
| 141 | + Icon::from_rgba(icon, width, height).map_err(Into::into) |
| 142 | + } |
| 143 | + |
| 144 | + fn path<P: AsRef<Path>>(path: P, size: Option<(u32, u32)>) -> Result<Self> |
| 145 | + where |
| 146 | + Self: Sized, |
| 147 | + { |
| 148 | + let (img, width, height) = load_image_from_path(path)?; |
| 149 | + if let Some((width, height)) = size { |
| 150 | + Ok(Icon::from_rgba(img, width, height)?) |
| 151 | + } else { |
| 152 | + Ok(Icon::from_rgba(img, width, height)?) |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/// Provides the default icon of the app |
| 158 | +pub fn default_icon<T: DioxusIconTrait>() -> Result<T> { |
| 159 | + T::get_icon() |
| 160 | +} |
| 161 | + |
| 162 | +/// Helper function to load image from include_bytes!("image.png") |
| 163 | +pub fn icon_from_memory<T: DioxusIconTrait>(value: &[u8]) -> Result<T> { |
| 164 | + T::from_memory(value) |
| 165 | +} |
| 166 | + |
| 167 | +/// Helper function to load image from path |
| 168 | +pub fn icon_from_path<T: DioxusIconTrait, P: AsRef<Path>>( |
| 169 | + path: P, |
| 170 | + size: Option<(u32, u32)>, |
| 171 | +) -> Result<T> { |
| 172 | + T::path(path, size) |
| 173 | +} |
0 commit comments