-
Notifications
You must be signed in to change notification settings - Fork 569
/
window.rs
645 lines (541 loc) · 19 KB
/
window.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![allow(clippy::single_match)]
use tracing;
use wayland_protocols::xdg_shell::client::xdg_popup;
use wayland_protocols::xdg_shell::client::xdg_positioner;
use wayland_protocols::xdg_shell::client::xdg_surface;
#[cfg(feature = "raw-win-handle")]
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, WaylandWindowHandle};
use super::application::{self, Timer};
use super::{error::Error, menu::Menu, outputs, surfaces};
use crate::{
dialog::FileDialogOptions,
error::Error as ShellError,
kurbo::{Insets, Point, Rect, Size},
mouse::{Cursor, CursorDesc},
piet::PietText,
scale::Scale,
text::Event,
window::{self, FileDialogToken, TimerToken, WinHandler, WindowLevel},
TextFieldToken,
};
pub use surfaces::idle::Handle as IdleHandle;
// holds references to the various components for a window implementation.
struct Inner {
pub(super) id: u64,
pub(super) decor: Box<dyn surfaces::Decor>,
pub(super) surface: Box<dyn surfaces::Handle>,
pub(super) outputs: Box<dyn surfaces::Outputs>,
pub(super) popup: Box<dyn surfaces::Popup>,
pub(super) appdata: std::sync::Weak<application::Data>,
}
#[derive(Clone)]
pub struct WindowHandle {
inner: std::sync::Arc<Inner>,
}
impl surfaces::Outputs for WindowHandle {
fn removed(&self, o: &outputs::Meta) {
self.inner.outputs.removed(o)
}
fn inserted(&self, o: &outputs::Meta) {
self.inner.outputs.inserted(o)
}
}
impl surfaces::Popup for WindowHandle {
fn surface(
&self,
popup: &wayland_client::Main<xdg_surface::XdgSurface>,
pos: &wayland_client::Main<xdg_positioner::XdgPositioner>,
) -> Result<wayland_client::Main<xdg_popup::XdgPopup>, Error> {
self.inner.popup.surface(popup, pos)
}
}
impl WindowHandle {
pub(super) fn new(
outputs: impl Into<Box<dyn surfaces::Outputs>>,
decor: impl Into<Box<dyn surfaces::Decor>>,
surface: impl Into<Box<dyn surfaces::Handle>>,
popup: impl Into<Box<dyn surfaces::Popup>>,
appdata: impl Into<std::sync::Weak<application::Data>>,
) -> Self {
Self {
inner: std::sync::Arc::new(Inner {
id: surfaces::GLOBAL_ID.next(),
outputs: outputs.into(),
decor: decor.into(),
surface: surface.into(),
popup: popup.into(),
appdata: appdata.into(),
}),
}
}
pub fn id(&self) -> u64 {
self.inner.id
}
pub fn show(&self) {
tracing::debug!("show initiated");
}
pub fn resizable(&self, _resizable: bool) {
tracing::warn!("resizable is unimplemented on wayland");
}
pub fn show_titlebar(&self, _show_titlebar: bool) {
tracing::warn!("show_titlebar is unimplemented on wayland");
}
pub fn set_position(&self, _position: Point) {
tracing::warn!("set_position is unimplemented on wayland");
}
pub fn get_position(&self) -> Point {
tracing::warn!("get_position is unimplemented on wayland");
Point::ZERO
}
pub fn content_insets(&self) -> Insets {
Insets::from(0.)
}
pub fn set_size(&self, size: Size) {
self.inner.surface.set_size(size);
}
pub fn get_size(&self) -> Size {
self.inner.surface.get_size()
}
pub fn set_window_state(&mut self, _current_state: window::WindowState) {
tracing::warn!("set_window_state is unimplemented on wayland");
}
pub fn get_window_state(&self) -> window::WindowState {
tracing::warn!("get_window_state is unimplemented on wayland");
window::WindowState::Maximized
}
pub fn handle_titlebar(&self, _val: bool) {
tracing::warn!("handle_titlebar is unimplemented on wayland");
}
/// Close the window.
pub fn close(&self) {
if let Some(appdata) = self.inner.appdata.upgrade() {
tracing::trace!(
"closing window initiated {:?}",
appdata.active_surface_id.borrow()
);
appdata.handles.borrow_mut().remove(&self.id());
appdata.active_surface_id.borrow_mut().pop_front();
self.inner.surface.release();
tracing::trace!(
"closing window completed {:?}",
appdata.active_surface_id.borrow()
);
}
}
/// Bring this window to the front of the window stack and give it focus.
pub fn bring_to_front_and_focus(&self) {
tracing::warn!("unimplemented bring_to_front_and_focus initiated");
}
/// Request a new paint, but without invalidating anything.
pub fn request_anim_frame(&self) {
self.inner.surface.request_anim_frame();
}
/// Request invalidation of the entire window contents.
pub fn invalidate(&self) {
self.inner.surface.invalidate();
}
/// Request invalidation of one rectangle, which is given in display points relative to the
/// drawing area.
pub fn invalidate_rect(&self, rect: Rect) {
self.inner.surface.invalidate_rect(rect);
}
pub fn text(&self) -> PietText {
PietText::new()
}
pub fn add_text_field(&self) -> TextFieldToken {
TextFieldToken::next()
}
pub fn remove_text_field(&self, token: TextFieldToken) {
self.inner.surface.remove_text_field(token);
}
pub fn set_focused_text_field(&self, active_field: Option<TextFieldToken>) {
self.inner.surface.set_focused_text_field(active_field);
}
pub fn update_text_field(&self, _token: TextFieldToken, _update: Event) {
// noop until we get a real text input implementation
}
pub fn request_timer(&self, deadline: std::time::Instant) -> TimerToken {
let appdata = match self.inner.appdata.upgrade() {
Some(d) => d,
None => {
tracing::warn!("requested timer on a window that was destroyed");
return Timer::new(self.id(), deadline).token();
}
};
let now = instant::Instant::now();
let mut timers = appdata.timers.borrow_mut();
let sooner = timers
.peek()
.map(|timer| deadline < timer.deadline())
.unwrap_or(true);
let timer = Timer::new(self.id(), deadline);
timers.push(timer);
// It is possible that the deadline has passed since it was set.
let timeout = if deadline < now {
std::time::Duration::ZERO
} else {
deadline - now
};
if sooner {
appdata.timer_handle.cancel_all_timeouts();
appdata.timer_handle.add_timeout(timeout, timer.token());
}
timer.token()
}
pub fn set_cursor(&mut self, cursor: &Cursor) {
if let Some(appdata) = self.inner.appdata.upgrade() {
appdata.set_cursor(cursor);
}
}
pub fn make_cursor(&self, _desc: &CursorDesc) -> Option<Cursor> {
tracing::warn!("unimplemented make_cursor initiated");
None
}
pub fn open_file(&mut self, _options: FileDialogOptions) -> Option<FileDialogToken> {
tracing::warn!("unimplemented open_file");
None
}
pub fn save_as(&mut self, _options: FileDialogOptions) -> Option<FileDialogToken> {
tracing::warn!("unimplemented save_as");
None
}
/// Get a handle that can be used to schedule an idle task.
pub fn get_idle_handle(&self) -> Option<IdleHandle> {
Some(self.inner.surface.get_idle_handle())
}
/// Get the `Scale` of the window.
pub fn get_scale(&self) -> Result<Scale, ShellError> {
Ok(self.inner.surface.get_scale())
}
pub fn set_menu(&self, _menu: Menu) {
tracing::warn!("set_menu not implement for wayland");
}
pub fn show_context_menu(&self, _menu: Menu, _pos: Point) {
tracing::warn!("show_context_menu not implement for wayland");
}
pub fn set_title(&self, title: impl Into<String>) {
self.inner.decor.set_title(title);
}
pub(super) fn run_idle(&self) {
self.inner.surface.run_idle();
}
pub(super) fn data(&self) -> Option<std::sync::Arc<surfaces::surface::Data>> {
self.inner.surface.data()
}
}
impl std::cmp::PartialEq for WindowHandle {
fn eq(&self, rhs: &Self) -> bool {
self.id() == rhs.id()
}
}
impl Eq for WindowHandle {}
impl std::default::Default for WindowHandle {
fn default() -> WindowHandle {
WindowHandle {
inner: std::sync::Arc::new(Inner {
id: surfaces::GLOBAL_ID.next(),
outputs: Box::new(surfaces::surface::Dead::default()),
decor: Box::new(surfaces::surface::Dead::default()),
surface: Box::new(surfaces::surface::Dead::default()),
popup: Box::new(surfaces::surface::Dead::default()),
appdata: std::sync::Weak::new(),
}),
}
}
}
#[cfg(feature = "raw-win-handle")]
unsafe impl HasRawWindowHandle for WindowHandle {
fn raw_window_handle(&self) -> RawWindowHandle {
tracing::error!("HasRawWindowHandle trait not implemented for wasm.");
RawWindowHandle::Wayland(WaylandWindowHandle::empty())
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct CustomCursor;
/// Builder abstraction for creating new windows
pub(crate) struct WindowBuilder {
appdata: std::sync::Weak<application::Data>,
handler: Option<Box<dyn WinHandler>>,
title: String,
menu: Option<Menu>,
position: Option<Point>,
level: WindowLevel,
state: Option<window::WindowState>,
// pre-scaled
size: Size,
min_size: Option<Size>,
resizable: bool,
show_titlebar: bool,
}
impl WindowBuilder {
pub fn new(app: application::Application) -> WindowBuilder {
WindowBuilder {
appdata: std::sync::Arc::downgrade(&app.data),
handler: None,
title: String::new(),
menu: None,
size: Size::new(0.0, 0.0),
position: None,
level: WindowLevel::AppWindow,
state: None,
min_size: None,
resizable: true,
show_titlebar: true,
}
}
pub fn set_handler(&mut self, handler: Box<dyn WinHandler>) {
self.handler = Some(handler);
}
pub fn set_size(&mut self, size: Size) {
self.size = size;
}
pub fn set_min_size(&mut self, size: Size) {
self.min_size = Some(size);
}
pub fn resizable(&mut self, resizable: bool) {
self.resizable = resizable;
}
pub fn show_titlebar(&mut self, show_titlebar: bool) {
self.show_titlebar = show_titlebar;
}
pub fn set_transparent(&mut self, _transparent: bool) {
tracing::warn!(
"set_transparent unimplemented for wayland, it allows transparency by default"
);
}
pub fn set_position(&mut self, position: Point) {
self.position = Some(position);
}
pub fn set_level(&mut self, level: WindowLevel) {
self.level = level;
}
pub fn set_window_state(&mut self, state: window::WindowState) {
self.state = Some(state);
}
pub fn set_title(&mut self, title: impl Into<String>) {
self.title = title.into();
}
pub fn set_menu(&mut self, menu: Menu) {
self.menu = Some(menu);
}
pub fn build(self) -> Result<WindowHandle, ShellError> {
if matches!(self.menu, Some(_)) {
tracing::warn!("menus unimplemented for wayland");
}
if let WindowLevel::DropDown(parent) = self.level {
let dim = self.min_size.unwrap_or(Size::ZERO);
let dim = Size::new(dim.width.max(1.), dim.height.max(1.));
let dim = Size::new(
self.size.width.max(dim.width),
self.size.height.max(dim.height),
);
let config = surfaces::popup::Config::default()
.with_size(dim)
.with_offset(Into::into(
self.position.unwrap_or_else(|| Into::into((0., 0.))),
));
tracing::debug!("popup {:?}", config);
return popup::create(&parent.0, &config, self.appdata, self.handler);
}
let appdata = match self.appdata.upgrade() {
Some(d) => d,
None => return Err(ShellError::ApplicationDropped),
};
let handler = self.handler.expect("must set a window handler");
// compute the initial window size.
let initial_size = WindowBuilder::initial_window_size(&appdata, self.size);
let surface =
surfaces::toplevel::Surface::new(appdata.clone(), handler, initial_size, self.min_size);
(&surface as &dyn surfaces::Decor).set_title(self.title);
let handle = WindowHandle::new(
surface.clone(),
surface.clone(),
surface.clone(),
surface.clone(),
self.appdata.clone(),
);
if appdata
.handles
.borrow_mut()
.insert(handle.id(), handle.clone())
.is_some()
{
return Err(ShellError::Platform(Error::string(
"wayland should use a unique id",
)));
}
appdata
.active_surface_id
.borrow_mut()
.push_front(handle.id());
surface.with_handler({
let handle = handle.clone();
move |winhandle| winhandle.connect(&handle.into())
});
Ok(handle)
}
pub(super) fn initial_window_size(
appdata: &application::Data,
defaults: kurbo::Size,
) -> kurbo::Size {
// compute the initial window size.
let initialwidth = if defaults.width == 0.0 {
f64::INFINITY
} else {
defaults.width
};
let initialheight = if defaults.height == 0.0 {
f64::INFINITY
} else {
defaults.height
};
return appdata.outputs.borrow().iter().fold(
kurbo::Size::from((initialwidth, initialheight)),
|computed, entry| {
kurbo::Size::new(
computed.width.min(entry.1.logical.width.into()),
computed.height.min(entry.1.logical.height.into()),
)
},
);
}
}
#[allow(unused)]
pub mod layershell {
use crate::error::Error as ShellError;
use crate::window::WinHandler;
use super::WindowHandle;
use crate::backend::wayland::application::{Application, Data};
use crate::backend::wayland::error::Error;
use crate::backend::wayland::surfaces;
/// Builder abstraction for creating new windows
pub(crate) struct Builder {
appdata: std::sync::Weak<Data>,
winhandle: Option<Box<dyn WinHandler>>,
pub(crate) config: surfaces::layershell::Config,
}
impl Builder {
pub fn new(app: Application) -> Builder {
Builder {
appdata: std::sync::Arc::downgrade(&app.data),
config: surfaces::layershell::Config::default(),
winhandle: None,
}
}
pub fn set_handler(&mut self, handler: Box<dyn WinHandler>) {
self.winhandle = Some(handler);
}
pub fn build(self) -> Result<WindowHandle, ShellError> {
let appdata = match self.appdata.upgrade() {
Some(d) => d,
None => return Err(ShellError::ApplicationDropped),
};
let winhandle = match self.winhandle {
Some(winhandle) => winhandle,
None => {
return Err(ShellError::Platform(Error::string(
"window handler required",
)))
}
};
let surface =
surfaces::layershell::Surface::new(appdata.clone(), winhandle, self.config.clone());
let handle = WindowHandle::new(
surface.clone(),
surfaces::surface::Dead::default(),
surface.clone(),
surface.clone(),
self.appdata.clone(),
);
if appdata
.handles
.borrow_mut()
.insert(handle.id(), handle.clone())
.is_some()
{
panic!("wayland should use unique object IDs");
}
appdata
.active_surface_id
.borrow_mut()
.push_front(handle.id());
surface.with_handler({
let handle = handle.clone();
move |winhandle| winhandle.connect(&handle.into())
});
Ok(handle)
}
}
}
#[allow(unused)]
pub mod popup {
use crate::error::Error as ShellError;
use crate::window::WinHandler;
use super::WindowBuilder;
use super::WindowHandle;
use crate::backend::wayland::application::{Application, Data};
use crate::backend::wayland::error::Error;
use crate::backend::wayland::surfaces;
pub(super) fn create(
parent: &WindowHandle,
config: &surfaces::popup::Config,
wappdata: std::sync::Weak<Data>,
winhandle: Option<Box<dyn WinHandler>>,
) -> Result<WindowHandle, ShellError> {
let appdata = match wappdata.upgrade() {
Some(d) => d,
None => return Err(ShellError::ApplicationDropped),
};
let winhandle = match winhandle {
Some(winhandle) => winhandle,
None => {
return Err(ShellError::Platform(Error::string(
"window handler required",
)))
}
};
// compute the initial window size.
let updated = config.clone();
let surface =
match surfaces::popup::Surface::new(appdata.clone(), winhandle, updated, parent) {
Err(cause) => return Err(ShellError::Platform(cause)),
Ok(s) => s,
};
let handle = WindowHandle::new(
surface.clone(),
surfaces::surface::Dead::default(),
surface.clone(),
surface.clone(),
wappdata,
);
if appdata
.handles
.borrow_mut()
.insert(handle.id(), handle.clone())
.is_some()
{
panic!("wayland should use unique object IDs");
}
appdata
.active_surface_id
.borrow_mut()
.push_front(handle.id());
surface.with_handler({
let handle = handle.clone();
move |winhandle| winhandle.connect(&handle.into())
});
Ok(handle)
}
}