-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
364 lines (298 loc) · 10.8 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//! # Cells
//!
//! 出典: [#Cells](https://eugenkiss.github.io/7guis/tasks#cells)
mod coord;
mod formula;
mod model;
use coord::*;
use gtk::{gdk, glib, prelude::*, ApplicationWindow, WindowPosition};
use model::TableData;
use std::{cell::RefCell, rc::Rc};
static STYLES: &[u8] = include_bytes!("styles.css");
struct Sheet {
inputs: GridArray<String>,
table: TableData,
editing_cell_opt: Option<Coord>,
}
impl Sheet {
fn new(size: Coord) -> Self {
Self {
inputs: GridArray::new(size),
table: TableData::new(size),
editing_cell_opt: None,
}
}
}
#[derive(Default)]
struct ViewState {
editor_rect_opt: Option<gtk::Rectangle>,
}
#[derive(Debug)]
enum Msg {
OnCellClick(Coord),
OnEditEnd { input: String },
}
fn nth_alphabet(n: usize) -> char {
(b'A' + n as u8) as char
}
fn build_ui(application: >k::Application) {
let row_count = 100;
let column_count = 26;
let size = Coord::from((row_count, column_count));
let view_state_ref: Rc<RefCell<ViewState>> = Rc::default();
let mut sheet = Sheet::new(size);
{
for y in 0..row_count {
for x in 0..column_count {
let text = format!("{},{}", nth_alphabet(x), y + 1);
let pos = Coord::from((y, x));
sheet.table.set(pos, &text);
sheet.inputs[pos] = text;
}
}
// 数式の記述例を初期状態に書き込んでおく:
for &(y, input) in &[
(1, "Examples:"),
(2, ";(add);20;+;22;->;=add(C2,E2)"),
(3, ";(sum);1;2;3;->;=sum(C3:E3)"),
] {
for (x, input) in input.split(';').enumerate() {
if input.is_empty() {
continue;
}
let p = Coord::new(y as u32, x as u32);
sheet.table.set(p, input);
sheet.inputs[p] = input.into();
}
}
sheet.table.update();
for p in Vec::from_iter(sheet.table.drain_changes()) {
let value = sheet.table.value_at(p);
sheet.inputs[p] = value.to_string();
}
}
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
// ## UIを組み立てる
// window {
// overlay {
// editor (overlay)
// scroll {
// grid
// }
// }
// }
let window = ApplicationWindow::new(application);
window.set_title("Cells");
window.set_default_size(640, 480);
window.set_position(WindowPosition::Center);
// UI要素を重ね合わせて表示するためのコンポーネント
let overlay = gtk::Overlay::default();
overlay.set_vexpand(true);
overlay.set_hexpand(true);
// セルの編集時に表示される入力欄
let editor = gtk::Entry::new();
editor.set_widget_name("cells-editor");
editor.set_visible(false);
editor.set_no_show_all(true);
let scroll = gtk::ScrolledWindow::default();
scroll.set_widget_name("cells-scrolled-window");
scroll.set_hexpand(true);
scroll.set_vexpand(true);
let grid = gtk::Grid::new();
grid.set_widget_name("cells-grid");
grid.set_hexpand(true);
grid.set_vexpand(true);
for i in 0..row_count + 1 {
grid.insert_row(i as i32);
}
for i in 0..column_count + 1 {
grid.insert_column(i as i32);
}
let _cross_header = {
let label = gtk::Label::new(None);
label.set_size_request(60, 25);
label.style_context().add_class("cross-header-cell");
grid.attach(&label, 0, 0, 1, 1);
label
};
let _row_headers = {
let mut labels = vec![];
for y in 0..row_count {
let label = gtk::Label::new(Some(&format!("{}", y)));
label.set_size_request(60, 25);
label.set_xalign(0.75);
label.style_context().add_class("row-header-cell");
grid.attach(&label, 0, (1 + y) as i32, 1, 1);
labels.push(label);
}
labels
};
let _column_headers = {
let mut labels = vec![];
for x in 0..column_count {
let label = gtk::Label::new(Some(&format!("{}", nth_alphabet(x))));
label.set_size_request(60, 25);
label.style_context().add_class("column-header-cell");
grid.attach(&label, (1 + x) as i32, 0, 1, 1);
labels.push(label);
}
labels
};
let mut data_cells = vec![vec![]; row_count];
{
for y in 0..row_count {
for x in 0..column_count {
let pos = Coord::from((y, x));
let label = gtk::Label::new(Some(&sheet.inputs[pos]));
label.set_size_request(60, 25);
label.style_context().add_class("cell-label");
if y == 0 {
label.style_context().add_class("first-row-cell");
}
if x == 0 {
label.style_context().add_class("first-column-cell");
}
let eb = gtk::EventBox::new();
eb.add(&label);
eb.connect_button_press_event({
let tx = tx.clone();
move |_, ev| {
if ev.button() == gdk::BUTTON_PRIMARY {
tx.send(Msg::OnCellClick(Coord::from((y, x)))).unwrap();
Inhibit(true)
} else {
Inhibit(false)
}
}
});
grid.attach(&eb, (1 + x) as i32, (1 + y) as i32, 1, 1);
data_cells[y].push((eb, label));
}
}
}
scroll.add(&grid);
overlay.add_overlay(&editor);
overlay.add(&scroll);
window.add(&overlay);
// ## UIのイベントにコールバックを接続する
// オーバーレイの外側のクリックでエディタを非表示にする
window.connect_button_press_event({
let editor = editor.clone();
let tx = tx.clone();
let view_state_ref = Rc::clone(&view_state_ref);
move |_, _| {
if gtk::prelude::EntryExt::is_visible(&editor) {
let input = editor.buffer().text();
view_state_ref.borrow_mut().editor_rect_opt = None;
editor.set_visible(false);
tx.send(Msg::OnEditEnd { input }).unwrap();
}
Inhibit(false)
}
});
// オーバーレイ上のマウス操作が下にあるグリッドに貫通 (pass-through) するように設定する
overlay.connect_realize(|overlay| {
overlay.window().unwrap().set_pass_through(true);
});
// オーバーレイ上のエディタの表示位置を絶対座標で決める
overlay.connect_get_child_position({
let view_state_ref = Rc::clone(&view_state_ref);
move |_, _| view_state_ref.borrow().editor_rect_opt
});
// editorのEscapeキーでエディタを非表示にする
editor.connect_key_press_event({
let tx = tx.clone();
let view_state_ref = Rc::clone(&view_state_ref);
move |editor, ev| {
let mut close = false;
if ev.keyval() == gdk::keys::constants::Return {
close = true;
let input = editor.buffer().text();
tx.send(Msg::OnEditEnd { input }).unwrap();
} else if ev.keyval() == gdk::keys::constants::Escape {
close = true;
let input = editor.buffer().text();
tx.send(Msg::OnEditEnd { input }).unwrap();
}
if close {
view_state_ref.borrow_mut().editor_rect_opt = None;
editor.set_visible(false);
}
Inhibit(false)
}
});
// ## イベントを処理する
rx.attach(None, {
// moves sheet
move |msg| {
eprintln!("msg {:?}", msg);
match msg {
Msg::OnCellClick(p) => {
let (y, x) = p.pair();
let (eb, _) = &data_cells[y][x];
let rect = eb.allocation();
eprintln!("click ({y}, {x}) ({}x{})", rect.width(), rect.height());
let p = 4; // margin, padding of grid
let offset_y = scroll.vadjustment().value() as i32;
let offset_x = scroll.hadjustment().value() as i32;
let size = gtk::Rectangle::new(
rect.x() - offset_x + p,
rect.y() - offset_y + p,
rect.width().max(100),
rect.height(),
);
// Update model:
let input;
{
let pos = Coord::from((y, x));
input = sheet.inputs[y][x].clone();
sheet.editing_cell_opt = Some(pos);
};
// Update view:
view_state_ref.borrow_mut().editor_rect_opt = Some(size);
editor.buffer().set_text(&input);
editor.set_visible(true);
editor.set_allocation(&size);
editor.set_has_focus(true);
overlay.queue_resize();
}
Msg::OnEditEnd { input } => {
if let Some(pos) = sheet.editing_cell_opt.take() {
// Update cell:
{
let (y, x) = pos.pair();
sheet.table.set(pos, &input);
sheet.inputs[y][x] = input;
}
// Propagate changes:
sheet.table.update();
for pos in Vec::from_iter(sheet.table.drain_changes()) {
let (y, x) = pos.pair();
let (_, label) = &data_cells[y][x];
label.set_text(&sheet.table.value_at(pos).to_string());
}
}
}
}
Continue(true)
}
});
// ## UIを初期化する
window.show_all();
}
fn main() {
let application = gtk::Application::new(Some("com.example.cells"), Default::default());
application.connect_activate(|app| {
let provider = gtk::CssProvider::new();
provider
.load_from_data(STYLES)
.expect("CssProvider::load_from_data");
gtk::StyleContext::add_provider_for_screen(
&gdk::Screen::default().expect("Screen::default"),
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
build_ui(app);
});
application.run();
}