Skip to content

winit

stable

Cross-platform window creation and event loop plugin built on winit 0.30. Provides a Window class for native OS windows and an EventLoop that delivers input, resize, focus, and lifecycle events via a callback.

use plugin winit::{Window.new, EventLoop.new, run, …}
25 functions Utilities
/ filter jk navigate Esc clear
Functions (25)
  1. Window.new Create a native OS window
  2. EventLoop.new Create an event loop
  3. run Start the event loop with an event callback
  4. title Get the window title
  5. set_title Set the window title
  6. size Get the inner window size
  7. set_size Resize the window
  8. id Get the window handle ID
  9. request_redraw Request a redraw event
  10. set_visible Show or hide the window
  11. raw_handle Get the raw window handle
  12. close Close the window
  13. hwnd Get the Win32 HWND (Windows only)
  14. native_handle Get a cross-platform native handle table
  15. set_cursor_grab Confine or release the cursor
  16. set_cursor_visible Show or hide the cursor
  17. position Get the window outer position
  18. set_position Move the window
  19. set_fullscreen Enter or exit fullscreen
  20. is_fullscreen Check fullscreen state
  21. is_focused Check if the window has focus
  22. set_min_size Set the minimum window size
  23. set_max_size Set the maximum window size
  24. set_decorations Show or hide window decorations
  25. scale_factor Get the DPI scale factor

Overview

winit wraps winit 0.30's ApplicationHandler API to give Zolo native, cross-platform OS windows. There are two handle-based classes: a Window (created with Window.new, but only realised once the loop starts) and an EventLoop (created with EventLoop.new, of which there may be exactly one per process). Everything is event-driven — you describe the window you want, then hand a single callback to EventLoop.run, and the loop drives your program by invoking that callback for every input, resize, focus, redraw, and lifecycle event until the last window closes.

Each event is delivered as a table with a type discriminator ("window", "app", or "device") and an event name string; window events also carry a window_id that matches Window.id. This plugin only manages the window and its event stream — it does no drawing itself. Use native_handle (cross-platform) or hwnd (Windows-only) to hand the surface to a renderer such as wgpu or a webview.

Common patterns

Open a window and react to close and keyboard events:

use plugin winit::{Window, EventLoop}

let win = Window.new(#{"title": "Hello", "width": 1024, "height": 768})
let el = EventLoop.new()

el.run(fn(event) {
  if event["event"] == "key_pressed" {
    print("pressed {event["key"]}")
  }
  if event["event"] == "close_requested" {
    print("goodbye")
  }
})

Wait for the OS window to be realised, then create a GPU surface from its native handle:

use plugin winit::{Window, EventLoop}
use plugin wgpu::{wgpu}

let win = Window.new(#{"title": "Renderer", "width": 800, "height": 600})
let el = EventLoop.new()

el.run(fn(event) {
  if event["event"] == "window_created" {
    let surface = wgpu.create_surface(win.native_handle())
    print("surface ready at scale {win.scale_factor()}")
  }
  if event["event"] == "redraw_requested" {
    // draw a frame, then ask for the next one
    win.request_redraw()
  }
})

Grab the cursor and hide it for FPS-style mouse-look, releasing it when focus is lost:

use plugin winit::{Window, EventLoop}

let win = Window.new(#{"title": "Game", "width": 1280, "height": 720})
let el = EventLoop.new()

el.run(fn(event) {
  if event["event"] == "window_created" {
    win.set_cursor_grab(true)
    win.set_cursor_visible(false)
  }
  if event["event"] == "mouse_moved" {
    print("look {event["delta_x"]}, {event["delta_y"]}")
  }
  if event["event"] == "focused" and event["focused"] == false {
    win.set_cursor_grab(false)
    win.set_cursor_visible(true)
  }
})

Create a native OS window

Creates a native OS window with the given configuration. The window is queued and realised when EventLoop.run starts. Config keys: title (string), width (int), height (int), transparent (bool), decorations (bool), visible (bool), x/y (int position).

use plugin winit::{Window, EventLoop}

let win = Window.new(#{"title": "My App", "width": 1024, "height": 768})
let el = EventLoop.new()

el.run(fn(event) {
  if event["event"] == "close_requested" {
    print("closing")
  }
})

Create an event loop

Creates a winit event loop. Only one event loop may exist per process.

Start the event loop with an event callback

Starts the event loop, calling callback with each event table until the last window is closed or the loop exits. The callback receives a table with at minimum a type and event field.

Event types: "app" (suspended, exiting, memory_warning), "window" (see below), "device" (raw input).

Window events include: window_created, close_requested, resized (width, height), moved (x, y), focused (focused: bool), redraw_requested, key_pressed/key_released (key, modifiers), mouse_moved (x, y, delta_x, delta_y), mouse_pressed/mouse_released (button), mouse_wheel (delta_x, delta_y), scale_factor_changed, theme_changed, file_dropped, and more.

use plugin winit::{Window, EventLoop}
use plugin wgpu::{wgpu}

let win = Window.new(#{"title": "Renderer", "width": 800, "height": 600})
let el = EventLoop.new()

el.run(fn(event) {
  if event["event"] == "window_created" {
    let surface = wgpu.create_surface(win.native_handle())
    print("window ready, surface created")
  }
  if event["event"] == "redraw_requested" {
    // render frame here
  }
  if event["event"] == "key_pressed" {
    print("key: {event["key"]}")
  }
})

Get the window title

Returns the current window title.

Set the window title

Updates the window title bar text.

Get the inner window size

Returns the current inner (client area) size of the window as a table with width and height integer fields.

use plugin winit::{Window, EventLoop}

let win = Window.new(#{"title": "Resize", "width": 800, "height": 600})
let el = EventLoop.new()

el.run(fn(event) {
  if event["event"] == "resized" {
    let s = win.size()
    print("now {s["width"]}x{s["height"]} (event said {event["width"]}x{event["height"]})")
  }
})

Resize the window

Requests a resize of the window's inner area.

Get the window handle ID

Returns the numeric handle ID for this window, which matches the window_id field in event tables.

Request a redraw event

Posts a redraw_requested event for this window. Call from within the event callback to schedule a render.

Show or hide the window

Shows (true) or hides (false) the window.

Get the raw window handle

Returns the internal window handle.

Close the window

Requests the window to close, triggering a close_requested event.

Get the Win32 HWND (Windows only)

Returns the Win32 HWND as an integer. Windows-only.

Get a cross-platform native handle table

Returns a cross-platform native handle table suitable for passing to wgpu.create_surface() or WebView.new(). The table contains a kind discriminator and platform-specific fields.

use plugin winit::{Window, EventLoop}
use plugin wgpu::{wgpu}

let win = Window.new(#{"title": "Demo", "width": 800, "height": 600})
let el = EventLoop.new()
el.run(fn(event) {
  if event["event"] == "window_created" {
    let nh = win.native_handle()
    let surface = wgpu.create_surface(nh)
  }
})

Confine or release the cursor

When true, confines the cursor to the window using CursorGrabMode::Confined (falls back to Locked). When false, releases the cursor.

Show or hide the cursor

Shows or hides the cursor while it is over this window.

Get the window outer position

Returns the window's outer position on screen.

Move the window

Moves the window to the given screen coordinates.

Enter or exit fullscreen

Enters (true) or exits (false) borderless fullscreen mode on the window's current monitor.

use plugin winit::{Window, EventLoop}

let win = Window.new(#{"title": "Toggle", "width": 800, "height": 600})
let el = EventLoop.new()

el.run(fn(event) {
  if event["event"] == "key_pressed" and event["key"] == "F11" {
    win.set_fullscreen(!win.is_fullscreen())
  }
})

Check fullscreen state

Returns true if the window is currently in fullscreen mode.

Check if the window has focus

Returns true if the window currently has keyboard focus.

Set the minimum window size

Sets the minimum allowed inner size for user resizing.

Set the maximum window size

Sets the maximum allowed inner size for user resizing.

Show or hide window decorations

Shows (true) or hides (false) the window title bar and border.

Get the DPI scale factor

Returns the DPI scale factor for the monitor the window is on. Multiply logical sizes by this to get physical pixels.

enespt-br