Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions winit-core/src/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use raw_window_handle_05::HasRawDisplayHandle as HasRawDisplayHandle05;

use crate::application::Application;
use crate::monitor::{Monitor, MonitorId};
use crate::window::{Window, WindowAttributes, WindowId};
use crate::window::{Surface, WindowAttributes, WindowId};

use self::proxy::EventLoopProxy;

Expand Down Expand Up @@ -37,9 +37,9 @@ pub trait EventLoopHandle: HasDisplayHandle {

fn num_windows(&self) -> usize;

fn get_window(&self, window_id: WindowId) -> Option<&dyn Window>;
fn get_window(&self, window_id: WindowId) -> Option<&dyn Surface>;

fn get_window_mut(&mut self, window_id: WindowId) -> Option<&mut dyn Window>;
fn get_window_mut(&mut self, window_id: WindowId) -> Option<&mut dyn Surface>;

fn get_monitor(&self, monitor_id: MonitorId) -> Option<&dyn Monitor>;

Expand Down
43 changes: 34 additions & 9 deletions winit-core/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
// TODO figure out how to do WindowId.

use std::any::Any;

pub use raw_window_handle::HasWindowHandle;
pub use raw_window_handle_05::HasRawWindowHandle as HasRawWindowHandle05;

use crate::dpi::{LogicalSize, PhysicalSize, Position, Size};
use crate::dpi::{LogicalSize, PhysicalPosition, PhysicalSize, Position, Size};
use crate::monitor::MonitorId;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WindowId(pub u128);

/// Common requests to perform on the window.
pub trait Window: HasWindowHandle + HasRawWindowHandle05 {
pub enum WindowRole<'a> {
Toplevel(&'a dyn Toplevel),
Subview(&'a dyn Subview)
}

pub enum WindowRoleMut<'a> {
Toplevel(&'a mut dyn Toplevel),
Subview(&'a mut dyn Subview)
}

/// Common API for all rendering targets.
pub trait Surface: HasWindowHandle + HasRawWindowHandle05 {
fn id(&self) -> WindowId;

fn scale_factor(&self) -> f64;

fn request_redraw(&mut self);

fn inner_size(&self) -> PhysicalSize<u32>;

/// Downcasts this surface to its specific type.
fn role(&self) -> WindowRole;
/// Downcasts this surface to its specific type. Returns a mutable reference.
fn role_mut(&mut self) -> WindowRoleMut;
}

/// API for toplevel windows and dialogs.
pub trait Toplevel: Surface {
/// Gets the current title of the window.
fn title(&self) -> &str;

Expand All @@ -22,12 +48,6 @@ pub trait Window: HasWindowHandle + HasRawWindowHandle05 {

fn set_theme(&mut self, theme: Option<Theme>);

fn scale_factor(&self) -> f64;

fn request_redraw(&mut self);

fn inner_size(&self) -> PhysicalSize<u32>;

fn set_minimized(&mut self, minimize: bool);

fn set_maximized(&mut self, maximized: bool);
Expand All @@ -37,6 +57,11 @@ pub trait Window: HasWindowHandle + HasRawWindowHandle05 {
fn primary_monitor(&self) -> Option<MonitorId>;
}

/// API for subviews (window style `WS_CHILD` on Windows, `NSView` on MacOS, `wl_subsurface` on Wayland).
pub trait Subview: Surface {
fn set_position(&self, position: PhysicalPosition<u32>);
}

/// Attributes to use when creating a window.
#[derive(Debug, Clone)]
pub struct WindowAttributes {
Expand Down
10 changes: 6 additions & 4 deletions winit-wayland/examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::Duration;
use winit_core::application::{Application, ApplicationWindow, StartCause};
use winit_core::dpi::PhysicalSize;
use winit_core::event_loop::{EventLoopHandle, EventLoopRequests};
use winit_core::window::WindowId;
use winit_core::window::{Toplevel, WindowId, WindowRole};
use winit_wayland::event_loop::EventLoop;
use winit_wayland::MyCoolTrait;

Expand Down Expand Up @@ -89,9 +89,11 @@ impl ApplicationWindow for State {
_ => return,
};

if let Some(monitor_id) = window.current_monitor() {
let monitor = loop_handle.get_monitor(monitor_id).unwrap();
println!("Current monitor name {:?}", monitor.name());
if let WindowRole::Toplevel(toplevel) = window.role() {
if let Some(monitor_id) = toplevel.current_monitor() {
let monitor = loop_handle.get_monitor(monitor_id).unwrap();
println!("Current monitor name {:?}", monitor.name());
}
}

let size = window.inner_size();
Expand Down
2 changes: 1 addition & 1 deletion winit-wayland/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use sctk::seat::{Capability as SeatCapability, SeatHandler, SeatState};
use winit_core::application::Application;
use winit_core::event_loop::proxy::EventLoopProxy as CoreEventLoopProxy;
use winit_core::event_loop::{EventLoopHandle, EventLoopRequests};
use winit_core::window::{Window as CoreWindow, WindowId};
use winit_core::window::{Surface as CoreSurface, WindowId};

use crate::state::WinitState;
use crate::MyCoolTrait;
Expand Down
20 changes: 10 additions & 10 deletions winit-wayland/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use sctk::subcompositor::SubcompositorState;
use winit_core::application::Application;
use winit_core::event_loop::proxy::EventLoopProxy as CoreEventLoopProxy;
use winit_core::event_loop::EventLoopHandle;
use winit_core::monitor::{Monitor as CoreMonitor, MonitorId};
use winit_core::window::{Window as CoreWindow, WindowAttributes, WindowId};
use winit_core::monitor::{Monitor as WinitMonitor, MonitorId};
use winit_core::window::{Surface as WinitSurface, WindowAttributes, WindowId};

use crate::monitor::Monitor;
use crate::window::Window;
Expand All @@ -52,38 +52,38 @@ impl<T: Application + 'static> EventLoopHandle for WinitState<T> {
self.windows.len()
}

fn get_window(&self, window_id: WindowId) -> Option<&dyn CoreWindow> {
fn get_window(&self, window_id: WindowId) -> Option<&dyn WinitSurface> {
let window = self.windows.get(&window_id)?;

if window.last_configure.is_none() {
return None;
} else {
Some(window as &dyn CoreWindow)
Some(window as &dyn WinitSurface)
}
}

fn get_window_mut(&mut self, window_id: WindowId) -> Option<&mut dyn CoreWindow> {
fn get_window_mut(&mut self, window_id: WindowId) -> Option<&mut dyn WinitSurface> {
let window = self.windows.get_mut(&window_id)?;
if window.last_configure.is_none() {
return None;
} else {
Some(window as &mut dyn CoreWindow)
Some(window as &mut dyn WinitSurface)
}
}

fn exit(&mut self) {
self.exit = true;
}

fn get_monitor(&self, monitor_id: MonitorId) -> Option<&dyn CoreMonitor> {
fn get_monitor(&self, monitor_id: MonitorId) -> Option<&dyn WinitMonitor> {
self.monitors
.iter()
.find(|monitor| monitor.id() == monitor_id)
.map(|monitor| monitor as &dyn CoreMonitor)
.map(|monitor| monitor as &dyn WinitMonitor)
}

fn monitors(&self) -> Vec<&dyn CoreMonitor> {
self.monitors.iter().map(|monitor| monitor as &dyn CoreMonitor).collect()
fn monitors(&self) -> Vec<&dyn WinitMonitor> {
self.monitors.iter().map(|monitor| monitor as &dyn WinitMonitor).collect()
}
}

Expand Down
52 changes: 31 additions & 21 deletions winit-wayland/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use wayland_client::{Connection, QueueHandle};
use winit_core::application::Application;
use winit_core::dpi::{LogicalSize, PhysicalSize, Size};
use winit_core::monitor::MonitorId;
use winit_core::window::{Theme, Window as CoreWindow, WindowAttributes, WindowId};
use winit_core::window::{Surface as WinitSurface, Theme, Toplevel as WinitToplevel, WindowAttributes, WindowId, WindowRole, WindowRoleMut};

use crate::event_loop::RuntimeState;
use crate::logical_to_physical_rounded;
Expand Down Expand Up @@ -295,7 +295,18 @@ impl<T: Application + 'static> Window<T> {
}
}

impl<T: Application + 'static> CoreWindow for Window<T> {
impl<T: Application + 'static> HasWindowHandle for Window<T> {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
let ptr = self.window.wl_surface().id().as_ptr();
let handle = WaylandWindowHandle::new({
std::ptr::NonNull::new(ptr as *mut _).expect("wl_surface should never be null")
});

unsafe { Ok(WindowHandle::borrow_raw(handle.into())) }
}
}

impl<T: Application + 'static> WinitSurface for Window<T> {
fn id(&self) -> WindowId {
crate::make_wid(&self.window.wl_surface())
}
Expand All @@ -304,6 +315,24 @@ impl<T: Application + 'static> CoreWindow for Window<T> {
self.redraw = true;
}

fn scale_factor(&self) -> f64 {
self.scale_factor
}

fn inner_size(&self) -> PhysicalSize<u32> {
crate::logical_to_physical_rounded(self.size, self.scale_factor)
}

fn role(&self) -> WindowRole<'_> {
WindowRole::Toplevel(self)
}

fn role_mut(&mut self) -> WindowRoleMut<'_> {
WindowRoleMut::Toplevel(self)
}
}

impl<T: Application + 'static> WinitToplevel for Window<T> {
fn title(&self) -> &str {
&self.title
}
Expand Down Expand Up @@ -341,14 +370,6 @@ impl<T: Application + 'static> CoreWindow for Window<T> {
self.title = title;
}

fn scale_factor(&self) -> f64 {
self.scale_factor
}

fn inner_size(&self) -> PhysicalSize<u32> {
crate::logical_to_physical_rounded(self.size, self.scale_factor)
}

fn set_minimized(&mut self, minimize: bool) {
if minimize {
self.window.set_minimized();
Expand All @@ -373,17 +394,6 @@ impl<T: Application + 'static> CoreWindow for Window<T> {
}
}

impl<T: Application + 'static> HasWindowHandle for Window<T> {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
let ptr = self.window.wl_surface().id().as_ptr();
let handle = WaylandWindowHandle::new({
std::ptr::NonNull::new(ptr as *mut _).expect("wl_surface will never be null")
});

unsafe { Ok(WindowHandle::borrow_raw(handle.into())) }
}
}

impl<T: Application + 'static> WindowHandler for RuntimeState<T> {
fn request_close(&mut self, _: &Connection, _: &QueueHandle<Self>, window: &XdgWindow) {
let window_id = crate::make_wid(window.wl_surface());
Expand Down