Skip to content
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre

# Unreleased

- On macOS, fix `WindowEvent::Destroyed` isn't emitted when the window is dropped.
- On Windows and macOS, add `Window::title` to query the current window title.
- On Windows, fix focusing menubar when pressing `Alt`.
- On MacOS, made `accepts_first_mouse` configurable.
Expand Down
3 changes: 3 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ pub enum WindowEvent<'a> {
CloseRequested,

/// The window has been destroyed.
///
/// However, it won't get emitted if the window is closed by the event loop set to exit.
/// Use [`Event::LoopDestroyed`] to check such behaviour instead.
Destroyed,

/// A file has been dropped into the window.
Expand Down
8 changes: 4 additions & 4 deletions src/platform_impl/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ pub(crate) const DEVICE_ID: RootDeviceId = RootDeviceId(DeviceId);
pub(crate) struct Window {
pub(crate) window: Id<WinitWindow, Shared>,
// We keep this around so that it doesn't get dropped until the window does.
_delegate: Id<WinitWindowDelegate, Shared>,
pub(crate) delegate: Id<WinitWindowDelegate, Shared>,
}

impl Drop for Window {
fn drop(&mut self) {
// Ensure the window is closed
util::close_async(Id::into_super(self.window.clone()));
util::close_async(self.delegate.clone());
}
}

Expand All @@ -84,8 +84,8 @@ impl Window {
attributes: WindowAttributes,
pl_attribs: PlatformSpecificWindowBuilderAttributes,
) -> Result<Self, RootOsError> {
let (window, _delegate) = autoreleasepool(|_| WinitWindow::new(attributes, pl_attribs))?;
Ok(Window { window, _delegate })
let (window, delegate) = autoreleasepool(|_| WinitWindow::new(attributes, pl_attribs))?;
Ok(Window { window, delegate })
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/platform_impl/macos/util/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use dispatch::Queue;
use objc2::foundation::{is_main_thread, CGFloat, NSPoint, NSSize, NSString};
use objc2::rc::{autoreleasepool, Id, Shared};

use crate::platform_impl::platform::window_delegate::WinitWindowDelegate;
use crate::{
dpi::LogicalSize,
platform_impl::platform::{
Expand Down Expand Up @@ -218,11 +219,11 @@ pub(crate) fn set_title_async(window: &NSWindow, title: String) {
//
// ArturKovacs: It's important that this operation keeps the underlying window alive
// through the `Id` because otherwise it would dereference free'd memory
pub(crate) fn close_async(window: Id<NSWindow, Shared>) {
let window = MainThreadSafe(window);
pub(crate) fn close_async(delegate: Id<WinitWindowDelegate, Shared>) {
let delegate = MainThreadSafe(delegate);
Queue::main().exec_async(move || {
autoreleasepool(move |_| {
window.close();
delegate.window.close();
});
});
}
2 changes: 1 addition & 1 deletion src/platform_impl/macos/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
declare_class!(
#[derive(Debug)]
pub(crate) struct WinitWindowDelegate {
window: IvarDrop<Id<WinitWindow, Shared>>,
pub(crate) window: IvarDrop<Id<WinitWindow, Shared>>,

// TODO: It's possible for delegate methods to be called asynchronously,
// causing data races / `RefCell` panics.
Expand Down