Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 39 ยท Windows and Menus

Tauri Windows: Create, Configure and Control

Create and control Tauri windows: define them in tauri.conf.json, build them in Rust, use the JavaScript window API to resize, move, maximize, close and listen to window events.

A Tauri app can show one window or many. Each window hosts a webview, and each is identified by a label, a unique string you use to address it from Rust, from JavaScript and in permission files. This lesson covers how to create a window and how to control it.

Native windows need the real runtime. Rust and TypeScript snippets are read-only here, with JavaScript stand-ins for the logic.

Windows in the configuration

The simplest way: list them in tauri.conf.json. Tauri creates them at startup.

JSON
{
  "app": {
    "windows": [
      {
        "label": "main",
        "title": "Notes",
        "width": 960,
        "height": 640,
        "minWidth": 480,
        "minHeight": 320,
        "resizable": true,
        "center": true,
        "url": "index.html"
      }
    ]
  }
}

Useful options:

Option Meaning
label Unique id (default main)
title Title bar text
width, height, minWidth, minHeight, maxWidth, maxHeight Sizes in logical pixels
x, y, center Position
resizable, maximizable, minimizable, closable What the user may do
fullscreen, maximized Initial state
decorations false removes the native title bar and border
alwaysOnTop Stay above other windows
visible Start hidden (show later, for example after the first paint)
transparent Transparent background (needs CSS support too)
url Page to load, relative to the frontend
create false means: do not create at startup; build it from code

A very common trick is "visible": false plus showing the window from the frontend after your app has rendered, so users never see a white flash.

Building windows in Rust

Use WebviewWindowBuilder:

Rust
use tauri::{Manager, WebviewUrl, WebviewWindowBuilder};

pub fn run() {
    tauri::Builder::default()
        .setup(|app| {
            WebviewWindowBuilder::new(app, "about", WebviewUrl::App("about.html".into()))
                .title("About")
                .inner_size(420.0, 320.0)
                .resizable(false)
                .center()
                .build()?;
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error");
}

WebviewUrl::App(path) loads one of your own frontend pages, WebviewUrl::External(url) loads a remote site (think carefully about security before you do), and the label must be unique or build() returns an error.

Finding and controlling a window

Rust
use tauri::Manager;

#[tauri::command]
fn focus_settings(app: tauri::AppHandle) -> Result<(), String> {
    let window = app.get_webview_window("settings").ok_or("settings window not found")?;
    window.show().map_err(|e| e.to_string())?;
    window.set_focus().map_err(|e| e.to_string())
}

A window offers many methods: set_title, set_size, set_position, center, maximize, unmaximize, minimize, set_fullscreen, set_always_on_top, hide, show, close, destroy, is_visible, is_maximized, inner_size, outer_position, scale_factor and more. close() runs close handlers (which can cancel); destroy() removes the window immediately.

The JavaScript window API

Get the window that your code runs in with getCurrentWindow:

TypeScript
import { getCurrentWindow, LogicalSize } from "@tauri-apps/api/window";

const win = getCurrentWindow();
await win.setTitle("Notes (unsaved)");
await win.setSize(new LogicalSize(800, 600));
await win.center();
await win.toggleMaximize();
console.log(await win.isMaximized());

Each of these calls needs a permission. Capabilities decide that: for example core:window:allow-set-title, core:window:allow-center. Without the matching permission the call rejects, which is the usual reason a perfectly good window call "does nothing".

Window events

React to what the user does to the window:

TypeScript
const unlisten = await win.onCloseRequested(async (event) => {
  if (hasUnsavedWork()) {
    event.preventDefault();          // stay open
    const ok = await askUserToDiscard();
    if (ok) await win.destroy();
  }
});

await win.onResized(({ payload: size }) => console.log("resized", size.width, size.height));
await win.onFocusChanged(({ payload: focused }) => console.log("focused:", focused));
await win.onMoved(({ payload: pos }) => console.log("moved", pos.x, pos.y));

In Rust use window.on_window_event(|event| ...) with WindowEvent::CloseRequested { api, .. } and call api.prevent_close() to cancel.

Logical and physical pixels

Screens differ in density. Logical pixels are scaled by the display scale factor; physical pixels are real device pixels. Config options use logical sizes. When you read sizes from events, check whether you got PhysicalSize or LogicalSize and convert with size.toLogical(scaleFactor).

Try it: a window manager model (stand-in)

This stand-in has labels, unique-label checks, size limits, and a close handler that can cancel, the same behaviors you configure in Tauri.

Node.js Stand-in: labels, limits and close requests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Try it: logical versus physical pixels (stand-in)

Node.js Stand-in: scale factor conversion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Continue with Multiple windows and webviews.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What identifies a Tauri window in code and in permission files?

    Labels must be unique and are used to find windows and to scope capabilities.
  2. 2. How can you stop a window from closing while there is unsaved work?

    onCloseRequested lets you cancel the close; destroy() would bypass that.
  3. 3. Why might a JavaScript window call such as setTitle reject?

    Window API calls are gated by permissions like core:window:allow-set-title.
  4. 4. What does 'visible: false' plus a later show() help avoid?

    You reveal the window once the frontend has painted.

Summary

  • Windows have unique labels and can be defined in config or built with WebviewWindowBuilder.
  • Control windows from Rust or with getCurrentWindow() in JavaScript, subject to permissions.
  • Handle close requests to protect unsaved work.
  • Config sizes are logical pixels; convert with the scale factor when needed.
  • Start hidden and show after first paint to avoid flashes.

Next: Multiple windows and webviews