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 @@ -23,6 +23,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Wayland, a new `wayland-csd-adwaita-crossfont` feature was added to use `crossfont` instead of `ab_glyph` for decorations.
- On Wayland, if not otherwise specified use upstream automatic CSD theme selection.
- On Windows, fixed ALT+Space shortcut to open window menu.
- 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 @@ -110,6 +110,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 @@ -163,6 +165,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
13 changes: 10 additions & 3 deletions src/platform_impl/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{
},
ffi::*,
util::{self, id_to_string_lossy, IdRef},
window::get_window_id,
window::{get_window_id, PlatformSpecificWindowBuilderAttributes},
DEVICE_ID,
},
window::WindowId,
Expand Down Expand Up @@ -86,6 +86,7 @@ pub(super) struct ViewState {
/// True if the current key event should be forwarded
/// to the application, even during IME
forward_key_to_app: bool,
accepts_first_mouse: bool,
}

impl ViewState {
Expand All @@ -98,7 +99,7 @@ impl ViewState {
}
}

pub fn new_view(ns_window: id) -> IdRef {
pub fn new_view(ns_window: id, pl_attribs: &PlatformSpecificWindowBuilderAttributes) -> IdRef {
let state = ViewState {
ns_window,
cursor_state: Default::default(),
Expand All @@ -109,6 +110,7 @@ pub fn new_view(ns_window: id) -> IdRef {
input_source: String::new(),
ime_allowed: false,
forward_key_to_app: false,
accepts_first_mouse: pl_attribs.accepts_first_mouse,
};
unsafe {
// This is free'd in `dealloc`
Expand Down Expand Up @@ -1165,7 +1167,12 @@ declare_class!(
#[sel(acceptsFirstMouse:)]
fn accepts_first_mouse(&self, _event: id) -> bool {
trace_scope!("acceptsFirstMouse:");
true
unsafe {
let state_ptr: *const c_void = *self.ivar("winitState");
let state = &*(state_ptr as *const ViewState);

state.accepts_first_mouse
}
}
}
);
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 @@ -89,6 +89,7 @@ pub struct PlatformSpecificWindowBuilderAttributes {
pub resize_increments: Option<LogicalSize<f64>>,
pub disallow_hidpi: bool,
pub has_shadow: bool,
pub accepts_first_mouse: bool,
}

impl Default for PlatformSpecificWindowBuilderAttributes {
Expand All @@ -104,6 +105,7 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
resize_increments: None,
disallow_hidpi: false,
has_shadow: true,
accepts_first_mouse: true,
}
}
}
Expand All @@ -112,7 +114,7 @@ unsafe fn create_view(
ns_window: id,
pl_attribs: &PlatformSpecificWindowBuilderAttributes,
) -> Option<IdRef> {
let ns_view = new_view(ns_window);
let ns_view = new_view(ns_window, pl_attribs);
ns_view.non_nil().map(|ns_view| {
// 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