Skip to content

Commit 0afedd0

Browse files
cleanup popup implementation.
- bunch of other misc cleanups
1 parent 1a4da40 commit 0afedd0

File tree

11 files changed

+362
-269
lines changed

11 files changed

+362
-269
lines changed

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

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,7 @@ impl Application {
181181
let (outputsaddedtx, outputsaddedrx) = calloop::channel::channel::<Output>();
182182

183183
let globals = wl::GlobalManager::new_with_cb(&attached_server, {
184-
move |event, registry, data| {
185-
tracing::debug!(
186-
"global manager event received {:?}\n{:?}\n{:?}",
187-
event,
188-
registry,
189-
data
190-
);
184+
move |event, registry, _ctx| {
191185
match event {
192186
wl::GlobalEvent::New {
193187
id,
@@ -270,15 +264,6 @@ impl Application {
270264
.sync_roundtrip(&mut (), |_, _, _| unreachable!())
271265
.map_err(Error::fatal)?;
272266

273-
let mut globals_list = globals.list();
274-
globals_list.sort_by(|(_, name1, version1), (_, name2, version2)| {
275-
name1.cmp(name2).then(version1.cmp(version2))
276-
});
277-
278-
for (id, name, version) in globals_list.into_iter() {
279-
tracing::debug!("{:?}@{:?} - {:?}", name, version, id);
280-
}
281-
282267
let xdg_base = globals
283268
.instantiate_exact::<XdgWmBase>(2)
284269
.map_err(|e| Error::global("xdg_wm_base", 2, e))?;
@@ -370,7 +355,7 @@ impl Application {
370355
if capabilities.contains(wl_seat::Capability::Keyboard)
371356
&& seat.keyboard.is_none()
372357
{
373-
seat.keyboard = Some(app_data.keyboard.attach(id, seat.wl_seat.clone()))
358+
seat.keyboard = Some(app_data.keyboard.attach(id, seat.wl_seat.clone()));
374359
}
375360
if capabilities.contains(wl_seat::Capability::Pointer)
376361
&& seat.pointer.is_none()
@@ -403,7 +388,7 @@ impl Application {
403388
}
404389

405390
pub fn run(self, _handler: Option<Box<dyn AppHandler>>) {
406-
tracing::info!("run initiated");
391+
tracing::info!("wayland event loop initiated");
407392
// NOTE if we want to call this function more than once, we will need to put the timer
408393
// source back.
409394
let timer_source = self.data.timer_source.borrow_mut().take().unwrap();
@@ -431,6 +416,7 @@ impl Application {
431416
}
432417
})
433418
.unwrap();
419+
434420
handle
435421
.insert_source(
436422
self.data.outputs_removed.borrow_mut().take().unwrap(),
@@ -456,20 +442,30 @@ impl Application {
456442
let signal = eventloop.get_signal();
457443
let handle = handle.clone();
458444

459-
eventloop
460-
.run(
461-
Duration::from_millis(20),
462-
&mut self.data.clone(),
463-
move |appdata| {
464-
if appdata.shutdown.get() {
465-
tracing::debug!("shutting down");
466-
signal.stop();
467-
}
445+
let res = eventloop.run(
446+
Duration::from_millis(20),
447+
&mut self.data.clone(),
448+
move |appdata| {
449+
if appdata.shutdown.get() {
450+
tracing::debug!("shutting down, requested");
451+
signal.stop();
452+
return;
453+
}
468454

469-
ApplicationData::idle_repaint(handle.clone());
470-
},
471-
)
472-
.unwrap();
455+
if appdata.handles.borrow().len() == 0 {
456+
tracing::debug!("shutting down, no window remaining");
457+
signal.stop();
458+
return;
459+
}
460+
461+
ApplicationData::idle_repaint(handle.clone());
462+
},
463+
);
464+
465+
match res {
466+
Ok(_) => tracing::info!("wayland event loop completed"),
467+
Err(cause) => tracing::error!("wayland event loop failed {:?}", cause),
468+
}
473469
}
474470

475471
pub fn quit(&self) {
@@ -576,18 +572,8 @@ impl ApplicationData {
576572
}
577573
}
578574

579-
pub(super) fn popup<'a>(&self, surface: &'a surfaces::popup::Surface) -> Result<(), Error> {
580-
match self.acquire_current_window() {
581-
None => return Err(Error::string("parent window does not exist")),
582-
Some(winhandle) => winhandle.popup(surface),
583-
}
584-
}
585-
586575
fn handle_timer_event(&self, _token: TimerToken) {
587-
// Shouldn't be necessary.
588-
self.timer_handle.cancel_all_timeouts();
589576
// Don't borrow the timers in case the callbacks want to add more.
590-
// TODO make this in the stack (smallvec)
591577
let mut expired_timers = Vec::with_capacity(1);
592578
let mut timers = self.timers.borrow_mut();
593579
let now = Instant::now();
@@ -601,7 +587,7 @@ impl ApplicationData {
601587
Some(s) => s,
602588
None => {
603589
// NOTE this might be expected
604-
log::warn!(
590+
tracing::warn!(
605591
"received event for surface that doesn't exist any more {:?} {:?}",
606592
expired,
607593
expired.id()

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ pub struct Manager {
106106
}
107107

108108
impl Manager {
109-
pub(super) fn new(display: &wl::Display, gm: &wl::GlobalManager) -> Result<Self, waylanderr::Error> {
109+
pub(super) fn new(
110+
display: &wl::Display,
111+
gm: &wl::GlobalManager,
112+
) -> Result<Self, waylanderr::Error> {
110113
let m = gm
111114
.instantiate_exact::<wl_data_device_manager::WlDataDeviceManager>(3)
112115
.map_err(|e| waylanderr::Error::global("wl_data_device_manager", 1, e))?;
@@ -257,13 +260,15 @@ impl Clipboard {
257260
pub fn get_string(&self) -> Option<String> {
258261
vec![Clipboard::UTF8, Clipboard::TEXT, Clipboard::UTF8_STRING]
259262
.iter()
260-
.find_map(|mimetype| match std::str::from_utf8(&self.inner.receive(*mimetype)?) {
261-
Ok(s) => Some(s.to_string()),
262-
Err(cause) => {
263-
tracing::error!("clipboard unable to retrieve utf8 content {:?}", cause);
264-
None
263+
.find_map(
264+
|mimetype| match std::str::from_utf8(&self.inner.receive(*mimetype)?) {
265+
Ok(s) => Some(s.to_string()),
266+
Err(cause) => {
267+
tracing::error!("clipboard unable to retrieve utf8 content {:?}", cause);
268+
None
269+
}
265270
},
266-
})
271+
)
267272
}
268273

269274
/// Given a list of supported clipboard types, returns the supported type which has

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
1717
pub mod application;
1818
pub mod clipboard;
19-
pub mod dialog;
2019
pub mod error;
2120
mod events;
2221
pub mod keyboard;
2322
pub mod menu;
2423
pub mod pointers;
2524
pub mod screen;
2625
pub mod surfaces;
27-
pub mod util;
2826
pub mod window;
2927

3028
/// Little enum to make it clearer what some return values mean.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub(crate) enum PointerEvent {
107107
}
108108

109109
/// An enum that we will convert into the different callbacks.
110+
#[derive(Debug)]
110111
pub(crate) enum MouseEvtKind {
111112
Move(mouse::MouseEvent),
112113
Up(mouse::MouseEvent),

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

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
use wayland_client as wlc;
22
use wayland_protocols::wlr::unstable::layer_shell::v1::client as layershell;
3+
use wayland_protocols::xdg_shell::client::xdg_surface;
34

45
use crate::kurbo;
56
use crate::window;
67

78
use super::super::application::Output;
89
use super::super::error;
9-
use super::popup;
1010
use super::surface;
1111
use super::Compositor;
1212
use super::CompositorHandle;
1313
use super::Handle;
1414
use super::Outputs;
1515
use super::Popup;
16-
use super::PopupHandle;
1716

1817
struct Inner {
1918
config: Config,
@@ -24,17 +23,32 @@ struct Inner {
2423
available: std::cell::RefCell<bool>,
2524
}
2625

27-
impl Drop for Inner {
28-
fn drop(&mut self) {
29-
self.ls_surface.borrow().destroy();
26+
impl Inner {
27+
fn popup<'a>(
28+
&self,
29+
surface: &'a wlc::Main<xdg_surface::XdgSurface>,
30+
pos: &'a wlc::Main<wayland_protocols::xdg_shell::client::xdg_positioner::XdgPositioner>,
31+
) -> wlc::Main<wayland_protocols::xdg_shell::client::xdg_popup::XdgPopup> {
32+
let popup = surface.get_popup(None, pos);
33+
self.ls_surface.borrow().get_popup(&popup);
34+
return popup;
3035
}
3136
}
3237

3338
impl Popup for Inner {
34-
fn popup_impl(&self, p: &popup::Surface) -> Result<(), error::Error> {
35-
self.ls_surface.borrow().get_popup(&p.get_xdg_popup());
36-
p.commit();
37-
Ok(())
39+
fn surface<'a>(
40+
&self,
41+
surface: &'a wlc::Main<xdg_surface::XdgSurface>,
42+
pos: &'a wlc::Main<wayland_protocols::xdg_shell::client::xdg_positioner::XdgPositioner>,
43+
) -> Result<wlc::Main<wayland_protocols::xdg_shell::client::xdg_popup::XdgPopup>, error::Error>
44+
{
45+
return Ok(self.popup(surface, pos));
46+
}
47+
}
48+
49+
impl Drop for Inner {
50+
fn drop(&mut self) {
51+
self.ls_surface.borrow().destroy();
3852
}
3953
}
4054

@@ -218,13 +232,6 @@ impl Surface {
218232
}
219233

220234
tracing::info!("attempting to initialize layershell");
221-
handle
222-
.inner
223-
.wl_surface
224-
.borrow()
225-
.set_popup_impl(PopupHandle {
226-
inner: handle.inner.clone(),
227-
});
228235

229236
handle.inner.ls_surface.borrow().quick_assign({
230237
let handle = handle.clone();
@@ -319,6 +326,17 @@ impl Outputs for Surface {
319326
}
320327
}
321328

329+
impl Popup for Surface {
330+
fn surface<'a>(
331+
&self,
332+
popup: &'a wlc::Main<xdg_surface::XdgSurface>,
333+
pos: &'a wlc::Main<wayland_protocols::xdg_shell::client::xdg_positioner::XdgPositioner>,
334+
) -> Result<wlc::Main<wayland_protocols::xdg_shell::client::xdg_popup::XdgPopup>, error::Error>
335+
{
336+
return Ok(self.inner.popup(popup, pos));
337+
}
338+
}
339+
322340
impl Handle for Surface {
323341
fn get_size(&self) -> kurbo::Size {
324342
return self.inner.wl_surface.borrow().get_size();
@@ -368,10 +386,6 @@ impl Handle for Surface {
368386
}
369387
}
370388

371-
fn popup(&self, popup: &popup::Surface) -> Result<(), error::Error> {
372-
return self.inner.wl_surface.borrow().popup(popup);
373-
}
374-
375389
fn release(&self) {
376390
return self.inner.wl_surface.borrow().release();
377391
}
@@ -404,3 +418,9 @@ impl From<Surface> for Box<dyn Outputs> {
404418
Box::new(s) as Box<dyn Outputs>
405419
}
406420
}
421+
422+
impl From<Surface> for Box<dyn Popup> {
423+
fn from(s: Surface) -> Self {
424+
Box::new(s) as Box<dyn Popup>
425+
}
426+
}

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use wayland_client::protocol::wl_shm::WlShm;
22
use wayland_client::{self as wlc, protocol::wl_surface::WlSurface};
33
use wayland_protocols::unstable::xdg_decoration::v1::client::zxdg_decoration_manager_v1::ZxdgDecorationManagerV1;
44
use wayland_protocols::wlr::unstable::layer_shell::v1::client::zwlr_layer_shell_v1::ZwlrLayerShellV1;
5+
use wayland_protocols::xdg_shell::client::xdg_popup;
56
use wayland_protocols::xdg_shell::client::xdg_positioner;
67
use wayland_protocols::xdg_shell::client::xdg_surface;
78

@@ -43,17 +44,11 @@ impl dyn Decor {
4344
}
4445

4546
pub trait Popup {
46-
fn popup_impl(&self, popup: &popup::Surface) -> Result<(), error::Error>;
47-
}
48-
49-
pub struct PopupHandle {
50-
inner: std::sync::Arc<dyn Popup>,
51-
}
52-
53-
impl PopupHandle {
54-
fn popup(&self, p: &popup::Surface) -> Result<(), error::Error> {
55-
self.inner.popup_impl(p)
56-
}
47+
fn surface<'a>(
48+
&self,
49+
popup: &'a wlc::Main<xdg_surface::XdgSurface>,
50+
pos: &'a wlc::Main<xdg_positioner::XdgPositioner>,
51+
) -> Result<wlc::Main<xdg_popup::XdgPopup>, error::Error>;
5752
}
5853

5954
pub(super) trait Outputs {
@@ -73,7 +68,6 @@ pub trait Handle {
7368
fn get_idle_handle(&self) -> idle::Handle;
7469
fn get_scale(&self) -> Scale;
7570
fn run_idle(&self);
76-
fn popup(&self, popup: &popup::Surface) -> Result<(), error::Error>;
7771
fn release(&self);
7872
fn data(&self) -> Option<std::sync::Arc<surface::Data>>;
7973
}

0 commit comments

Comments
 (0)