-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcanvas.rs
More file actions
115 lines (99 loc) · 2.73 KB
/
canvas.rs
File metadata and controls
115 lines (99 loc) · 2.73 KB
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
use crate::{
BorrowedWindow, Component, ComponentSender, DrawingContext, Enable, Layoutable, MouseButton,
Point, Size, Visible, ui,
};
/// A simple drawing canvas.
#[derive(Debug)]
pub struct Canvas {
widget: ui::Canvas,
}
impl Canvas {
/// Create the [`DrawingContext`] of the current canvas.
pub fn context(&mut self) -> DrawingContext<'_> {
DrawingContext::new(self.widget.context())
}
}
impl Visible for Canvas {
fn is_visible(&self) -> bool {
self.widget.is_visible()
}
fn set_visible(&mut self, v: bool) {
self.widget.set_visible(v);
}
}
impl Enable for Canvas {
fn is_enabled(&self) -> bool {
self.widget.is_enabled()
}
fn set_enabled(&mut self, v: bool) {
self.widget.set_enabled(v);
}
}
impl Layoutable for Canvas {
fn loc(&self) -> Point {
self.widget.loc()
}
fn set_loc(&mut self, p: Point) {
self.widget.set_loc(p)
}
fn size(&self) -> Size {
self.widget.size()
}
fn set_size(&mut self, v: Size) {
self.widget.set_size(v)
}
}
/// Events of [`Canvas`].
#[non_exhaustive]
pub enum CanvasEvent {
/// The canvas needs redraw.
Redraw,
/// The mouse moves.
MouseMove(Point),
/// The mouse button pressed down.
MouseDown(MouseButton),
/// The mouse button released.
MouseUp(MouseButton),
}
impl Component for Canvas {
type Event = CanvasEvent;
type Init<'a> = BorrowedWindow<'a>;
type Message = ();
fn init(init: Self::Init<'_>, _sender: &ComponentSender<Self>) -> Self {
let widget = ui::Canvas::new(init);
Self { widget }
}
async fn start(&mut self, sender: &ComponentSender<Self>) -> ! {
let fut_redraw = async {
loop {
self.widget.wait_redraw().await;
sender.output(CanvasEvent::Redraw);
}
};
let fut_move = async {
loop {
let p = self.widget.wait_mouse_move().await;
sender.output(CanvasEvent::MouseMove(p));
}
};
let fut_down = async {
loop {
let b = self.widget.wait_mouse_down().await;
sender.output(CanvasEvent::MouseDown(b));
}
};
let fut_up = async {
loop {
let b = self.widget.wait_mouse_up().await;
sender.output(CanvasEvent::MouseUp(b));
}
};
futures_util::future::join4(fut_redraw, fut_move, fut_down, fut_up)
.await
.0
}
async fn update(&mut self, _message: Self::Message, _sender: &ComponentSender<Self>) -> bool {
false
}
fn render(&mut self, _sender: &ComponentSender<Self>) {}
}