-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbutton.rs
More file actions
88 lines (72 loc) · 1.9 KB
/
button.rs
File metadata and controls
88 lines (72 loc) · 1.9 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
use std::rc::Rc;
use gtk4::{glib::object::Cast, prelude::ButtonExt};
use crate::{
AsWindow, Point, Size,
ui::{Callback, Widget},
};
#[derive(Debug)]
pub struct Button {
on_click: Rc<Callback<()>>,
widget: gtk4::Button,
handle: Widget,
}
impl Button {
pub fn new(parent: impl AsWindow) -> Self {
let widget = gtk4::Button::new();
let handle = Widget::new(parent, unsafe { widget.clone().unsafe_cast() });
let on_click = Rc::new(Callback::new());
widget.connect_clicked({
let on_click = Rc::downgrade(&on_click);
move |_| {
if let Some(on_click) = on_click.upgrade() {
on_click.signal(());
}
}
});
Self {
on_click,
widget,
handle,
}
}
pub fn is_visible(&self) -> bool {
self.handle.is_visible()
}
pub fn set_visible(&mut self, v: bool) {
self.handle.set_visible(v);
}
pub fn is_enabled(&self) -> bool {
self.handle.is_enabled()
}
pub fn set_enabled(&mut self, v: bool) {
self.handle.set_enabled(v);
}
pub fn preferred_size(&self) -> Size {
self.handle.preferred_size()
}
pub fn loc(&self) -> Point {
self.handle.loc()
}
pub fn set_loc(&mut self, p: Point) {
self.handle.set_loc(p);
}
pub fn size(&self) -> Size {
self.handle.size()
}
pub fn set_size(&mut self, s: Size) {
self.handle.set_size(s);
}
pub fn text(&self) -> String {
self.widget
.label()
.map(|s| s.to_string())
.unwrap_or_default()
}
pub fn set_text(&mut self, s: impl AsRef<str>) {
self.widget.set_label(s.as_ref());
self.handle.reset_preferred_size();
}
pub async fn wait_click(&self) {
self.on_click.wait().await
}
}