-
Notifications
You must be signed in to change notification settings - Fork 25
/
times-tables.rs
82 lines (71 loc) · 2.3 KB
/
times-tables.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Do you know your times tables?
use kas::prelude::*;
use kas::view::{driver, MatrixData, MatrixView, SelectionMode, SelectionMsg, SharedData};
use kas::widgets::{column, row, Adapt, EditBox, ScrollBars};
#[derive(Debug)]
struct TableSize(usize);
impl SharedData for TableSize {
type Key = (usize, usize);
type Item = usize;
type ItemRef<'b> = usize;
fn contains_key(&self, key: &Self::Key) -> bool {
key.0 < self.0 && key.1 < self.0
}
fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>> {
self.contains_key(key).then_some((key.0 + 1) * (key.1 + 1))
}
}
impl MatrixData for TableSize {
type ColKey = usize;
type RowKey = usize;
fn is_empty(&self) -> bool {
self.0 == 0
}
fn len(&self) -> (usize, usize) {
(self.0, self.0)
}
#[allow(refining_impl_trait)]
fn col_iter_from(&self, start: usize, limit: usize) -> std::ops::Range<usize> {
let end = self.0.min(start + limit);
start..end
}
#[allow(refining_impl_trait)]
fn row_iter_from(&self, start: usize, limit: usize) -> std::ops::Range<usize> {
let end = self.0.min(start + limit);
start..end
}
fn make_key(&self, col: &Self::ColKey, row: &Self::RowKey) -> Self::Key {
(*col, *row)
}
}
fn main() -> kas::app::Result<()> {
env_logger::init();
let table = MatrixView::new(driver::NavView)
.with_num_visible(12, 12)
.with_selection_mode(SelectionMode::Single);
let table = ScrollBars::new(table);
#[derive(Debug)]
struct SetLen(usize);
let ui = column![
row![
"From 1 to",
EditBox::parser(|data: &TableSize| data.0, SetLen)
],
table.align(AlignHints::RIGHT),
];
let ui = Adapt::new(ui, TableSize(12))
.on_message(|_, data, SetLen(len)| data.0 = len)
.on_message(|_, _, selection| match selection {
SelectionMsg::<(usize, usize)>::Select((col, row)) => {
let (c, r) = (col + 1, row + 1);
println!("{} × {} = {}", c, r, c * r);
}
_ => (),
});
let window = Window::new(ui, "Times-Tables");
let theme = kas::theme::SimpleTheme::new();
kas::app::Default::with_theme(theme)
.build(())?
.with(window)
.run()
}