@@ -64,57 +64,45 @@ impl NotifyIconInternal {
64
64
parent : Weak < bindings:: NotifyIcon > ,
65
65
hookenabled : bool ,
66
66
) -> Result < ( ) > {
67
- let internal_id = GLOBAL_COUNTER . next ( ) ;
68
- let h_wnd = MessageWindow :: new ( w ! ( "LibSimbolMudah.NotifyIcon" ) , Some ( notify_proc) ) ?;
69
- let h_menu = NotifyIconMenu :: new ( hookenabled) ?;
70
-
71
- let res = Self {
72
- h_wnd,
73
- internal_id,
74
- h_icon : unsafe { LoadIconW ( None , IDI_WARNING ) } ?,
75
- h_menu : Some ( h_menu) ,
76
- report_open_settings : DelegateStorage :: new ( ) ,
77
- report_exit_app : DelegateStorage :: new ( ) ,
78
- report_set_listening : DelegateStorage :: new ( ) ,
79
- on_state_changed_token : EventRegistrationToken :: default ( ) ,
80
- parent,
81
- } ;
82
-
83
- res. register_notify_icon ( hookenabled) ?;
84
-
67
+ let res = Self :: new ( hookenabled, parent) ?;
85
68
INTERNAL_NOTIFYICON . set ( Some ( res) ) ;
86
69
Ok ( ( ) )
87
70
}
88
71
89
- fn register_notify_icon ( & self , listening : bool ) -> Result < ( ) > {
72
+ fn new ( listening : bool , parent : Weak < bindings:: NotifyIcon > ) -> Result < Self > {
73
+ let internal_id = GLOBAL_COUNTER . next ( ) ;
74
+ let h_wnd = MessageWindow :: new ( w ! ( "LibSimbolMudah.NotifyIcon" ) , Some ( notify_proc) ) ?;
75
+ let h_menu = NotifyIconMenu :: new ( listening) ?;
76
+ let h_icon = unsafe { LoadIconW ( None , IDI_WARNING ) } ?;
77
+
90
78
let nid = NOTIFYICONDATAW {
91
79
cbSize : std:: mem:: size_of :: < NOTIFYICONDATAW > ( ) as u32 ,
92
- hWnd : self . h_wnd . handle ( ) ,
93
- uID : self . internal_id ,
80
+ hWnd : h_wnd. handle ( ) ,
81
+ uID : internal_id,
94
82
uFlags : NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP ,
95
83
uCallbackMessage : WM_USER_TRAYICON ,
96
- hIcon : self . h_icon ,
84
+ hIcon : h_icon,
97
85
szTip : get_tooltip ( listening) ,
98
86
Anonymous : NOTIFYICONDATAW_0 {
99
87
uVersion : NOTIFYICON_VERSION_4 ,
100
88
} ,
101
89
..Default :: default ( )
102
90
} ;
103
91
104
- if unsafe { Shell_NotifyIconW ( NIM_ADD , & nid) } . into ( ) {
105
- if unsafe { Shell_NotifyIconW ( NIM_SETVERSION , & nid) } . into ( ) {
106
- Ok ( ( ) )
107
- } else {
108
- let _ = unsafe { Shell_NotifyIconW ( NIM_DELETE , & nid ) } ;
109
- Err ( fail_message (
110
- "Failed to set notify icon version, deleting icon (Shell_NotifyIconW)" ,
111
- ) )
112
- }
113
- } else {
114
- Err ( fail_message (
115
- "Failed to register notify icon (Shell_NotifyIconW)" ,
116
- ) )
117
- }
92
+ unsafe { Shell_NotifyIconW ( NIM_ADD , & nid) } . ok ( ) ? ;
93
+ unsafe { Shell_NotifyIconW ( NIM_SETVERSION , & nid) } . ok ( ) ? ;
94
+
95
+ Ok ( Self {
96
+ h_wnd ,
97
+ internal_id ,
98
+ h_icon ,
99
+ h_menu : Some ( h_menu ) ,
100
+ report_open_settings : DelegateStorage :: new ( ) ,
101
+ report_exit_app : DelegateStorage :: new ( ) ,
102
+ report_set_listening : DelegateStorage :: new ( ) ,
103
+ on_state_changed_token : EventRegistrationToken :: default ( ) ,
104
+ parent ,
105
+ } )
118
106
}
119
107
120
108
fn update_notify_icon ( & self , listening : bool ) -> Result < ( ) > {
@@ -150,13 +138,7 @@ impl NotifyIconInternal {
150
138
..Default :: default ( )
151
139
} ;
152
140
153
- if unsafe { Shell_NotifyIconW ( NIM_DELETE , & nid) } . into ( ) {
154
- Ok ( ( ) )
155
- } else {
156
- Err ( fail_message (
157
- "Failed to remove notify icon (Shell_NotifyIconW)" ,
158
- ) )
159
- }
141
+ unsafe { Shell_NotifyIconW ( NIM_DELETE , & nid) } . ok ( )
160
142
}
161
143
162
144
pub ( super ) fn update_listening_check ( & mut self , listening : bool ) -> Result < ( ) > {
0 commit comments