[v2] How to open a file in an existing window instead of creating a new window? #10997
-
I am currently working on a text editor app that closes to tray when exiting, meaning the app is still running in the background with the window hidden and when I open a new text file, I want it to be opened in the existing, hidden window, but it instead creates a new window with a second tray icon, both being created with the label 'main'. I already set up the general logic to achieve this, where I check for an existing webview-window at the start of setup() and if none could be found, it creates a new window. Here's a simplified version of my current approach: fn create_window(app: tauri::AppHandle) {
tauri::WebviewWindowBuilder::new(&app, "main", tauri::WebviewUrl::default())
.build()
.unwrap();
}
pub fn run() {
let app = tauri::Builder::default()
.setup(|app| {
let handle = app.handle();
if let Some(window) = handle.get_webview_window("main") {
if window.is_visible().unwrap() {
window.set_focus().unwrap();
} else {
window.show().unwrap();
window.set_focus().unwrap();
}
} else {
create_window(handle.clone());
//...tray initilization logic...
}
Ok(())
})
.build(tauri::generate_context!())
.expect("error while running tauri application");
app.run(|_app_handle, _event| {});
} My current problem is that So my main issue is actually retrieving all open windows. I also don't want enforce a single-instance, because I still want the user to be able to open multiple text files in separate windows for side-by-side comparison, but when the user closes one window to the tray and opens another file, that file should be opened in the hidden tray window. I chose this closing-to-tray behavior because without it, opening a new instance of the app whenever opening a file has a noticeable delay with a short flash in the beginning, even though my app is quite minimal. That seems to be a general problem with tauri apps (maybe because of the webview being created and maybe just/most noticeable on windows). Is there something I'm missing, like a simpler approach, or is my desired behavior not possible? result of [-] Packages |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
So i assume with opening a text file you're talking about file association (double click on the text file automatically opens it in your app)? If so then you do need something like the single-instance plugin because Windows will always spawn a new app instance to open the file, this is just how file assocations work so really no way around it. Also, just to be sure: app instances != window instances. The single-instance plugin prevents multiple app instances (and therefore multiple tray icons too) but does not care about the amount of windows belonging to that one main instance. |
Beta Was this translation helpful? Give feedback.
So i assume with opening a text file you're talking about file association (double click on the text file automatically opens it in your app)? If so then you do need something like the single-instance plugin because Windows will always spawn a new app instance to open the file, this is just how file assocations work so really no way around it.
Also, just to be sure: app instances != window instances. The single-instance plugin prevents multiple app instances (and therefore multiple tray icons too) but does not care about the amount of windows belonging to that one main instance.