-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.rs
205 lines (182 loc) · 6.42 KB
/
mouse.rs
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use penrose::{
core::{
bindings::{MouseBindings, MouseButton, MouseEvent, MouseEventHandler},
ClientSet, State, WindowManager,
},
custom_error,
pure::geometry::{Point, Rect},
x::{XConn, XConnExt, XEvent},
Xid,
};
#[derive(Debug, Default, Clone)]
pub struct MouseHandler {
current_point: Point,
data: Option<ClickData>,
}
#[derive(Debug, Clone)]
pub struct ClickData {
start_point: Point,
start_rect: Rect,
xid: Xid,
button: MouseButton,
}
impl Default for ClickData {
fn default() -> Self {
Self {
start_point: Point::default(),
start_rect: Rect::default(),
xid: Xid::default(),
button: MouseButton::Left,
}
}
}
impl MouseHandler {
pub fn new() -> Self {
Self {
data: None,
..Default::default()
}
}
pub fn install_mouse_handler<X: XConn + 'static>(mut wm: WindowManager<X>) -> WindowManager<X> {
wm.state.add_extension(Self::new());
wm.state.config.compose_or_set_event_hook(Self::event_hook);
wm
}
pub fn event_hook<X: XConn>(e: &XEvent, s: &mut State<X>, _x: &X) -> penrose::Result<bool> {
match e {
XEvent::MouseEvent(e) => {
let handler = s.extension::<Self>()?;
handler.borrow_mut().current_point = e.rpt;
}
_ => {}
}
Ok(true)
}
pub fn mouse_bindings<X>() -> MouseBindings<X>
where
X: XConn,
{
gen_mousebindings!(
// This is forced to be ScrollDown due to https://github.com/sminez/penrose/issues/113
Motion ScrollDown + [Meta] => MouseHandler::drag(),
Press Left + [Meta] => MouseHandler::start_drag(),
Release Left + [Meta] => MouseHandler::stop_drag(),
Press Right + [Meta] => MouseHandler::start_drag(),
Release Right + [Meta] => MouseHandler::stop_drag()
)
}
pub fn current_mouse_position(&self) -> Point {
self.current_point
}
fn start_drag<X: XConn>() -> Box<dyn MouseEventHandler<X>> {
Box::new(
move |e: &MouseEvent, s: &mut State<X>, x: &X| -> penrose::Result<()> {
let cs = &mut s.client_set;
let stack = cs.current_stack();
let Some(stack) = stack else {
return Ok(());
};
let xid = *stack.focused();
let client_rect = x.client_geometry(xid)?;
// Keep the internal representation of the client in sync with the X server
cs.float(xid, client_rect)?;
let handler = s.extension::<Self>()?;
let mut handler = handler.borrow_mut();
if handler.data.is_none() {
handler.data = Some(ClickData {
start_point: e.rpt,
start_rect: client_rect,
xid,
button: e.state.button,
});
} else {
return Err(custom_error!("already dragging"));
}
// Don't call `x.refresh()` because it re-focuses the mouse
Ok(())
},
)
}
fn stop_drag<X: XConn>() -> Box<dyn MouseEventHandler<X>> {
Box::new(
move |e: &MouseEvent, s: &mut State<X>, _x: &X| -> penrose::Result<()> {
let handler = s.extension::<Self>()?;
let mut handler = handler.borrow_mut();
if handler.data.is_none() {
return Err(custom_error!("no drag in progress"));
};
assert!(handler.data.as_ref().unwrap().button == e.state.button);
handler.data = None;
Ok(())
},
)
}
fn drag<X: XConn>() -> Box<dyn MouseEventHandler<X>> {
Box::new(
move |e: &MouseEvent, s: &mut State<X>, x: &X| -> penrose::Result<()> {
let handler = &s.extension::<Self>()?;
let handler = handler.borrow();
let Some(ref data) = handler.data else {
return Err(custom_error!("no drag in progress"));
};
let (dx, dy) = (
e.rpt.x as i32 - data.start_point.x as i32,
e.rpt.y as i32 - data.start_point.y as i32,
);
let mut new_rect = data.start_rect.clone();
match data.button {
MouseButton::Left => {
new_rect.reposition(dx, dy);
}
MouseButton::Right => {
new_rect.resize(dx, dy);
}
MouseButton::Middle | MouseButton::ScrollUp | MouseButton::ScrollDown => {
// Don't handle these yet
return Ok(());
}
};
// Keep the internal representation of the client in sync with the X server
let cs = &mut s.client_set;
cs.float(data.xid, new_rect)?;
x.position_client(data.xid, new_rect)?;
// Don't call `x.refresh()` because it re-focuses the mouse
Ok(())
},
)
}
}
#[allow(unused)]
fn mouse_modify_with<F, X>(f: F) -> Box<dyn MouseEventHandler<X>>
where
F: Fn(&mut ClientSet) + Clone + 'static,
X: XConn,
{
Box::new(move |_: &MouseEvent, s: &mut State<X>, x: &X| x.modify_and_refresh(s, f.clone()))
}
/// Make creating all of the mouse bindings less verbose
#[macro_export]
macro_rules! gen_mousebindings {
{
$($kind:ident $button:ident + [$($modifier:ident),+] => $action:expr),+
} => {
{
let mut _map = penrose::core::bindings::MouseBindings::new();
$(
let mut modifiers = Vec::new();
$(modifiers.push(penrose::core::bindings::ModifierKey::$modifier);)+
let state = penrose::core::bindings::MouseState::new(
penrose::core::bindings::MouseButton::$button,
modifiers
);
let kind = penrose::core::bindings::MouseEventKind::$kind;
_map.insert(
(kind, state),
$action
);
)+
_map
}
};
}
pub use gen_mousebindings;