Skip to main content

Window

The window module provides comprehensive functions for managing application windows, including opening, closing, resizing, and styling windows.

Creating Windows

pub fn open(settings: Settings) -> (Id, Task<Id>)
Opens a new window with the given settings, producing the window Id on completion.
pub fn close<T>(id: Id) -> Task<T>
Closes the window with the given Id.

Window Queries

Getting Window IDs

pub fn oldest() -> Task<Option<Id>>
Gets the window Id of the oldest window.
pub fn latest() -> Task<Option<Id>>
Gets the window Id of the most recently created window.
pub fn raw_id(id: Id) -> Task<u64>
Gets a unique identifier provided by the underlying windowing system.

Size and Position

pub fn size(id: Id) -> Task<Size>
Gets the window size in logical dimensions.
pub fn position(id: Id) -> Task<Option<Point>>
Gets the position in logical coordinates.
pub fn scale_factor(id: Id) -> Task<f32>
Gets the scale factor (DPI) of the window.
pub fn monitor_size(id: Id) -> Task<Option<Size>>
Gets the logical dimensions of the monitor containing the window.

Window State

pub fn is_maximized(id: Id) -> Task<bool>
Gets the maximized state of the window.
pub fn is_minimized(id: Id) -> Task<Option<bool>>
Gets the minimized state of the window. Platform-specific: Always returns None on Wayland.
pub fn mode(id: Id) -> Task<Mode>
Gets the current Mode of the window (windowed, fullscreen, etc.).

Window Manipulation

Positioning

pub fn move_to<T>(id: Id, position: Point) -> Task<T>
Moves the window to the given logical coordinates. Platform-specific: Unsupported on Wayland.
pub fn drag<T>(id: Id) -> Task<T>
Begins dragging the window while the left mouse button is held.
There’s no guarantee this will work unless the left mouse button was pressed immediately before calling this function.
pub fn drag_resize<T>(id: Id, direction: Direction) -> Task<T>
Begins resizing the window in the given direction while the left mouse button is held.

Sizing

pub fn resize<T>(id: Id, new_size: Size) -> Task<T>
Resizes the window to the given logical dimensions.
pub fn set_min_size<T>(id: Id, size: Option<Size>) -> Task<T>
Sets the minimum inner window size.
pub fn set_max_size<T>(id: Id, size: Option<Size>) -> Task<T>
Sets the maximum inner window size.
pub fn set_resizable<T>(id: Id, resizable: bool) -> Task<T>
Sets whether the window can be resized.
pub fn set_resize_increments<T>(id: Id, increments: Option<Size>) -> Task<T>
Sets the window size increment (useful for terminal emulators that need “blocky” resizing).

Window State Control

pub fn maximize<T>(id: Id, maximized: bool) -> Task<T>
Maximizes or restores the window.
pub fn minimize<T>(id: Id, minimized: bool) -> Task<T>
Minimizes or restores the window.
pub fn toggle_maximize<T>(id: Id) -> Task<T>
Toggles the window between maximized and normal state.
pub fn set_mode<T>(id: Id, mode: Mode) -> Task<T>
Changes the window mode (windowed, fullscreen, etc.).

Appearance

pub fn toggle_decorations<T>(id: Id) -> Task<T>
Toggles window decorations (title bar, borders). Platform-specific: Not implemented on X11, unsupported on Web.
pub fn set_icon<T>(id: Id, icon: Icon) -> Task<T>
Changes the window icon. Platform-specific: Unsupported on Web, Wayland, and macOS. On Windows, this sets the small icon (16×16 or multiples accounting for screen scaling).
pub fn set_level<T>(id: Id, level: Level) -> Task<T>
Changes the window level (normal, always on top, always on bottom).

Focus and Attention

pub fn gain_focus<T>(id: Id) -> Task<T>
Brings the window to the front and sets input focus.
This steals input focus from other applications. Only use when certain that’s what the user wants, as focus stealing causes an extremely disruptive user experience.
Platform-specific: Unsupported on Web and Wayland.
pub fn request_user_attention<T>(
    id: Id,
    user_attention: Option<UserAttention>,
) -> Task<T>
Requests user attention to the window. Has no effect if the application is already focused. Platform-specific: Unsupported on iOS, Android, and Web. On macOS, None has no effect. On X11 and Wayland, requests must be manually cleared.

Advanced

pub fn show_system_menu<T>(id: Id) -> Task<T>
Shows the system menu at cursor position. Platform-specific: Only supported on Windows.
pub fn screenshot(id: Id) -> Task<Screenshot>
Captures a screenshot from the window viewport.
pub fn enable_mouse_passthrough<T>(id: Id) -> Task<T>
Disables mouse events for the window, passing them through to whatever is underneath.
pub fn disable_mouse_passthrough<T>(id: Id) -> Task<T>
Re-enables mouse events for the window.
pub fn run<T>(id: Id, f: impl FnOnce(&dyn Window) -> T + Send + 'static) -> Task<T>
where
    T: Send + 'static,
Runs a callback with a reference to the window handle.
If the window closes before this call is processed, the callback will not be run.
pub fn allow_automatic_tabbing<T>(enabled: bool) -> Task<T>
Sets whether the system can automatically organize windows into tabs. Platform-specific: macOS only.

Subscriptions

Frame Updates

pub fn frames() -> Subscription<Instant>
Subscribes to the frame events of the first application window. Useful for smooth animations at the refresh rate.

Window Events

pub fn events() -> Subscription<(Id, Event)>
Subscribes to all window events.
pub fn open_events() -> Subscription<Id>
Subscribes to Event::Opened occurrences.
pub fn close_events() -> Subscription<Id>
Subscribes to Event::Closed occurrences.
pub fn resize_events() -> Subscription<(Id, Size)>
Subscribes to Event::Resized occurrences.
pub fn close_requests() -> Subscription<Id>
Subscribes to Event::CloseRequested occurrences (when user clicks the close button).

Examples

Opening a new window

use iced::window::{self, Settings};

enum Message {
    OpenWindow,
    WindowOpened(window::Id),
}

fn update(state: &mut State, message: Message) -> Task<Message> {
    match message {
        Message::OpenWindow => {
            let (id, task) = window::open(Settings {
                size: Size::new(800.0, 600.0),
                position: Position::Centered,
                ..Default::default()
            });
            state.new_window_id = id;
            task.map(Message::WindowOpened)
        }
        Message::WindowOpened(id) => {
            println!("Window opened: {:?}", id);
            Task::none()
        }
    }
}

Resizing a window

use iced::window;

fn update(state: &mut State, message: Message) -> Task<Message> {
    match message {
        Message::ResizeWindow => {
            window::resize(state.window_id, Size::new(1024.0, 768.0))
        }
        _ => Task::none(),
    }
}

Subscribing to window resize events

use iced::{Subscription, window};

enum Message {
    WindowResized { id: window::Id, size: Size },
}

fn subscription(state: &State) -> Subscription<Message> {
    window::resize_events()
        .map(|(id, size)| Message::WindowResized { id, size })
}

Taking a screenshot

use iced::window;

enum Message {
    TakeScreenshot,
    ScreenshotTaken(window::Screenshot),
}

fn update(state: &mut State, message: Message) -> Task<Message> {
    match message {
        Message::TakeScreenshot => {
            window::screenshot(state.window_id)
                .map(Message::ScreenshotTaken)
        }
        Message::ScreenshotTaken(screenshot) => {
            // Save screenshot
            Task::none()
        }
    }
}

See Also

Build docs developers (and LLMs) love