forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircledraw.rs
153 lines (129 loc) · 4.58 KB
/
circledraw.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
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: MIT
use slint::Model;
use slint::VecModel;
use std::cell::RefCell;
use std::rc::Rc;
slint::slint!(import { MainWindow } from "circledraw.slint";);
enum Change {
CircleAdded { row: usize },
CircleRemoved { row: usize, circle: Circle },
CircleResized { row: usize, old_d: f32 },
}
struct UndoStack<F> {
stack: Vec<Option<Change>>,
// Everything at and after this is a redo action
redo_offset: usize,
undo2redo: F,
}
impl<F> UndoStack<F>
where
F: Fn(Change) -> Change,
{
fn new(undo2redo: F) -> Self {
Self { stack: Vec::new(), redo_offset: 0, undo2redo }
}
fn push(&mut self, change: Change) {
self.stack.truncate(self.redo_offset);
self.stack.push(Some(change));
self.redo_offset += 1;
}
fn undoable(&self) -> bool {
self.redo_offset > 0
}
fn redoable(&self) -> bool {
self.redo_offset < self.stack.len()
}
fn undo(&mut self) {
self.redo_offset -= 1;
let undo = self.stack.get_mut(self.redo_offset).unwrap().take().unwrap();
let redo = (self.undo2redo)(undo);
self.stack[self.redo_offset] = Some(redo);
}
fn redo(&mut self) {
let redo = self.stack.get_mut(self.redo_offset).unwrap().take().unwrap();
let undo = (self.undo2redo)(redo);
self.stack[self.redo_offset] = Some(undo);
self.redo_offset += 1;
}
}
pub fn main() {
let main_window = MainWindow::new().unwrap();
let model = Rc::new(VecModel::default());
main_window.set_model(model.clone().into());
let undo_stack;
{
let model = model.clone();
undo_stack = Rc::new(RefCell::new(UndoStack::new(move |change| match change {
Change::CircleAdded { row } => {
let circle = model.row_data(row).unwrap();
model.remove(row);
Change::CircleRemoved { row, circle }
}
Change::CircleRemoved { row, circle } => {
model.insert(row, circle);
Change::CircleAdded { row }
}
Change::CircleResized { row, old_d } => {
let mut circle = model.row_data(row).unwrap();
let d = circle.d;
circle.d = old_d;
model.set_row_data(row, circle);
Change::CircleResized { row, old_d: d }
}
})));
}
{
let model = model.clone();
let undo_stack = undo_stack.clone();
let window_weak = main_window.as_weak();
main_window.on_background_clicked(move |x, y| {
let mut undo_stack = undo_stack.borrow_mut();
let main_window = window_weak.unwrap();
model.push(Circle { x: x as f32, y: y as f32, d: 30.0 });
undo_stack.push(Change::CircleAdded { row: model.row_count() - 1 });
main_window.set_undoable(undo_stack.undoable());
main_window.set_redoable(undo_stack.redoable());
});
}
{
let undo_stack = undo_stack.clone();
let window_weak = main_window.as_weak();
main_window.on_undo_clicked(move || {
let mut undo_stack = undo_stack.borrow_mut();
let main_window = window_weak.unwrap();
undo_stack.undo();
main_window.set_undoable(undo_stack.undoable());
main_window.set_redoable(undo_stack.redoable());
});
}
{
let undo_stack = undo_stack.clone();
let window_weak = main_window.as_weak();
main_window.on_redo_clicked(move || {
let mut undo_stack = undo_stack.borrow_mut();
let main_window = window_weak.unwrap();
undo_stack.redo();
main_window.set_undoable(undo_stack.undoable());
main_window.set_redoable(undo_stack.redoable());
});
}
{
let model = model.clone();
let undo_stack = undo_stack.clone();
let window_weak = main_window.as_weak();
main_window.on_circle_resized(move |row, diameter| {
let row = row as usize;
let mut undo_stack = undo_stack.borrow_mut();
let main_window = window_weak.unwrap();
let mut circle = model.row_data(row).unwrap();
let old_d = circle.d;
circle.d = diameter;
model.set_row_data(row, circle);
undo_stack.push(Change::CircleResized { row, old_d });
main_window.set_undoable(undo_stack.undoable());
main_window.set_redoable(undo_stack.redoable());
});
}
main_window.run().unwrap();
}