How to Handle Window Management (Minimize, Maximize, Close) in Tauri
Tauri provides a built-in window plugin that exposes native window operations through JavaScript commands and Rust APIs, delegating platform-specific handling to OS-native dispatchers.
The tauri-apps/tauri repository implements window management through a plugin architecture that bridges frontend JavaScript with native OS window managers. This system allows developers to control window states programmatically from both the frontend and backend, handling minimize, maximize, and close operations across desktop platforms.
Core Window Management Architecture
Tauri’s window management follows a four-step command flow that abstracts platform differences while providing consistent APIs.
The Command Dispatch Flow
-
Window Identification – Obtain a
Window<R>handle usingWebviewWindowBuilderduring creation orapp.get_webview_window(label)for existing windows (as seen in [examples/api/src-tauri/src/tray.rs](https://github.com/tauri-apps/tauri/blob/dev/examples/api/src-tauri/src/tray.rs#L73-L77)). -
JavaScript Invocation – The frontend calls
window.tauri.invoke('plugin:window|minimize', { label }), which routes through the window plugin registered in [crates/tauri/src/window/plugin.rs](https://github.com/tauri-apps/tauri/blob/dev/crates/tauri/src/window/plugin.rs#L39-L46). -
Rust Method Execution – The plugin resolves the target window via
get_windowand calls the corresponding method (minimize(),maximize(),close()) on theWindow<R>struct defined in [crates/tauri/src/window/mod.rs](https://github.com/tauri-apps/tauri/blob/dev/crates/tauri/src/window/mod.rs#L1759-L1762). -
Platform Delegation – The dispatcher forwards calls to OS-specific backends (Win32, Cocoa, GTK), returning
Result<()>to JavaScript as a Promise.
Label Resolution Logic
The get_window helper in plugin.rs determines which window receives the command:
fn get_window<R: Runtime>(window: Window<R>, label: Option<String>) -> crate::Result<Window<R>> {
match label {
Some(l) if !l.is_empty() => window.manager().get_window(&l)
.ok_or(crate::Error::WindowNotFound),
_ => Ok(window),
}
}
If the label parameter is omitted or empty, the command targets the window that invoked it.
Implementing Window Controls in Rust
The Rust API provides direct methods on the Window struct for state management. All methods return crate::Result<()> and propagate errors (such as missing windows or unsupported platforms) to the caller.
Minimize and Unminimize
To minimize a window and later restore it:
// Minimize the window to the taskbar/dock
if let Some(win) = app.get_webview_window("main") {
win.minimize()?; // Calls dispatcher.minimize()
// Later: restore from minimized state
win.unminimize()?; // Calls dispatcher.unminimize()
}
Source implementations: minimize and unminimize in crates/tauri/src/window/mod.rs.
Maximize and Unmaximize
Toggle between maximized and windowed states:
if let Some(win) = app.get_webview_window("main") {
// Expand to fill screen
win.maximize()?; // Dispatcher: maximize()
// Restore previous dimensions
win.unmaximize()?; // Dispatcher: unmaximize()
}
Source implementations: maximize and unmaximize.
Closing Windows
Gracefully close a window, which first emits RunEvent::CloseRequested:
if let Some(win) = app.get_webview_window("main") {
win.close()?; // Dispatcher: close()
}
Source: close.
JavaScript API Integration
The frontend accesses these capabilities through Tauri’s command system using the plugin:window namespace.
Basic Window Operations
import { invoke } from '@tauri-apps/api/core';
// Minimize current window
await invoke('plugin:window|minimize');
// Maximize specific window by label
await invoke('plugin:window|maximize', { label: 'main' });
// Close the window
await invoke('plugin:window|close');
These commands map directly to the setter macros defined in [crates/tauri/src/window/plugin.rs](https://github.com/tauri-apps/tauri/blob/dev/crates/tauri/src/window/plugin.rs#L14-L15), which register the JavaScript-accessible endpoints.
Real-World Implementation: Tray Integration
The official API example demonstrates practical window management from system tray interactions. When handling tray clicks, the application restores minimized windows before focusing them:
// Inside tray click handler (examples/api/src-tauri/src/tray.rs)
if let Some(window) = app.get_webview_window("main") {
let _ = window.unminimize(); // Restore if minimized
let _ = window.show(); // Ensure visibility
let _ = window.set_focus(); // Bring to foreground
}
This pattern ensures the window is interactable regardless of its previous state.
Summary
- Window Plugin Architecture – Tauri exposes native operations via
plugin:windowcommands defined incrates/tauri/src/window/plugin.rs, bridging JavaScript and Rust. - Label-Based Targeting – Commands accept an optional
labelparameter; omitting it targets the invoking window through theget_windowhelper. - Cross-Platform Dispatch – Methods in
crates/tauri/src/window/mod.rsdelegate to OS-specific backends, returningResult<()>for error handling. - Tray Integration – Real-world usage often combines
unminimize(),show(), andset_focus()to handle window restoration from system tray events.
Frequently Asked Questions
How do I target a specific window instead of the current one?
Pass the window label as a parameter in your JavaScript invocation: await invoke('plugin:window|minimize', { label: 'secondary' }). In Rust, use app.get_webview_window("secondary") to obtain the handle. If the label is omitted or empty, the command acts on the window that invoked it.
Why does my window close immediately without triggering CloseRequested?
Direct calls to window.close() emit RunEvent::CloseRequested before closing, but if your event loop doesn't handle this event or if the window is force-closed at the OS level, the event may not fire. Ensure your Tauri application implements a RunEvent handler in the main loop to intercept close requests.
Are these window methods supported on mobile platforms?
No. The window management plugin is designed for desktop environments (Windows, macOS, Linux). Calls to minimize, maximize, or close windows will return errors or be no-ops on mobile platforms, as the underlying window dispatchers are not implemented for iOS or Android.
What is the difference between unminimize and show?
unminimize() restores a window from the minimized state (taskbar/dock) to its previous windowed state, while show() makes a hidden window visible. In tray applications, you typically call both: unminimize() to restore from the taskbar, then show() to ensure visibility, followed by set_focus() to activate the window.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →