winctx 0.0.14

A minimal window context for Rust.
Documentation

winctx

A minimal window context for Rust.

The showcase popup menu

In order to do most productive things in a Windows desktop environment, you need to construct and interact with a window. Constructing this window allows for processing messages which fill a wide range of functions.

Doing this allows applications to:

There are a few additional APIs provided by this crate because they are so common:

Note that crate is fairly opinionated, not everything that is possible through the underlying APIs will be exposed.

Example

The primary purpose of this crate is to:

  • Define a window and its capabilities. I.e. if it should have a context menu or receive clipboard events.
  • Handle incoming Events from the window.

The basic loop looks like this:

use std::pin::pin;

use tokio::signal::ctrl_c;
use winctx::{Event, Icons, WindowBuilder};

const ICON: &[u8] = include_bytes!("tokio.ico");

let mut icons = Icons::new();
let icon = icons.push_buffer(ICON, 22, 22);

let mut window = WindowBuilder::new("se.tedro.Example")
    .window_name("Example Application")
    .icons(icons);

let area = window.new_area().icon(icon);

let menu = area.popup_menu();

let first = menu.push_entry("Example Application").id();
menu.push_separator();
let quit = menu.push_entry("Quit").id();
menu.set_default(first);

let (sender, mut event_loop) = window
    .build()
    .await?;

let mut ctrl_c = pin!(ctrl_c());
let mut shutdown = false;

loop {
    let event = tokio::select! {
        _ = ctrl_c.as_mut(), if !shutdown => {
            sender.shutdown();
            shutdown = true;
            continue;
        }
        event = event_loop.tick() => {
            event?
        }
    };

    match event {
        Event::MenuItemClicked(item_id) => {
            println!("Menu entry clicked: {item_id:?}");

            if item_id == quit {
                sender.shutdown();
            }
        }
        Event::Shutdown => {
            println!("Window shut down");
            break;
        }
        _ => {}
    }
}