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
use std::any::Any;
use std::time::Instant;
use druid_shell::kurbo::{Point, Rect, Size};
use druid_shell::piet::{Color, Piet, RenderContext};
use druid_shell::{Application, Region, WinHandler, WindowBuilder, WindowHandle};
struct InvalidateTest {
handle: WindowHandle,
size: Size,
start_time: Instant,
color: Color,
rect: Rect,
cursor: Rect,
}
impl InvalidateTest {
fn update_color_and_rect(&mut self) {
let time_since_start = (Instant::now() - self.start_time).as_millis();
let (r, g, b, _) = self.color.as_rgba8();
self.color = match (time_since_start % 2, time_since_start % 3) {
(0, _) => Color::rgb8(r.wrapping_add(10), g, b),
(_, 0) => Color::rgb8(r, g.wrapping_add(10), b),
(_, _) => Color::rgb8(r, g, b.wrapping_add(10)),
};
self.rect.x0 = (self.rect.x0 + 5.0) % self.size.width;
self.rect.x1 = (self.rect.x1 + 5.5) % self.size.width;
self.rect.y0 = (self.rect.y0 + 3.0) % self.size.height;
self.rect.y1 = (self.rect.y1 + 3.5) % self.size.height;
self.handle.invalidate_rect(self.cursor);
self.cursor.x0 += 4.0;
self.cursor.x1 += 4.0;
if self.cursor.x0 > self.size.width {
self.cursor.x1 = self.cursor.width();
self.cursor.x0 = 0.0;
}
self.handle.invalidate_rect(self.cursor);
}
}
impl WinHandler for InvalidateTest {
fn connect(&mut self, handle: &WindowHandle) {
self.handle = handle.clone();
}
fn prepare_paint(&mut self) {
self.update_color_and_rect();
self.handle.invalidate_rect(self.rect);
}
fn paint(&mut self, piet: &mut Piet, region: &Region) {
piet.fill(region.bounding_box(), &self.color);
piet.fill(self.cursor, &Color::WHITE);
self.handle.request_anim_frame();
}
fn size(&mut self, size: Size) {
self.size = size;
}
fn command(&mut self, id: u32) {
match id {
0x100 => self.handle.close(),
_ => println!("unexpected id {}", id),
}
}
fn request_close(&mut self) {
self.handle.close();
}
fn destroy(&mut self) {
Application::global().quit()
}
fn as_any(&mut self) -> &mut dyn Any {
self
}
}
fn main() {
tracing_subscriber::fmt().init();
let app = Application::new().unwrap();
let mut builder = WindowBuilder::new(app.clone());
let inv_test = InvalidateTest {
size: Size::ZERO,
handle: Default::default(),
start_time: Instant::now(),
rect: Rect::from_origin_size(Point::ZERO, (10.0, 20.0)),
cursor: Rect::from_origin_size(Point::ZERO, (2.0, 100.0)),
color: Color::WHITE,
};
builder.set_handler(Box::new(inv_test));
builder.set_title("Invalidate tester");
let window = builder.build().unwrap();
window.show();
app.run(None);
}