Add raw window handle implementations to GPUI (#7101)
This is in preparation for experiments with wgpu. This should have no external effect. Release Notes: - N/A
This commit is contained in:
@@ -8,13 +8,14 @@ mod test;
|
||||
use crate::{
|
||||
Action, AnyWindowHandle, AsyncWindowContext, BackgroundExecutor, Bounds, DevicePixels, Font,
|
||||
FontId, FontMetrics, FontRun, ForegroundExecutor, GlobalPixels, GlyphId, Keymap, LineLayout,
|
||||
Pixels, PlatformInput, Point, RenderGlyphParams, RenderImageParams, RenderSvgParams, Result,
|
||||
Scene, SharedString, Size, Task, TaskLabel, WindowContext,
|
||||
Pixels, PlatformInput, Point, RenderGlyphParams, RenderImageParams, RenderSvgParams, Scene,
|
||||
SharedString, Size, Task, TaskLabel, WindowContext,
|
||||
};
|
||||
use anyhow::anyhow;
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_task::Runnable;
|
||||
use futures::channel::oneshot;
|
||||
use parking::Unparker;
|
||||
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
|
||||
use seahash::SeaHasher;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::borrow::Cow;
|
||||
@@ -138,7 +139,7 @@ impl Debug for DisplayId {
|
||||
|
||||
unsafe impl Send for DisplayId {}
|
||||
|
||||
pub(crate) trait PlatformWindow {
|
||||
pub(crate) trait PlatformWindow: HasWindowHandle + HasDisplayHandle {
|
||||
fn bounds(&self) -> WindowBounds;
|
||||
fn content_size(&self) -> Size<Pixels>;
|
||||
fn scale_factor(&self) -> f32;
|
||||
|
||||
@@ -32,6 +32,10 @@ use objc::{
|
||||
sel, sel_impl,
|
||||
};
|
||||
use parking_lot::Mutex;
|
||||
use raw_window_handle::{
|
||||
AppKitDisplayHandle, AppKitWindowHandle, DisplayHandle, HasDisplayHandle, HasWindowHandle,
|
||||
RawWindowHandle, WindowHandle,
|
||||
};
|
||||
use smallvec::SmallVec;
|
||||
use std::{
|
||||
any::Any,
|
||||
@@ -41,7 +45,7 @@ use std::{
|
||||
ops::Range,
|
||||
os::raw::c_char,
|
||||
path::PathBuf,
|
||||
ptr,
|
||||
ptr::{self, NonNull},
|
||||
rc::Rc,
|
||||
sync::{Arc, Weak},
|
||||
time::Duration,
|
||||
@@ -316,6 +320,7 @@ struct MacWindowState {
|
||||
handle: AnyWindowHandle,
|
||||
executor: ForegroundExecutor,
|
||||
native_window: id,
|
||||
native_view: NonNull<id>,
|
||||
renderer: MetalRenderer,
|
||||
kind: WindowKind,
|
||||
request_frame_callback: Option<Box<dyn FnMut()>>,
|
||||
@@ -523,6 +528,7 @@ impl MacWindow {
|
||||
handle,
|
||||
executor,
|
||||
native_window,
|
||||
native_view: NonNull::new_unchecked(native_view as *mut _),
|
||||
renderer: MetalRenderer::new(true),
|
||||
kind: options.kind,
|
||||
request_frame_callback: None,
|
||||
@@ -1011,6 +1017,28 @@ impl PlatformWindow for MacWindow {
|
||||
}
|
||||
}
|
||||
|
||||
impl HasWindowHandle for MacWindow {
|
||||
fn window_handle(
|
||||
&self,
|
||||
) -> Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
|
||||
// SAFETY: The AppKitWindowHandle is a wrapper around a pointer to an NSView
|
||||
unsafe {
|
||||
Ok(WindowHandle::borrow_raw(RawWindowHandle::AppKit(
|
||||
AppKitWindowHandle::new(self.0.lock().native_view.cast()),
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HasDisplayHandle for MacWindow {
|
||||
fn display_handle(
|
||||
&self,
|
||||
) -> Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
|
||||
// SAFETY: This is a no-op on macOS
|
||||
unsafe { Ok(DisplayHandle::borrow_raw(AppKitDisplayHandle::new().into())) }
|
||||
}
|
||||
}
|
||||
|
||||
fn get_scale_factor(native_window: id) -> f32 {
|
||||
let factor = unsafe {
|
||||
let screen: id = msg_send![native_window, screen];
|
||||
|
||||
@@ -5,6 +5,7 @@ use crate::{
|
||||
};
|
||||
use collections::HashMap;
|
||||
use parking_lot::Mutex;
|
||||
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
|
||||
use std::{
|
||||
rc::{Rc, Weak},
|
||||
sync::{self, Arc},
|
||||
@@ -29,6 +30,22 @@ pub(crate) struct TestWindowState {
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct TestWindow(pub(crate) Arc<Mutex<TestWindowState>>);
|
||||
|
||||
impl HasWindowHandle for TestWindow {
|
||||
fn window_handle(
|
||||
&self,
|
||||
) -> Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
|
||||
unimplemented!("Test Windows are not backed by a real platform window")
|
||||
}
|
||||
}
|
||||
|
||||
impl HasDisplayHandle for TestWindow {
|
||||
fn display_handle(
|
||||
&self,
|
||||
) -> Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
|
||||
unimplemented!("Test Windows are not backed by a real platform window")
|
||||
}
|
||||
}
|
||||
|
||||
impl TestWindow {
|
||||
pub fn new(
|
||||
options: WindowOptions,
|
||||
|
||||
Reference in New Issue
Block a user