api: convert Window to dyn Window

This should allow us to make future split of backends much easier.
The `Box<dyn Window>` is a _temporary_ solution, which will be
removed with the future updates when we decide on how the Window
should be stored.
This commit is contained in:
Kirill Chibisov 2024-08-23 23:40:27 +03:00 committed by GitHub
parent e716adcc0a
commit 241b7a80bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
41 changed files with 2625 additions and 2448 deletions

View file

@ -18,8 +18,7 @@ use tracing::warn;
use x11rb::connection::RequestConnection;
use x11rb::errors::{ConnectError, ConnectionError, IdsExhausted, ReplyError};
use x11rb::protocol::xinput::{self, ConnectionExt as _};
use x11rb::protocol::xkb;
use x11rb::protocol::xproto::{self, ConnectionExt as _};
use x11rb::protocol::{xkb, xproto};
use x11rb::x11_utils::X11Error as LogicalError;
use x11rb::xcb_ffi::ReplyOrIdError;
@ -33,9 +32,11 @@ use crate::event_loop::{
use crate::platform::pump_events::PumpStatus;
use crate::platform_impl::common::xkb::Context;
use crate::platform_impl::platform::{min_timeout, WindowId};
use crate::platform_impl::x11::window::Window;
use crate::platform_impl::{OsError, OwnedDisplayHandle, PlatformCustomCursor};
use crate::window::{
CustomCursor as RootCustomCursor, CustomCursorSource, Theme, WindowAttributes,
CustomCursor as RootCustomCursor, CustomCursorSource, Theme, Window as CoreWindow,
WindowAttributes,
};
mod activation;
@ -46,7 +47,7 @@ pub mod ffi;
mod ime;
mod monitor;
mod util;
mod window;
pub(crate) mod window;
mod xdisplay;
mod xsettings;
@ -687,10 +688,8 @@ impl RootActiveEventLoop for ActiveEventLoop {
fn create_window(
&self,
window_attributes: WindowAttributes,
) -> Result<crate::window::Window, RootOsError> {
let window = crate::platform_impl::x11::Window::new(self, window_attributes)?;
let window = crate::platform_impl::Window::X(window);
Ok(crate::window::Window { window })
) -> Result<Box<dyn CoreWindow>, RootOsError> {
Ok(Box::new(Window::new(self, window_attributes)?))
}
fn create_custom_cursor(
@ -827,39 +826,6 @@ impl FingerId {
}
}
pub(crate) struct Window(Arc<UnownedWindow>);
impl Deref for Window {
type Target = UnownedWindow;
#[inline]
fn deref(&self) -> &UnownedWindow {
&self.0
}
}
impl Window {
pub(crate) fn new(
event_loop: &ActiveEventLoop,
attribs: WindowAttributes,
) -> Result<Self, RootOsError> {
let window = Arc::new(UnownedWindow::new(event_loop, attribs)?);
event_loop.windows.borrow_mut().insert(window.id(), Arc::downgrade(&window));
Ok(Window(window))
}
}
impl Drop for Window {
fn drop(&mut self) {
let window = self.deref();
let xconn = &window.xconn;
if let Ok(c) = xconn.xcb_connection().destroy_window(window.id().0 as xproto::Window) {
c.ignore_error();
}
}
}
#[derive(Clone)]
pub struct EventLoopProxy {
ping: Ping,

View file

@ -1,6 +1,7 @@
use std::ffi::CString;
use std::mem::replace;
use std::num::NonZeroU32;
use std::ops::Deref;
use std::os::raw::*;
use std::path::Path;
use std::sync::{Arc, Mutex, MutexGuard};
@ -30,14 +31,312 @@ use crate::platform_impl::x11::{
xinput_fp1616_to_float, MonitorHandle as X11MonitorHandle, WakeSender, X11Error,
};
use crate::platform_impl::{
Fullscreen, MonitorHandle as PlatformMonitorHandle, OsError, PlatformCustomCursor,
common, Fullscreen, MonitorHandle as PlatformMonitorHandle, OsError, PlatformCustomCursor,
PlatformIcon, VideoModeHandle as PlatformVideoModeHandle,
};
use crate::window::{
CursorGrabMode, ImePurpose, ResizeDirection, Theme, UserAttentionType, WindowAttributes,
WindowButtons, WindowLevel,
CursorGrabMode, ImePurpose, ResizeDirection, Theme, UserAttentionType, Window as CoreWindow,
WindowAttributes, WindowButtons, WindowLevel,
};
pub(crate) struct Window(Arc<UnownedWindow>);
impl Deref for Window {
type Target = UnownedWindow;
#[inline]
fn deref(&self) -> &UnownedWindow {
&self.0
}
}
impl Window {
pub(crate) fn new(
event_loop: &ActiveEventLoop,
attribs: WindowAttributes,
) -> Result<Self, RootOsError> {
let window = Arc::new(UnownedWindow::new(event_loop, attribs)?);
event_loop.windows.borrow_mut().insert(window.id(), Arc::downgrade(&window));
Ok(Window(window))
}
}
impl CoreWindow for Window {
fn id(&self) -> crate::window::WindowId {
crate::window::WindowId(self.0.id())
}
fn scale_factor(&self) -> f64 {
self.0.scale_factor()
}
fn request_redraw(&self) {
self.0.request_redraw()
}
fn pre_present_notify(&self) {
self.0.pre_present_notify()
}
fn reset_dead_keys(&self) {
common::xkb::reset_dead_keys();
}
fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
self.0.inner_position()
}
fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
self.0.outer_position()
}
fn set_outer_position(&self, position: Position) {
self.0.set_outer_position(position)
}
fn inner_size(&self) -> PhysicalSize<u32> {
self.0.inner_size()
}
fn request_inner_size(&self, size: Size) -> Option<PhysicalSize<u32>> {
self.0.request_inner_size(size)
}
fn outer_size(&self) -> PhysicalSize<u32> {
self.0.outer_size()
}
fn set_min_inner_size(&self, min_size: Option<Size>) {
self.0.set_min_inner_size(min_size)
}
fn set_max_inner_size(&self, max_size: Option<Size>) {
self.0.set_max_inner_size(max_size)
}
fn resize_increments(&self) -> Option<PhysicalSize<u32>> {
self.0.resize_increments()
}
fn set_resize_increments(&self, increments: Option<Size>) {
self.0.set_resize_increments(increments)
}
fn set_title(&self, title: &str) {
self.0.set_title(title);
}
fn set_transparent(&self, transparent: bool) {
self.0.set_transparent(transparent);
}
fn set_blur(&self, blur: bool) {
self.0.set_blur(blur);
}
fn set_visible(&self, visible: bool) {
self.0.set_visible(visible);
}
fn is_visible(&self) -> Option<bool> {
self.0.is_visible()
}
fn set_resizable(&self, resizable: bool) {
self.0.set_resizable(resizable);
}
fn is_resizable(&self) -> bool {
self.0.is_resizable()
}
fn set_enabled_buttons(&self, buttons: WindowButtons) {
self.0.set_enabled_buttons(buttons)
}
fn enabled_buttons(&self) -> WindowButtons {
self.0.enabled_buttons()
}
fn set_minimized(&self, minimized: bool) {
self.0.set_minimized(minimized)
}
fn is_minimized(&self) -> Option<bool> {
self.0.is_minimized()
}
fn set_maximized(&self, maximized: bool) {
self.0.set_maximized(maximized)
}
fn is_maximized(&self) -> bool {
self.0.is_maximized()
}
fn set_fullscreen(&self, fullscreen: Option<crate::window::Fullscreen>) {
self.0.set_fullscreen(fullscreen.map(Into::into))
}
fn fullscreen(&self) -> Option<crate::window::Fullscreen> {
self.0.fullscreen().map(Into::into)
}
fn set_decorations(&self, decorations: bool) {
self.0.set_decorations(decorations);
}
fn is_decorated(&self) -> bool {
self.0.is_decorated()
}
fn set_window_level(&self, level: WindowLevel) {
self.0.set_window_level(level);
}
fn set_window_icon(&self, window_icon: Option<crate::window::Icon>) {
self.0.set_window_icon(window_icon.map(|inner| inner.inner))
}
fn set_ime_cursor_area(&self, position: Position, size: Size) {
self.0.set_ime_cursor_area(position, size);
}
fn set_ime_allowed(&self, allowed: bool) {
self.0.set_ime_allowed(allowed);
}
fn set_ime_purpose(&self, purpose: ImePurpose) {
self.0.set_ime_purpose(purpose);
}
fn focus_window(&self) {
self.0.focus_window();
}
fn has_focus(&self) -> bool {
self.0.has_focus()
}
fn request_user_attention(&self, request_type: Option<UserAttentionType>) {
self.0.request_user_attention(request_type);
}
fn set_theme(&self, theme: Option<Theme>) {
self.0.set_theme(theme);
}
fn theme(&self) -> Option<Theme> {
self.0.theme()
}
fn set_content_protected(&self, protected: bool) {
self.0.set_content_protected(protected);
}
fn title(&self) -> String {
self.0.title()
}
fn set_cursor(&self, cursor: Cursor) {
self.0.set_cursor(cursor);
}
fn set_cursor_position(&self, position: Position) -> Result<(), ExternalError> {
self.0.set_cursor_position(position)
}
fn set_cursor_grab(&self, mode: CursorGrabMode) -> Result<(), ExternalError> {
self.0.set_cursor_grab(mode)
}
fn set_cursor_visible(&self, visible: bool) {
self.0.set_cursor_visible(visible);
}
fn drag_window(&self) -> Result<(), ExternalError> {
self.0.drag_window()
}
fn drag_resize_window(&self, direction: ResizeDirection) -> Result<(), ExternalError> {
self.0.drag_resize_window(direction)
}
fn show_window_menu(&self, position: Position) {
self.0.show_window_menu(position);
}
fn set_cursor_hittest(&self, hittest: bool) -> Result<(), ExternalError> {
self.0.set_cursor_hittest(hittest)
}
fn current_monitor(&self) -> Option<crate::monitor::MonitorHandle> {
self.0
.current_monitor()
.map(crate::platform_impl::MonitorHandle::X)
.map(|inner| crate::monitor::MonitorHandle { inner })
}
fn available_monitors(&self) -> Box<dyn Iterator<Item = crate::monitor::MonitorHandle>> {
Box::new(
self.0
.available_monitors()
.into_iter()
.map(crate::platform_impl::MonitorHandle::X)
.map(|inner| crate::monitor::MonitorHandle { inner }),
)
}
fn primary_monitor(&self) -> Option<crate::monitor::MonitorHandle> {
self.0
.primary_monitor()
.map(crate::platform_impl::MonitorHandle::X)
.map(|inner| crate::monitor::MonitorHandle { inner })
}
#[cfg(feature = "rwh_06")]
fn rwh_06_display_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self
}
#[cfg(feature = "rwh_06")]
fn rwh_06_window_handle(&self) -> &dyn rwh_06::HasWindowHandle {
self
}
}
#[cfg(feature = "rwh_06")]
impl rwh_06::HasDisplayHandle for Window {
fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
let raw = self.0.raw_display_handle_rwh_06()?;
unsafe { Ok(rwh_06::DisplayHandle::borrow_raw(raw)) }
}
}
#[cfg(feature = "rwh_06")]
impl rwh_06::HasWindowHandle for Window {
fn window_handle(&self) -> Result<rwh_06::WindowHandle<'_>, rwh_06::HandleError> {
let raw = self.0.raw_window_handle_rwh_06()?;
unsafe { Ok(rwh_06::WindowHandle::borrow_raw(raw)) }
}
}
impl Drop for Window {
fn drop(&mut self) {
let window = &self.0;
let xconn = &window.xconn;
// Restore the video mode on drop.
if let Some(Fullscreen::Exclusive(_)) = window.fullscreen() {
window.set_fullscreen(None);
}
if let Ok(c) = xconn.xcb_connection().destroy_window(window.id().0 as xproto::Window) {
c.ignore_error();
}
}
}
#[derive(Debug)]
pub struct SharedState {
pub cursor_pos: Option<(f64, f64)>,