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
impl Window
Sourcepub fn new(ui: &Ui, title: &str, width: f32, height: f32) -> Result<Self>
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?
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
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}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}Sourcepub fn id(&self) -> WindowId
pub fn id(&self) -> WindowId
The id that Event::Window gives for this window.
Examples found in repository?
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
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}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}Sourcepub fn show(&self)
pub fn show(&self)
Show the window and bring it to the front.
Examples found in repository?
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
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}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}Sourcepub fn add<K>(&mut self, element: impl Into<Element<K>>) -> Id<K>
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?
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
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}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}Sourcepub fn set_root(&mut self, root: impl Into<AnyId>)
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?
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
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}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}Sourcepub fn text<K: HasText>(&self, id: Id<K>) -> String
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?
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}Sourcepub fn set_text<K: HasText>(&mut self, id: Id<K>, value: &str)
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?
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
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}Sourcepub fn set_enabled(&mut self, id: impl Into<AnyId>, enabled: bool)
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.
Sourcepub fn set_visible(&mut self, id: impl Into<AnyId>, visible: bool)
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?
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}Sourcepub fn set_children<I, A>(&mut self, id: impl Into<AnyId>, children: I)
pub fn set_children<I, A>(&mut self, id: impl Into<AnyId>, children: I)
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.
Sourcepub fn card_text(&self, id: Id<Card>) -> (String, String)
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.
Sourcepub fn set_card_text(&mut self, id: Id<Card>, title: &str, body: &str)
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.
Sourcepub fn set_tint(&mut self, id: Id<Card>, tint: Option<Tint>)
pub fn set_tint(&mut self, id: Id<Card>, tint: Option<Tint>)
Give a card a color, or take its color away.
Sourcepub fn set_pinned(&mut self, id: Id<Card>, pinned: bool)
pub fn set_pinned(&mut self, id: Id<Card>, pinned: bool)
Show or hide the pin in the corner of a card.
Sourcepub fn set_side_mode(&mut self, id: Id<SideBar>, mode: SideMode)
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.
Sourcepub fn side_mode(&self, id: Id<SideBar>) -> SideMode
pub fn side_mode(&self, id: Id<SideBar>) -> SideMode
How much of a side bar shows.
Examples found in repository?
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}Sourcepub fn is_visible(&self, id: impl Into<AnyId>) -> bool
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.
Sourcepub fn set_selected(&mut self, id: Id<NavItem>, selected: bool)
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?
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}Sourcepub fn is_selected(&self, id: Id<NavItem>) -> bool
pub fn is_selected(&self, id: Id<NavItem>) -> bool
Whether a navigation item has the mark.
Sourcepub fn focus(&mut self, id: impl Into<AnyId>)
pub fn focus(&mut self, id: impl Into<AnyId>)
Give the keyboard focus to a widget that can take it.
Sourcepub fn set_icon(&self, icon: &Icon) -> Result<()>
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.
Sourcepub fn set_min_size(&self, width: f32, height: f32)
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?
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
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}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}Sourcepub fn set_material(&self, material: Material)
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?
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}Sourcepub fn next_event(&mut self) -> Option<WindowEvent>
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?
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
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}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}