Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Window

A window is the core component of an application. It is the object where all events from child controls are processed.

Window styles, backgrounds, and flags

The Type changes a window’s frame, while Background communicates the meaning of its content. Initialization flags control interactions such as resizing and closing.

These screenshots show focused windows rendered by the window guide example with the default theme. The example installs the built-in AppCUI palette (.palette(Palette::BuiltIn)) so the named colors match the theme. An inactive window uses the theme’s inactive background instead.

Frame styles

TypeScreenshot
Type::Classic
Double-line frame while focused;
single-line when inactive or moving.
Classic window with a double-line border and a bracketed close button.
Type::Rounded
Single-line frame with rounded corners.
Rounded window with rounded corners and a bracketed close button.
Type::Panel
Title bar with no surrounding frame.
The title is centered.
Panel window with a title bar and no surrounding border.
Type::Borderless
No frame and no inner margin.
The title is left-aligned.
The tag, when set, is on the right.
Borderless window with a left-aligned title and a close button on the right.

Backgrounds

The colors below are the default theme’s; other themes can choose different colors.

BackgroundScreenshot
Background::Normal
Dark blue (#000080); the default.
Normal window with a dark blue background.
Background::Error
Dark red (#800000) for failures.
Error window with a dark red background.
Background::Warning
Olive (#808000) for cautions.
Warning window with an olive background.
Background::Notification
Dark green (#008000) for information.
Notification window with a dark green background.

Initialization flags

FlagsScreenshot
Flags::None
Movable, fixed size, with a close button.
Default window with a close button and no resize grip.
Flags::Sizeable
Adds the maximize/restore button
and the resize grip.
Sizeable window with maximize, close, and resize controls.
Flags::NoCloseButton
Hides the close button.
Window whose title bar has no close button.
Flags::NoTitle
Hides the title text.
The frame, buttons, tag, and hotkey stay.
Classic window with a frame and a close button, and no title text.

Flags::FixedPosition prevents moving the window and adds no separate visual indicator. Flags can be combined with |; for example, Flags::Sizeable | Flags::NoCloseButton permits resizing while hiding the close button. Flags::NoTitle only hides the title text: a Classic or Rounded frame is still drawn, and the close button, tag, and hotkey remain.

Run the example from the repository root to try each option:

cargo run --example window_guide -- classic
cargo run --example window_guide -- notification
cargo run --example window_guide -- sizeable

The other choices are rounded, panel, borderless, error, warning, no-close, no-title, and fixed-position.

For example, a resizable error dialog can combine Flags::Sizeable with Background::Error:

#![allow(unused)]
fn main() {
let window = Window::with_type(
    "Import failed",
    layout!("a:c,w:40,h:10"),
    window::Flags::Sizeable,
    window::Type::Rounded,
    window::Background::Error,
);
}

To create a Window use:

  • Window::new method (with 3 parameters: a title, a layout and initialization flags)
  • Window::with_type method (with 5 parameters: a title, a layout, initialization flags, a type and a background)
  • macro window!.
let w = Window::new("Title", layout!("x:10,y:5,w:15,h:9"),window::Flags::None);
let w2 = window!("Title,a:c,w:10,h:10");
let w3 = window!("title='Some Title',a:c,w:30,h:10,flags=[Sizeable])");
let w4 = window!("title='WithTag',a:c,w:30,h:10,tag:MyTag)");
let w5 = window!("Title,a:c,w:10,h:10,key:Alt+F10");
let w6 = window!("Title,a:c,w:10,h:10,key:auto");

Keep in mind that window will NOT handle any events from its children.

A window supports all common parameters (as they are described in Instantiate via Macros section). Besides them, the following named parameters are also accepted:

Parameter nameTypePositional parameterPurpose
title or text or captionStringYes (first positional parameter)The title (text) of the window
flagsString or ListNoWindow initialization flags
typeStringNoWindow type (visual)
bg or back or backgroundStringNoThe background of the window.
tagStringNoThe tag of the window
hotkey or hot-key or keyKeyNoThe hotkey associated with a window. You can also use the auto value to ask the framework to find the first available key (from Alt+1 to Alt+9)

To create a window that will handle events from its children, use the #[Window(...)] attribute:

#[Window(events=..., )]
struct MyWindow {
    // specific fields
}

The attribute rewrites that struct in three ways:

  • it inserts a field base: Window as the first field (every constructor initializes base)
  • it implements Deref and DerefMut, so Window methods such as add and set_title are called on base
  • it implements every event trait with an empty body, except the traits named in events. Those are left for you to write.

For example, #[Window(events = ButtonEvents)] struct MyWin { x: i32 } becomes a struct with base: Window and x: i32, plus Deref / DerefMut, plus an empty impl CheckBoxEvents for MyWin, an empty impl RadioBoxEvents for MyWin, and an empty impl for every other event trait. impl ButtonEvents for MyWin is not generated.

The expansion, the compiler errors that follow from it (missing field 'base', conflicting trait implementations, Control not implemented), and the same code written without the attribute are described in What the #[Window] attribute does.

A window supports the following initialization flags:

  • window::Flags::None - regular window (with a close button)
  • window::Flags::Sizeable or Sizeable (for window! macro) - a window that has the resize grip and the maximize button
  • window::Flags::NoCloseButton or NoCloseButton (for window! macro) - a window without a close button
  • window::Flags::FixedPosition or FixedPosition (for window! macro) - a window that can not be moved
  • window::Flags::NoTitle or NoTitle (for window! macro) - hides the title text. The frame (when the type has one), the close button, the tag, and the hotkey stay visible

the following types:

  • window::Type::Classic or Classic (for window! macro) - a regular window with a classic look (double border). The title is centered
  • window::Type::Rounded or Rounded (for window! macro) - a window with rounded corners. The title is centered
  • window::Type::Panel or Panel (for window! macro) - a window with a top bar and no borders. The title is centered
  • window::Type::Borderless or Borderless (for window! macro) - a window with no frame and no inner margin. The title is left-aligned, and the tag is placed on the right

the following backgrounds:

  • window::Background::Normal or Normal (for window! macro) - a regular window (using the default background color)
  • window::Background::Error or Error (for window! macro) - a window with a red background to indicate an error message
  • window::Background::Notification or Notification (for window! macro) - a window with a different background designed for notification messages
  • window::Background::Warning or Warning (for window! macro) - a window with a different background designed for Warning messages

Methods

Besides the Common methods for all Controls, a window also has the following additional methods:

MethodPurpose
add(...)Adds a new control as a child of the current window
control(...)Returns an immutable reference to a control based on its handle
control_mut(...)Returns a mutable reference to a control based on its handle
request_focus_for_control(...)Requests the focus for a specific control, given a specific handle
toolbar()Returns a mutable reference to the current window’s toolbar
set_title(...)Sets the window title.
Example: win.set_title("Title") — this sets the title of the window to Title.
title()Returns the title of the current window
set_tag(...)Sets the window tag.
Example: win.set_tag("ABC") — this sets the tag of the window to ABC.
tag()Returns the tag of the current window
clear_tag()Clears the current tag. It is equivalent to set_tag("").
set_auto_hotkey()Automatically selects a free hotkey (in the format Alt+{number} where {number} is between 1 and 9).
enter_resize_mode()Enters resize mode programmatically.
closeCloses the current window.

Key association

In terms of key association, a Window has two modes:

  • Normal mode (for when a window has focus)
  • Resize/Move mode (in this mode you can use arrows and various combinations to move the window)

For normal mode

KeyPurpose
Ctrl+Alt+M or
Ctrl+Alt+R
Switch the window to resize/move mode
EscapeTriggers a call to on_cancel(...) method. By default this will close the window. However you can change the behavior and return ActionRequest::Deny from the on_cancel callback
Up or
Alt+Up or
Ctrl+Up
Moves to the closest control on the upper side of the current one.
Down or
Alt+Down or
Ctrl+Down
Moves to the closest control on the bottom side of the current one
Left or
Alt+Left or
Ctrl+Left
Moves to the closest control on the left side of the current one
Right or
Alt+Right or
Ctrl+Right
Moves to the closest control on the right side of the current one

OBS: Keep in mind that if any of these keys (in particular Left, Right, Up and Down) are captured by one of the children of a window, they will not be processed by the window.

For resize/move mode

KeyPurpose
Escape or Enter or Tab or SpaceSwitch back to the normal mode
Left, Up, Right, DownArrow keys can be used to move the window
CCenters the current window to the Desktop
M or RMaximizes or Restores the size of the current window
Alt+{Left, Up, Right, Down}Moves the window towards one of the margins of the Desktop. For example Alt+Up will move current window to the top margin of the client space of the Desktop
Ctrl+{Left, Up, Right, Down}Increases or decreases the Width or Height of the current Window

Events

Window related events can be intercepted via WindowEvents trait. You will need to add WindowEvents in the list of events like in the following example:

#![allow(unused)]
fn main() {
#[Window(events=WindowEvents)]
struct MyWindow { ... }
impl WindowEvents for MyWindow { ... }
}

WindowEvents is defined in the following way:

#![allow(unused)]
fn main() {
pub trait WindowEvents {
    fn on_close(&mut self) -> EventProcessStatus {
        EventProcessStatus::Ignored
    }
    fn on_layout_changed(&mut self, old_layout: Rect, new_layout: Rect) {}
    fn on_activate(&mut self) {}
    fn on_deactivate(&mut self) {}
    fn on_accept(&mut self) {}
    fn on_cancel(&mut self) -> ActionRequest {
        ActionRequest::Allow
    }
}
}

These methods are called under the following scenarios:

MethodCalled when
on_layout_changed(...)Called whenever the size or position of a window changes.
on_activate(...)Called whenever a window or a modal window receives the focus
on_deactivate(...)Called whenever a window or a modal window loses the focus
on_accept(...)Called only for modal windows when you hit the Enter key
on_cancel(...)For a modal window this method is called when you press Escape. You can use this method to disable closing via Escape key and for an exit with a value (via method exit_with(...)

For a regular window (non-modal) this method can be called when you pressed Esc key or when you pressed the close button from a window.

Window Tags

For every window, a tag can be set for a window (a tag is a string associated with a Window that reflects its purpose). To set a tag use .set_tag("<name") method. For example, the following code:

fn main() -> Result<(), appcui::system::Error> {
    App::new()
        .window(|| {
            let mut win = window!("Title,a:c,w:40,h:9");
            win.set_tag("TAG");
            win
        })
        .run()
}

should generate a window that looks like the following:

On a Classic, Rounded, or Panel window the tag is drawn on the left side of the title bar, as in the picture above. On a Borderless window the tag is drawn on the right.

Window Hot Key

You can also associate a hot key to a window. A hot key allows you to quickly switch between windows. In the next example, we set up Alt+7 as a hot key for a window.

fn main() -> Result<(), appcui::system::Error> {
    App::new()
        .window(|| {
            let mut win = window!("Title,a:c,w:40,h:9");
            win.set_hotkey(key!("Alt+7"));
            win
        })
        .run()
}

should generate a window that looks like the following: