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
//! Top-level windows.
use super::*;
impl Window {
/// A hidden top-level window; add widgets to it and call [`Window::show`].
pub fn new(title: &str) -> Window {
Window::from_id(core::create(Kind::Window, None, |n| {
n.text = title.to_string()
}))
}
/// Change the title.
pub fn set_title(&self, t: &str) {
set_text(self.id(), t, false)
}
/// The title.
pub fn title(&self) -> String {
text_of(self.id())
}
/// Client-area size in logical pixels. Without this call the window sizes itself to its content.
pub fn set_size(&self, w: i32, h: i32) {
if core::update(self.id(), true, |n| {
if let Some(win) = n.window_mut() {
win.client = Size::new(w.clamp(1, MAX_WINDOW_PX), h.clamp(1, MAX_WINDOW_PX));
win.explicit_size = true;
}
})
.is_some()
{
core::layout_window(self.id());
}
}
/// The client-area size: the last size set or reported by the user, else the size layout chose.
pub fn size(&self) -> (i32, i32) {
core::read(self.id(), |n| n.window().map(|w| (w.client.w, w.client.h)))
.flatten()
.unwrap_or((0, 0))
}
/// Allow or forbid resizing by the user.
pub fn set_resizable(&self, v: bool) {
core::set(
self.id(),
false,
|n| {
if let Some(w) = n.window_mut() {
w.resizable = v
}
},
Prop::Resizable(v),
);
}
/// Show the window (and lay it out).
pub fn show(&self) {
self.set_visible(true)
}
/// Hide the window.
pub fn hide(&self) {
self.set_visible(false)
}
/// Close handler: return `true` to allow closing (default), `false` to veto.
pub fn on_close(&self, f: impl FnMut() -> bool + 'static) {
core::modify(self.id(), |n| {
if let Some(w) = n.window_mut() {
w.on_close = Some(Box::new(f))
}
});
}
/// Called with the new client size when the user resizes the window.
pub fn on_resize(&self, mut f: impl FnMut(i32, i32) + 'static) {
on(self.id(), Ev::Resized, move |e| {
if let Event::Resized { w, h } = e {
f(*w, *h)
}
});
}
/// Move the window's outer frame to screen position (x, y) in logical pixels. Some platforms
/// (Wayland, some window managers) ignore this.
pub fn set_position(&self, x: i32, y: i32) {
core::set(
self.id(),
false,
|n| {
if let Some(w) = n.window_mut() {
w.position = Some((x, y))
}
},
Prop::Position { x, y },
);
}
/// The last position set with [`Window::set_position`] or reported by the platform after the
/// user moved the window; `None` if neither happened (the window manager placed it).
pub fn position(&self) -> Option<(i32, i32)> {
core::read(self.id(), |n| n.window().and_then(|w| w.position)).flatten()
}
/// Called with the new screen position when the user moves the window (not on every
/// platform; see [`Window::position`]).
pub fn on_move(&self, mut f: impl FnMut(i32, i32) + 'static) {
on(self.id(), Ev::Moved, move |e| {
if let Event::Moved { x, y } = e {
f(*x, *y)
}
});
}
/// Close now (no `on_close` check).
pub fn close(&self) {
self.destroy()
}
}