Skip to content

Commit 482512d

Browse files
decouple window handles from wayland id. (linebender#2033)
allows for us to replace inner surfaces dynamically. (necessary for layershells).
1 parent 0abcebc commit 482512d

File tree

5 files changed

+43
-71
lines changed

5 files changed

+43
-71
lines changed

druid-shell/src/backend/shared/xkb/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl Context {
104104
///
105105
/// Because `xkb` has a `critical` error, each rust error maps to 1 above (e.g. error ->
106106
/// critical, warn -> error etc.)
107+
#[allow(unused)]
107108
pub fn set_log_level(&self, level: tracing::Level) {
108109
use tracing::Level;
109110
let level = match level {

druid-shell/src/backend/wayland/application.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use calloop;
2626
use std::{
2727
cell::{Cell, RefCell},
2828
collections::{BTreeMap, BinaryHeap},
29-
num::NonZeroU32,
3029
rc::Rc,
3130
time::{Duration, Instant},
3231
};
@@ -52,14 +51,14 @@ use wayland_protocols::xdg_shell::client::xdg_surface;
5251
use wayland_protocols::xdg_shell::client::xdg_wm_base::{self, XdgWmBase};
5352

5453
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55-
pub(crate) struct Timer(backend::shared::Timer<u32>);
54+
pub(crate) struct Timer(backend::shared::Timer<u64>);
5655

5756
impl Timer {
58-
pub(crate) fn new(id: u32, deadline: Instant) -> Self {
57+
pub(crate) fn new(id: u64, deadline: Instant) -> Self {
5958
Self(backend::shared::Timer::new(deadline, id))
6059
}
6160

62-
pub(crate) fn id(self) -> u32 {
61+
pub(crate) fn id(self) -> u64 {
6362
self.0.data
6463
}
6564

@@ -118,14 +117,14 @@ pub(crate) struct ApplicationData {
118117
// pub(super) surfaces: RefCell<im::OrdMap<u32, std::sync::Arc<surfaces::surface::Data>>>,
119118

120119
/// Handles to any surfaces that have been created.
121-
pub(super) handles: RefCell<im::OrdMap<u32, WindowHandle>>,
120+
pub(super) handles: RefCell<im::OrdMap<u64, WindowHandle>>,
122121

123122
/// Available pixel formats
124123
pub(super) formats: RefCell<Vec<wl_shm::Format>>,
125124
/// Close flag
126125
pub(super) shutdown: Cell<bool>,
127126
/// The currently active surface, if any (by wayland object ID)
128-
pub(super) active_surface_id: RefCell<std::collections::VecDeque<NonZeroU32>>,
127+
pub(super) active_surface_id: RefCell<std::collections::VecDeque<u64>>,
129128
// Stuff for timers
130129
/// A calloop event source for timers. We always set it to fire at the next set timer, if any.
131130
pub(super) timer_handle: calloop::timer::TimerHandle<TimerToken>,
@@ -472,11 +471,13 @@ impl ApplicationData {
472471
Ok(())
473472
}
474473

475-
fn current_window_id(&self) -> u32 {
476-
match self.active_surface_id.borrow().get(0) {
477-
Some(u) => u.get(),
478-
None => 0,
479-
}
474+
fn current_window_id(&self) -> u64 {
475+
static DEFAULT: u64 = 0 as u64;
476+
self.active_surface_id
477+
.borrow()
478+
.get(0)
479+
.unwrap_or_else(|| &DEFAULT)
480+
.clone()
480481
}
481482

482483
pub(super) fn initial_window_size(&self, defaults: kurbo::Size) -> kurbo::Size {
@@ -559,14 +560,8 @@ impl ApplicationData {
559560
}
560561

561562
/// Shallow clones surfaces so we can modify it during iteration.
562-
fn handles_iter(&self) -> impl Iterator<Item = (u32, WindowHandle)> {
563+
fn handles_iter(&self) -> impl Iterator<Item = (u64, WindowHandle)> {
563564
self.handles.borrow().clone().into_iter()
564-
// make sure the borrow gets dropped.
565-
// let surfaces = {
566-
// let surfaces = self.surfaces.borrow();
567-
// surfaces.clone()
568-
// };
569-
// surfaces.into_iter()
570565
}
571566

572567
fn idle_repaint<'a>(loophandle: calloop::LoopHandle<'a, std::sync::Arc<ApplicationData>>) {

druid-shell/src/backend/wayland/surfaces/mod.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub mod popup;
1919
pub mod surface;
2020
pub mod toplevel;
2121

22+
pub const GLOBAL_ID: crate::Counter = crate::Counter::new();
23+
2224
pub trait Compositor {
2325
fn output(&self, id: &u32) -> Option<application::Output>;
2426
fn create_surface(&self) -> wlc::Main<WlSurface>;
@@ -56,7 +58,6 @@ impl PopupHandle {
5658

5759
// handle on given surface.
5860
pub trait Handle {
59-
fn wayland_surface_id(&self) -> u32;
6061
fn get_size(&self) -> kurbo::Size;
6162
fn set_size(&self, dim: kurbo::Size);
6263
fn request_anim_frame(&self);
@@ -159,17 +160,6 @@ impl Compositor for CompositorHandle {
159160
}
160161
}
161162

162-
// fn get_xdg_popup(
163-
// &self,
164-
// pos: &wlc::Main<xdg_positioner::XdgPositioner>,
165-
// parent: Option<&xdg_surface::XdgSurface>,
166-
// ) -> wlc::Main<xdg_popup::XdgPopup> {
167-
// match self.inner.upgrade() {
168-
// None => panic!("unable to acquire underyling compositor to acquire xdg popup surface"),
169-
// Some(c) => c.get_xdg_popup(pos, parent),
170-
// }
171-
// }
172-
173163
fn zxdg_decoration_manager_v1(&self) -> wlc::Main<ZxdgDecorationManagerV1> {
174164
match self.inner.upgrade() {
175165
None => {
@@ -188,7 +178,3 @@ impl Compositor for CompositorHandle {
188178
}
189179
}
190180
}
191-
192-
pub fn id(s: impl Into<u32>) -> u32 {
193-
s.into()
194-
}

druid-shell/src/backend/wayland/surfaces/surface.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ impl Handle {
104104
}
105105

106106
impl SurfaceHandle for Handle {
107-
fn wayland_surface_id(&self) -> u32 {
108-
self.inner.wayland_surface_id()
109-
}
110-
111107
fn get_size(&self) -> kurbo::Size {
112108
self.inner.get_size()
113109
}
@@ -574,11 +570,6 @@ impl Decor for Dead {
574570
}
575571

576572
impl SurfaceHandle for Dead {
577-
fn wayland_surface_id(&self) -> u32 {
578-
tracing::warn!("wayland_surface_id invoked on a dead surface");
579-
0
580-
}
581-
582573
fn get_size(&self) -> kurbo::Size {
583574
kurbo::Size::ZERO
584575
}

druid-shell/src/backend/wayland/window.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub use surfaces::idle::Handle as IdleHandle;
3838

3939
// holds references to the various components for a window implementation.
4040
struct Inner {
41+
pub(super) id: u64,
4142
pub(super) decor: Box<dyn surfaces::Decor>,
4243
pub(super) surface: Box<dyn surfaces::Handle>,
4344
pub(super) appdata: std::sync::Weak<ApplicationData>,
@@ -56,13 +57,18 @@ impl WindowHandle {
5657
) -> Self {
5758
Self {
5859
inner: std::sync::Arc::new(Inner {
60+
id: surfaces::GLOBAL_ID.next(),
5961
decor: decor.into(),
6062
surface: surface.into(),
6163
appdata: appdata.into(),
6264
}),
6365
}
6466
}
6567

68+
pub fn id(&self) -> u64 {
69+
self.inner.id
70+
}
71+
6672
pub fn show(&self) {
6773
tracing::info!("show initiated");
6874
}
@@ -125,10 +131,7 @@ impl WindowHandle {
125131
"closing window initiated {:?}",
126132
appdata.active_surface_id.borrow()
127133
);
128-
appdata
129-
.handles
130-
.borrow_mut()
131-
.remove(&self.inner.surface.wayland_surface_id());
134+
appdata.handles.borrow_mut().remove(&self.id());
132135
appdata.active_surface_id.borrow_mut().pop_front();
133136
self.inner.surface.release();
134137
tracing::trace!(
@@ -197,16 +200,14 @@ impl WindowHandle {
197200
None => panic!("requested timer on a window that was destroyed"),
198201
};
199202

200-
let sid = self.inner.surface.wayland_surface_id();
201-
202203
let now = instant::Instant::now();
203204
let mut timers = appdata.timers.borrow_mut();
204205
let sooner = timers
205206
.peek()
206207
.map(|timer| deadline < timer.deadline())
207208
.unwrap_or(true);
208209

209-
let timer = Timer::new(sid, deadline);
210+
let timer = Timer::new(self.id(), deadline);
210211
timers.push(timer);
211212

212213
// It is possible that the deadline has passed since it was set.
@@ -277,6 +278,7 @@ impl WindowHandle {
277278
self.inner.surface.run_idle();
278279
}
279280

281+
#[allow(unused)]
280282
pub(super) fn popup<'a>(&self, s: &'a surfaces::popup::Surface) -> Result<(), error::Error> {
281283
self.inner.surface.popup(s)
282284
}
@@ -296,6 +298,7 @@ impl std::default::Default for WindowHandle {
296298
fn default() -> WindowHandle {
297299
WindowHandle {
298300
inner: std::sync::Arc::new(Inner {
301+
id: surfaces::GLOBAL_ID.next(),
299302
decor: Box::new(surfaces::surface::Dead::default()),
300303
surface: Box::new(surfaces::surface::Dead::default()),
301304
appdata: std::sync::Weak::new(),
@@ -404,21 +407,19 @@ impl WindowBuilder {
404407

405408
(&surface as &dyn surfaces::Decor).set_title(self.title);
406409

407-
let sid = match std::num::NonZeroU32::new(surfaces::id(&surface)) {
408-
Some(u) => u,
409-
None => panic!("surface id must be non-zero"),
410-
};
411-
412410
let handle = WindowHandle::new(surface.clone(), surface.clone(), self.app_data.clone());
413411

414412
if let Some(_) = appdata
415413
.handles
416414
.borrow_mut()
417-
.insert(sid.get(), handle.clone())
415+
.insert(handle.id(), handle.clone())
418416
{
419417
panic!("wayland should use unique object IDs");
420418
}
421-
appdata.active_surface_id.borrow_mut().push_front(sid);
419+
appdata
420+
.active_surface_id
421+
.borrow_mut()
422+
.push_front(handle.id());
422423

423424
surface.with_handler({
424425
let handle = handle.clone();
@@ -429,6 +430,7 @@ impl WindowBuilder {
429430
}
430431
}
431432

433+
#[allow(unused)]
432434
pub mod layershell {
433435
use crate::error::Error as ShellError;
434436
use crate::window::WinHandler;
@@ -478,11 +480,6 @@ pub mod layershell {
478480

479481
let surface = surfaces::layershell::Surface::new(appdata.clone(), winhandle, updated);
480482

481-
let sid = match std::num::NonZeroU32::new(surfaces::id(&surface)) {
482-
Some(u) => u,
483-
None => panic!("surface id must be non-zero"),
484-
};
485-
486483
let handle = WindowHandle::new(
487484
surfaces::surface::Dead::default(),
488485
surface.clone(),
@@ -492,11 +489,14 @@ pub mod layershell {
492489
if let Some(_) = appdata
493490
.handles
494491
.borrow_mut()
495-
.insert(sid.get(), handle.clone())
492+
.insert(handle.id(), handle.clone())
496493
{
497494
panic!("wayland should use unique object IDs");
498495
}
499-
appdata.active_surface_id.borrow_mut().push_front(sid);
496+
appdata
497+
.active_surface_id
498+
.borrow_mut()
499+
.push_front(handle.id());
500500

501501
surface.with_handler({
502502
let handle = handle.clone();
@@ -508,6 +508,7 @@ pub mod layershell {
508508
}
509509
}
510510

511+
#[allow(unused)]
511512
pub mod popup {
512513
use crate::error::Error as ShellError;
513514
use crate::window::WinHandler;
@@ -560,11 +561,6 @@ pub mod popup {
560561
return Err(ShellError::Platform(cause));
561562
}
562563

563-
let sid = match std::num::NonZeroU32::new(surfaces::id(&surface)) {
564-
Some(u) => u,
565-
None => panic!("surface id must be non-zero"),
566-
};
567-
568564
let handle = WindowHandle::new(
569565
surfaces::surface::Dead::default(),
570566
surface.clone(),
@@ -574,11 +570,14 @@ pub mod popup {
574570
if let Some(_) = appdata
575571
.handles
576572
.borrow_mut()
577-
.insert(sid.get(), handle.clone())
573+
.insert(handle.id(), handle.clone())
578574
{
579575
panic!("wayland should use unique object IDs");
580576
}
581-
appdata.active_surface_id.borrow_mut().push_front(sid);
577+
appdata
578+
.active_surface_id
579+
.borrow_mut()
580+
.push_front(handle.id());
582581

583582
surface.with_handler({
584583
let handle = handle.clone();

0 commit comments

Comments
 (0)