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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::{cell::RefCell, convert::TryInto, rc::Rc};
use wayland_client::{
protocol::{wl_output, wl_seat, wl_surface},
DispatchData,
};
use wayland_protocols::xdg_shell::client::{xdg_surface, xdg_toplevel, xdg_wm_base};
use super::{Event, ShellSurface};
pub(crate) struct Xdg {
surface: xdg_surface::XdgSurface,
toplevel: xdg_toplevel::XdgToplevel,
}
impl Xdg {
pub(crate) fn create<Impl>(
surface: &wl_surface::WlSurface,
shell: &xdg_wm_base::XdgWmBase,
implementation: Impl,
) -> Xdg
where
Impl: FnMut(Event, DispatchData) + 'static,
{
let pending_configure = Rc::new(RefCell::new(None));
let pending_configure_2 = pending_configure.clone();
let implementation = Rc::new(RefCell::new(implementation));
let implementation_2 = implementation.clone();
let xdgs = shell.get_xdg_surface(surface);
xdgs.quick_assign(move |xdgs, evt, ddata| match evt {
xdg_surface::Event::Configure { serial } => {
xdgs.ack_configure(serial);
if let Some((new_size, states)) = pending_configure_2.borrow_mut().take() {
(&mut *implementation_2.borrow_mut())(
Event::Configure { new_size, states },
ddata,
);
}
}
_ => unreachable!(),
});
let toplevel = xdgs.get_toplevel();
toplevel.quick_assign(move |_, evt, ddata| {
match evt {
xdg_toplevel::Event::Close => {
(&mut *implementation.borrow_mut())(Event::Close, ddata)
}
xdg_toplevel::Event::Configure { width, height, states } => {
use std::cmp::max;
let new_size = if width == 0 || height == 0 {
None
} else {
Some((max(width, 1) as u32, max(height, 1) as u32))
};
let translated_states = states
.chunks_exact(4)
.map(|c| u32::from_ne_bytes(c.try_into().unwrap()))
.flat_map(xdg_toplevel::State::from_raw)
.collect::<Vec<_>>();
*pending_configure.borrow_mut() = Some((new_size, translated_states));
}
_ => unreachable!(),
}
});
surface.commit();
Xdg { surface: xdgs.detach(), toplevel: toplevel.detach() }
}
}
impl ShellSurface for Xdg {
fn resize(&self, seat: &wl_seat::WlSeat, serial: u32, edges: xdg_toplevel::ResizeEdge) {
self.toplevel.resize(seat, serial, edges);
}
fn move_(&self, seat: &wl_seat::WlSeat, serial: u32) {
self.toplevel._move(seat, serial);
}
fn set_title(&self, title: String) {
self.toplevel.set_title(title);
}
fn set_app_id(&self, app_id: String) {
self.toplevel.set_app_id(app_id);
}
fn set_fullscreen(&self, output: Option<&wl_output::WlOutput>) {
self.toplevel.set_fullscreen(output)
}
fn unset_fullscreen(&self) {
self.toplevel.unset_fullscreen();
}
fn set_maximized(&self) {
self.toplevel.set_maximized();
}
fn unset_maximized(&self) {
self.toplevel.unset_maximized();
}
fn set_minimized(&self) {
self.toplevel.set_minimized();
}
fn show_window_menu(&self, seat: &wl_seat::WlSeat, serial: u32, x: i32, y: i32) {
self.toplevel.show_window_menu(seat, serial, x, y);
}
fn set_geometry(&self, x: i32, y: i32, width: i32, height: i32) {
self.surface.set_window_geometry(x, y, width, height);
}
fn set_min_size(&self, size: Option<(i32, i32)>) {
if let Some((w, h)) = size {
self.toplevel.set_min_size(w, h);
} else {
self.toplevel.set_min_size(0, 0);
}
}
fn set_max_size(&self, size: Option<(i32, i32)>) {
if let Some((w, h)) = size {
self.toplevel.set_max_size(w, h);
} else {
self.toplevel.set_max_size(0, 0);
}
}
fn get_xdg(&self) -> Option<&xdg_toplevel::XdgToplevel> {
Some(&self.toplevel)
}
}
impl Drop for Xdg {
fn drop(&mut self) {
self.toplevel.destroy();
self.surface.destroy();
}
}