zng

Module window

source
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: &window::WindowCloseRequestedArgs| {
            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();
            });
        };
    }
}

§Full API

See zng_ext_window, zng_app::window and zng_wgt_window for the full window API.

Modules§

Structs§

Enums§

Statics§

Traits§

Functions§