Expand description
Window service, widget, events, commands and other types.
The Window! widget instantiates a window root, the windows service uses the window root as the
root widget of new window.
The example below declares a window that toggles if it can close.
use zng::prelude::*;
fn app() {
APP.defaults().run_window(async { window() });
}
fn window() -> window::WindowRoot {
let allow_close = var(true);
Window! {
on_close_requested = hn!(allow_close, |args| {
if !allow_close.get() {
args.propagation().stop();
}
});
title = "Can I Close?";
child_align = layout::Align::CENTER;
child = Toggle! {
child = Text!(allow_close.map(|a| formatx!("allow close = {a:?}")));
checked = allow_close;
};
}
}The WINDOWS service can be used to open, manage and close windows. The example below
opens a parent and child window.
use zng::prelude::*;
fn app() {
APP.defaults().run(async {
let r = WINDOWS.open(async { main_window() });
println!("opened {}", r.wait_rsp().await);
});
}
fn main_window() -> window::WindowRoot {
Window! {
title = "Main Window";
child_align = layout::Align::CENTER;
child = {
let enabled = var(true);
Button! {
child = Text!("Open/Close Child");
on_click = async_hn!(enabled, |_| {
enabled.set(false);
if WINDOWS.is_open("child-id") {
if let Ok(r) = WINDOWS.close("child-id") {
r.wait_done().await;
}
} else {
let parent = WINDOW.id();
WINDOWS.open_id("child-id", async move { child_window(parent) }).wait_done().await;
}
enabled.set(true);
});
widget::enabled;
}
};
}
}
fn child_window(parent: WindowId) -> window::WindowRoot {
Window! {
parent;
title = "Child Window";
size = (200, 100);
child = Button! {
child = Text!("Close");
on_click = hn!(|_| {
let _ = WINDOW.close();
});
};
}
}§Theming
Themes are a collection of widget styles that replace the look and feel of the entire application. This can be achieved by setting contextual properties that change the style and appearance for all widgets in all windows.
The WINDOWS.register_style_fn method is a convenient entry point for applying themes. It injects a window
style in all subsequent window instances. A custom theme can define a window style that sets all the contextual properties
that affect the other widgets. Widgets either implement the zng::style feature or provide their own contextual properties.
Note that the theme concept is different from the accent_color and color_scheme (light/dark). Themes usually provide a more drastic
change in appearance, not just changing the colors. Custom theme implementers should strongly consider incorporating these contextual
color values, these values are usually provided by the operating system, derived from the user preferences.
use zng::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Theme {
Default,
Custom,
}
impl Theme {
pub fn window_style_fn(&self) -> zng::style::StyleFn {
match self {
Theme::Default => style_fn!(|_| zng::window::DefaultStyle!()),
Theme::Custom => style_fn!(|_| themes::custom()),
}
}
}
mod themes {
use zng::{prelude::*, style::StyleBuilder};
pub fn custom() -> StyleBuilder {
let base_color = colors::RED.with_alpha(60.pct());
zng::window::DefaultStyle! {
zng::button::style_fn = Style! {
color::base_color = base_color;
};
zng::toggle::style_fn = Style! {
color::base_color = base_color;
};
zng::toggle::check_style_fn = Style! {
color::accent_color = colors::RED;
};
}
}
}
let theme = var(Theme::Default);
WINDOWS.register_style_fn(theme.map(|t| t.window_style_fn()));The example above provide a simple theming setup and a very basic custom theme, the theme will be applied for all
subsequent window instances and will dynamically update on variable change. Usually in a full app the
Theme enum would be serializable and the variable provided by the zng::config::CONFIG service.
See zng::style docs for more details about styles that fully replace the appearance of a widget.
§Full API
See zng_ext_window, zng_app::window and zng_wgt_window for the full window API.
Modules§
Structs§
- Auto
Size - Window auto-size config.
- Default
Style WDefault window style.- Frame
Image Ready Args FRAME_IMAGE_READY_EVENTargs.- Headless
Monitor - “Monitor” configuration used by windows in headless mode.
- ImeArgs
- Arguments for
IME_EVENT. - MONITORS
- Monitors service.
- Monitor
Id - Unique identifier of a monitor screen.
- Monitor
Info - All information about a monitor that
MONITORScan provide. - Monitors
Changed Args MONITORS_CHANGED_EVENTargs.- Parallel
Win - Defines what window operations can run in parallel, between windows.
- Video
Mode - Exclusive video mode info.
- WINDOW
- Current context window.
- WINDOWS
- Windows service.
- Window
WA window container.- Window
Button - Window chrome buttons.
- Window
Changed Args WINDOW_CHANGED_EVENTargs.- Window
Close Args WINDOW_CLOSE_EVENTargs.- Window
Close Requested Args WINDOW_CLOSE_REQUESTED_EVENTargs.- Window
Id - Unique identifier of an open window.
- Window
Loading Handle - Represents a handle that stops the window from loading while the handle is alive.
- Window
Open Args WINDOW_OPEN_EVENTargs.- Window
Root - Window root node and values.
- Window
Root Extender Args - Arguments for
WINDOWS.register_root_extender. - Window
State Allowed - Mask of allowed
WindowStatestates of a window. - Window
Vars - Variables that configure the opening or open window.
Enums§
- Block
Window Load - Defines if a widget load affects the parent window load.
- Close
Window Result - Response message of
closeandclose_together. - Focus
Indicator - Represents a focus request indicator.
- Frame
Capture Mode - Frame image capture mode in a window.
- Monitor
Query - A selector that returns a
MonitorInfo. - Render
Mode - Render backend preference.
- Start
Position - Window startup position.
- Window
Icon - Window icon.
- Window
Mode - Mode of an open window.
- Window
State - Window state.
Statics§
- FRAME_
IMAGE_ READY_ EVENT - A window frame has finished rendering.
- IME_
EVENT - Input Method Editor event targeting a text input widget.
- MONITORS_
CHANGED_ EVENT - Monitors added, removed or modified event.
- WINDOW_
CHANGED_ EVENT - Window moved, resized or other state changed.
- WINDOW_
CLOSE_ EVENT - Window closed event.
- WINDOW_
CLOSE_ REQUESTED_ EVENT - Window close requested event.
- WINDOW_
LOAD_ EVENT - Window finished loading and has opened in the view-process.
- WINDOW_
OPEN_ EVENT - New window has inited.
Traits§
- AppRun
Window Ext - Extension trait, adds
run_windowtoAppExtended. - Headless
AppWindow Ext - Window extension methods for
HeadlessApp. - WINDOWS_
Ext - Extension methods for
WINDOWS. - WINDOW_
Ext - Extensions methods for
WINDOWcontexts of windows open byWINDOWS. - Widget
Info Builder ImeArea - IME extension methods for
WidgetInfoBuilder. - Widget
Info ImeArea - IME extension methods for
WidgetInfo.
Functions§
- default_
mobile_ nested_ open_ handler - Default handler registered in mobile platforms.
- on_
frame_ image_ ready POn window frame rendered.- on_ime
POn Input Method Editor event.- on_
pre_ frame_ image_ ready PPreviewon_frame_image_readyevent.- on_
pre_ ime PPreviewon_imeevent.- on_
pre_ window_ changed PPreviewon_window_changedevent.- on_
pre_ window_ close_ requested PPreviewon_window_close_requestedevent.- on_
pre_ window_ exited_ fullscreen PPreviewon_window_exited_fullscreenevent.- on_
pre_ window_ fullscreen PPreviewon_window_fullscreenevent.- on_
pre_ window_ load PPreviewon_window_loadevent.- on_
pre_ window_ maximized PPreviewon_window_maximizedevent.- on_
pre_ window_ minimized PPreviewon_window_minimizedevent.- on_
pre_ window_ moved PPreviewon_window_movedevent.- on_
pre_ window_ open PPreviewon_window_openevent.- on_
pre_ window_ resized PPreviewon_window_resizedevent.- on_
pre_ window_ restored PPreviewon_window_restoredevent.- on_
pre_ window_ state_ changed PPreviewon_window_state_changedevent.- on_
pre_ window_ unmaximized PPreviewon_window_unmaximizedevent.- on_
pre_ window_ unminimized PPreviewon_window_unminimizedevent.- on_
window_ changed POn window moved, resized or other state changed.- on_
window_ close_ requested POn window close requested.- on_
window_ exited_ fullscreen POn window state changed fromWindowState::is_fullscreen.- on_
window_ fullscreen POn window state changed toWindowState::is_fullscreen.- on_
window_ load POn window loaded.- on_
window_ maximized POn window state changed toWindowState::Maximized.- on_
window_ minimized POn window state changed toWindowState::Minimized.- on_
window_ moved POn window position changed.- on_
window_ open POn window opened.- on_
window_ resized POn window size changed.- on_
window_ restored POn window state changed toWindowState::Normal.- on_
window_ state_ changed POn window state changed.- on_
window_ unmaximized POn window state changed fromWindowState::Maximized.- on_
window_ unminimized POn window state changed fromWindowState::Minimized.