-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathundecorated_resizing.rs
323 lines (288 loc) · 10 KB
/
undecorated_resizing.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
#![cfg(any(
windows,
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
const CLIENT: isize = 0b0000;
const LEFT: isize = 0b0001;
const RIGHT: isize = 0b0010;
const TOP: isize = 0b0100;
const BOTTOM: isize = 0b1000;
const TOPLEFT: isize = TOP | LEFT;
const TOPRIGHT: isize = TOP | RIGHT;
const BOTTOMLEFT: isize = BOTTOM | LEFT;
const BOTTOMRIGHT: isize = BOTTOM | RIGHT;
const BORDERLESS_RESIZE_INSET: f64 = 5.0;
#[cfg(not(windows))]
pub use self::gtk::*;
#[cfg(windows)]
pub use self::windows::*;
#[cfg(windows)]
type WindowDimensions = u32;
#[cfg(not(windows))]
type WindowDimensions = i32;
#[cfg(windows)]
type WindowPositions = i32;
#[cfg(not(windows))]
type WindowPositions = f64;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum HitTestResult {
Client,
Left,
Right,
Top,
Bottom,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
NoWhere,
}
fn hit_test(
width: WindowDimensions,
height: WindowDimensions,
x: WindowPositions,
y: WindowPositions,
scale: f64,
) -> HitTestResult {
#[cfg(windows)]
let (top, left) = (0, 0);
#[cfg(not(windows))]
let (top, left) = (0., 0.);
let bottom = top + height as WindowPositions;
let right = left + width as WindowPositions;
let inset = (BORDERLESS_RESIZE_INSET * scale) as WindowPositions;
#[rustfmt::skip]
let result =
(LEFT * (if x < (left + inset) { 1 } else { 0 }))
| (RIGHT * (if x >= (right - inset) { 1 } else { 0 }))
| (TOP * (if y < (top + inset) { 1 } else { 0 }))
| (BOTTOM * (if y >= (bottom - inset) { 1 } else { 0 }));
match result {
CLIENT => HitTestResult::Client,
LEFT => HitTestResult::Left,
RIGHT => HitTestResult::Right,
TOP => HitTestResult::Top,
BOTTOM => HitTestResult::Bottom,
TOPLEFT => HitTestResult::TopLeft,
TOPRIGHT => HitTestResult::TopRight,
BOTTOMLEFT => HitTestResult::BottomLeft,
BOTTOMRIGHT => HitTestResult::BottomRight,
_ => HitTestResult::NoWhere,
}
}
#[cfg(windows)]
mod windows {
use super::{hit_test, HitTestResult};
use tao::window::{CursorIcon, ResizeDirection, Window};
const MESSAGE_MOUSEMOVE: &str = "__internal_on_mousemove__|";
const MESSAGE_MOUSEDOWN: &str = "__internal_on_mousedown__|";
pub const SCRIPT: &str = r#"
;(function () {
document.addEventListener('mousemove', (e) => {
window.ipc.postMessage(
`__internal_on_mousemove__|${e.clientX},${e.clientY}`
)
})
document.addEventListener('mousedown', (e) => {
if (e.button === 0) {
window.ipc.postMessage(
`__internal_on_mousedown__|${e.clientX},${e.clientY}`
)
}
})
})()
"#;
impl HitTestResult {
fn drag_resize_window(&self, window: &Window) {
self.change_cursor(window);
let edge = match self {
HitTestResult::Left => ResizeDirection::West,
HitTestResult::Right => ResizeDirection::East,
HitTestResult::Top => ResizeDirection::North,
HitTestResult::Bottom => ResizeDirection::South,
HitTestResult::TopLeft => ResizeDirection::NorthWest,
HitTestResult::TopRight => ResizeDirection::NorthEast,
HitTestResult::BottomLeft => ResizeDirection::SouthWest,
HitTestResult::BottomRight => ResizeDirection::SouthEast,
// if not on an edge, don't start resizing
_ => return,
};
let _ = window.drag_resize_window(edge);
}
fn change_cursor(&self, window: &Window) {
let cursor = match self {
HitTestResult::Left => CursorIcon::WResize,
HitTestResult::Right => CursorIcon::EResize,
HitTestResult::Top => CursorIcon::NResize,
HitTestResult::Bottom => CursorIcon::SResize,
HitTestResult::TopLeft => CursorIcon::NwResize,
HitTestResult::TopRight => CursorIcon::NeResize,
HitTestResult::BottomLeft => CursorIcon::SwResize,
HitTestResult::BottomRight => CursorIcon::SeResize,
// if not on an edge, don't change the cursor, otherwise we cause flickering
_ => return,
};
let _ = window.set_cursor_icon(cursor);
}
}
// Returns whether handled or not
pub fn handle_request<T: crate::UserEvent>(
context: crate::Context<T>,
window_id: crate::WindowId,
request: &str,
) -> bool {
if let Some(args) = request.strip_prefix(MESSAGE_MOUSEMOVE) {
if let Some(window) = context
.main_thread
.windows
.borrow()
.get(&window_id)
.and_then(|w| w.inner.as_ref())
{
if !window.is_decorated() && window.is_resizable() && !window.is_maximized() {
let (x, y) = args.split_once(',').unwrap();
let (x, y) = (x.parse().unwrap(), y.parse().unwrap());
let size = window.inner_size();
hit_test(size.width, size.height, x, y, window.scale_factor()).change_cursor(&window);
}
}
return true;
} else if let Some(args) = request.strip_prefix(MESSAGE_MOUSEDOWN) {
if let Some(window) = context
.main_thread
.windows
.borrow()
.get(&window_id)
.and_then(|w| w.inner.as_ref())
{
if !window.is_decorated() && window.is_resizable() && !window.is_maximized() {
let (x, y) = args.split_once(',').unwrap();
let (x, y) = (x.parse().unwrap(), y.parse().unwrap());
let size = window.inner_size();
hit_test(size.width, size.height, x, y, window.scale_factor())
.drag_resize_window(&window);
}
}
return true;
}
false
}
}
#[cfg(not(windows))]
mod gtk {
use super::{hit_test, HitTestResult};
impl HitTestResult {
fn to_gtk_edge(self) -> gtk::gdk::WindowEdge {
match self {
HitTestResult::Client | HitTestResult::NoWhere => gtk::gdk::WindowEdge::__Unknown(0),
HitTestResult::Left => gtk::gdk::WindowEdge::West,
HitTestResult::Right => gtk::gdk::WindowEdge::East,
HitTestResult::Top => gtk::gdk::WindowEdge::North,
HitTestResult::Bottom => gtk::gdk::WindowEdge::South,
HitTestResult::TopLeft => gtk::gdk::WindowEdge::NorthWest,
HitTestResult::TopRight => gtk::gdk::WindowEdge::NorthEast,
HitTestResult::BottomLeft => gtk::gdk::WindowEdge::SouthWest,
HitTestResult::BottomRight => gtk::gdk::WindowEdge::SouthEast,
}
}
}
pub fn attach_resize_handler(webview: &wry::WebView) {
use gtk::{
gdk::{prelude::*, WindowEdge},
glib::Propagation,
prelude::*,
};
use wry::WebViewExtUnix;
let webview = webview.webview();
webview.add_events(
gtk::gdk::EventMask::BUTTON1_MOTION_MASK
| gtk::gdk::EventMask::BUTTON_PRESS_MASK
| gtk::gdk::EventMask::TOUCH_MASK,
);
webview.connect_button_press_event(
|webview: &webkit2gtk::WebView, event: >k::gdk::EventButton| {
if event.button() == 1 {
// This one should be GtkBox
if let Some(widget) = webview.parent() {
// This one should be GtkWindow
if let Some(window) = widget.parent() {
// Safe to unwrap unless this is not from tao
let window: gtk::Window = window.downcast().unwrap();
if !window.is_decorated() && window.is_resizable() && !window.is_maximized() {
if let Some(window) = window.window() {
let (root_x, root_y) = event.root();
let (window_x, window_y) = window.position();
let (client_x, client_y) = (root_x - window_x as f64, root_y - window_y as f64);
let edge = hit_test(
window.width(),
window.height(),
client_x,
client_y,
window.scale_factor() as f64,
)
.to_gtk_edge();
// we ignore the `__Unknown` variant so the webview receives the click correctly if it is not on the edges.
match edge {
WindowEdge::__Unknown(_) => (),
_ => {
window.begin_resize_drag(edge, 1, root_x as i32, root_y as i32, event.time())
}
}
}
}
}
}
}
Propagation::Proceed
},
);
webview.connect_touch_event(|webview: &webkit2gtk::WebView, event: >k::gdk::Event| {
// This one should be GtkBox
if let Some(widget) = webview.parent() {
// This one should be GtkWindow
if let Some(window) = widget.parent() {
// Safe to unwrap unless this is not from tao
let window: gtk::Window = window.downcast().unwrap();
if !window.is_decorated() && window.is_resizable() && !window.is_maximized() {
if let Some(window) = window.window() {
if let Some((root_x, root_y)) = event.root_coords() {
if let Some(device) = event.device() {
let (window_x, window_y) = window.position();
let (client_x, client_y) = (root_x - window_x as f64, root_y - window_y as f64);
let edge = hit_test(
window.width(),
window.height(),
client_x,
client_y,
window.scale_factor() as f64,
)
.to_gtk_edge();
// we ignore the `__Unknown` variant so the window receives the click correctly if it is not on the edges.
match edge {
WindowEdge::__Unknown(_) => (),
_ => window.begin_resize_drag_for_device(
edge,
&device,
0,
root_x as i32,
root_y as i32,
event.time(),
),
}
}
}
}
}
}
}
Propagation::Proceed
});
}
}