Skip to content
Kavachकवच

Standard Library: std.window

● Intermediate5 min

Every type, method and function of std.window: what the spec lists, then each declaration in its source, with its examples and the line that declares it.

Spec 14.22 · learn it in Windows.

A declaration below shows what a program writes against: a type's fields, a function's parameters, what it gives, its contracts (require, ensure) and its examples. kavach test runs every example, so each one is true of the code.

What the spec lists

Function or methodGives
window.open(title, width, height) -> AppWindow ! WindowErrorA new window of width × height pixels, shown at once
win.events() -> [Event]The events that came, waiting for at least one
win.poll() -> [Event]The events that came, without waiting; empty when none did
win.run(f)Calls f(event) for each event as it comes, until f gives false or the window closes
win.listen_input()From now on, keyboard, mouse and text events come too. Needs Input
win.size() -> (width: Int, height: Int)The inside of the window, in pixels
win.scale() -> FloatHow many pixels make one point on this screen: 2.0 on most high-DPI screens
win.set_title(title)Changes the title
win.set_fullscreen(on)Fills the screen the window is on, or leaves it
win.lock_cursor(on)Hides the mouse pointer and keeps it in the window, as a game's camera wants; Motion events still come. Needs Input
win.number() -> IntThe window's number, which std.gpu (14.24) draws into it by
win.close()Closes the window; a window is closed anyway when its last holder ends

Event is a union:

EventWhen
ClosedThe person asked to close the window. It stays open until close() or its last holder ends, so a program can ask "save first?"
Resized(width: Int, height: Int)Its inside has this size now, in pixels
Scaled(factor: Float)It moved to a screen with another scale
Focused(on: Bool)It gained or lost the keyboard
Key(key: String, down: Bool)A key went down or up. key names the key's place, not its letter, as the W3C's KeyboardEvent.code does: KeyW is the same key on every keyboard layout, as a game wants. Keys held down don't repeat
Text(text: String)Text typed, after the keyboard layout and input methods (IME), as a text box wants; held keys repeat here
MouseMoved(x: Float, y: Float)The pointer is here, in pixels from the window's top left
MouseButton(button: Int, down: Bool)A button went down or up: 0 left, 1 right, 2 middle, 3 back, 4 forward
Wheel(x: Float, y: Float)The wheel turned, in lines; y above 0 is away from the person
Motion(x: Float, y: Float)The mouse moved this much, however the pointer moved or stopped: for a game's camera

std.window · window.kav

use std.window: 3 types and 1 function. stdlib/window/window.kav

WindowError type

Why a window couldn't be opened: the system shows no windows here, as on a server with no screen.

pub type WindowError = Unavailable(reason: String)

Event type

Something that happened to a window. The last six come only after listen_input().

pub type Event = Closed | Resized(width: Int, height: Int) | Scaled(factor: Float) | Focused(on: Bool) | Key(key: String, down: Bool) | Text(text: String) | MouseMoved(x: Float, y: Float) | MouseButton(button: Int, down: Bool) | Wheel(x: Float, y: Float) | Motion(x: Float, y: Float)

AppWindow type

An open window. It closes when its last holder ends, or at close().

pub type AppWindow: Drop {
    id: Int
}

events method

The events that came, waiting for at least one; none once the window is closed.

pub fn events(self) -> [Event] uses Window

poll method

The events that came, without waiting.

pub fn poll(self) -> [Event] uses Window

run method

Calls f with each event as it comes, until f gives false or the window is closed.

pub fn run(self, f: fn(Event) -> Bool) uses Window

listen_input method

From now on, keyboard, mouse and text events come too.

pub fn listen_input(self) uses Input

size method

The inside of the window, in pixels.

pub fn size(self) -> (width: Int, height: Int) uses Window

scale method

How many pixels make one point on this screen.

pub fn scale(self) -> Float uses Window

set_title method

Changes the title.

pub fn set_title(self, title: String) uses Window

set_fullscreen method

Fills the screen the window is on, or leaves it.

pub fn set_fullscreen(self, on: Bool) uses Window

lock_cursor method

Hides the pointer and keeps it in the window; Motion events still come.

pub fn lock_cursor(self, on: Bool) uses Input

number method

The window's number, which std.gpu draws into it by.

pub fn number(self) -> Int

close method

Closes the window; closing it again does nothing.

pub fn close(self) uses Window

open function

A window of width × height pixels titled title, shown at once.

pub fn open(title: String, width: Int, height: Int) -> AppWindow ! WindowError uses Window
    require width >= 1 and height >= 1