Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/platform/macos/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ impl WindowContext {

pub fn request_close(&self) {
let Some(view) = self.view.load() else { return };
BaseviewView::close(view.inner_ref());
let Some(view) = view.inner_ref() else { return };
BaseviewView::close(view);
}

pub fn has_focus(&self) -> bool {
Expand Down Expand Up @@ -58,7 +59,7 @@ impl WindowContext {

pub fn resize(&self, size: Size) {
let Some(view) = self.view.load() else { return };
let view = view.inner_ref();
let Some(view) = view.inner_ref() else { return };
if view.inner.state.closed.get() {
return;
}
Expand All @@ -82,7 +83,7 @@ impl WindowContext {

#[cfg(feature = "opengl")]
pub fn gl_context(&self) -> Option<crate::gl::GlContext> {
Some(crate::gl::GlContext::new(self.view.load()?.inner().gl_context.get()?.clone()))
Some(crate::gl::GlContext::new(self.view.load()?.inner()?.gl_context.get()?.clone()))
}

pub fn window_handle(&self) -> Option<raw_window_handle::WindowHandle<'_>> {
Expand Down
8 changes: 6 additions & 2 deletions src/platform/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,18 @@ impl BaseviewView {
let timer_view = Weak::new(view.view);
view.frame_timer.set(TimerHandle::new(0.015, move || {
if let Some(view) = timer_view.load() {
Self::trigger_frame(view.inner_ref());
if let Some(view) = view.inner_ref() {
Self::trigger_frame(view);
}
}
}));

let notifier_view = Weak::new(view.view);
let observer = NotificationCenterObserver::register_window_key_change(move |n| {
if let Some(view) = notifier_view.load() {
BaseviewView::handle_notification(view.inner_ref(), n);
if let Some(view) = view.inner_ref() {
BaseviewView::handle_notification(view, n);
}
}
});
view.notification_center_observer.set(Some(observer));
Expand Down
17 changes: 9 additions & 8 deletions src/platform/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ pub struct WindowHandle {
state: Rc<WindowSharedState>,
}

impl Drop for WindowHandle {
fn drop(&mut self) {
let Some(view) = self.view.load() else { return };
let Some(view) = view.inner_ref() else { return };

BaseviewView::close(view);
}
}

impl WindowHandle {
pub fn create_window(mut options: WindowOpenOptions, handler: WindowHandlerBuilder) -> Self {
autoreleasepool(|_| {
Expand Down Expand Up @@ -75,14 +84,6 @@ impl WindowHandle {
NSApplication::sharedApplication(self.mtm).run();
}

pub fn close(&self) {
let Some(view) = self.view.load() else {
return;
};

BaseviewView::close(view.inner_ref());
}

pub fn is_open(&self) -> bool {
self.state.closed.get()
}
Expand Down
32 changes: 16 additions & 16 deletions src/platform/win/window.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use windows_core::{ComObject, Result, HSTRING};
use windows_sys::Win32::{
Foundation::{HWND, LPARAM, LRESULT, RECT, WPARAM},
Foundation::{LPARAM, LRESULT, RECT, WPARAM},
UI::{
Controls::WM_MOUSELEAVE,
WindowsAndMessaging::{
PostMessageW, HTCLIENT, WHEEL_DELTA, WM_CHAR, WM_CLOSE, WM_DPICHANGED,
WM_INPUTLANGCHANGE, WM_KEYDOWN, WM_KEYUP, WM_KILLFOCUS, WM_LBUTTONDOWN, WM_LBUTTONUP,
WM_MBUTTONDOWN, WM_MBUTTONUP, WM_MOUSEHWHEEL, WM_MOUSEMOVE, WM_MOUSEWHEEL,
WM_RBUTTONDOWN, WM_RBUTTONUP, WM_SETCURSOR, WM_SETFOCUS, WM_SIZE, WM_SYSCHAR,
WM_SYSKEYDOWN, WM_SYSKEYUP, WM_TIMER, WM_USER, WM_XBUTTONDOWN, WM_XBUTTONUP,
HTCLIENT, WHEEL_DELTA, WM_CHAR, WM_CLOSE, WM_DPICHANGED, WM_INPUTLANGCHANGE,
WM_KEYDOWN, WM_KEYUP, WM_KILLFOCUS, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MBUTTONDOWN,
WM_MBUTTONUP, WM_MOUSEHWHEEL, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_RBUTTONDOWN,
WM_RBUTTONUP, WM_SETCURSOR, WM_SETFOCUS, WM_SIZE, WM_SYSCHAR, WM_SYSKEYDOWN,
WM_SYSKEYUP, WM_TIMER, WM_USER, WM_XBUTTONDOWN, WM_XBUTTONUP,
},
},
};
Expand Down Expand Up @@ -50,7 +50,7 @@ const WIN_FRAME_TIMER: NonZeroUsize = match NonZeroUsize::new(4242) {
};

pub struct WindowHandle {
hwnd: Cell<Option<HWND>>,
hwnd: Cell<Option<HWnd>>,
is_open: Rc<Cell<bool>>,
}

Expand All @@ -59,19 +59,19 @@ impl WindowHandle {
run_thread_message_loop_until(|| !self.is_open()).unwrap();
}

pub fn close(&self) {
if let Some(hwnd) = self.hwnd.take() {
unsafe {
PostMessageW(hwnd, BV_WINDOW_MUST_CLOSE, 0, 0);
}
}
}

pub fn is_open(&self) -> bool {
self.is_open.get()
}
}

impl Drop for WindowHandle {
fn drop(&mut self) {
if let Some(hwnd) = self.hwnd.take() {
let _ = hwnd.destroy();
}
}
}

struct ParentHandle {
is_open: Rc<Cell<bool>>,
}
Expand Down Expand Up @@ -474,7 +474,7 @@ impl WindowHandle {

window.show_and_activate();

WindowHandle { hwnd: Some(hwnd).into(), is_open: Rc::clone(&is_open) }
WindowHandle { hwnd: Some(window).into(), is_open: Rc::clone(&is_open) }
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/platform/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ impl WindowHandle {
});
}

pub fn close(&self) {
pub fn is_open(&self) -> bool {
self.is_open.load(Ordering::Relaxed)
}
}

impl Drop for WindowHandle {
fn drop(&mut self) {
self.close_requested.store(true, Ordering::Relaxed);
if let Some(event_loop) = self.event_loop_handle.take() {
let _ = event_loop.join();
}
}

pub fn is_open(&self) -> bool {
self.is_open.load(Ordering::Relaxed)
}
}

pub(crate) struct ParentHandle {
Expand Down
4 changes: 2 additions & 2 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl WindowHandle {
}

/// Close the window
pub fn close(&self) {
self.window_handle.close();
pub fn close(self) {
drop(self)
}

/// Returns `true` if the window is still open, and returns `false`
Expand Down
19 changes: 12 additions & 7 deletions src/wrappers/appkit/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<V: ViewImpl> View<V> {

let view: Retained<View<V>> = unsafe { msg_send![view, initWithFrame: frame] };

init(view.inner_ref());
init(view.inner_ref().unwrap());

view
}
Expand All @@ -68,23 +68,28 @@ impl<V: ViewImpl> View<V> {
let ivar = class.instance_variable(BASEVIEW_STATE_IVAR).unwrap();
let ivar = unsafe { ivar.load_ptr::<*mut c_void>(this) };
let raw = unsafe { ivar.read() };

if raw.is_null() {
return;
}

let inner = unsafe { Box::<ViewInner<V>>::from_raw(raw.cast()) };
unsafe { ivar.write(core::ptr::null_mut()) };
drop(inner);
}

fn get_inner(&self) -> &ViewInner<V> {
fn get_inner(&self) -> Option<&ViewInner<V>> {
let ivar = self.class().instance_variable(BASEVIEW_STATE_IVAR).unwrap();
let ivar = unsafe { ivar.load::<*mut c_void>(self) };
unsafe { ivar.cast::<ViewInner<V>>().as_ref() }.unwrap()
unsafe { ivar.cast::<ViewInner<V>>().as_ref() }
}

pub fn inner(&self) -> &V {
&self.get_inner().inner
pub fn inner(&self) -> Option<&V> {
Some(&self.get_inner()?.inner)
}

pub fn inner_ref(&self) -> ViewRef<'_, V> {
ViewRef { view: self, inner: self.inner() }
pub fn inner_ref(&self) -> Option<ViewRef<'_, V>> {
Some(ViewRef { view: self, inner: self.inner()? })
}

pub fn window_handle_from_weak(this: &Weak<Self>) -> Option<WindowHandle<'_>> {
Expand Down
Loading
Loading