Skip to content

Commit

Permalink
Makes WindowHandler requires WinitWindow (#1252)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Jan 19, 2025
1 parent f84826a commit 649a3fd
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 19 deletions.
15 changes: 12 additions & 3 deletions gui/src/graphics/metal/window.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<u32>) -> Result<(), Box<dyn Error + Send + Sync>> {
todo!()
}
Expand Down
23 changes: 20 additions & 3 deletions gui/src/graphics/vulkan/window.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<u32>) -> Result<(), Box<dyn Error + Send + Sync>> {
// Vulkan windows does not allowed to resize.
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion gui/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn create_window(attrs: WindowAttributes) -> Result<Window, OsError> {
/// - If the underlying winit window on `win` already registered.
/// - If called from [`Drop`] implementation.
pub fn register_window<T: WindowHandler + 'static>(win: &Rc<T>) {
let id = win.window_id();
let id = win.id();
let win = Rc::downgrade(win);

Context::with(move |cx| {
Expand Down
7 changes: 4 additions & 3 deletions gui/src/rt/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>) -> Result<(), Box<dyn Error + Send + Sync>>;
fn on_close_requested(&self) -> Result<(), Box<dyn Error + Send + Sync>>;
fn on_focused(&self, gained: bool) -> Result<(), Box<dyn Error + Send + Sync>>;
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions gui/src/ui/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 19 additions & 3 deletions gui/src/ui/backend/window.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<u32>,
Expand Down
6 changes: 2 additions & 4 deletions gui/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<T: ComponentHandle> 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 + '_ {
Expand All @@ -65,11 +65,9 @@ impl<T: ComponentHandle> 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()
}
}

Expand Down

0 comments on commit 649a3fd

Please sign in to comment.