Skip to content

Commit

Permalink
Accepts first mouse (rust-windowing#2457)
Browse files Browse the repository at this point in the history
* MacOS: set value for `accepts_first_mouse`

* Update CHANGELOG and FEATURES

* Field doesn't need to be public

* Convert `bool` to `BOOL`

* Fix formatting

* Move flag from window state to view instance

* Feedback from PR

* Fix changelog location
  • Loading branch information
madsmtm committed Sep 13, 2022
1 parent 58f2455 commit 48b843e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
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, made `accepts_first_mouse` configurable.
- Migrated `WindowBuilderExtUnix::with_resize_increments` to `WindowBuilder`.
- Added `Window::resize_increments`/`Window::set_resize_increments` to update resize increments at runtime for X11/macOS.
- macOS/iOS: Use `objc2` instead of `objc` internally.
Expand Down
1 change: 1 addition & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ If your PR makes notable changes to Winit's features, please update this section
* Hidden titlebar
* Hidden titlebar buttons
* Full-size content view
* Accepts first mouse

### Unix
* Window urgency
Expand Down
8 changes: 8 additions & 0 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ pub trait WindowBuilderExtMacOS {
fn with_fullsize_content_view(self, fullsize_content_view: bool) -> WindowBuilder;
fn with_disallow_hidpi(self, disallow_hidpi: bool) -> WindowBuilder;
fn with_has_shadow(self, has_shadow: bool) -> WindowBuilder;
/// Window accepts click-through mouse events.
fn with_accepts_first_mouse(self, accepts_first_mouse: bool) -> WindowBuilder;
}

impl WindowBuilderExtMacOS for WindowBuilder {
Expand Down Expand Up @@ -164,6 +166,12 @@ impl WindowBuilderExtMacOS for WindowBuilder {
self.platform_specific.has_shadow = has_shadow;
self
}

#[inline]
fn with_accepts_first_mouse(mut self, accepts_first_mouse: bool) -> WindowBuilder {
self.platform_specific.accepts_first_mouse = accepts_first_mouse;
self
}
}

pub trait EventLoopBuilderExtMacOS {
Expand Down
22 changes: 17 additions & 5 deletions src/platform_impl/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ declare_class!(
_ns_window: IvarDrop<Id<WinitWindow, Shared>>,
pub(super) state: IvarDrop<Box<ViewState>>,
marked_text: IvarDrop<Id<NSMutableAttributedString, Owned>>,
accepts_first_mouse: bool,
}

unsafe impl ClassType for WinitView {
Expand All @@ -144,8 +145,12 @@ declare_class!(
}

unsafe impl WinitView {
#[sel(initWithId:)]
fn init_with_id(&mut self, window: *mut WinitWindow) -> Option<&mut Self> {
#[sel(initWithId:acceptsFirstMouse:)]
fn init_with_id(
&mut self,
window: *mut WinitWindow,
accepts_first_mouse: bool,
) -> Option<&mut Self> {
let this: Option<&mut Self> = unsafe { msg_send![super(self), init] };
this.map(|this| {
let state = ViewState {
Expand All @@ -165,6 +170,7 @@ declare_class!(
);
Ivar::write(&mut this.state, Box::new(state));
Ivar::write(&mut this.marked_text, NSMutableAttributedString::new());
Ivar::write(&mut this.accepts_first_mouse, accepts_first_mouse);

this.setPostsFrameChangedNotifications(true);

Expand Down Expand Up @@ -911,14 +917,20 @@ declare_class!(
#[sel(acceptsFirstMouse:)]
fn accepts_first_mouse(&self, _event: &NSEvent) -> bool {
trace_scope!("acceptsFirstMouse:");
true
*self.accepts_first_mouse
}
}
);

impl WinitView {
pub(super) fn new(window: &WinitWindow) -> Id<Self, Shared> {
unsafe { msg_send_id![msg_send_id![Self::class(), alloc], initWithId: window] }
pub(super) fn new(window: &WinitWindow, accepts_first_mouse: bool) -> Id<Self, Shared> {
unsafe {
msg_send_id![
msg_send_id![Self::class(), alloc],
initWithId: window,
acceptsFirstMouse: accepts_first_mouse,
]
}
}

fn window(&self) -> Id<WinitWindow, Shared> {
Expand Down
4 changes: 3 additions & 1 deletion src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub struct PlatformSpecificWindowBuilderAttributes {
pub fullsize_content_view: bool,
pub disallow_hidpi: bool,
pub has_shadow: bool,
pub accepts_first_mouse: bool,
}

impl Default for PlatformSpecificWindowBuilderAttributes {
Expand All @@ -93,6 +94,7 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
fullsize_content_view: false,
disallow_hidpi: false,
has_shadow: true,
accepts_first_mouse: true,
}
}
}
Expand Down Expand Up @@ -354,7 +356,7 @@ impl WinitWindow {
})
.ok_or_else(|| os_error!(OsError::CreationError("Couldn't create `NSWindow`")))?;

let view = WinitView::new(&this);
let view = WinitView::new(&this, pl_attrs.accepts_first_mouse);

// The default value of `setWantsBestResolutionOpenGLSurface:` was `false` until
// macos 10.14 and `true` after 10.15, we should set it to `YES` or `NO` to avoid
Expand Down

0 comments on commit 48b843e

Please sign in to comment.