Skip to content

Commit 82e4746

Browse files
committed
Update Calculator
1 parent 23fc5d1 commit 82e4746

1 file changed

Lines changed: 55 additions & 58 deletions

File tree

src/calculator.md

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
```rust
88
# extern crate kas;
9-
use kas::event::NamedKey;
10-
use kas::prelude::*;
11-
use kas::widgets::{AccessLabel, Adapt, Button, EditBox};
129
use std::num::ParseFloatError;
1310
use std::str::FromStr;
1411

12+
use kas::event::NamedKey;
13+
use kas::prelude::*;
14+
use kas::widgets::{AccessLabel, Adapt, Button, EditBox, column, grid};
15+
1516
type Key = kas::event::Key<kas::event::SmolStr>;
1617

1718
fn key_button(label: &str) -> Button<AccessLabel> {
@@ -23,15 +24,14 @@ fn key_button_with(label: &str, key: Key) -> Button<AccessLabel> {
2324
Button::label_msg(label, key.clone()).with_access_key(key)
2425
}
2526

26-
fn calc_ui() -> impl Widget<Data = ()> {
27+
fn calc_ui() -> Window<()> {
2728
// We could use kas::widget::Text, but EditBox looks better.
2829
let display = EditBox::string(|calc: &Calculator| calc.display())
2930
.with_multi_line(true)
30-
.with_lines(3, 3)
31+
.with_lines(3.0, 3.0)
3132
.with_width_em(5.0, 10.0);
3233

33-
// We use map_any to avoid passing input data (not wanted by buttons):
34-
let buttons = kas::grid! {
34+
let buttons = grid! {
3535
// Key bindings: C, Del
3636
(0, 0) => Button::label_msg("&clear", Key::Named(NamedKey::Clear))
3737
.with_access_key(NamedKey::Delete.into()),
@@ -57,22 +57,22 @@ fn calc_ui() -> impl Widget<Data = ()> {
5757
}
5858
.map_any();
5959

60-
Adapt::new(kas::column![display, buttons], Calculator::new())
61-
.on_message(|_, calc, key| calc.handle(key))
62-
.on_configure(|cx, _| {
63-
cx.disable_nav_focus(true);
64-
cx.enable_alt_bypass(true);
65-
})
60+
let ui = Adapt::new(column![display, buttons], Calculator::new())
61+
.on_message(|_, calc, key| calc.handle(key));
62+
63+
Window::new(ui, "Calculator")
64+
.escapable()
65+
.with_alt_bypass()
66+
.without_nav_focus()
6667
}
6768

68-
fn main() -> kas::app::Result<()> {
69+
fn main() -> kas::runner::Result<()> {
6970
env_logger::init();
7071

71-
let theme = kas_wgpu::ShadedTheme::new().with_font_size(16.0);
72-
kas::app::Default::with_theme(theme)
73-
.build(())?
74-
.with(Window::new(calc_ui(), "Calculator"))
75-
.run()
72+
let theme = kas_wgpu::ShadedTheme::new();
73+
let mut app = kas::runner::Runner::with_theme(theme).build(())?;
74+
let _ = app.config_mut().font.set_size(24.0);
75+
app.with(calc_ui()).run()
7676
}
7777

7878
#[derive(Clone, Debug)]
@@ -139,7 +139,7 @@ fn key_button_with(label: &str, key: Key) -> Button<AccessLabel> {
139139

140140
### Navigation focus and <kbd>Alt</kbd>-bypass
141141

142-
Normally, access keys are only active while holding <kbd>Alt</kbd>. To avoid this requirement we call [`enable_alt_bypass`]. Further, we disable <kbd>Tab</kbd> key navigation with [`disable_nav_focus`].
142+
Normally, access keys are only active while holding <kbd>Alt</kbd>. To avoid this requirement we call [`with_alt_bypass`]. Further, we disable <kbd>Tab</kbd> key navigation with [`without_nav_focus`] and ensure that the window can be closed with the <kbd>Esc</kbd> key.
143143
```rust
144144
# extern crate kas;
145145
# use kas::{Widget, widgets::{label_any, Adapt}};
@@ -150,21 +150,18 @@ Normally, access keys are only active while holding <kbd>Alt</kbd>. To avoid thi
150150
# fn handle(&mut self, _key: ()) {}
151151
# }
152152
# fn ui() -> impl Widget<Data = ()> {
153-
# let display = label_any("");
154-
# let buttons = label_any("");
155-
Adapt::new(kas::column![display, buttons], Calculator::new())
156-
.on_message(|_, calc, key| calc.handle(key))
157-
.on_configure(|cx, _| {
158-
cx.disable_nav_focus(true);
159-
cx.enable_alt_bypass(true);
160-
})
153+
# let ui = label_any("");
154+
Window::new(ui, "Calculator")
155+
.escapable()
156+
.with_alt_bypass()
157+
.without_nav_focus()
161158
# }
162159
```
163160

164161

165162
## Grid layout
166163

167-
We already saw column and row layouts. This time, we'll use [`kas::grid!`] for layout.
164+
We already saw column and row layouts. This time, we'll use [`grid!`] for layout.
168165
```rust
169166
# extern crate kas;
170167
# use kas::event::NamedKey;
@@ -180,36 +177,36 @@ We already saw column and row layouts. This time, we'll use [`kas::grid!`] for l
180177
# Button::label_msg(label, key.clone()).with_access_key(key)
181178
# }
182179
# fn ui() -> impl Widget<Data = i32> {
183-
let buttons = kas::grid! {
184-
// Key bindings: C, Del
185-
(0, 0) => Button::label_msg("&clear", Key::Named(NamedKey::Clear))
186-
.with_access_key(NamedKey::Delete.into()),
187-
// Widget is hidden but has key binding.
188-
// TODO(opt): exclude from layout & drawing.
189-
(0, 0) => key_button_with("", NamedKey::Backspace.into()),
190-
(1, 0) => key_button_with("", Key::Character("/".into())),
191-
(2, 0) => key_button_with("", Key::Character("*".into())),
192-
(3, 0) => key_button_with("&−", Key::Character("-".into())),
193-
(0, 1) => key_button("&7"),
194-
(1, 1) => key_button("&8"),
195-
(2, 1) => key_button("&9"),
196-
(3, 1..3) => key_button("&+"),
197-
(0, 2) => key_button("&4"),
198-
(1, 2) => key_button("&5"),
199-
(2, 2) => key_button("&6"),
200-
(0, 3) => key_button("&1"),
201-
(1, 3) => key_button("&2"),
202-
(2, 3) => key_button("&3"),
203-
(3, 3..5) => key_button_with("&=", NamedKey::Enter.into()),
204-
(0..2, 4) => key_button("&0"),
205-
(2, 4) => key_button("&."),
206-
}
207-
.map_any();
180+
let buttons = grid! {
181+
// Key bindings: C, Del
182+
(0, 0) => Button::label_msg("&clear", Key::Named(NamedKey::Clear))
183+
.with_access_key(NamedKey::Delete.into()),
184+
// Widget is hidden but has key binding.
185+
// TODO(opt): exclude from layout & drawing.
186+
(0, 0) => key_button_with("", NamedKey::Backspace.into()),
187+
(1, 0) => key_button_with("", Key::Character("/".into())),
188+
(2, 0) => key_button_with("", Key::Character("*".into())),
189+
(3, 0) => key_button_with("&−", Key::Character("-".into())),
190+
(0, 1) => key_button("&7"),
191+
(1, 1) => key_button("&8"),
192+
(2, 1) => key_button("&9"),
193+
(3, 1..3) => key_button("&+"),
194+
(0, 2) => key_button("&4"),
195+
(1, 2) => key_button("&5"),
196+
(2, 2) => key_button("&6"),
197+
(0, 3) => key_button("&1"),
198+
(1, 3) => key_button("&2"),
199+
(2, 3) => key_button("&3"),
200+
(3, 3..5) => key_button_with("&=", NamedKey::Enter.into()),
201+
(0..2, 4) => key_button("&0"),
202+
(2, 4) => key_button("&."),
203+
}
204+
.map_any();
208205
# buttons
209206
# }
210207
```
211208

212-
Worth noting is our hidden `Backspace` button. This is just another cell, but hidden under the `clear` button. Yes, this is a sub-optimal hack (the widget is still sized and drawn); it works but might see a less hacky solution in the future.
209+
Worth noting is our hidden `Backspace` button. This is just another cell, but hidden under the `clear` button. Yes, this is a sub-optimal hack.
213210

214211
Again, we use <code>.[map_any][]()</code> to make our buttons (input `Data = ()`) compatible with the parent UI element (input `Data = Calculator`).
215212

@@ -220,7 +217,7 @@ Again, we use <code>.[map_any][]()</code> to make our buttons (input `Data = ()`
220217
[`Button`]: https://docs.rs/kas/latest/kas/widgets/struct.Button.html
221218
[`Button::with_access_key`]: https://docs.rs/kas/latest/kas/widgets/struct.Button.html#method.with_access_key
222219
[`Activate`]: https://docs.rs/kas/latest/kas/messages/struct.Activate.html
223-
[`disable_nav_focus`]: https://docs.rs/kas/latest/kas/event/struct.ConfigCx.html#method.disable_nav_focus
224-
[`enable_alt_bypass`]: https://docs.rs/kas/latest/kas/event/struct.EventState.html#method.enable_alt_bypass
225-
[`kas::grid!`]: https://docs.rs/kas/latest/kas/macro.grid.html
220+
[`without_nav_focus`]: https://docs.rs/kas/latest/kas/window/struct.Window.html#method.without_nav_focus
221+
[`with_alt_bypass`]: https://docs.rs/kas/latest/kas/window/struct.Window.html#method.with_alt_bypass
222+
[`grid!`]: https://docs.rs/kas/latest/kas/widgets/macro.grid.html
226223
[map_any]: https://docs.rs/kas/latest/kas/widgets/trait.AdaptWidgetAny.html#method.map_any

0 commit comments

Comments
 (0)