plugins-workspace icon indicating copy to clipboard operation
plugins-workspace copied to clipboard

[single-instance] Callback function does not run if the app is not visible (after calling window.hide())

Open arjanvillon opened this issue 1 year ago • 3 comments

Hi,

I've discovered that the single instance callback function after initialization doesn't work when the app is not visible (I call window.hide() function). What I want to do is to show the app again when another instance is opened by using window.show(). Running window.miniminize() makes the single instance work but I'm hoping to use the hide() function. I also tried the one from the v2 docs about focusing on the new instances and it also does not work. Please help, thank you! 🙏

arjanvillon avatar Aug 01 '24 18:08 arjanvillon

Thank you for the report, which OS did you test this on?

FabianLars avatar Aug 01 '24 19:08 FabianLars

I'm using MacOS

arjanvillon avatar Aug 01 '24 19:08 arjanvillon

I have the same issue. Is there anything progress?

gusxodnjs avatar Sep 05 '24 10:09 gusxodnjs

@FabianLars Is this issue going on? Or do you have any plans?

gusxodnjs avatar Nov 04 '24 07:11 gusxodnjs

i assume you're also on macos? This issue is actually unrelated to the single-instance plugin.

macOS has a built-in single-instance mechanism so for 99% of (macOS) apps this plugin doesn't provide any value. So the title should be more like "Callback function does not run on macOS". See https://github.com/tauri-apps/plugins-workspace/issues/287 for an example use-case for the plugin on macOS and the reason why we added macOS support to the plugin even if almost nobody needs it.

In theory you're looking for a way to handle the OS' applicationShouldHandleReopen event via https://docs.rs/tauri/2.0.0/x86_64-apple-darwin/tauri/enum.RunEvent.html#variant.Reopen but you could also use app.hide() instead of window.hide() for now which will automatically show your window(s) again if needed.

FabianLars avatar Nov 04 '24 08:11 FabianLars

@FabianLars Thanks for the clarification. I was running into the exact same issue. I am building a tray app, which should open up a window, once the app is started again.

Interestingly this worked when running in dev mode, but once I build a release, the callback .plugin(tauri_plugin_single_instance::init(|_app, _args, _cwd| {}) is not called anymore. Do you know, why it behaves differently and how I can accomplish to open up a window, once it is opened again?

don41382 avatar Nov 06 '24 07:11 don41382

As long as your (built) app is running macOS' single instance mechanism takes over, whether or not you have windows open. This means you'll also have to handle the Reopen event linked in my last message and the single-instance plugin will not trigger.

FabianLars avatar Nov 06 '24 10:11 FabianLars

@FabianLars Thanks - that makes sense. I am going to use tauri_plugin_single_instance::init for windows and .run(|app, event| match event { RunEvent::Reopen { .. } => for MacOS.

The only confusing part to me was, that the Reopen is not called in dev mode. Do you know why?

don41382 avatar Nov 06 '24 14:11 don41382

deep links and the single instance mechanism on macos only really work in actual app bundles, so they only fully work if you run the myapp.app (which tauri dev does not do)

FabianLars avatar Nov 06 '24 14:11 FabianLars