[READ-ONLY] Mirror of https://github.com/probablykasper/remind-me-again. Toggleable cron reminders app for Mac, Linux and Windows
linux macos notifications reminder tauri windows
0

Configure Feed

Select the types of activity you want to include in your feed.

Refactor show/hide logic

Kasper (May 7, 2023, 2:13 AM +0200) a26a6943 3131c992

+52 -42
+52 -42
src-tauri/src/main.rs
··· 142 142 .system_tray(SystemTray::new()) 143 143 .on_system_tray_event(|app, event| match event { 144 144 SystemTrayEvent::LeftClick { .. } => { 145 - let window = match app.get_window("main") { 146 - Some(window) => window, 147 - None => create_window(&app.app_handle()), 148 - }; 149 - match window.is_visible().expect("Window visible?") { 150 - true => { 151 - // hide the window instead of closing due to processes not closing memory leak: https://github.com/tauri-apps/wry/issues/590 152 - window.hide().expect("Hide window"); 153 - // window.close().expect("winclose"); 154 - set_is_accessory_policy(true); 155 - } 156 - false => { 157 - set_is_accessory_policy(false); 158 - std::thread::sleep(std::time::Duration::from_millis(5)); 159 - #[cfg(not(target_os = "macos"))] 160 - { 161 - window.show().expect("Show window"); 162 - } 163 - window.unminimize().expect("Unminimize window"); 164 - window.set_focus().expect("Focus window"); 165 - } 145 + let is_visible = app 146 + .get_window("main") 147 + .map(|w| w.is_visible().unwrap()) 148 + .unwrap_or(false); 149 + if is_visible { 150 + hide(&app.app_handle()); 151 + } else { 152 + show(&app.app_handle()); 166 153 } 167 154 } 168 155 _ => {} ··· 182 169 tauri::RunEvent::WindowEvent { event, .. } => match event { 183 170 tauri::WindowEvent::CloseRequested { api, .. } => { 184 171 api.prevent_close(); 185 - let window = app_handle.get_window("main").expect("getwin"); 186 - // hide the window instead of closing due to processes not closing memory leak: https://github.com/tauri-apps/wry/issues/590 187 - window.hide().expect("winhide"); 188 - // window.close().expect("winclose"); 189 - set_is_accessory_policy(true); 172 + hide(app_handle); 190 173 } 191 174 _ => {} 192 175 }, ··· 194 177 }); 195 178 } 196 179 180 + // Considerations for hide/show handling: 181 + // - window.lose() causes memory leak: https://github.com/tauri-apps/wry/issues/590 182 + // - Due to the accessory activation policy being enabled when the window hides, the underlying app's windows all get focusd. But the accessory policy is needed to control the dock. 183 + 184 + fn show(app: &AppHandle) { 185 + let window = match app.get_window("main") { 186 + Some(window) => window, 187 + None => create_window(&app.app_handle()), 188 + }; 189 + #[cfg(target_os = "macos")] 190 + app.show().unwrap(); 191 + window.show().unwrap(); 192 + window.unminimize().unwrap(); 193 + window.set_focus().unwrap(); 194 + #[cfg(target_os = "macos")] 195 + { 196 + set_is_accessory_policy(false); 197 + } 198 + } 199 + fn hide(app: &AppHandle) { 200 + let window = app.get_window("main").unwrap(); 201 + window.unminimize().unwrap(); 202 + window.hide().unwrap(); 203 + #[cfg(target_os = "macos")] 204 + { 205 + app.hide().unwrap(); 206 + set_is_accessory_policy(true); 207 + } 208 + } 209 + 197 210 fn create_window(app: &AppHandle) -> Window { 198 211 let win = WindowBuilder::new(app, "main", WindowUrl::default()) 199 212 .title("Remind Me Again") ··· 231 244 win 232 245 } 233 246 234 - #[allow(unused_variables)] 247 + #[cfg(target_os = "macos")] 235 248 fn set_is_accessory_policy(accessory: bool) { 236 - #[cfg(target_os = "macos")] 237 - { 238 - use cocoa::appkit::NSApplication; 239 - use cocoa::appkit::NSApplicationActivationPolicy::{ 240 - NSApplicationActivationPolicyAccessory, NSApplicationActivationPolicyRegular, 241 - }; 242 - use objc::*; 249 + use cocoa::appkit::NSApplication; 250 + use cocoa::appkit::NSApplicationActivationPolicy::{ 251 + NSApplicationActivationPolicyAccessory, NSApplicationActivationPolicyRegular, 252 + }; 253 + use objc::*; 243 254 244 - let cls = objc::runtime::Class::get("NSApplication").unwrap(); 245 - let app: cocoa::base::id = unsafe { msg_send![cls, sharedApplication] }; 246 - unsafe { 247 - if accessory { 248 - app.setActivationPolicy_(NSApplicationActivationPolicyAccessory); 249 - } else { 250 - app.setActivationPolicy_(NSApplicationActivationPolicyRegular); 251 - } 255 + let cls = objc::runtime::Class::get("NSApplication").unwrap(); 256 + let app: cocoa::base::id = unsafe { msg_send![cls, sharedApplication] }; 257 + unsafe { 258 + if accessory { 259 + app.setActivationPolicy_(NSApplicationActivationPolicyAccessory); 260 + } else { 261 + app.setActivationPolicy_(NSApplicationActivationPolicyRegular); 252 262 } 253 263 } 254 264 }