-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbutton.rs
More file actions
94 lines (76 loc) · 1.89 KB
/
button.rs
File metadata and controls
94 lines (76 loc) · 1.89 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
use crate::{
BorrowedWindow, Component, ComponentSender, Enable, Layoutable, Point, Size, Visible, ui,
};
/// A simple button.
#[derive(Debug)]
pub struct Button {
widget: ui::Button,
}
impl Button {
/// The text.
pub fn text(&self) -> String {
self.widget.text()
}
/// Set the text.
pub fn set_text(&mut self, s: impl AsRef<str>) {
self.widget.set_text(s)
}
}
impl Visible for Button {
fn is_visible(&self) -> bool {
self.widget.is_visible()
}
fn set_visible(&mut self, v: bool) {
self.widget.set_visible(v);
}
}
impl Enable for Button {
fn is_enabled(&self) -> bool {
self.widget.is_enabled()
}
fn set_enabled(&mut self, v: bool) {
self.widget.set_enabled(v);
}
}
impl Layoutable for Button {
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)
}
fn preferred_size(&self) -> Size {
self.widget.preferred_size()
}
}
/// Events of [`Button`].
#[non_exhaustive]
pub enum ButtonEvent {
/// The button has been clicked.
Click,
}
impl Component for Button {
type Event = ButtonEvent;
type Init<'a> = BorrowedWindow<'a>;
type Message = ();
fn init(init: Self::Init<'_>, _sender: &ComponentSender<Self>) -> Self {
let widget = ui::Button::new(init);
Self { widget }
}
async fn start(&mut self, sender: &ComponentSender<Self>) {
loop {
self.widget.wait_click().await;
sender.output(ButtonEvent::Click);
}
}
async fn update(&mut self, _message: Self::Message, _sender: &ComponentSender<Self>) -> bool {
false
}
fn render(&mut self, _sender: &ComponentSender<Self>) {}
}