Skip to main content

window/
window.rs

1// Copyright (c) 2026 John Law
2// SPDX-License-Identifier: MIT
3
4//! A glass window with widgets: a title, a card with a counter, a text box, and two
5//! buttons.
6//!
7//! Run it with `cargo run -p glasskit --example window`. Pass `glass` or `solid` to see the
8//! other materials.
9
10use glasskit::Ui;
11use glasskit::events::{Event, EventLoop};
12use glasskit::widget::{Button, Card, Column, Insets, Row, Spacer, Text, TextBox, TextStyle};
13use glasskit::window::{Key, Material, Window, WindowEvent};
14
15fn main() -> glasskit::Result<()> {
16    let ui = Ui::new()?;
17    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
18    let mut window = Window::new(&ui, "glasskit", 420.0, 330.0)?;
19    window.set_min_size(320.0, 290.0);
20    match std::env::args().nth(1).as_deref() {
21        Some("glass") => window.set_material(Material::Glass),
22        Some("solid") => window.set_material(Material::Solid),
23        _ => {}
24    }
25
26    let title = window.add(Text::new("Settings").style(TextStyle::Title));
27    let note = window.add(Text::new(
28        "Widgets lay out in rows and columns, and the window follows the display scale.",
29    ));
30    let count = window.add(Text::new("Count: 0").style(TextStyle::BodyStrong));
31    let add = window.add(Button::new("Add one"));
32    let counter = window.add(Row::new().gap(12.0).child(count).child(add));
33    let card = window.add(Card::new().child(counter));
34    let name = window.add(TextBox::new().placeholder("Your name").max_length(40));
35    let spacer = window.add(Spacer::fill());
36    let cancel = window.add(Button::new("Cancel"));
37    let ok = window.add(Button::new("OK").accent());
38    let buttons = window.add(Row::new().gap(8.0).child(spacer).child(cancel).child(ok));
39    let gap = window.add(Spacer::fill());
40    let root = window.add(
41        Column::new()
42            .padding(Insets::all(24.0))
43            .gap(12.0)
44            .child(title)
45            .child(note)
46            .child(card)
47            .child(name)
48            .child(gap)
49            .child(buttons),
50    );
51    window.set_root(root);
52    window.show();
53
54    let mut n = 0;
55    loop {
56        match events.next(None)? {
57            Event::Window(id) if id == window.id() => {
58                while let Some(event) = window.next_event() {
59                    match event {
60                        WindowEvent::Clicked(b) if b == add => {
61                            n += 1;
62                            window.set_text(count, &format!("Count: {n}"));
63                            println!("count {n}");
64                        }
65                        WindowEvent::Clicked(b) if b == ok => {
66                            println!("ok {}", window.text(name));
67                            return Ok(());
68                        }
69                        WindowEvent::Submitted(b) if b == name => {
70                            println!("ok {}", window.text(name));
71                            return Ok(());
72                        }
73                        WindowEvent::Clicked(b) if b == cancel => return Ok(()),
74                        WindowEvent::Key(k) if k.is_plain(Key::ESCAPE) => {
75                            println!("escape");
76                            return Ok(());
77                        }
78                        WindowEvent::CloseRequested => return Ok(()),
79                        _ => {}
80                    }
81                }
82            }
83            Event::Quit => return Ok(()),
84            _ => {}
85        }
86    }
87}