1
0
Fork 0

Add cross-platform Window wrapper, currently only macOS support

This commit is contained in:
Joakim Frostegård 2020-11-23 19:52:50 +01:00
parent ff09c3a256
commit 4ba64d8f86
3 changed files with 48 additions and 15 deletions

View file

@ -2,26 +2,21 @@ use raw_window_handle::RawWindowHandle;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
mod win; mod win;
#[cfg(target_os = "windows")]
pub use win::*;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
mod x11; mod x11;
#[cfg(target_os = "linux")]
pub use crate::x11::*;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
mod macos; mod macos;
#[cfg(target_os = "macos")]
pub use macos::*;
mod event; mod event;
mod keyboard; mod keyboard;
mod mouse_cursor; mod mouse_cursor;
mod window;
mod window_info; mod window_info;
mod window_open_options; mod window_open_options;
pub use event::*; pub use event::*;
pub use mouse_cursor::MouseCursor; pub use mouse_cursor::MouseCursor;
pub use window::*;
pub use window_info::*; pub use window_info::*;
pub use window_open_options::*; pub use window_open_options::*;

View file

@ -58,12 +58,12 @@ impl WindowHandle {
impl Window { impl Window {
pub fn open<H, B>(options: WindowOpenOptions, build: B) -> WindowHandle pub fn open<H, B>(options: WindowOpenOptions, build: B) -> WindowHandle
where H: WindowHandler, where H: WindowHandler,
B: FnOnce(&mut Window) -> H, B: FnOnce(&mut crate::window::Window) -> H,
B: Send + 'static B: Send + 'static
{ {
let _pool = unsafe { NSAutoreleasePool::new(nil) }; let _pool = unsafe { NSAutoreleasePool::new(nil) };
let mut window = match options.parent { let window = match options.parent {
Parent::WithParent(parent) => { Parent::WithParent(parent) => {
if let RawWindowHandle::MacOS(handle) = parent { if let RawWindowHandle::MacOS(handle) = parent {
let ns_view = handle.ns_view as *mut objc::runtime::Object; let ns_view = handle.ns_view as *mut objc::runtime::Object;
@ -155,10 +155,12 @@ impl Window {
}, },
}; };
let mut window = crate::window::Window(window);
let window_handler = build(&mut window); let window_handler = build(&mut window);
let window_state_arc = Arc::new(WindowState { let window_state_arc = Arc::new(WindowState {
window, window: window,
window_handler, window_handler,
keyboard_state: KeyboardState::new(), keyboard_state: KeyboardState::new(),
}); });
@ -168,7 +170,7 @@ impl Window {
) as *mut c_void; ) as *mut c_void;
unsafe { unsafe {
(*window_state_arc.window.ns_view).set_ivar( (*window_state_arc.window.0.ns_view).set_ivar(
WINDOW_STATE_IVAR_NAME, WINDOW_STATE_IVAR_NAME,
window_state_pointer window_state_pointer
); );
@ -183,13 +185,13 @@ impl Window {
let timer: id = msg_send![ let timer: id = msg_send![
::objc::class!(NSTimer), ::objc::class!(NSTimer),
scheduledTimerWithTimeInterval:timer_interval scheduledTimerWithTimeInterval:timer_interval
target:window_state_arc.window.ns_view target:window_state_arc.window.0.ns_view
selector:selector selector:selector
userInfo:nil userInfo:nil
repeats:YES repeats:YES
]; ];
(*window_state_arc.window.ns_view).set_ivar( (*window_state_arc.window.0.ns_view).set_ivar(
FRAME_TIMER_IVAR_NAME, FRAME_TIMER_IVAR_NAME,
timer as *mut c_void, timer as *mut c_void,
) )
@ -201,7 +203,7 @@ impl Window {
pub(super) struct WindowState<H: WindowHandler> { pub(super) struct WindowState<H: WindowHandler> {
window: Window, window: crate::window::Window,
window_handler: H, window_handler: H,
keyboard_state: KeyboardState, keyboard_state: KeyboardState,
} }

36
src/window.rs Normal file
View file

@ -0,0 +1,36 @@
use crate::WindowHandler;
use crate::window_open_options::WindowOpenOptions;
#[cfg(target_os = "windows")]
use crate::win as platform;
#[cfg(target_os = "linux")]
use crate::x11 as platform;
#[cfg(target_os = "macos")]
use crate::macos as platform;
pub struct WindowHandle(platform::WindowHandle);
impl WindowHandle {
pub fn app_run_blocking(self){
self.0.app_run_blocking();
}
}
pub struct Window(pub(crate) platform::Window);
impl Window {
pub fn open<H, B>(
options: WindowOpenOptions,
build: B
) -> WindowHandle
where H: WindowHandler,
B: FnOnce(&mut Window) -> H,
B: Send + 'static
{
WindowHandle(platform::Window::open::<H, B>(options, build))
}
}