Skip to main content

Window

Struct Window 

Source
pub struct Window { /* private fields */ }
Expand description

A top-level window with a glass look and a tree of widgets.

Make it with a Ui, add widgets, set the root, and show it. The event loop gives Event::Window when it has events. Dropping the window closes it.

Implementations§

Source§

impl Window

Source

pub fn new(ui: &Ui, title: &str, width: f32, height: f32) -> Result<Self>

A hidden window with this title and a client area of this size, in DIPs. It shows after Window::show.

Examples found in repository?
examples/kanban.rs (line 26)
23fn main() -> glasskit::Result<()> {
24    let ui = Ui::new()?;
25    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
26    let mut window = Window::new(&ui, "Kanban", 820.0, 520.0)?;
27    window.set_min_size(560.0, 360.0);
28
29    let mut board = Board::new(&mut window, &["To do", "Doing", "Done"]);
30    for (lane, title, body) in CARDS {
31        board.add_card(&mut window, lane, CardData::new(title, body));
32    }
33    let pinned = CardData {
34        pinned: true,
35        ..CardData::new("Ask for the budget", "")
36    };
37    board.add_card(&mut window, 1, pinned);
38
39    let title = window.add(Text::new("Board").style(TextStyle::Title));
40    let gap = window.add(Spacer::fill());
41    let archived = window.add(Button::new("Show archived"));
42    let top = window.add(Row::new().child(title).child(gap).child(archived));
43    let root = window.add(
44        Column::new()
45            .padding(Insets::all(16.0))
46            .gap(12.0)
47            .child(top)
48            .child(board.widget()),
49    );
50    window.set_root(root);
51    window.show();
52
53    loop {
54        match events.next(None)? {
55            Event::Window(id) if id == window.id() => {
56                while let Some(event) = window.next_event() {
57                    match event {
58                        WindowEvent::CloseRequested => return Ok(()),
59                        WindowEvent::Clicked(id) if id == archived => {
60                            let show = !board.show_archived();
61                            board.set_show_archived(&mut window, show);
62                            let label = if show {
63                                "Hide archived"
64                            } else {
65                                "Show archived"
66                            };
67                            window.set_text(archived, label);
68                        }
69                        event => {
70                            if let Some(change) = board.handle(&ui, &mut window, &event)? {
71                                println!("{}", describe(&board, &change));
72                            }
73                        }
74                    }
75                }
76            }
77            Event::Quit => return Ok(()),
78            _ => {}
79        }
80    }
81}
More examples
Hide additional examples
examples/sidebar.rs (line 25)
22fn main() -> glasskit::Result<()> {
23    let ui = Ui::new()?;
24    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
25    let mut window = Window::new(&ui, "Side bar", 560.0, 360.0)?;
26    window.set_min_size(360.0, 260.0);
27
28    let items = PAGES.map(|(name, icon)| window.add(NavItem::new(name).icon(icon)));
29    let pages = PAGES.map(|(name, _)| {
30        let title = window.add(Text::new(name).style(TextStyle::Title));
31        window.add(Column::new().padding(Insets::all(16.0)).child(title))
32    });
33    let gap = window.add(Spacer::fill());
34    let mode = match std::env::args().nth(1).as_deref() {
35        Some("icons") => SideMode::Icons,
36        Some("collapsed") => SideMode::Collapsed,
37        _ => SideMode::Expanded,
38    };
39    let side = window.add(
40        SideBar::new()
41            .title("glasskit")
42            .expanded_width(180.0)
43            .mode(mode)
44            .gap(4.0)
45            .child(items[0])
46            .child(items[1])
47            .child(gap)
48            .child(items[2]),
49    );
50    let stack = window.add(Stack::new().width(Length::Fill(1.0)).children(pages));
51    let root = window.add(
52        Row::new()
53            .padding(Insets::all(8.0))
54            .gap(8.0)
55            .align(Align::Stretch)
56            .child(side)
57            .child(stack),
58    );
59    window.set_root(root);
60    let show = |window: &mut Window, shown: usize| {
61        for (i, (item, page)) in items.into_iter().zip(pages).enumerate() {
62            window.set_selected(item, i == shown);
63            window.set_visible(page, i == shown);
64        }
65    };
66    show(&mut window, 0);
67    window.show();
68
69    loop {
70        match events.next(None)? {
71            Event::Window(id) if id == window.id() => {
72                while let Some(event) = window.next_event() {
73                    match event {
74                        WindowEvent::Clicked(id) => {
75                            if let Some(shown) = items.iter().position(|&item| id == item) {
76                                show(&mut window, shown);
77                            }
78                        }
79                        WindowEvent::SideModeChanged(id) if id == side => {
80                            println!("{:?}", window.side_mode(side));
81                        }
82                        WindowEvent::CloseRequested => return Ok(()),
83                        _ => {}
84                    }
85                }
86            }
87            Event::Quit => return Ok(()),
88            _ => {}
89        }
90    }
91}
examples/window.rs (line 18)
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}
Source

pub fn id(&self) -> WindowId

The id that Event::Window gives for this window.

Examples found in repository?
examples/kanban.rs (line 55)
23fn main() -> glasskit::Result<()> {
24    let ui = Ui::new()?;
25    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
26    let mut window = Window::new(&ui, "Kanban", 820.0, 520.0)?;
27    window.set_min_size(560.0, 360.0);
28
29    let mut board = Board::new(&mut window, &["To do", "Doing", "Done"]);
30    for (lane, title, body) in CARDS {
31        board.add_card(&mut window, lane, CardData::new(title, body));
32    }
33    let pinned = CardData {
34        pinned: true,
35        ..CardData::new("Ask for the budget", "")
36    };
37    board.add_card(&mut window, 1, pinned);
38
39    let title = window.add(Text::new("Board").style(TextStyle::Title));
40    let gap = window.add(Spacer::fill());
41    let archived = window.add(Button::new("Show archived"));
42    let top = window.add(Row::new().child(title).child(gap).child(archived));
43    let root = window.add(
44        Column::new()
45            .padding(Insets::all(16.0))
46            .gap(12.0)
47            .child(top)
48            .child(board.widget()),
49    );
50    window.set_root(root);
51    window.show();
52
53    loop {
54        match events.next(None)? {
55            Event::Window(id) if id == window.id() => {
56                while let Some(event) = window.next_event() {
57                    match event {
58                        WindowEvent::CloseRequested => return Ok(()),
59                        WindowEvent::Clicked(id) if id == archived => {
60                            let show = !board.show_archived();
61                            board.set_show_archived(&mut window, show);
62                            let label = if show {
63                                "Hide archived"
64                            } else {
65                                "Show archived"
66                            };
67                            window.set_text(archived, label);
68                        }
69                        event => {
70                            if let Some(change) = board.handle(&ui, &mut window, &event)? {
71                                println!("{}", describe(&board, &change));
72                            }
73                        }
74                    }
75                }
76            }
77            Event::Quit => return Ok(()),
78            _ => {}
79        }
80    }
81}
More examples
Hide additional examples
examples/sidebar.rs (line 71)
22fn main() -> glasskit::Result<()> {
23    let ui = Ui::new()?;
24    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
25    let mut window = Window::new(&ui, "Side bar", 560.0, 360.0)?;
26    window.set_min_size(360.0, 260.0);
27
28    let items = PAGES.map(|(name, icon)| window.add(NavItem::new(name).icon(icon)));
29    let pages = PAGES.map(|(name, _)| {
30        let title = window.add(Text::new(name).style(TextStyle::Title));
31        window.add(Column::new().padding(Insets::all(16.0)).child(title))
32    });
33    let gap = window.add(Spacer::fill());
34    let mode = match std::env::args().nth(1).as_deref() {
35        Some("icons") => SideMode::Icons,
36        Some("collapsed") => SideMode::Collapsed,
37        _ => SideMode::Expanded,
38    };
39    let side = window.add(
40        SideBar::new()
41            .title("glasskit")
42            .expanded_width(180.0)
43            .mode(mode)
44            .gap(4.0)
45            .child(items[0])
46            .child(items[1])
47            .child(gap)
48            .child(items[2]),
49    );
50    let stack = window.add(Stack::new().width(Length::Fill(1.0)).children(pages));
51    let root = window.add(
52        Row::new()
53            .padding(Insets::all(8.0))
54            .gap(8.0)
55            .align(Align::Stretch)
56            .child(side)
57            .child(stack),
58    );
59    window.set_root(root);
60    let show = |window: &mut Window, shown: usize| {
61        for (i, (item, page)) in items.into_iter().zip(pages).enumerate() {
62            window.set_selected(item, i == shown);
63            window.set_visible(page, i == shown);
64        }
65    };
66    show(&mut window, 0);
67    window.show();
68
69    loop {
70        match events.next(None)? {
71            Event::Window(id) if id == window.id() => {
72                while let Some(event) = window.next_event() {
73                    match event {
74                        WindowEvent::Clicked(id) => {
75                            if let Some(shown) = items.iter().position(|&item| id == item) {
76                                show(&mut window, shown);
77                            }
78                        }
79                        WindowEvent::SideModeChanged(id) if id == side => {
80                            println!("{:?}", window.side_mode(side));
81                        }
82                        WindowEvent::CloseRequested => return Ok(()),
83                        _ => {}
84                    }
85                }
86            }
87            Event::Quit => return Ok(()),
88            _ => {}
89        }
90    }
91}
examples/window.rs (line 57)
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}
Source

pub fn show(&self)

Show the window and bring it to the front.

Examples found in repository?
examples/kanban.rs (line 51)
23fn main() -> glasskit::Result<()> {
24    let ui = Ui::new()?;
25    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
26    let mut window = Window::new(&ui, "Kanban", 820.0, 520.0)?;
27    window.set_min_size(560.0, 360.0);
28
29    let mut board = Board::new(&mut window, &["To do", "Doing", "Done"]);
30    for (lane, title, body) in CARDS {
31        board.add_card(&mut window, lane, CardData::new(title, body));
32    }
33    let pinned = CardData {
34        pinned: true,
35        ..CardData::new("Ask for the budget", "")
36    };
37    board.add_card(&mut window, 1, pinned);
38
39    let title = window.add(Text::new("Board").style(TextStyle::Title));
40    let gap = window.add(Spacer::fill());
41    let archived = window.add(Button::new("Show archived"));
42    let top = window.add(Row::new().child(title).child(gap).child(archived));
43    let root = window.add(
44        Column::new()
45            .padding(Insets::all(16.0))
46            .gap(12.0)
47            .child(top)
48            .child(board.widget()),
49    );
50    window.set_root(root);
51    window.show();
52
53    loop {
54        match events.next(None)? {
55            Event::Window(id) if id == window.id() => {
56                while let Some(event) = window.next_event() {
57                    match event {
58                        WindowEvent::CloseRequested => return Ok(()),
59                        WindowEvent::Clicked(id) if id == archived => {
60                            let show = !board.show_archived();
61                            board.set_show_archived(&mut window, show);
62                            let label = if show {
63                                "Hide archived"
64                            } else {
65                                "Show archived"
66                            };
67                            window.set_text(archived, label);
68                        }
69                        event => {
70                            if let Some(change) = board.handle(&ui, &mut window, &event)? {
71                                println!("{}", describe(&board, &change));
72                            }
73                        }
74                    }
75                }
76            }
77            Event::Quit => return Ok(()),
78            _ => {}
79        }
80    }
81}
More examples
Hide additional examples
examples/sidebar.rs (line 67)
22fn main() -> glasskit::Result<()> {
23    let ui = Ui::new()?;
24    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
25    let mut window = Window::new(&ui, "Side bar", 560.0, 360.0)?;
26    window.set_min_size(360.0, 260.0);
27
28    let items = PAGES.map(|(name, icon)| window.add(NavItem::new(name).icon(icon)));
29    let pages = PAGES.map(|(name, _)| {
30        let title = window.add(Text::new(name).style(TextStyle::Title));
31        window.add(Column::new().padding(Insets::all(16.0)).child(title))
32    });
33    let gap = window.add(Spacer::fill());
34    let mode = match std::env::args().nth(1).as_deref() {
35        Some("icons") => SideMode::Icons,
36        Some("collapsed") => SideMode::Collapsed,
37        _ => SideMode::Expanded,
38    };
39    let side = window.add(
40        SideBar::new()
41            .title("glasskit")
42            .expanded_width(180.0)
43            .mode(mode)
44            .gap(4.0)
45            .child(items[0])
46            .child(items[1])
47            .child(gap)
48            .child(items[2]),
49    );
50    let stack = window.add(Stack::new().width(Length::Fill(1.0)).children(pages));
51    let root = window.add(
52        Row::new()
53            .padding(Insets::all(8.0))
54            .gap(8.0)
55            .align(Align::Stretch)
56            .child(side)
57            .child(stack),
58    );
59    window.set_root(root);
60    let show = |window: &mut Window, shown: usize| {
61        for (i, (item, page)) in items.into_iter().zip(pages).enumerate() {
62            window.set_selected(item, i == shown);
63            window.set_visible(page, i == shown);
64        }
65    };
66    show(&mut window, 0);
67    window.show();
68
69    loop {
70        match events.next(None)? {
71            Event::Window(id) if id == window.id() => {
72                while let Some(event) = window.next_event() {
73                    match event {
74                        WindowEvent::Clicked(id) => {
75                            if let Some(shown) = items.iter().position(|&item| id == item) {
76                                show(&mut window, shown);
77                            }
78                        }
79                        WindowEvent::SideModeChanged(id) if id == side => {
80                            println!("{:?}", window.side_mode(side));
81                        }
82                        WindowEvent::CloseRequested => return Ok(()),
83                        _ => {}
84                    }
85                }
86            }
87            Event::Quit => return Ok(()),
88            _ => {}
89        }
90    }
91}
examples/window.rs (line 52)
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}
Source

pub fn add<K>(&mut self, element: impl Into<Element<K>>) -> Id<K>

Add a widget, and give its id. It shows when it is in the tree under the root.

Examples found in repository?
examples/kanban.rs (line 39)
23fn main() -> glasskit::Result<()> {
24    let ui = Ui::new()?;
25    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
26    let mut window = Window::new(&ui, "Kanban", 820.0, 520.0)?;
27    window.set_min_size(560.0, 360.0);
28
29    let mut board = Board::new(&mut window, &["To do", "Doing", "Done"]);
30    for (lane, title, body) in CARDS {
31        board.add_card(&mut window, lane, CardData::new(title, body));
32    }
33    let pinned = CardData {
34        pinned: true,
35        ..CardData::new("Ask for the budget", "")
36    };
37    board.add_card(&mut window, 1, pinned);
38
39    let title = window.add(Text::new("Board").style(TextStyle::Title));
40    let gap = window.add(Spacer::fill());
41    let archived = window.add(Button::new("Show archived"));
42    let top = window.add(Row::new().child(title).child(gap).child(archived));
43    let root = window.add(
44        Column::new()
45            .padding(Insets::all(16.0))
46            .gap(12.0)
47            .child(top)
48            .child(board.widget()),
49    );
50    window.set_root(root);
51    window.show();
52
53    loop {
54        match events.next(None)? {
55            Event::Window(id) if id == window.id() => {
56                while let Some(event) = window.next_event() {
57                    match event {
58                        WindowEvent::CloseRequested => return Ok(()),
59                        WindowEvent::Clicked(id) if id == archived => {
60                            let show = !board.show_archived();
61                            board.set_show_archived(&mut window, show);
62                            let label = if show {
63                                "Hide archived"
64                            } else {
65                                "Show archived"
66                            };
67                            window.set_text(archived, label);
68                        }
69                        event => {
70                            if let Some(change) = board.handle(&ui, &mut window, &event)? {
71                                println!("{}", describe(&board, &change));
72                            }
73                        }
74                    }
75                }
76            }
77            Event::Quit => return Ok(()),
78            _ => {}
79        }
80    }
81}
More examples
Hide additional examples
examples/sidebar.rs (line 28)
22fn main() -> glasskit::Result<()> {
23    let ui = Ui::new()?;
24    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
25    let mut window = Window::new(&ui, "Side bar", 560.0, 360.0)?;
26    window.set_min_size(360.0, 260.0);
27
28    let items = PAGES.map(|(name, icon)| window.add(NavItem::new(name).icon(icon)));
29    let pages = PAGES.map(|(name, _)| {
30        let title = window.add(Text::new(name).style(TextStyle::Title));
31        window.add(Column::new().padding(Insets::all(16.0)).child(title))
32    });
33    let gap = window.add(Spacer::fill());
34    let mode = match std::env::args().nth(1).as_deref() {
35        Some("icons") => SideMode::Icons,
36        Some("collapsed") => SideMode::Collapsed,
37        _ => SideMode::Expanded,
38    };
39    let side = window.add(
40        SideBar::new()
41            .title("glasskit")
42            .expanded_width(180.0)
43            .mode(mode)
44            .gap(4.0)
45            .child(items[0])
46            .child(items[1])
47            .child(gap)
48            .child(items[2]),
49    );
50    let stack = window.add(Stack::new().width(Length::Fill(1.0)).children(pages));
51    let root = window.add(
52        Row::new()
53            .padding(Insets::all(8.0))
54            .gap(8.0)
55            .align(Align::Stretch)
56            .child(side)
57            .child(stack),
58    );
59    window.set_root(root);
60    let show = |window: &mut Window, shown: usize| {
61        for (i, (item, page)) in items.into_iter().zip(pages).enumerate() {
62            window.set_selected(item, i == shown);
63            window.set_visible(page, i == shown);
64        }
65    };
66    show(&mut window, 0);
67    window.show();
68
69    loop {
70        match events.next(None)? {
71            Event::Window(id) if id == window.id() => {
72                while let Some(event) = window.next_event() {
73                    match event {
74                        WindowEvent::Clicked(id) => {
75                            if let Some(shown) = items.iter().position(|&item| id == item) {
76                                show(&mut window, shown);
77                            }
78                        }
79                        WindowEvent::SideModeChanged(id) if id == side => {
80                            println!("{:?}", window.side_mode(side));
81                        }
82                        WindowEvent::CloseRequested => return Ok(()),
83                        _ => {}
84                    }
85                }
86            }
87            Event::Quit => return Ok(()),
88            _ => {}
89        }
90    }
91}
examples/window.rs (line 26)
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}
Source

pub fn set_root(&mut self, root: impl Into<AnyId>)

The widget that fills the window. Its children, and their children, show.

Examples found in repository?
examples/kanban.rs (line 50)
23fn main() -> glasskit::Result<()> {
24    let ui = Ui::new()?;
25    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
26    let mut window = Window::new(&ui, "Kanban", 820.0, 520.0)?;
27    window.set_min_size(560.0, 360.0);
28
29    let mut board = Board::new(&mut window, &["To do", "Doing", "Done"]);
30    for (lane, title, body) in CARDS {
31        board.add_card(&mut window, lane, CardData::new(title, body));
32    }
33    let pinned = CardData {
34        pinned: true,
35        ..CardData::new("Ask for the budget", "")
36    };
37    board.add_card(&mut window, 1, pinned);
38
39    let title = window.add(Text::new("Board").style(TextStyle::Title));
40    let gap = window.add(Spacer::fill());
41    let archived = window.add(Button::new("Show archived"));
42    let top = window.add(Row::new().child(title).child(gap).child(archived));
43    let root = window.add(
44        Column::new()
45            .padding(Insets::all(16.0))
46            .gap(12.0)
47            .child(top)
48            .child(board.widget()),
49    );
50    window.set_root(root);
51    window.show();
52
53    loop {
54        match events.next(None)? {
55            Event::Window(id) if id == window.id() => {
56                while let Some(event) = window.next_event() {
57                    match event {
58                        WindowEvent::CloseRequested => return Ok(()),
59                        WindowEvent::Clicked(id) if id == archived => {
60                            let show = !board.show_archived();
61                            board.set_show_archived(&mut window, show);
62                            let label = if show {
63                                "Hide archived"
64                            } else {
65                                "Show archived"
66                            };
67                            window.set_text(archived, label);
68                        }
69                        event => {
70                            if let Some(change) = board.handle(&ui, &mut window, &event)? {
71                                println!("{}", describe(&board, &change));
72                            }
73                        }
74                    }
75                }
76            }
77            Event::Quit => return Ok(()),
78            _ => {}
79        }
80    }
81}
More examples
Hide additional examples
examples/sidebar.rs (line 59)
22fn main() -> glasskit::Result<()> {
23    let ui = Ui::new()?;
24    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
25    let mut window = Window::new(&ui, "Side bar", 560.0, 360.0)?;
26    window.set_min_size(360.0, 260.0);
27
28    let items = PAGES.map(|(name, icon)| window.add(NavItem::new(name).icon(icon)));
29    let pages = PAGES.map(|(name, _)| {
30        let title = window.add(Text::new(name).style(TextStyle::Title));
31        window.add(Column::new().padding(Insets::all(16.0)).child(title))
32    });
33    let gap = window.add(Spacer::fill());
34    let mode = match std::env::args().nth(1).as_deref() {
35        Some("icons") => SideMode::Icons,
36        Some("collapsed") => SideMode::Collapsed,
37        _ => SideMode::Expanded,
38    };
39    let side = window.add(
40        SideBar::new()
41            .title("glasskit")
42            .expanded_width(180.0)
43            .mode(mode)
44            .gap(4.0)
45            .child(items[0])
46            .child(items[1])
47            .child(gap)
48            .child(items[2]),
49    );
50    let stack = window.add(Stack::new().width(Length::Fill(1.0)).children(pages));
51    let root = window.add(
52        Row::new()
53            .padding(Insets::all(8.0))
54            .gap(8.0)
55            .align(Align::Stretch)
56            .child(side)
57            .child(stack),
58    );
59    window.set_root(root);
60    let show = |window: &mut Window, shown: usize| {
61        for (i, (item, page)) in items.into_iter().zip(pages).enumerate() {
62            window.set_selected(item, i == shown);
63            window.set_visible(page, i == shown);
64        }
65    };
66    show(&mut window, 0);
67    window.show();
68
69    loop {
70        match events.next(None)? {
71            Event::Window(id) if id == window.id() => {
72                while let Some(event) = window.next_event() {
73                    match event {
74                        WindowEvent::Clicked(id) => {
75                            if let Some(shown) = items.iter().position(|&item| id == item) {
76                                show(&mut window, shown);
77                            }
78                        }
79                        WindowEvent::SideModeChanged(id) if id == side => {
80                            println!("{:?}", window.side_mode(side));
81                        }
82                        WindowEvent::CloseRequested => return Ok(()),
83                        _ => {}
84                    }
85                }
86            }
87            Event::Quit => return Ok(()),
88            _ => {}
89        }
90    }
91}
examples/window.rs (line 51)
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}
Source

pub fn text<K: HasText>(&self, id: Id<K>) -> String

The text of a text or a text box, or the label of a button.

Examples found in repository?
examples/window.rs (line 66)
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}
Source

pub fn set_text<K: HasText>(&mut self, id: Id<K>, value: &str)

Change the text of a text or a text box, or the label of a button. A change here gives no WindowEvent::Changed.

Examples found in repository?
examples/kanban.rs (line 67)
23fn main() -> glasskit::Result<()> {
24    let ui = Ui::new()?;
25    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
26    let mut window = Window::new(&ui, "Kanban", 820.0, 520.0)?;
27    window.set_min_size(560.0, 360.0);
28
29    let mut board = Board::new(&mut window, &["To do", "Doing", "Done"]);
30    for (lane, title, body) in CARDS {
31        board.add_card(&mut window, lane, CardData::new(title, body));
32    }
33    let pinned = CardData {
34        pinned: true,
35        ..CardData::new("Ask for the budget", "")
36    };
37    board.add_card(&mut window, 1, pinned);
38
39    let title = window.add(Text::new("Board").style(TextStyle::Title));
40    let gap = window.add(Spacer::fill());
41    let archived = window.add(Button::new("Show archived"));
42    let top = window.add(Row::new().child(title).child(gap).child(archived));
43    let root = window.add(
44        Column::new()
45            .padding(Insets::all(16.0))
46            .gap(12.0)
47            .child(top)
48            .child(board.widget()),
49    );
50    window.set_root(root);
51    window.show();
52
53    loop {
54        match events.next(None)? {
55            Event::Window(id) if id == window.id() => {
56                while let Some(event) = window.next_event() {
57                    match event {
58                        WindowEvent::CloseRequested => return Ok(()),
59                        WindowEvent::Clicked(id) if id == archived => {
60                            let show = !board.show_archived();
61                            board.set_show_archived(&mut window, show);
62                            let label = if show {
63                                "Hide archived"
64                            } else {
65                                "Show archived"
66                            };
67                            window.set_text(archived, label);
68                        }
69                        event => {
70                            if let Some(change) = board.handle(&ui, &mut window, &event)? {
71                                println!("{}", describe(&board, &change));
72                            }
73                        }
74                    }
75                }
76            }
77            Event::Quit => return Ok(()),
78            _ => {}
79        }
80    }
81}
More examples
Hide additional examples
examples/window.rs (line 62)
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}
Source

pub fn set_enabled(&mut self, id: impl Into<AnyId>, enabled: bool)

Turn a widget on or off. A button that is off takes no clicks and no focus.

Source

pub fn set_visible(&mut self, id: impl Into<AnyId>, visible: bool)

Show or hide a widget and all widgets in it. A hidden widget takes no space in its row, column, or stack, and no clicks and no focus. A new widget is visible.

To change pages, put the pages in a Stack, and show one of them.

Examples found in repository?
examples/sidebar.rs (line 63)
22fn main() -> glasskit::Result<()> {
23    let ui = Ui::new()?;
24    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
25    let mut window = Window::new(&ui, "Side bar", 560.0, 360.0)?;
26    window.set_min_size(360.0, 260.0);
27
28    let items = PAGES.map(|(name, icon)| window.add(NavItem::new(name).icon(icon)));
29    let pages = PAGES.map(|(name, _)| {
30        let title = window.add(Text::new(name).style(TextStyle::Title));
31        window.add(Column::new().padding(Insets::all(16.0)).child(title))
32    });
33    let gap = window.add(Spacer::fill());
34    let mode = match std::env::args().nth(1).as_deref() {
35        Some("icons") => SideMode::Icons,
36        Some("collapsed") => SideMode::Collapsed,
37        _ => SideMode::Expanded,
38    };
39    let side = window.add(
40        SideBar::new()
41            .title("glasskit")
42            .expanded_width(180.0)
43            .mode(mode)
44            .gap(4.0)
45            .child(items[0])
46            .child(items[1])
47            .child(gap)
48            .child(items[2]),
49    );
50    let stack = window.add(Stack::new().width(Length::Fill(1.0)).children(pages));
51    let root = window.add(
52        Row::new()
53            .padding(Insets::all(8.0))
54            .gap(8.0)
55            .align(Align::Stretch)
56            .child(side)
57            .child(stack),
58    );
59    window.set_root(root);
60    let show = |window: &mut Window, shown: usize| {
61        for (i, (item, page)) in items.into_iter().zip(pages).enumerate() {
62            window.set_selected(item, i == shown);
63            window.set_visible(page, i == shown);
64        }
65    };
66    show(&mut window, 0);
67    window.show();
68
69    loop {
70        match events.next(None)? {
71            Event::Window(id) if id == window.id() => {
72                while let Some(event) = window.next_event() {
73                    match event {
74                        WindowEvent::Clicked(id) => {
75                            if let Some(shown) = items.iter().position(|&item| id == item) {
76                                show(&mut window, shown);
77                            }
78                        }
79                        WindowEvent::SideModeChanged(id) if id == side => {
80                            println!("{:?}", window.side_mode(side));
81                        }
82                        WindowEvent::CloseRequested => return Ok(()),
83                        _ => {}
84                    }
85                }
86            }
87            Event::Quit => return Ok(()),
88            _ => {}
89        }
90    }
91}
Source

pub fn set_children<I, A>(&mut self, id: impl Into<AnyId>, children: I)
where I: IntoIterator<Item = A>, A: Into<AnyId>,

Give a container these children, in this order, in place of the ones that it has. A widget that was in another container moves out of it. A widget that is in no container does not show, and the app can put it in a container again later.

The texts of a card with a title, and the button and the title of a side bar, stay.

Source

pub fn card_text(&self, id: Id<Card>) -> (String, String)

The title and the body of a card. They are empty for a card without a text.

Source

pub fn set_card_text(&mut self, id: Id<Card>, title: &str, body: &str)

Change the title and the body of a card that was made with a title or a body. A card with an empty body shows only its title.

Source

pub fn set_tint(&mut self, id: Id<Card>, tint: Option<Tint>)

Give a card a color, or take its color away.

Source

pub fn set_pinned(&mut self, id: Id<Card>, pinned: bool)

Show or hide the pin in the corner of a card.

Source

pub fn set_side_mode(&mut self, id: Id<SideBar>, mode: SideMode)

Change how much of a side bar shows. It gives no WindowEvent::SideModeChanged.

Source

pub fn side_mode(&self, id: Id<SideBar>) -> SideMode

How much of a side bar shows.

Examples found in repository?
examples/sidebar.rs (line 80)
22fn main() -> glasskit::Result<()> {
23    let ui = Ui::new()?;
24    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
25    let mut window = Window::new(&ui, "Side bar", 560.0, 360.0)?;
26    window.set_min_size(360.0, 260.0);
27
28    let items = PAGES.map(|(name, icon)| window.add(NavItem::new(name).icon(icon)));
29    let pages = PAGES.map(|(name, _)| {
30        let title = window.add(Text::new(name).style(TextStyle::Title));
31        window.add(Column::new().padding(Insets::all(16.0)).child(title))
32    });
33    let gap = window.add(Spacer::fill());
34    let mode = match std::env::args().nth(1).as_deref() {
35        Some("icons") => SideMode::Icons,
36        Some("collapsed") => SideMode::Collapsed,
37        _ => SideMode::Expanded,
38    };
39    let side = window.add(
40        SideBar::new()
41            .title("glasskit")
42            .expanded_width(180.0)
43            .mode(mode)
44            .gap(4.0)
45            .child(items[0])
46            .child(items[1])
47            .child(gap)
48            .child(items[2]),
49    );
50    let stack = window.add(Stack::new().width(Length::Fill(1.0)).children(pages));
51    let root = window.add(
52        Row::new()
53            .padding(Insets::all(8.0))
54            .gap(8.0)
55            .align(Align::Stretch)
56            .child(side)
57            .child(stack),
58    );
59    window.set_root(root);
60    let show = |window: &mut Window, shown: usize| {
61        for (i, (item, page)) in items.into_iter().zip(pages).enumerate() {
62            window.set_selected(item, i == shown);
63            window.set_visible(page, i == shown);
64        }
65    };
66    show(&mut window, 0);
67    window.show();
68
69    loop {
70        match events.next(None)? {
71            Event::Window(id) if id == window.id() => {
72                while let Some(event) = window.next_event() {
73                    match event {
74                        WindowEvent::Clicked(id) => {
75                            if let Some(shown) = items.iter().position(|&item| id == item) {
76                                show(&mut window, shown);
77                            }
78                        }
79                        WindowEvent::SideModeChanged(id) if id == side => {
80                            println!("{:?}", window.side_mode(side));
81                        }
82                        WindowEvent::CloseRequested => return Ok(()),
83                        _ => {}
84                    }
85                }
86            }
87            Event::Quit => return Ok(()),
88            _ => {}
89        }
90    }
91}
Source

pub fn is_visible(&self, id: impl Into<AnyId>) -> bool

Whether set_visible shows the widget. A visible widget in a hidden container does not show.

Source

pub fn set_selected(&mut self, id: Id<NavItem>, selected: bool)

Mark a navigation item as the one whose page shows, or remove the mark.

Examples found in repository?
examples/sidebar.rs (line 62)
22fn main() -> glasskit::Result<()> {
23    let ui = Ui::new()?;
24    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
25    let mut window = Window::new(&ui, "Side bar", 560.0, 360.0)?;
26    window.set_min_size(360.0, 260.0);
27
28    let items = PAGES.map(|(name, icon)| window.add(NavItem::new(name).icon(icon)));
29    let pages = PAGES.map(|(name, _)| {
30        let title = window.add(Text::new(name).style(TextStyle::Title));
31        window.add(Column::new().padding(Insets::all(16.0)).child(title))
32    });
33    let gap = window.add(Spacer::fill());
34    let mode = match std::env::args().nth(1).as_deref() {
35        Some("icons") => SideMode::Icons,
36        Some("collapsed") => SideMode::Collapsed,
37        _ => SideMode::Expanded,
38    };
39    let side = window.add(
40        SideBar::new()
41            .title("glasskit")
42            .expanded_width(180.0)
43            .mode(mode)
44            .gap(4.0)
45            .child(items[0])
46            .child(items[1])
47            .child(gap)
48            .child(items[2]),
49    );
50    let stack = window.add(Stack::new().width(Length::Fill(1.0)).children(pages));
51    let root = window.add(
52        Row::new()
53            .padding(Insets::all(8.0))
54            .gap(8.0)
55            .align(Align::Stretch)
56            .child(side)
57            .child(stack),
58    );
59    window.set_root(root);
60    let show = |window: &mut Window, shown: usize| {
61        for (i, (item, page)) in items.into_iter().zip(pages).enumerate() {
62            window.set_selected(item, i == shown);
63            window.set_visible(page, i == shown);
64        }
65    };
66    show(&mut window, 0);
67    window.show();
68
69    loop {
70        match events.next(None)? {
71            Event::Window(id) if id == window.id() => {
72                while let Some(event) = window.next_event() {
73                    match event {
74                        WindowEvent::Clicked(id) => {
75                            if let Some(shown) = items.iter().position(|&item| id == item) {
76                                show(&mut window, shown);
77                            }
78                        }
79                        WindowEvent::SideModeChanged(id) if id == side => {
80                            println!("{:?}", window.side_mode(side));
81                        }
82                        WindowEvent::CloseRequested => return Ok(()),
83                        _ => {}
84                    }
85                }
86            }
87            Event::Quit => return Ok(()),
88            _ => {}
89        }
90    }
91}
Source

pub fn is_selected(&self, id: Id<NavItem>) -> bool

Whether a navigation item has the mark.

Source

pub fn focus(&mut self, id: impl Into<AnyId>)

Give the keyboard focus to a widget that can take it.

Source

pub fn set_icon(&self, icon: &Icon) -> Result<()>

Show this picture in the title bar, the taskbar, and Alt+Tab. Windows scales it to each place, so give a large one, such as 64 by 64 pixels.

Source

pub fn set_title(&self, title: &str)

Change the title.

Source

pub fn set_min_size(&self, width: f32, height: f32)

The smallest size of the client area that the user can make, in DIPs.

Examples found in repository?
examples/kanban.rs (line 27)
23fn main() -> glasskit::Result<()> {
24    let ui = Ui::new()?;
25    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
26    let mut window = Window::new(&ui, "Kanban", 820.0, 520.0)?;
27    window.set_min_size(560.0, 360.0);
28
29    let mut board = Board::new(&mut window, &["To do", "Doing", "Done"]);
30    for (lane, title, body) in CARDS {
31        board.add_card(&mut window, lane, CardData::new(title, body));
32    }
33    let pinned = CardData {
34        pinned: true,
35        ..CardData::new("Ask for the budget", "")
36    };
37    board.add_card(&mut window, 1, pinned);
38
39    let title = window.add(Text::new("Board").style(TextStyle::Title));
40    let gap = window.add(Spacer::fill());
41    let archived = window.add(Button::new("Show archived"));
42    let top = window.add(Row::new().child(title).child(gap).child(archived));
43    let root = window.add(
44        Column::new()
45            .padding(Insets::all(16.0))
46            .gap(12.0)
47            .child(top)
48            .child(board.widget()),
49    );
50    window.set_root(root);
51    window.show();
52
53    loop {
54        match events.next(None)? {
55            Event::Window(id) if id == window.id() => {
56                while let Some(event) = window.next_event() {
57                    match event {
58                        WindowEvent::CloseRequested => return Ok(()),
59                        WindowEvent::Clicked(id) if id == archived => {
60                            let show = !board.show_archived();
61                            board.set_show_archived(&mut window, show);
62                            let label = if show {
63                                "Hide archived"
64                            } else {
65                                "Show archived"
66                            };
67                            window.set_text(archived, label);
68                        }
69                        event => {
70                            if let Some(change) = board.handle(&ui, &mut window, &event)? {
71                                println!("{}", describe(&board, &change));
72                            }
73                        }
74                    }
75                }
76            }
77            Event::Quit => return Ok(()),
78            _ => {}
79        }
80    }
81}
More examples
Hide additional examples
examples/sidebar.rs (line 26)
22fn main() -> glasskit::Result<()> {
23    let ui = Ui::new()?;
24    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
25    let mut window = Window::new(&ui, "Side bar", 560.0, 360.0)?;
26    window.set_min_size(360.0, 260.0);
27
28    let items = PAGES.map(|(name, icon)| window.add(NavItem::new(name).icon(icon)));
29    let pages = PAGES.map(|(name, _)| {
30        let title = window.add(Text::new(name).style(TextStyle::Title));
31        window.add(Column::new().padding(Insets::all(16.0)).child(title))
32    });
33    let gap = window.add(Spacer::fill());
34    let mode = match std::env::args().nth(1).as_deref() {
35        Some("icons") => SideMode::Icons,
36        Some("collapsed") => SideMode::Collapsed,
37        _ => SideMode::Expanded,
38    };
39    let side = window.add(
40        SideBar::new()
41            .title("glasskit")
42            .expanded_width(180.0)
43            .mode(mode)
44            .gap(4.0)
45            .child(items[0])
46            .child(items[1])
47            .child(gap)
48            .child(items[2]),
49    );
50    let stack = window.add(Stack::new().width(Length::Fill(1.0)).children(pages));
51    let root = window.add(
52        Row::new()
53            .padding(Insets::all(8.0))
54            .gap(8.0)
55            .align(Align::Stretch)
56            .child(side)
57            .child(stack),
58    );
59    window.set_root(root);
60    let show = |window: &mut Window, shown: usize| {
61        for (i, (item, page)) in items.into_iter().zip(pages).enumerate() {
62            window.set_selected(item, i == shown);
63            window.set_visible(page, i == shown);
64        }
65    };
66    show(&mut window, 0);
67    window.show();
68
69    loop {
70        match events.next(None)? {
71            Event::Window(id) if id == window.id() => {
72                while let Some(event) = window.next_event() {
73                    match event {
74                        WindowEvent::Clicked(id) => {
75                            if let Some(shown) = items.iter().position(|&item| id == item) {
76                                show(&mut window, shown);
77                            }
78                        }
79                        WindowEvent::SideModeChanged(id) if id == side => {
80                            println!("{:?}", window.side_mode(side));
81                        }
82                        WindowEvent::CloseRequested => return Ok(()),
83                        _ => {}
84                    }
85                }
86            }
87            Event::Quit => return Ok(()),
88            _ => {}
89        }
90    }
91}
examples/window.rs (line 19)
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}
Source

pub fn size(&self) -> (f32, f32)

The size of the client area, in DIPs.

Source

pub fn set_material(&self, material: Material)

Change the background. If transparency effects are off, or high contrast is on, the window shows a solid color until they change.

Examples found in repository?
examples/window.rs (line 21)
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}
Source

pub fn material(&self) -> Material

The background that the app asked for.

Source

pub fn next_event(&mut self) -> Option<WindowEvent>

The next thing that the user did, or None when there is nothing more now.

Examples found in repository?
examples/kanban.rs (line 56)
23fn main() -> glasskit::Result<()> {
24    let ui = Ui::new()?;
25    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
26    let mut window = Window::new(&ui, "Kanban", 820.0, 520.0)?;
27    window.set_min_size(560.0, 360.0);
28
29    let mut board = Board::new(&mut window, &["To do", "Doing", "Done"]);
30    for (lane, title, body) in CARDS {
31        board.add_card(&mut window, lane, CardData::new(title, body));
32    }
33    let pinned = CardData {
34        pinned: true,
35        ..CardData::new("Ask for the budget", "")
36    };
37    board.add_card(&mut window, 1, pinned);
38
39    let title = window.add(Text::new("Board").style(TextStyle::Title));
40    let gap = window.add(Spacer::fill());
41    let archived = window.add(Button::new("Show archived"));
42    let top = window.add(Row::new().child(title).child(gap).child(archived));
43    let root = window.add(
44        Column::new()
45            .padding(Insets::all(16.0))
46            .gap(12.0)
47            .child(top)
48            .child(board.widget()),
49    );
50    window.set_root(root);
51    window.show();
52
53    loop {
54        match events.next(None)? {
55            Event::Window(id) if id == window.id() => {
56                while let Some(event) = window.next_event() {
57                    match event {
58                        WindowEvent::CloseRequested => return Ok(()),
59                        WindowEvent::Clicked(id) if id == archived => {
60                            let show = !board.show_archived();
61                            board.set_show_archived(&mut window, show);
62                            let label = if show {
63                                "Hide archived"
64                            } else {
65                                "Show archived"
66                            };
67                            window.set_text(archived, label);
68                        }
69                        event => {
70                            if let Some(change) = board.handle(&ui, &mut window, &event)? {
71                                println!("{}", describe(&board, &change));
72                            }
73                        }
74                    }
75                }
76            }
77            Event::Quit => return Ok(()),
78            _ => {}
79        }
80    }
81}
More examples
Hide additional examples
examples/sidebar.rs (line 72)
22fn main() -> glasskit::Result<()> {
23    let ui = Ui::new()?;
24    let mut events: EventLoop<()> = EventLoop::new(&ui)?;
25    let mut window = Window::new(&ui, "Side bar", 560.0, 360.0)?;
26    window.set_min_size(360.0, 260.0);
27
28    let items = PAGES.map(|(name, icon)| window.add(NavItem::new(name).icon(icon)));
29    let pages = PAGES.map(|(name, _)| {
30        let title = window.add(Text::new(name).style(TextStyle::Title));
31        window.add(Column::new().padding(Insets::all(16.0)).child(title))
32    });
33    let gap = window.add(Spacer::fill());
34    let mode = match std::env::args().nth(1).as_deref() {
35        Some("icons") => SideMode::Icons,
36        Some("collapsed") => SideMode::Collapsed,
37        _ => SideMode::Expanded,
38    };
39    let side = window.add(
40        SideBar::new()
41            .title("glasskit")
42            .expanded_width(180.0)
43            .mode(mode)
44            .gap(4.0)
45            .child(items[0])
46            .child(items[1])
47            .child(gap)
48            .child(items[2]),
49    );
50    let stack = window.add(Stack::new().width(Length::Fill(1.0)).children(pages));
51    let root = window.add(
52        Row::new()
53            .padding(Insets::all(8.0))
54            .gap(8.0)
55            .align(Align::Stretch)
56            .child(side)
57            .child(stack),
58    );
59    window.set_root(root);
60    let show = |window: &mut Window, shown: usize| {
61        for (i, (item, page)) in items.into_iter().zip(pages).enumerate() {
62            window.set_selected(item, i == shown);
63            window.set_visible(page, i == shown);
64        }
65    };
66    show(&mut window, 0);
67    window.show();
68
69    loop {
70        match events.next(None)? {
71            Event::Window(id) if id == window.id() => {
72                while let Some(event) = window.next_event() {
73                    match event {
74                        WindowEvent::Clicked(id) => {
75                            if let Some(shown) = items.iter().position(|&item| id == item) {
76                                show(&mut window, shown);
77                            }
78                        }
79                        WindowEvent::SideModeChanged(id) if id == side => {
80                            println!("{:?}", window.side_mode(side));
81                        }
82                        WindowEvent::CloseRequested => return Ok(()),
83                        _ => {}
84                    }
85                }
86            }
87            Event::Quit => return Ok(()),
88            _ => {}
89        }
90    }
91}
examples/window.rs (line 58)
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}

Trait Implementations§

Source§

impl Debug for Window

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Window

§

impl !Send for Window

§

impl !Sync for Window

§

impl !UnwindSafe for Window

§

impl Forget for Window

§

impl Freeze for Window

§

impl Move for Window

§

impl Unpin for Window

§

impl UnsafeUnpin for Window

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.