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")]
mod win;
#[cfg(target_os = "windows")]
pub use win::*;
#[cfg(target_os = "linux")]
mod x11;
#[cfg(target_os = "linux")]
pub use crate::x11::*;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::*;
mod event;
mod keyboard;
mod mouse_cursor;
mod window;
mod window_info;
mod window_open_options;
pub use event::*;
pub use mouse_cursor::MouseCursor;
pub use window::*;
pub use window_info::*;
pub use window_open_options::*;

View file

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