Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accepts first mouse #2457

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- Fixed unbound version specified for `raw-window-handle` leading to compilation failures.
- Empty `Ime::Preedit` event will be sent before `Ime::Commit` to help clearing preedit.
- On X11, fixed IME context picking by querying for supported styles beforehand.
- On MacOS, made `accepts_first_mouse` configurable.

# 0.27.2 (2022-8-12)

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