Skip to content

wayland: Add WindowExtWayland::xdg_toplevel #4076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
24 changes: 22 additions & 2 deletions src/platform/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
//! * `wayland-csd-adwaita` (default).
//! * `wayland-csd-adwaita-crossfont`.
//! * `wayland-csd-adwaita-notitle`.

use std::ffi::c_void;
use std::ptr::NonNull;

#[cfg(wayland_platform)]
use sctk::reexports::client::Proxy;

use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder};
use crate::monitor::MonitorHandle;
pub use crate::window::Theme;
Expand Down Expand Up @@ -73,9 +80,22 @@ impl EventLoopBuilderExtWayland for EventLoopBuilder {
/// Additional methods on [`Window`] that are specific to Wayland.
///
/// [`Window`]: crate::window::Window
pub trait WindowExtWayland {}
pub trait WindowExtWayland {
/// Returns `xdg_toplevel` of the window or [`None`] if the window is X11 window.
fn xdg_toplevel(&self) -> Option<NonNull<c_void>>;
}

impl WindowExtWayland for dyn CoreWindow + '_ {}
#[cfg(wayland_platform)]
impl WindowExtWayland for dyn CoreWindow + '_ {
#[inline]
fn xdg_toplevel(&self) -> Option<NonNull<c_void>> {
let w = self.as_any().downcast_ref::<crate::platform_impl::wayland::Window>()?;
let id = w.xdg_toplevel().id();
let ptr = NonNull::new(id.as_ptr().cast()).expect("xdg_toplevel should not be null");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you move this logic to wayland/window/mod.rs and return pointer from there instead, so we don't have an export here. We could also not fail when we can not cast.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

We could also not fail when we can not cast.

Sorry I don't understand what you mean. Do you mean we should use NonNull::new_unchecked instead of NonNull::new?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, that you just forward option up and not panic.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


Some(ptr)
}
}

/// Additional methods on [`WindowAttributes`] that are specific to Wayland.
pub trait WindowAttributesExtWayland {
Expand Down
6 changes: 6 additions & 0 deletions src/platform_impl/linux/wayland/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use sctk::reexports::client::protocol::wl_display::WlDisplay;
use sctk::reexports::client::protocol::wl_surface::WlSurface;
use sctk::reexports::client::{Proxy, QueueHandle};
use sctk::reexports::protocols::xdg::activation::v1::client::xdg_activation_v1::XdgActivationV1;
use sctk::reexports::protocols::xdg::shell::client::xdg_toplevel::XdgToplevel;
use sctk::shell::xdg::window::{Window as SctkWindow, WindowDecorations};
use sctk::shell::WaylandSurface;
use tracing::warn;
Expand Down Expand Up @@ -239,6 +240,11 @@ impl Window {
pub fn surface(&self) -> &WlSurface {
self.window.wl_surface()
}

#[inline]
pub fn xdg_toplevel(&self) -> &XdgToplevel {
self.window.xdg_toplevel()
}
}

impl Drop for Window {
Expand Down
Loading