Skip to content

Commit

Permalink
fix(macos): do not fire Event::Moved when checking is_maximized (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 13, 2022
1 parent 4173dbb commit 25890b9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-moved-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Do not fire `WindowEvent::Moved` when `is_maximized` is called on macOS.
14 changes: 11 additions & 3 deletions src/platform_impl/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) const DEVICE_ID: RootDeviceId = RootDeviceId(DeviceId);
pub struct Window {
window: Arc<UnownedWindow>,
// We keep this around so that it doesn't get dropped until the window does.
_delegate: util::IdRef,
delegate: util::IdRef,
}

#[non_exhaustive]
Expand All @@ -86,8 +86,16 @@ impl Window {
attributes: WindowAttributes,
pl_attribs: PlatformSpecificWindowBuilderAttributes,
) -> Result<Self, RootOsError> {
let (window, _delegate) = UnownedWindow::new(attributes, pl_attribs)?;
Ok(Window { window, _delegate })
let (window, delegate) = UnownedWindow::new(attributes, pl_attribs)?;
Ok(Window { window, delegate })
}

#[inline]
pub fn is_maximized(&self) -> bool {
let () = unsafe { msg_send![*self.delegate, markIsCheckingZoomedIn] };
let f = self.window.is_zoomed();
let () = unsafe { msg_send![*self.delegate, clearIsCheckingZoomedIn] };
f
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ impl UnownedWindow {

// Roll back temp styles
if needs_temp_mask {
self.set_style_mask_async(curr_mask);
self.set_style_mask_sync(curr_mask);
}

is_zoomed != NO
Expand Down Expand Up @@ -752,11 +752,6 @@ impl UnownedWindow {
shared_state_lock.fullscreen.clone()
}

#[inline]
pub fn is_maximized(&self) -> bool {
self.is_zoomed()
}

#[inline]
pub fn is_visible(&self) -> bool {
let is_visible: BOOL = unsafe { msg_send![*self.ns_window, isVisible] };
Expand Down
31 changes: 29 additions & 2 deletions src/platform_impl/macos/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub struct WindowDelegateState {

// Used to prevent redundant events.
previous_scale_factor: f64,

// Used to prevent resized events from being fired
// when we are using our workaround in the `is_zoomed` function.
is_checking_zoomed_in: bool,
}

impl WindowDelegateState {
Expand All @@ -61,6 +65,7 @@ impl WindowDelegateState {
initial_fullscreen,
previous_position: None,
previous_scale_factor: scale_factor,
is_checking_zoomed_in: false,
};
if (scale_factor - 1.0).abs() > f64::EPSILON {
delegate_state.emit_static_scale_factor_changed_event();
Expand Down Expand Up @@ -154,6 +159,14 @@ lazy_static! {
sel!(initWithTao:),
init_with_tao as extern "C" fn(&Object, Sel, *mut c_void) -> id,
);
decl.add_method(
sel!(markIsCheckingZoomedIn),
mark_is_checking_zoomed_in as extern "C" fn(&Object, Sel),
);
decl.add_method(
sel!(clearIsCheckingZoomedIn),
clear_is_checking_zoomed_in as extern "C" fn(&Object, Sel),
);

decl.add_method(
sel!(windowShouldClose:),
Expand Down Expand Up @@ -265,6 +278,18 @@ extern "C" fn init_with_tao(this: &Object, _sel: Sel, state: *mut c_void) -> id
}
}

extern "C" fn mark_is_checking_zoomed_in(this: &Object, _sel: Sel) {
with_state(&*this, |state| {
state.is_checking_zoomed_in = true;
});
}

extern "C" fn clear_is_checking_zoomed_in(this: &Object, _sel: Sel) {
with_state(&*this, |state| {
state.is_checking_zoomed_in = false;
});
}

extern "C" fn window_should_close(this: &Object, _: Sel, _: id) -> BOOL {
trace!("Triggered `windowShouldClose:`");
with_state(this, |state| state.emit_event(WindowEvent::CloseRequested));
Expand All @@ -289,8 +314,10 @@ extern "C" fn window_will_close(this: &Object, _: Sel, _: id) {
extern "C" fn window_did_resize(this: &Object, _: Sel, _: id) {
trace!("Triggered `windowDidResize:`");
with_state(this, |state| {
state.emit_resize_event();
state.emit_move_event();
if !state.is_checking_zoomed_in {
state.emit_resize_event();
state.emit_move_event();
}
});
trace!("Completed `windowDidResize:`");
}
Expand Down

0 comments on commit 25890b9

Please sign in to comment.