@@ -6,10 +6,10 @@ use std::path::Path;
66
77/// Trait that creates icons for various types
88pub 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" ) ) ]
2525use 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
3434fn 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" ) ) ]
4242impl 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" ) ) ) ]
8182impl 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;
119120use tao:: platform:: windows:: IconExtWindows ;
120121
121122impl 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
0 commit comments