Skip to content
CShift0.45

Window

Struct · namespace System.Graphics · Since 0.45

struct Window : IDisposable

A window that shows frames: images of a program (a game) or the frames of a System.Ui.Ui. One window at a time.

var window = try Window.Open("Demo", 400, 300);
var ui = Ui.Create();
while (window.Update()) // waits for input; false once the window was closed
{
ui.Begin(window);
ui.Label("Hello");
window.Show(ui);
}
Name Description
Open Opens a window whose inside is width x height pixels at scale 1 (on a screen with a higher scale it is larger by that factor).
SystemPrefersDark Whether the user chose dark colors for apps in the settings of the system: true or false on Windows (the setting “Choose your app mode”), null where it is not known (Linux, the browser, AmigaOS).
static WindowError<Window> Open(string title, int width, int height)

Opens a window whose inside is width x height pixels at scale 1 (on a screen with a higher scale it is larger by that factor).

static Optional<bool> SystemPrefersDark()

Whether the user chose dark colors for apps in the settings of the system: true or false on Windows (the setting “Choose your app mode”), null where it is not known (Linux, the browser, AmigaOS). It can change while the program runs; asking costs a look into the registry.

bool dark = Window.SystemPrefersDark() ?? false;
ui.SetTheme(dark ? Theme.Dark() : Theme.Light());
window.SetDarkFrame(dark);
Name Description
Update Handles the events of the window and waits first: for input, as long as the last frame asked for (see Window.WaitAtMost; a System.Ui.Ui asks for it), or after Window.Show of an image until the next frame of Window.SetFrameRate is due (without a frame rate it does not wait).
Input The input since the last frame.
Width The width of the inside of the window in pixels.
Height The height of the inside of the window in pixels.
Scale The scale of the screen the window is on: 1 at 96 dpi, 1.5 at 144 dpi, …
DeltaTime The seconds between the last two Window.Updates, at most a tenth of a second (so that a game does not jump after the window was dragged): how far things move in this frame.
SetFrameRate Limits a loop of Window.Update and Window.Show of images to framesPerSecond (0: as fast as the program can).
Show Shows a frame that is not an image: window.Show(ui) ends the frame of a System.Ui.Ui and shows it.
ShowScaled Shows an image scaled to the window with sharp pixels, the largest whole factor that fits (a game’s screen of 320 x 180 pixels in a window of any size).
ToScreen A position in pixels of the window (the mouse of Input) in pixels of the image that Window.ShowScaled showed last; it may lie outside of the image (on the black border).
WaitAtMost How long the next Window.Update waits for input: -1 until there is input, 0 not at all, else at most that many milliseconds.
SetCursor The shape of the mouse pointer over the window.
SetTitle Changes the text in the title bar.
SetDarkFrame Draws the frame of the window (the title bar and the borders) in dark colors, to go with a dark theme, or in the light ones again.
ConfirmClose When confirm is true, the user closing the window (its close button, Alt+F4, the close gadget) does not close it: the next frame’s input has Input.CloseRequested, and Window.Update stays true.
Close Closes the window: the next Window.Update returns false.
Dispose Closes the window.
bool Update()

Handles the events of the window and waits first: for input, as long as the last frame asked for (see Window.WaitAtMost; a System.Ui.Ui asks for it), or after Window.Show of an image until the next frame of Window.SetFrameRate is due (without a frame rate it does not wait). False once the window was closed: by the user (unless Window.ConfirmClose is on) or by Window.Close.

Input Input()

The input since the last frame.

int Width()

The width of the inside of the window in pixels.

int Height()

The height of the inside of the window in pixels.

double Scale()

The scale of the screen the window is on: 1 at 96 dpi, 1.5 at 144 dpi, …

double DeltaTime()

The seconds between the last two Window.Updates, at most a tenth of a second (so that a game does not jump after the window was dragged): how far things move in this frame.

void SetFrameRate(int framesPerSecond)

Limits a loop of Window.Update and Window.Show of images to framesPerSecond (0: as fast as the program can). In the browser the page’s frames set the pace anyway.

void Show(const ref IWindowFrame frame)

Shows a frame that is not an image: window.Show(ui) ends the frame of a System.Ui.Ui and shows it.

void Show(Image image)

Shows an image (drawn from the top left corner, one pixel of the image on one of the window); the next Window.Update does not wait for input, so a loop of Update and Show draws frames continuously (an animation, a game; see Window.SetFrameRate).

void ShowScaled(Image screen)

Shows an image scaled to the window with sharp pixels, the largest whole factor that fits (a game’s screen of 320 x 180 pixels in a window of any size). Its pixels count as opaque. Window.ToScreen gives the position of the mouse in pixels of the image.

void ShowScaled(Image screen, Scaling scaling)

Shows an image scaled to the window as scaling says (see Scaling).

Point ToScreen(int x, int y)

A position in pixels of the window (the mouse of Input) in pixels of the image that Window.ShowScaled showed last; it may lie outside of the image (on the black border).

Point ToScreen(const ref Input input)

The position of the mouse in pixels of the image that Window.ShowScaled showed last.

void WaitAtMost(int milliseconds)

How long the next Window.Update waits for input: -1 until there is input, 0 not at all, else at most that many milliseconds. (A System.Ui.Ui sets it for its frames.)

void SetCursor(Cursor cursor)

The shape of the mouse pointer over the window.

void SetTitle(string title)

Changes the text in the title bar.

void SetDarkFrame(bool dark)

Draws the frame of the window (the title bar and the borders) in dark colors, to go with a dark theme, or in the light ones again. Windows 10 (20H1) and 11; elsewhere the window manager or the browser draws the frame, and it does nothing.

void ConfirmClose(bool confirm)

When confirm is true, the user closing the window (its close button, Alt+F4, the close gadget) does not close it: the next frame’s input has Input.CloseRequested, and Window.Update stays true. The program can then ask (“Save the changes?”) and close the window with Window.Close, or do nothing to keep it open. False (the default): closing it ends Window.Update at once. In the browser the page cannot be closed by the program, so there it has no effect.

window.ConfirmClose(true);
while (window.Update())
{
ui.Begin(window);
if (ui.Input().CloseRequested)
asking = true;
if (asking)
{
using (ui.Modal("quit", "Quit without saving?"))
{
if (ui.Button("Quit"))
window.Close();
if (ui.Button("Cancel"))
asking = false;
}
}
window.Show(ui);
}
void Close()

Closes the window: the next Window.Update returns false. The same as Window.Dispose.

void Dispose()

Closes the window.