Skip to content

Commit a32563d

Browse files
committed
fix: windows crash when creating a webview if winres fails
1 parent 02c5721 commit a32563d

3 files changed

Lines changed: 43 additions & 36 deletions

File tree

packages/desktop/src/default_icon.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use std::path::Path;
66

77
/// Trait that creates icons for various types
88
pub trait DioxusIconTrait {
9-
fn get_icon() -> Self
9+
fn get_icon() -> Result<Self>
1010
where
1111
Self: Sized;
12-
fn from_memory(value: &[u8]) -> Self
12+
fn from_memory(value: &[u8]) -> Result<Self>
1313
where
1414
Self: Sized;
1515
fn path<P: AsRef<Path>>(path: P, size: Option<(u32, u32)>) -> Result<Self>
@@ -24,11 +24,11 @@ static DEFAULT_ICON: &[u8] = include_bytes!("./assets/default_icon.png");
2424
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
2525
use crate::trayicon::DioxusTrayIcon;
2626

27-
fn load_image_from_memory(value: &[u8]) -> (Vec<u8>, u32, u32) {
28-
let img = load_from_memory(value).expect("MISSING DEFAULT ICON");
27+
fn load_image_from_memory(value: &[u8]) -> Result<(Vec<u8>, u32, u32)> {
28+
let img = load_from_memory(value)?;
2929
let rgba = img.to_rgba8();
3030
let (width, height) = img.dimensions();
31-
(rgba.to_vec(), width, height)
31+
Ok((rgba.to_vec(), width, height))
3232
}
3333

3434
fn load_image_from_path<P: AsRef<Path>>(path: P) -> Result<(Vec<u8>, u32, u32)> {
@@ -40,25 +40,26 @@ fn load_image_from_path<P: AsRef<Path>>(path: P) -> Result<(Vec<u8>, u32, u32)>
4040

4141
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
4242
impl DioxusIconTrait for DioxusTrayIcon {
43-
fn get_icon() -> Self
43+
fn get_icon() -> Result<Self>
4444
where
4545
Self: Sized,
4646
{
47+
#[cfg(target_os = "windows")]
4748
#[cfg(any(target_os = "linux", target_os = "macos"))]
4849
{
49-
let (img, width, height) = load_image_from_memory(DEFAULT_ICON);
50-
DioxusTrayIcon::from_rgba(img, width, height).expect("image parse failed")
50+
let (img, width, height) = load_image_from_memory(DEFAULT_ICON)?;
51+
DioxusTrayIcon::from_rgba(img, width, height).map_err(Into::into)
5152
}
5253
#[cfg(target_os = "windows")]
53-
DioxusTrayIcon::from_resource(32512, None).expect("image parse failed")
54+
DioxusTrayIcon::from_resource(32512, None).map_err(Into::into)
5455
}
5556

56-
fn from_memory(value: &[u8]) -> Self
57+
fn from_memory(value: &[u8]) -> Result<Self>
5758
where
5859
Self: Sized,
5960
{
60-
let (icon, width, height) = load_image_from_memory(value);
61-
DioxusTrayIcon::from_rgba(icon, width, height).expect("image parse failed")
61+
let (icon, width, height) = load_image_from_memory(value)?;
62+
DioxusTrayIcon::from_rgba(icon, width, height).map_err(Into::into)
6263
}
6364

6465
fn path<P: AsRef<Path>>(path: P, size: Option<(u32, u32)>) -> Result<Self>
@@ -79,25 +80,25 @@ use crate::menubar::DioxusMenuIcon;
7980

8081
#[cfg(not(any(target_os = "ios", target_os = "android")))]
8182
impl DioxusIconTrait for DioxusMenuIcon {
82-
fn get_icon() -> Self
83+
fn get_icon() -> Result<Self>
8384
where
8485
Self: Sized,
8586
{
8687
#[cfg(not(target_os = "windows"))]
8788
{
88-
let (img, width, height) = load_image_from_memory(DEFAULT_ICON);
89-
DioxusMenuIcon::from_rgba(img, width, height).expect("image parse failed")
89+
let (img, width, height) = load_image_from_memory(DEFAULT_ICON)?;
90+
DioxusMenuIcon::from_rgba(img, width, height).map_err(Into::into)
9091
}
9192
#[cfg(target_os = "windows")]
92-
DioxusMenuIcon::from_resource(32512, None).expect("image parse failed")
93+
DioxusMenuIcon::from_resource(32512, None).map_err(Into::into)
9394
}
9495

95-
fn from_memory(value: &[u8]) -> Self
96+
fn from_memory(value: &[u8]) -> Result<Self>
9697
where
9798
Self: Sized,
9899
{
99-
let (icon, width, height) = load_image_from_memory(value);
100-
DioxusMenuIcon::from_rgba(icon, width, height).expect("image parse failed")
100+
let (icon, width, height) = load_image_from_memory(value)?;
101+
DioxusMenuIcon::from_rgba(icon, width, height).map_err(Into::into)
101102
}
102103

103104
fn path<P: AsRef<Path>>(path: P, size: Option<(u32, u32)>) -> Result<Self>
@@ -119,25 +120,25 @@ use tao::window::Icon;
119120
use tao::platform::windows::IconExtWindows;
120121

121122
impl DioxusIconTrait for Icon {
122-
fn get_icon() -> Self
123+
fn get_icon() -> Result<Self>
123124
where
124125
Self: Sized,
125126
{
126127
#[cfg(not(target_os = "windows"))]
127128
{
128-
let (img, width, height) = load_image_from_memory(DEFAULT_ICON);
129-
Icon::from_rgba(img, width, height).expect("image parse failed")
129+
let (img, width, height) = load_image_from_memory(DEFAULT_ICON)?;
130+
Icon::from_rgba(img, width, height).map_err(Into::into)
130131
}
131132
#[cfg(target_os = "windows")]
132-
Icon::from_resource(32512, None).expect("image parse failed")
133+
Icon::from_resource(32512, None).map_err(Into::into)
133134
}
134135

135-
fn from_memory(value: &[u8]) -> Self
136+
fn from_memory(value: &[u8]) -> Result<Self>
136137
where
137138
Self: Sized,
138139
{
139-
let (icon, width, height) = load_image_from_memory(value);
140-
Icon::from_rgba(icon, width, height).expect("image parse failed")
140+
let (icon, width, height) = load_image_from_memory(value)?;
141+
Icon::from_rgba(icon, width, height).map_err(Into::into)
141142
}
142143

143144
fn path<P: AsRef<Path>>(path: P, size: Option<(u32, u32)>) -> Result<Self>
@@ -154,12 +155,12 @@ impl DioxusIconTrait for Icon {
154155
}
155156

156157
/// Provides the default icon of the app
157-
pub fn default_icon<T: DioxusIconTrait>() -> T {
158+
pub fn default_icon<T: DioxusIconTrait>() -> Result<T> {
158159
T::get_icon()
159160
}
160161

161162
/// Helper function to load image from include_bytes!("image.png")
162-
pub fn icon_from_memory<T: DioxusIconTrait>(value: &[u8]) -> T {
163+
pub fn icon_from_memory<T: DioxusIconTrait>(value: &[u8]) -> Result<T> {
163164
T::from_memory(value)
164165
}
165166

packages/desktop/src/trayicon.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,21 @@ pub type DioxusTray = ();
2828
pub fn init_tray_icon(menu: DioxusTrayMenu, icon: Option<DioxusTrayIcon>) -> DioxusTray {
2929
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
3030
{
31-
let builder = tray_icon::TrayIconBuilder::new()
31+
let icon = icon.map(Ok).unwrap_or_else(|| crate::default_icon());
32+
33+
let tray = tray_icon::TrayIconBuilder::new()
3234
.with_menu(Box::new(menu))
33-
.with_menu_on_left_click(false)
34-
.with_icon(match icon {
35-
Some(value) => value,
36-
None => crate::default_icon(),
37-
});
35+
.with_menu_on_left_click(false);
36+
37+
let tray = match icon {
38+
Ok(icon) => tray.with_icon(icon),
39+
Err(err) => {
40+
tracing::trace!("No tray icon: {:?}", err);
41+
tray
42+
}
43+
};
3844

39-
provide_context(builder.build().expect("tray icon builder failed"))
45+
provide_context(tray.build().expect("tray icon builder failed"))
4046
}
4147
}
4248

packages/desktop/src/webview.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl WebviewInstance {
234234

235235
// We assume that if the icon is None in cfg, then the user just didnt set it
236236
if cfg.window.window.window_icon.is_none() {
237-
window = window.with_window_icon(Some(crate::default_icon()));
237+
window = window.with_window_icon(crate::default_icon().ok());
238238
}
239239

240240
let window = Arc::new(window.build(&shared.target).unwrap());

0 commit comments

Comments
 (0)