1
0
Fork 0

api unification: make cross-platform Window hold a reference

This commit is contained in:
Joakim Frostegård 2020-11-23 21:59:17 +01:00
parent f9226c366e
commit 185bd62447
3 changed files with 16 additions and 16 deletions

View file

@ -63,7 +63,7 @@ impl Window {
{ {
let _pool = unsafe { NSAutoreleasePool::new(nil) }; let _pool = unsafe { NSAutoreleasePool::new(nil) };
let window = match options.parent { let mut 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,12 +155,10 @@ impl Window {
}, },
}; };
let mut window = crate::window::Window(window); let window_handler = build(&mut crate::window::Window(&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(),
}); });
@ -170,7 +168,7 @@ impl Window {
) as *mut c_void; ) as *mut c_void;
unsafe { unsafe {
(*window_state_arc.window.0.ns_view).set_ivar( (*window_state_arc.window.ns_view).set_ivar(
WINDOW_STATE_IVAR_NAME, WINDOW_STATE_IVAR_NAME,
window_state_pointer window_state_pointer
); );
@ -185,13 +183,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.0.ns_view target:window_state_arc.window.ns_view
selector:selector selector:selector
userInfo:nil userInfo:nil
repeats:YES repeats:YES
]; ];
(*window_state_arc.window.0.ns_view).set_ivar( (*window_state_arc.window.ns_view).set_ivar(
FRAME_TIMER_IVAR_NAME, FRAME_TIMER_IVAR_NAME,
timer as *mut c_void, timer as *mut c_void,
) )
@ -203,7 +201,7 @@ impl Window {
pub(super) struct WindowState<H: WindowHandler> { pub(super) struct WindowState<H: WindowHandler> {
window: crate::window::Window, window: Window,
window_handler: H, window_handler: H,
keyboard_state: KeyboardState, keyboard_state: KeyboardState,
} }
@ -222,7 +220,10 @@ impl <H: WindowHandler>WindowState<H> {
} }
pub(super) fn trigger_event(&mut self, event: Event){ pub(super) fn trigger_event(&mut self, event: Event){
self.window_handler.on_event(&mut self.window, event); self.window_handler.on_event(
&mut crate::window::Window(&mut self.window),
event
);
} }
pub(super) fn trigger_frame(&mut self){ pub(super) fn trigger_frame(&mut self){

View file

@ -72,7 +72,8 @@ unsafe extern "system" fn wnd_proc<H: WindowHandler>(
let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void; let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void;
if !win_ptr.is_null() { if !win_ptr.is_null() {
let window_state = &*(win_ptr as *const RefCell<WindowState<H>>); let window_state = &*(win_ptr as *const RefCell<WindowState<H>>);
let mut window = crate::window::Window(Window { hwnd }); let mut window = Window { hwnd };
let mut window = crate::window::Window(&mut window);
match msg { match msg {
WM_MOUSEMOVE => { WM_MOUSEMOVE => {
@ -240,9 +241,7 @@ impl Window {
); );
// todo: manage error ^ // todo: manage error ^
let mut window = crate::window::Window(Window { hwnd }); let handler = build(&mut crate::window::Window(&mut Window { hwnd }));
let handler = build(&mut window);
let window_state = Box::new(RefCell::new(WindowState { let window_state = Box::new(RefCell::new(WindowState {
window_class, window_class,

View file

@ -19,10 +19,10 @@ impl WindowHandle {
} }
pub struct Window(pub(crate) platform::Window); pub struct Window<'a>(pub(crate) &'a mut platform::Window);
impl Window { impl <'a>Window<'a> {
pub fn open<H, B>( pub fn open<H, B>(
options: WindowOpenOptions, options: WindowOpenOptions,
build: B build: B