From 1d6e2e102d542d5f3b0ae240e42c6541ca9f5133 Mon Sep 17 00:00:00 2001 From: Francesca Frangipane Date: Fri, 20 Apr 2018 12:34:28 -0400 Subject: [PATCH] Also emit Moved after resizes that change position --- src/platform/macos/window.rs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/platform/macos/window.rs b/src/platform/macos/window.rs index 4fbc1703b64..e78d5d50d75 100644 --- a/src/platform/macos/window.rs +++ b/src/platform/macos/window.rs @@ -22,7 +22,7 @@ use std; use std::ops::Deref; use std::os::raw::c_void; use std::sync::Weak; -use std::cell::{Cell,RefCell}; +use std::cell::{Cell, RefCell}; use super::events_loop::{EventsLoop, Shared}; @@ -43,6 +43,9 @@ struct DelegateState { // This is set when WindowBuilder::with_fullscreen was set, // see comments of `window_did_fail_to_enter_fullscreen` handle_with_fullscreen: bool, + + // During windowDidResize, we use this to only send Moved if the position changed. + previous_position: Option<(i32, i32)>, } impl DelegateState { @@ -161,6 +164,17 @@ impl WindowDelegate { emit_event(state, WindowEvent::Resized(width, height)); } + unsafe fn emit_move_event(state: &mut DelegateState) { + let frame_rect = NSWindow::frame(*state.window); + let x = frame_rect.origin.x as _; + let y = Window2::bottom_left_to_top_left(frame_rect); + let moved = state.previous_position != Some((x, y)); + if moved { + state.previous_position = Some((x, y)); + emit_event(state, WindowEvent::Moved(x, y)); + } + } + extern fn window_should_close(this: &Object, _: Sel, _: id) -> BOOL { unsafe { let state: *mut c_void = *this.get_ivar("winitState"); @@ -177,11 +191,14 @@ impl WindowDelegate { emit_event(state, WindowEvent::Destroyed); + let window_id = get_window_id(*state.window); + // Remove the window from the shared state. if let Some(shared) = state.shared.upgrade() { - let window_id = get_window_id(*state.window); shared.find_and_remove_window(window_id); } + + state.previous_position.take(); } } @@ -190,18 +207,16 @@ impl WindowDelegate { let state: *mut c_void = *this.get_ivar("winitState"); let state = &mut *(state as *mut DelegateState); emit_resize_event(state); + emit_move_event(state); } } + // This won't be triggered if the move was part of a resize. extern fn window_did_move(this: &Object, _: Sel, _: id) { unsafe { let state: *mut c_void = *this.get_ivar("winitState"); let state = &mut *(state as *mut DelegateState); - - let frame_rect = NSWindow::frame(*state.window); - let x = frame_rect.origin.x as _; - let y = Window2::bottom_left_to_top_left(frame_rect); - emit_event(state, WindowEvent::Moved(x, y)); + emit_move_event(state); } } @@ -567,11 +582,12 @@ impl Window2 { let ds = DelegateState { view: view.clone(), window: window.clone(), + shared, win_attribs: RefCell::new(win_attribs.clone()), standard_frame: Cell::new(None), save_style_mask: Cell::new(None), handle_with_fullscreen: win_attribs.fullscreen.is_some(), - shared: shared, + previous_position: None, }; ds.win_attribs.borrow_mut().fullscreen = None;