diff --git a/gui/src/graphics/metal/window.rs b/gui/src/graphics/metal/window.rs index 6a807afb7..dad870ad0 100644 --- a/gui/src/graphics/metal/window.rs +++ b/gui/src/graphics/metal/window.rs @@ -1,5 +1,5 @@ use super::Metal; -use crate::rt::{Hook, WindowHandler}; +use crate::rt::{Hook, WindowHandler, WinitWindow}; use metal::{CAMetalLayer, MetalLayer}; use objc::runtime::{Object, NO, YES}; use objc::{msg_send, sel, sel_impl}; @@ -52,11 +52,20 @@ impl Drop for MetalWindow { } } -impl WindowHandler for MetalWindow { - fn window_id(&self) -> WindowId { +impl WinitWindow for MetalWindow { + fn id(&self) -> WindowId { self.window.id() } + fn handle(&self) -> impl HasWindowHandle + '_ + where + Self: Sized, + { + &self.window + } +} + +impl WindowHandler for MetalWindow { fn on_resized(&self, new: PhysicalSize) -> Result<(), Box> { todo!() } diff --git a/gui/src/graphics/vulkan/window.rs b/gui/src/graphics/vulkan/window.rs index 7b43526ac..11caf48f0 100644 --- a/gui/src/graphics/vulkan/window.rs +++ b/gui/src/graphics/vulkan/window.rs @@ -1,7 +1,8 @@ use super::engine::Vulkan; use super::GraphicsError; -use crate::rt::{Hook, WindowHandler}; +use crate::rt::{Hook, WindowHandler, WinitWindow}; use ash::vk::SurfaceKHR; +use raw_window_handle::HasWindowHandle; use std::error::Error; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -45,11 +46,27 @@ impl Drop for VulkanWindow { } } -impl WindowHandler for VulkanWindow { - fn window_id(&self) -> WindowId { +impl WinitWindow for VulkanWindow { + fn id(&self) -> WindowId { self.window.id() } + fn handle(&self) -> impl HasWindowHandle + '_ + where + Self: Sized, + { + &self.window + } + + #[cfg(target_os = "linux")] + fn xdg_toplevel(&self) -> *mut std::ffi::c_void { + use winit::platform::wayland::WindowExtWayland; + + self.window.xdg_toplevel() + } +} + +impl WindowHandler for VulkanWindow { fn on_resized(&self, _: PhysicalSize) -> Result<(), Box> { // Vulkan windows does not allowed to resize. Ok(()) diff --git a/gui/src/rt/mod.rs b/gui/src/rt/mod.rs index 76edb18a4..d257d2e55 100644 --- a/gui/src/rt/mod.rs +++ b/gui/src/rt/mod.rs @@ -165,7 +165,7 @@ pub fn create_window(attrs: WindowAttributes) -> Result { /// - If the underlying winit window on `win` already registered. /// - If called from [`Drop`] implementation. pub fn register_window(win: &Rc) { - let id = win.window_id(); + let id = win.id(); let win = Rc::downgrade(win); Context::with(move |cx| { diff --git a/gui/src/rt/window.rs b/gui/src/rt/window.rs index 61d3f91bb..e02d4219c 100644 --- a/gui/src/rt/window.rs +++ b/gui/src/rt/window.rs @@ -7,8 +7,7 @@ use winit::window::WindowId; /// Encapsulates winit window with window-specific logic. /// /// The event loop will exit immediately if any method return an error. -pub trait WindowHandler { - fn window_id(&self) -> WindowId; +pub trait WindowHandler: WinitWindow { fn on_resized(&self, new: PhysicalSize) -> Result<(), Box>; fn on_close_requested(&self) -> Result<(), Box>; fn on_focused(&self, gained: bool) -> Result<(), Box>; @@ -35,7 +34,9 @@ pub trait WindowHandler { /// Provides method to return winit properties. pub trait WinitWindow { fn id(&self) -> WindowId; - fn handle(&self) -> impl HasWindowHandle + '_; + fn handle(&self) -> impl HasWindowHandle + '_ + where + Self: Sized; #[cfg(target_os = "linux")] fn xdg_toplevel(&self) -> *mut std::ffi::c_void; } diff --git a/gui/src/ui/backend/mod.rs b/gui/src/ui/backend/mod.rs index d59cb9376..942be6f4a 100644 --- a/gui/src/ui/backend/mod.rs +++ b/gui/src/ui/backend/mod.rs @@ -2,7 +2,7 @@ pub(super) use self::wayland::*; pub(super) use self::window::Window; -use crate::rt::{create_window, raw_display_handle, WindowHandler}; +use crate::rt::{create_window, raw_display_handle, WinitWindow}; use i_slint_core::graphics::RequestedGraphicsAPI; use i_slint_renderer_skia::SkiaRenderer; #[cfg(target_os = "linux")] @@ -169,7 +169,7 @@ impl slint::platform::Platform for Platform { .unwrap() .windows .borrow_mut() - .insert(win.window_id(), Rc::downgrade(&win)) + .insert(win.id(), Rc::downgrade(&win)) .is_none()); crate::rt::register_window(&win); diff --git a/gui/src/ui/backend/window.rs b/gui/src/ui/backend/window.rs index 044b4c7c9..cd5d0fb37 100644 --- a/gui/src/ui/backend/window.rs +++ b/gui/src/ui/backend/window.rs @@ -1,4 +1,4 @@ -use crate::rt::{Signal, WindowHandler}; +use crate::rt::{Signal, WindowHandler, WinitWindow}; use i_slint_core::window::WindowAdapterInternal; use i_slint_core::InternalToken; use i_slint_renderer_skia::SkiaRenderer; @@ -66,11 +66,27 @@ impl Window { } } -impl WindowHandler for Window { - fn window_id(&self) -> WindowId { +impl WinitWindow for Window { + fn id(&self) -> WindowId { self.winit.id() } + fn handle(&self) -> impl HasWindowHandle + '_ + where + Self: Sized, + { + &self.winit + } + + #[cfg(target_os = "linux")] + fn xdg_toplevel(&self) -> *mut std::ffi::c_void { + use winit::platform::wayland::WindowExtWayland; + + self.winit.xdg_toplevel() + } +} + +impl WindowHandler for Window { fn on_resized( &self, new: winit::dpi::PhysicalSize, diff --git a/gui/src/ui/mod.rs b/gui/src/ui/mod.rs index fdc0890f4..38087f959 100644 --- a/gui/src/ui/mod.rs +++ b/gui/src/ui/mod.rs @@ -56,7 +56,7 @@ impl WinitWindow for T { fn id(&self) -> WindowId { let win = WindowInner::from_pub(self.window()).window_adapter(); - Window::from_adapter(win.as_ref()).winit().id() + Window::from_adapter(win.as_ref()).id() } fn handle(&self) -> impl HasWindowHandle + '_ { @@ -65,11 +65,9 @@ impl WinitWindow for T { #[cfg(target_os = "linux")] fn xdg_toplevel(&self) -> *mut std::ffi::c_void { - use winit::platform::wayland::WindowExtWayland; - let win = WindowInner::from_pub(self.window()).window_adapter(); - Window::from_adapter(win.as_ref()).winit().xdg_toplevel() + Window::from_adapter(win.as_ref()).xdg_toplevel() } }