Fix declare_class! indentation (#2461)

* Fix NSWindow delegate indentation

* Fix NSView delegate indentation
This commit is contained in:
Mads Marquart 2022-09-02 19:38:32 +02:00 committed by GitHub
parent d67c928120
commit e517e468f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 1178 additions and 1176 deletions

View file

@ -289,7 +289,7 @@ fn mouse_motion(this: &Object, event: id) {
} }
} }
declare_class! { declare_class!(
#[derive(Debug)] #[derive(Debug)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
struct WinitView { struct WinitView {
@ -303,18 +303,18 @@ declare_class! {
} }
unsafe impl WinitView { unsafe impl WinitView {
#[sel(dealloc)] #[sel(dealloc)]
fn dealloc(&mut self) { fn dealloc(&mut self) {
unsafe { unsafe {
let marked_text: id = *self.ivar("markedText"); let marked_text: id = *self.ivar("markedText");
let _: () = msg_send![marked_text, release]; let _: () = msg_send![marked_text, release];
let state: *mut c_void = *self.ivar("winitState"); let state: *mut c_void = *self.ivar("winitState");
drop(Box::from_raw(state as *mut ViewState)); drop(Box::from_raw(state as *mut ViewState));
} }
} }
#[sel(initWithWinit:)] #[sel(initWithWinit:)]
fn init_with_winit(&mut self, state: *mut c_void) -> Option<&mut Self> { fn init_with_winit(&mut self, state: *mut c_void) -> Option<&mut Self> {
let this: Option<&mut Self> = unsafe { msg_send![self, init] }; let this: Option<&mut Self> = unsafe { msg_send![self, init] };
this.map(|this| unsafe { this.map(|this| unsafe {
(*this).set_ivar("winitState", state); (*this).set_ivar("winitState", state);
@ -340,12 +340,12 @@ fn init_with_winit(&mut self, state: *mut c_void) -> Option<&mut Self> {
winit_state.input_source = this.current_input_source(); winit_state.input_source = this.current_input_source();
this this
}) })
} }
} }
unsafe impl WinitView { unsafe impl WinitView {
#[sel(viewDidMoveToWindow)] #[sel(viewDidMoveToWindow)]
fn view_did_move_to_window(&self) { fn view_did_move_to_window(&self) {
trace_scope!("viewDidMoveToWindow"); trace_scope!("viewDidMoveToWindow");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
@ -365,10 +365,10 @@ fn view_did_move_to_window(&self) {
]; ];
state.tracking_rect = Some(tracking_rect); state.tracking_rect = Some(tracking_rect);
} }
} }
#[sel(frameDidChange:)] #[sel(frameDidChange:)]
fn frame_did_change(&self, _event: id) { fn frame_did_change(&self, _event: id) {
trace_scope!("frameDidChange:"); trace_scope!("frameDidChange:");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
@ -391,17 +391,18 @@ fn frame_did_change(&self, _event: id) {
// Emit resize event here rather than from windowDidResize because: // Emit resize event here rather than from windowDidResize because:
// 1. When a new window is created as a tab, the frame size may change without a window resize occurring. // 1. When a new window is created as a tab, the frame size may change without a window resize occurring.
// 2. Even when a window resize does occur on a new tabbed window, it contains the wrong size (includes tab height). // 2. Even when a window resize does occur on a new tabbed window, it contains the wrong size (includes tab height).
let logical_size = LogicalSize::new(rect.size.width as f64, rect.size.height as f64); let logical_size =
LogicalSize::new(rect.size.width as f64, rect.size.height as f64);
let size = logical_size.to_physical::<u32>(state.get_scale_factor()); let size = logical_size.to_physical::<u32>(state.get_scale_factor());
AppState::queue_event(EventWrapper::StaticEvent(Event::WindowEvent { AppState::queue_event(EventWrapper::StaticEvent(Event::WindowEvent {
window_id: WindowId(get_window_id(state.ns_window)), window_id: WindowId(get_window_id(state.ns_window)),
event: WindowEvent::Resized(size), event: WindowEvent::Resized(size),
})); }));
} }
} }
#[sel(drawRect:)] #[sel(drawRect:)]
fn draw_rect(&self, rect: NSRect) { fn draw_rect(&self, rect: NSRect) {
trace_scope!("drawRect:"); trace_scope!("drawRect:");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
@ -411,25 +412,25 @@ fn draw_rect(&self, rect: NSRect) {
let _: () = msg_send![super(self), drawRect: rect]; let _: () = msg_send![super(self), drawRect: rect];
} }
} }
#[sel(acceptsFirstResponder)] #[sel(acceptsFirstResponder)]
fn accepts_first_responder(&self) -> bool { fn accepts_first_responder(&self) -> bool {
trace_scope!("acceptsFirstResponder"); trace_scope!("acceptsFirstResponder");
true true
} }
// This is necessary to prevent a beefy terminal error on MacBook Pros: // This is necessary to prevent a beefy terminal error on MacBook Pros:
// IMKInputSession [0x7fc573576ff0 presentFunctionRowItemTextInputViewWithEndpoint:completionHandler:] : [self textInputContext]=0x7fc573558e10 *NO* NSRemoteViewController to client, NSError=Error Domain=NSCocoaErrorDomain Code=4099 "The connection from pid 0 was invalidated from this process." UserInfo={NSDebugDescription=The connection from pid 0 was invalidated from this process.}, com.apple.inputmethod.EmojiFunctionRowItem // IMKInputSession [0x7fc573576ff0 presentFunctionRowItemTextInputViewWithEndpoint:completionHandler:] : [self textInputContext]=0x7fc573558e10 *NO* NSRemoteViewController to client, NSError=Error Domain=NSCocoaErrorDomain Code=4099 "The connection from pid 0 was invalidated from this process." UserInfo={NSDebugDescription=The connection from pid 0 was invalidated from this process.}, com.apple.inputmethod.EmojiFunctionRowItem
// TODO: Add an API extension for using `NSTouchBar` // TODO: Add an API extension for using `NSTouchBar`
#[sel(touchBar)] #[sel(touchBar)]
fn touch_bar(&self) -> bool { fn touch_bar(&self) -> bool {
trace_scope!("touchBar"); trace_scope!("touchBar");
false false
} }
#[sel(resetCursorRects)] #[sel(resetCursorRects)]
fn reset_cursor_rects(&self) { fn reset_cursor_rects(&self) {
trace_scope!("resetCursorRects"); trace_scope!("resetCursorRects");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
@ -447,22 +448,21 @@ fn reset_cursor_rects(&self) {
cursor:cursor cursor:cursor
]; ];
} }
} }
} }
unsafe impl Protocol<NSTextInputClient> for WinitView { unsafe impl Protocol<NSTextInputClient> for WinitView {
#[sel(hasMarkedText)]
#[sel(hasMarkedText)] fn has_marked_text(&self) -> bool {
fn has_marked_text(&self) -> bool {
trace_scope!("hasMarkedText"); trace_scope!("hasMarkedText");
unsafe { unsafe {
let marked_text: id = *self.ivar("markedText"); let marked_text: id = *self.ivar("markedText");
marked_text.length() > 0 marked_text.length() > 0
} }
} }
#[sel(markedRange)] #[sel(markedRange)]
fn marked_range(&self) -> NSRange { fn marked_range(&self) -> NSRange {
trace_scope!("markedRange"); trace_scope!("markedRange");
unsafe { unsafe {
let marked_text: id = *self.ivar("markedText"); let marked_text: id = *self.ivar("markedText");
@ -473,21 +473,21 @@ fn marked_range(&self) -> NSRange {
util::EMPTY_RANGE util::EMPTY_RANGE
} }
} }
} }
#[sel(selectedRange)] #[sel(selectedRange)]
fn selected_range(&self) -> NSRange { fn selected_range(&self) -> NSRange {
trace_scope!("selectedRange"); trace_scope!("selectedRange");
util::EMPTY_RANGE util::EMPTY_RANGE
} }
#[sel(setMarkedText:selectedRange:replacementRange:)] #[sel(setMarkedText:selectedRange:replacementRange:)]
fn set_marked_text( fn set_marked_text(
&mut self, &mut self,
string: id, string: id,
_selected_range: NSRange, _selected_range: NSRange,
_replacement_range: NSRange, _replacement_range: NSRange,
) { ) {
trace_scope!("setMarkedText:selectedRange:replacementRange:"); trace_scope!("setMarkedText:selectedRange:replacementRange:");
unsafe { unsafe {
// Get pre-edit text // Get pre-edit text
@ -538,10 +538,10 @@ fn set_marked_text(
event: WindowEvent::Ime(Ime::Preedit(preedit_string, cursor_range)), event: WindowEvent::Ime(Ime::Preedit(preedit_string, cursor_range)),
})); }));
} }
} }
#[sel(unmarkText)] #[sel(unmarkText)]
fn unmark_text(&self) { fn unmark_text(&self) {
trace_scope!("unmarkText"); trace_scope!("unmarkText");
unsafe { unsafe {
let marked_text: id = *self.ivar("markedText"); let marked_text: id = *self.ivar("markedText");
@ -565,42 +565,44 @@ fn unmark_text(&self) {
warn!("Expected to have IME enabled when receiving unmarkText"); warn!("Expected to have IME enabled when receiving unmarkText");
} }
} }
} }
#[sel(validAttributesForMarkedText)] #[sel(validAttributesForMarkedText)]
fn valid_attributes_for_marked_text(&self) -> id { fn valid_attributes_for_marked_text(&self) -> id {
trace_scope!("validAttributesForMarkedText"); trace_scope!("validAttributesForMarkedText");
unsafe { msg_send![class!(NSArray), array] } unsafe { msg_send![class!(NSArray), array] }
} }
#[sel(attributedSubstringForProposedRange:actualRange:)] #[sel(attributedSubstringForProposedRange:actualRange:)]
fn attributed_substring_for_proposed_range( fn attributed_substring_for_proposed_range(
&self, &self,
_range: NSRange, _range: NSRange,
_actual_range: *mut c_void, // *mut NSRange _actual_range: *mut c_void, // *mut NSRange
) -> id { ) -> id {
trace_scope!("attributedSubstringForProposedRange:actualRange:"); trace_scope!("attributedSubstringForProposedRange:actualRange:");
nil nil
} }
#[sel(characterIndexForPoint:)] #[sel(characterIndexForPoint:)]
fn character_index_for_point(&self, _point: NSPoint) -> NSUInteger { fn character_index_for_point(&self, _point: NSPoint) -> NSUInteger {
trace_scope!("characterIndexForPoint:"); trace_scope!("characterIndexForPoint:");
0 0
} }
#[sel(firstRectForCharacterRange:actualRange:)] #[sel(firstRectForCharacterRange:actualRange:)]
fn first_rect_for_character_range( fn first_rect_for_character_range(
&self, &self,
_range: NSRange, _range: NSRange,
_actual_range: *mut c_void, // *mut NSRange _actual_range: *mut c_void, // *mut NSRange
) -> NSRect { ) -> NSRect {
trace_scope!("firstRectForCharacterRange:actualRange:"); trace_scope!("firstRectForCharacterRange:actualRange:");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState); let state = &mut *(state_ptr as *mut ViewState);
let content_rect = let content_rect = NSWindow::contentRectForFrameRect_(
NSWindow::contentRectForFrameRect_(state.ns_window, NSWindow::frame(state.ns_window)); state.ns_window,
NSWindow::frame(state.ns_window),
);
let base_x = content_rect.origin.x as f64; let base_x = content_rect.origin.x as f64;
let base_y = (content_rect.origin.y + content_rect.size.height) as f64; let base_y = (content_rect.origin.y + content_rect.size.height) as f64;
let x = base_x + state.ime_position.x; let x = base_x + state.ime_position.x;
@ -611,10 +613,10 @@ fn first_rect_for_character_range(
// no way to express this. Same goes for the `NSSize`. // no way to express this. Same goes for the `NSSize`.
NSRect::new(NSPoint::new(x as _, y as _), NSSize::new(0.0, 0.0)) NSRect::new(NSPoint::new(x as _, y as _), NSSize::new(0.0, 0.0))
} }
} }
#[sel(insertText:replacementRange:)] #[sel(insertText:replacementRange:)]
fn insert_text(&self, string: id, _replacement_range: NSRange) { fn insert_text(&self, string: id, _replacement_range: NSRange) {
trace_scope!("insertText:replacementRange:"); trace_scope!("insertText:replacementRange:");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
@ -635,10 +637,10 @@ fn insert_text(&self, string: id, _replacement_range: NSRange) {
state.ime_state = ImeState::Commited; state.ime_state = ImeState::Commited;
} }
} }
} }
#[sel(doCommandBySelector:)] #[sel(doCommandBySelector:)]
fn do_command_by_selector(&self, _command: Sel) { fn do_command_by_selector(&self, _command: Sel) {
trace_scope!("doCommandBySelector:"); trace_scope!("doCommandBySelector:");
// Basically, we're sent this message whenever a keyboard event that doesn't generate a "human // Basically, we're sent this message whenever a keyboard event that doesn't generate a "human
// readable" character happens, i.e. newlines, tabs, and Ctrl+C. // readable" character happens, i.e. newlines, tabs, and Ctrl+C.
@ -661,13 +663,12 @@ fn do_command_by_selector(&self, _command: Sel) {
state.ime_state = ImeState::Enabled; state.ime_state = ImeState::Enabled;
} }
} }
} }
} }
unsafe impl WinitView { unsafe impl WinitView {
#[sel(keyDown:)]
#[sel(keyDown:)] fn key_down(&self, event: id) {
fn key_down(&self, event: id) {
trace_scope!("keyDown:"); trace_scope!("keyDown:");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
@ -748,10 +749,10 @@ fn key_down(&self, event: id) {
} }
} }
} }
} }
#[sel(keyUp:)] #[sel(keyUp:)]
fn key_up(&self, event: id) { fn key_up(&self, event: id) {
trace_scope!("keyUp:"); trace_scope!("keyUp:");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
@ -782,10 +783,10 @@ fn key_up(&self, event: id) {
AppState::queue_event(EventWrapper::StaticEvent(window_event)); AppState::queue_event(EventWrapper::StaticEvent(window_event));
} }
} }
} }
#[sel(flagsChanged:)] #[sel(flagsChanged:)]
fn flags_changed(&self, event: id) { fn flags_changed(&self, event: id) {
trace_scope!("flagsChanged:"); trace_scope!("flagsChanged:");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
@ -843,10 +844,10 @@ fn flags_changed(&self, event: id) {
event: WindowEvent::ModifiersChanged(state.modifiers), event: WindowEvent::ModifiersChanged(state.modifiers),
})); }));
} }
} }
#[sel(insertTab:)] #[sel(insertTab:)]
fn insert_tab(&self, _sender: id) { fn insert_tab(&self, _sender: id) {
trace_scope!("insertTab:"); trace_scope!("insertTab:");
unsafe { unsafe {
let window: id = msg_send![self, window]; let window: id = msg_send![self, window];
@ -856,10 +857,10 @@ fn insert_tab(&self, _sender: id) {
let _: () = msg_send![window, selectNextKeyView: self]; let _: () = msg_send![window, selectNextKeyView: self];
} }
} }
} }
#[sel(insertBackTab:)] #[sel(insertBackTab:)]
fn insert_back_tab(&self, _sender: id) { fn insert_back_tab(&self, _sender: id) {
trace_scope!("insertBackTab:"); trace_scope!("insertBackTab:");
unsafe { unsafe {
let window: id = msg_send![self, window]; let window: id = msg_send![self, window];
@ -869,12 +870,12 @@ fn insert_back_tab(&self, _sender: id) {
let _: () = msg_send![window, selectPreviousKeyView: self]; let _: () = msg_send![window, selectPreviousKeyView: self];
} }
} }
} }
// Allows us to receive Cmd-. (the shortcut for closing a dialog) // Allows us to receive Cmd-. (the shortcut for closing a dialog)
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=300620#c6 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=300620#c6
#[sel(cancelOperation:)] #[sel(cancelOperation:)]
fn cancel_operation(&self, _sender: id) { fn cancel_operation(&self, _sender: id) {
trace_scope!("cancelOperation:"); trace_scope!("cancelOperation:");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
@ -905,74 +906,74 @@ fn cancel_operation(&self, _sender: id) {
AppState::queue_event(EventWrapper::StaticEvent(window_event)); AppState::queue_event(EventWrapper::StaticEvent(window_event));
} }
} }
#[sel(mouseDown:)] #[sel(mouseDown:)]
fn mouse_down(&self, event: id) { fn mouse_down(&self, event: id) {
trace_scope!("mouseDown:"); trace_scope!("mouseDown:");
mouse_motion(self, event); mouse_motion(self, event);
mouse_click(self, event, MouseButton::Left, ElementState::Pressed); mouse_click(self, event, MouseButton::Left, ElementState::Pressed);
} }
#[sel(mouseUp:)] #[sel(mouseUp:)]
fn mouse_up(&self, event: id) { fn mouse_up(&self, event: id) {
trace_scope!("mouseUp:"); trace_scope!("mouseUp:");
mouse_motion(self, event); mouse_motion(self, event);
mouse_click(self, event, MouseButton::Left, ElementState::Released); mouse_click(self, event, MouseButton::Left, ElementState::Released);
} }
#[sel(rightMouseDown:)] #[sel(rightMouseDown:)]
fn right_mouse_down(&self, event: id) { fn right_mouse_down(&self, event: id) {
trace_scope!("rightMouseDown:"); trace_scope!("rightMouseDown:");
mouse_motion(self, event); mouse_motion(self, event);
mouse_click(self, event, MouseButton::Right, ElementState::Pressed); mouse_click(self, event, MouseButton::Right, ElementState::Pressed);
} }
#[sel(rightMouseUp:)] #[sel(rightMouseUp:)]
fn right_mouse_up(&self, event: id) { fn right_mouse_up(&self, event: id) {
trace_scope!("rightMouseUp:"); trace_scope!("rightMouseUp:");
mouse_motion(self, event); mouse_motion(self, event);
mouse_click(self, event, MouseButton::Right, ElementState::Released); mouse_click(self, event, MouseButton::Right, ElementState::Released);
} }
#[sel(otherMouseDown:)] #[sel(otherMouseDown:)]
fn other_mouse_down(&self, event: id) { fn other_mouse_down(&self, event: id) {
trace_scope!("otherMouseDown:"); trace_scope!("otherMouseDown:");
mouse_motion(self, event); mouse_motion(self, event);
mouse_click(self, event, MouseButton::Middle, ElementState::Pressed); mouse_click(self, event, MouseButton::Middle, ElementState::Pressed);
} }
#[sel(otherMouseUp:)] #[sel(otherMouseUp:)]
fn other_mouse_up(&self, event: id) { fn other_mouse_up(&self, event: id) {
trace_scope!("otherMouseUp:"); trace_scope!("otherMouseUp:");
mouse_motion(self, event); mouse_motion(self, event);
mouse_click(self, event, MouseButton::Middle, ElementState::Released); mouse_click(self, event, MouseButton::Middle, ElementState::Released);
} }
// No tracing on these because that would be overly verbose // No tracing on these because that would be overly verbose
#[sel(mouseMoved:)] #[sel(mouseMoved:)]
fn mouse_moved(&self, event: id) { fn mouse_moved(&self, event: id) {
mouse_motion(self, event); mouse_motion(self, event);
} }
#[sel(mouseDragged:)] #[sel(mouseDragged:)]
fn mouse_dragged(&self, event: id) { fn mouse_dragged(&self, event: id) {
mouse_motion(self, event); mouse_motion(self, event);
} }
#[sel(rightMouseDragged:)] #[sel(rightMouseDragged:)]
fn right_mouse_dragged(&self, event: id) { fn right_mouse_dragged(&self, event: id) {
mouse_motion(self, event); mouse_motion(self, event);
} }
#[sel(otherMouseDragged:)] #[sel(otherMouseDragged:)]
fn other_mouse_dragged(&self, event: id) { fn other_mouse_dragged(&self, event: id) {
mouse_motion(self, event); mouse_motion(self, event);
} }
#[sel(mouseEntered:)] #[sel(mouseEntered:)]
fn mouse_entered(&self, _event: id) { fn mouse_entered(&self, _event: id) {
trace_scope!("mouseEntered:"); trace_scope!("mouseEntered:");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
@ -987,10 +988,10 @@ fn mouse_entered(&self, _event: id) {
AppState::queue_event(EventWrapper::StaticEvent(enter_event)); AppState::queue_event(EventWrapper::StaticEvent(enter_event));
} }
} }
#[sel(mouseExited:)] #[sel(mouseExited:)]
fn mouse_exited(&self, _event: id) { fn mouse_exited(&self, _event: id) {
trace_scope!("mouseExited:"); trace_scope!("mouseExited:");
unsafe { unsafe {
let state_ptr: *mut c_void = *self.ivar("winitState"); let state_ptr: *mut c_void = *self.ivar("winitState");
@ -1005,10 +1006,10 @@ fn mouse_exited(&self, _event: id) {
AppState::queue_event(EventWrapper::StaticEvent(window_event)); AppState::queue_event(EventWrapper::StaticEvent(window_event));
} }
} }
#[sel(scrollWheel:)] #[sel(scrollWheel:)]
fn scroll_wheel(&self, event: id) { fn scroll_wheel(&self, event: id) {
trace_scope!("scrollWheel:"); trace_scope!("scrollWheel:");
mouse_motion(self, event); mouse_motion(self, event);
@ -1020,7 +1021,8 @@ fn scroll_wheel(&self, event: id) {
let delta = { let delta = {
let (x, y) = (event.scrollingDeltaX(), event.scrollingDeltaY()); let (x, y) = (event.scrollingDeltaX(), event.scrollingDeltaY());
if Bool::from_raw(event.hasPreciseScrollingDeltas()).as_bool() { if Bool::from_raw(event.hasPreciseScrollingDeltas()).as_bool() {
let delta = LogicalPosition::new(x, y).to_physical(state.get_scale_factor()); let delta =
LogicalPosition::new(x, y).to_physical(state.get_scale_factor());
MouseScrollDelta::PixelDelta(delta) MouseScrollDelta::PixelDelta(delta)
} else { } else {
MouseScrollDelta::LineDelta(x as f32, y as f32) MouseScrollDelta::LineDelta(x as f32, y as f32)
@ -1031,7 +1033,8 @@ fn scroll_wheel(&self, event: id) {
// be mutually exclusive anyhow, which is why the API is rather incoherent). If no momentum // be mutually exclusive anyhow, which is why the API is rather incoherent). If no momentum
// phase is recorded (or rather, the started/ended cases of the momentum phase) then we // phase is recorded (or rather, the started/ended cases of the momentum phase) then we
// report the touch phase. // report the touch phase.
let phase = match event.momentumPhase() { let phase =
match event.momentumPhase() {
NSEventPhase::NSEventPhaseMayBegin | NSEventPhase::NSEventPhaseBegan => { NSEventPhase::NSEventPhaseMayBegin | NSEventPhase::NSEventPhaseBegan => {
TouchPhase::Started TouchPhase::Started
} }
@ -1039,12 +1042,10 @@ fn scroll_wheel(&self, event: id) {
TouchPhase::Ended TouchPhase::Ended
} }
_ => match event.phase() { _ => match event.phase() {
NSEventPhase::NSEventPhaseMayBegin | NSEventPhase::NSEventPhaseBegan => { NSEventPhase::NSEventPhaseMayBegin
TouchPhase::Started | NSEventPhase::NSEventPhaseBegan => TouchPhase::Started,
} NSEventPhase::NSEventPhaseEnded
NSEventPhase::NSEventPhaseEnded | NSEventPhase::NSEventPhaseCancelled => { | NSEventPhase::NSEventPhaseCancelled => TouchPhase::Ended,
TouchPhase::Ended
}
_ => TouchPhase::Moved, _ => TouchPhase::Moved,
}, },
}; };
@ -1072,10 +1073,10 @@ fn scroll_wheel(&self, event: id) {
AppState::queue_event(EventWrapper::StaticEvent(device_event)); AppState::queue_event(EventWrapper::StaticEvent(device_event));
AppState::queue_event(EventWrapper::StaticEvent(window_event)); AppState::queue_event(EventWrapper::StaticEvent(window_event));
} }
} }
#[sel(magnifyWithEvent:)] #[sel(magnifyWithEvent:)]
fn magnify_with_event(&self, event: id) { fn magnify_with_event(&self, event: id) {
trace_scope!("magnifyWithEvent:"); trace_scope!("magnifyWithEvent:");
unsafe { unsafe {
@ -1102,10 +1103,10 @@ fn magnify_with_event(&self, event: id) {
AppState::queue_event(EventWrapper::StaticEvent(window_event)); AppState::queue_event(EventWrapper::StaticEvent(window_event));
} }
} }
#[sel(rotateWithEvent:)] #[sel(rotateWithEvent:)]
fn rotate_with_event(&self, event: id) { fn rotate_with_event(&self, event: id) {
trace_scope!("rotateWithEvent:"); trace_scope!("rotateWithEvent:");
unsafe { unsafe {
@ -1132,10 +1133,10 @@ fn rotate_with_event(&self, event: id) {
AppState::queue_event(EventWrapper::StaticEvent(window_event)); AppState::queue_event(EventWrapper::StaticEvent(window_event));
} }
} }
#[sel(pressureChangeWithEvent:)] #[sel(pressureChangeWithEvent:)]
fn pressure_change_with_event(&self, event: id) { fn pressure_change_with_event(&self, event: id) {
trace_scope!("pressureChangeWithEvent:"); trace_scope!("pressureChangeWithEvent:");
mouse_motion(self, event); mouse_motion(self, event);
@ -1158,24 +1159,24 @@ fn pressure_change_with_event(&self, event: id) {
AppState::queue_event(EventWrapper::StaticEvent(window_event)); AppState::queue_event(EventWrapper::StaticEvent(window_event));
} }
} }
// Allows us to receive Ctrl-Tab and Ctrl-Esc. // Allows us to receive Ctrl-Tab and Ctrl-Esc.
// Note that this *doesn't* help with any missing Cmd inputs. // Note that this *doesn't* help with any missing Cmd inputs.
// https://github.com/chromium/chromium/blob/a86a8a6bcfa438fa3ac2eba6f02b3ad1f8e0756f/ui/views/cocoa/bridged_content_view.mm#L816 // https://github.com/chromium/chromium/blob/a86a8a6bcfa438fa3ac2eba6f02b3ad1f8e0756f/ui/views/cocoa/bridged_content_view.mm#L816
#[sel(_wantsKeyDownForEvent:)] #[sel(_wantsKeyDownForEvent:)]
fn wants_key_down_for_event(&self, _event: id) -> bool { fn wants_key_down_for_event(&self, _event: id) -> bool {
trace_scope!("_wantsKeyDownForEvent:"); trace_scope!("_wantsKeyDownForEvent:");
true true
} }
#[sel(acceptsFirstMouse:)] #[sel(acceptsFirstMouse:)]
fn accepts_first_mouse(&self, _event: id) -> bool { fn accepts_first_mouse(&self, _event: id) -> bool {
trace_scope!("acceptsFirstMouse:"); trace_scope!("acceptsFirstMouse:");
true true
}
} }
} }
);
impl WinitView { impl WinitView {
fn current_input_source(&self) -> String { fn current_input_source(&self) -> String {

View file

@ -128,7 +128,7 @@ pub fn new_delegate(window: &Arc<UnownedWindow>, initial_fullscreen: bool) -> Id
} }
} }
declare_class! { declare_class!(
#[derive(Debug)] #[derive(Debug)]
struct WinitWindowDelegate { struct WinitWindowDelegate {
state: *mut c_void, state: *mut c_void,
@ -139,15 +139,15 @@ declare_class! {
} }
unsafe impl WinitWindowDelegate { unsafe impl WinitWindowDelegate {
#[sel(dealloc)] #[sel(dealloc)]
fn dealloc(&mut self) { fn dealloc(&mut self) {
self.with_state(|state| unsafe { self.with_state(|state| unsafe {
drop(Box::from_raw(state as *mut WindowDelegateState)); drop(Box::from_raw(state as *mut WindowDelegateState));
}); });
} }
#[sel(initWithWinit:)] #[sel(initWithWinit:)]
fn init_with_winit(&mut self, state: *mut c_void) -> Option<&mut Self> { fn init_with_winit(&mut self, state: *mut c_void) -> Option<&mut Self> {
let this: Option<&mut Self> = unsafe { msg_send![self, init] }; let this: Option<&mut Self> = unsafe { msg_send![self, init] };
this.map(|this| { this.map(|this| {
*this.state = state; *this.state = state;
@ -156,20 +156,20 @@ fn init_with_winit(&mut self, state: *mut c_void) -> Option<&mut Self> {
}); });
this this
}) })
} }
} }
// NSWindowDelegate + NSDraggingDestination protocols // NSWindowDelegate + NSDraggingDestination protocols
unsafe impl WinitWindowDelegate { unsafe impl WinitWindowDelegate {
#[sel(windowShouldClose:)] #[sel(windowShouldClose:)]
fn window_should_close(&self, _: id) -> bool { fn window_should_close(&self, _: id) -> bool {
trace_scope!("windowShouldClose:"); trace_scope!("windowShouldClose:");
self.with_state(|state| state.emit_event(WindowEvent::CloseRequested)); self.with_state(|state| state.emit_event(WindowEvent::CloseRequested));
false false
} }
#[sel(windowWillClose:)] #[sel(windowWillClose:)]
fn window_will_close(&self, _: id) { fn window_will_close(&self, _: id) {
trace_scope!("windowWillClose:"); trace_scope!("windowWillClose:");
self.with_state(|state| unsafe { self.with_state(|state| unsafe {
// `setDelegate:` retains the previous value and then autoreleases it // `setDelegate:` retains the previous value and then autoreleases it
@ -180,46 +180,46 @@ fn window_will_close(&self, _: id) {
}); });
state.emit_event(WindowEvent::Destroyed); state.emit_event(WindowEvent::Destroyed);
}); });
} }
#[sel(windowDidResize:)] #[sel(windowDidResize:)]
fn window_did_resize(&self, _: id) { fn window_did_resize(&self, _: id) {
trace_scope!("windowDidResize:"); trace_scope!("windowDidResize:");
self.with_state(|state| { self.with_state(|state| {
// NOTE: WindowEvent::Resized is reported in frameDidChange. // NOTE: WindowEvent::Resized is reported in frameDidChange.
state.emit_move_event(); state.emit_move_event();
}); });
} }
// This won't be triggered if the move was part of a resize. // This won't be triggered if the move was part of a resize.
#[sel(windowDidMove:)] #[sel(windowDidMove:)]
fn window_did_move(&self, _: id) { fn window_did_move(&self, _: id) {
trace_scope!("windowDidMove:"); trace_scope!("windowDidMove:");
self.with_state(|state| { self.with_state(|state| {
state.emit_move_event(); state.emit_move_event();
}); });
} }
#[sel(windowDidChangeBackingProperties:)] #[sel(windowDidChangeBackingProperties:)]
fn window_did_change_backing_properties(&self, _: id) { fn window_did_change_backing_properties(&self, _: id) {
trace_scope!("windowDidChangeBackingProperties:"); trace_scope!("windowDidChangeBackingProperties:");
self.with_state(|state| { self.with_state(|state| {
state.emit_static_scale_factor_changed_event(); state.emit_static_scale_factor_changed_event();
}); });
} }
#[sel(windowDidBecomeKey:)] #[sel(windowDidBecomeKey:)]
fn window_did_become_key(&self, _: id) { fn window_did_become_key(&self, _: id) {
trace_scope!("windowDidBecomeKey:"); trace_scope!("windowDidBecomeKey:");
self.with_state(|state| { self.with_state(|state| {
// TODO: center the cursor if the window had mouse grab when it // TODO: center the cursor if the window had mouse grab when it
// lost focus // lost focus
state.emit_event(WindowEvent::Focused(true)); state.emit_event(WindowEvent::Focused(true));
}); });
} }
#[sel(windowDidResignKey:)] #[sel(windowDidResignKey:)]
fn window_did_resign_key(&self, _: id) { fn window_did_resign_key(&self, _: id) {
trace_scope!("windowDidResignKey:"); trace_scope!("windowDidResignKey:");
self.with_state(|state| { self.with_state(|state| {
// It happens rather often, e.g. when the user is Cmd+Tabbing, that the // It happens rather often, e.g. when the user is Cmd+Tabbing, that the
@ -247,18 +247,19 @@ fn window_did_resign_key(&self, _: id) {
state.emit_event(WindowEvent::Focused(false)); state.emit_event(WindowEvent::Focused(false));
}); });
} }
/// Invoked when the dragged image enters destination bounds or frame /// Invoked when the dragged image enters destination bounds or frame
#[sel(draggingEntered:)] #[sel(draggingEntered:)]
fn dragging_entered(&self, sender: id) -> bool { fn dragging_entered(&self, sender: id) -> bool {
trace_scope!("draggingEntered:"); trace_scope!("draggingEntered:");
use cocoa::{appkit::NSPasteboard, foundation::NSFastEnumeration}; use cocoa::{appkit::NSPasteboard, foundation::NSFastEnumeration};
use std::path::PathBuf; use std::path::PathBuf;
let pb: id = unsafe { msg_send![sender, draggingPasteboard] }; let pb: id = unsafe { msg_send![sender, draggingPasteboard] };
let filenames = unsafe { NSPasteboard::propertyListForType(pb, appkit::NSFilenamesPboardType) }; let filenames =
unsafe { NSPasteboard::propertyListForType(pb, appkit::NSFilenamesPboardType) };
for file in unsafe { filenames.iter() } { for file in unsafe { filenames.iter() } {
use cocoa::foundation::NSString; use cocoa::foundation::NSString;
@ -275,25 +276,26 @@ fn dragging_entered(&self, sender: id) -> bool {
} }
true true
} }
/// Invoked when the image is released /// Invoked when the image is released
#[sel(prepareForDragOperation:)] #[sel(prepareForDragOperation:)]
fn prepare_for_drag_operation(&self, _: id) -> bool { fn prepare_for_drag_operation(&self, _: id) -> bool {
trace_scope!("prepareForDragOperation:"); trace_scope!("prepareForDragOperation:");
true true
} }
/// Invoked after the released image has been removed from the screen /// Invoked after the released image has been removed from the screen
#[sel(performDragOperation:)] #[sel(performDragOperation:)]
fn perform_drag_operation(&self, sender: id) -> bool { fn perform_drag_operation(&self, sender: id) -> bool {
trace_scope!("performDragOperation:"); trace_scope!("performDragOperation:");
use cocoa::{appkit::NSPasteboard, foundation::NSFastEnumeration}; use cocoa::{appkit::NSPasteboard, foundation::NSFastEnumeration};
use std::path::PathBuf; use std::path::PathBuf;
let pb: id = unsafe { msg_send![sender, draggingPasteboard] }; let pb: id = unsafe { msg_send![sender, draggingPasteboard] };
let filenames = unsafe { NSPasteboard::propertyListForType(pb, appkit::NSFilenamesPboardType) }; let filenames =
unsafe { NSPasteboard::propertyListForType(pb, appkit::NSFilenamesPboardType) };
for file in unsafe { filenames.iter() } { for file in unsafe { filenames.iter() } {
use cocoa::foundation::NSString; use cocoa::foundation::NSString;
@ -310,26 +312,24 @@ fn perform_drag_operation(&self, sender: id) -> bool {
} }
true true
} }
/// Invoked when the dragging operation is complete /// Invoked when the dragging operation is complete
#[sel(concludeDragOperation:)] #[sel(concludeDragOperation:)]
fn conclude_drag_operation(&self, _: id) { fn conclude_drag_operation(&self, _: id) {
trace_scope!("concludeDragOperation:"); trace_scope!("concludeDragOperation:");
} }
/// Invoked when the dragging operation is cancelled /// Invoked when the dragging operation is cancelled
#[sel(draggingExited:)] #[sel(draggingExited:)]
fn dragging_exited(&self, _: id) { fn dragging_exited(&self, _: id) {
trace_scope!("draggingExited:"); trace_scope!("draggingExited:");
self.with_state(|state| { self.with_state(|state| state.emit_event(WindowEvent::HoveredFileCancelled));
state.emit_event(WindowEvent::HoveredFileCancelled) }
});
}
/// Invoked when before enter fullscreen /// Invoked when before enter fullscreen
#[sel(windowWillEnterFullscreen:)] #[sel(windowWillEnterFullscreen:)]
fn window_will_enter_fullscreen(&self, _: id) { fn window_will_enter_fullscreen(&self, _: id) {
trace_scope!("windowWillEnterFullscreen:"); trace_scope!("windowWillEnterFullscreen:");
self.with_state(|state| { self.with_state(|state| {
@ -356,11 +356,11 @@ fn window_will_enter_fullscreen(&self, _: id) {
shared_state.in_fullscreen_transition = true; shared_state.in_fullscreen_transition = true;
}) })
}); });
} }
/// Invoked when before exit fullscreen /// Invoked when before exit fullscreen
#[sel(windowWillExitFullScreen:)] #[sel(windowWillExitFullScreen:)]
fn window_will_exit_fullscreen(&self, _: id) { fn window_will_exit_fullscreen(&self, _: id) {
trace_scope!("windowWillExitFullScreen:"); trace_scope!("windowWillExitFullScreen:");
self.with_state(|state| { self.with_state(|state| {
@ -369,14 +369,14 @@ fn window_will_exit_fullscreen(&self, _: id) {
shared_state.in_fullscreen_transition = true; shared_state.in_fullscreen_transition = true;
}); });
}); });
} }
#[sel(window:willUseFullScreenPresentationOptions:)] #[sel(window:willUseFullScreenPresentationOptions:)]
fn window_will_use_fullscreen_presentation_options( fn window_will_use_fullscreen_presentation_options(
&self, &self,
_: id, _: id,
proposed_options: NSUInteger, proposed_options: NSUInteger,
) -> NSUInteger { ) -> NSUInteger {
trace_scope!("window:willUseFullScreenPresentationOptions:"); trace_scope!("window:willUseFullScreenPresentationOptions:");
// Generally, games will want to disable the menu bar and the dock. Ideally, // Generally, games will want to disable the menu bar and the dock. Ideally,
// this would be configurable by the user. Unfortunately because of our // this would be configurable by the user. Unfortunately because of our
@ -401,11 +401,11 @@ fn window_will_use_fullscreen_presentation_options(
}); });
options options
} }
/// Invoked when entered fullscreen /// Invoked when entered fullscreen
#[sel(windowDidEnterFullscreen:)] #[sel(windowDidEnterFullscreen:)]
fn window_did_enter_fullscreen(&self, _: id) { fn window_did_enter_fullscreen(&self, _: id) {
trace_scope!("windowDidEnterFullscreen:"); trace_scope!("windowDidEnterFullscreen:");
self.with_state(|state| { self.with_state(|state| {
state.initial_fullscreen = false; state.initial_fullscreen = false;
@ -419,11 +419,11 @@ fn window_did_enter_fullscreen(&self, _: id) {
} }
}); });
}); });
} }
/// Invoked when exited fullscreen /// Invoked when exited fullscreen
#[sel(windowDidExitFullscreen:)] #[sel(windowDidExitFullscreen:)]
fn window_did_exit_fullscreen(&self, _: id) { fn window_did_exit_fullscreen(&self, _: id) {
trace_scope!("windowDidExitFullscreen:"); trace_scope!("windowDidExitFullscreen:");
self.with_state(|state| { self.with_state(|state| {
@ -438,30 +438,31 @@ fn window_did_exit_fullscreen(&self, _: id) {
} }
}) })
}); });
} }
/// Invoked when fail to enter fullscreen /// Invoked when fail to enter fullscreen
/// ///
/// When this window launch from a fullscreen app (e.g. launch from VS Code /// When this window launch from a fullscreen app (e.g. launch from VS Code
/// terminal), it creates a new virtual destkop and a transition animation. /// terminal), it creates a new virtual destkop and a transition animation.
/// This animation takes one second and cannot be disable without /// This animation takes one second and cannot be disable without
/// elevated privileges. In this animation time, all toggleFullscreen events /// elevated privileges. In this animation time, all toggleFullscreen events
/// will be failed. In this implementation, we will try again by using /// will be failed. In this implementation, we will try again by using
/// performSelector:withObject:afterDelay: until window_did_enter_fullscreen. /// performSelector:withObject:afterDelay: until window_did_enter_fullscreen.
/// It should be fine as we only do this at initialzation (i.e with_fullscreen /// It should be fine as we only do this at initialzation (i.e with_fullscreen
/// was set). /// was set).
/// ///
/// From Apple doc: /// From Apple doc:
/// In some cases, the transition to enter full-screen mode can fail, /// In some cases, the transition to enter full-screen mode can fail,
/// due to being in the midst of handling some other animation or user gesture. /// due to being in the midst of handling some other animation or user gesture.
/// This method indicates that there was an error, and you should clean up any /// This method indicates that there was an error, and you should clean up any
/// work you may have done to prepare to enter full-screen mode. /// work you may have done to prepare to enter full-screen mode.
#[sel(windowDidFailToEnterFullscreen:)] #[sel(windowDidFailToEnterFullscreen:)]
fn window_did_fail_to_enter_fullscreen(&self, _: id) { fn window_did_fail_to_enter_fullscreen(&self, _: id) {
trace_scope!("windowDidFailToEnterFullscreen:"); trace_scope!("windowDidFailToEnterFullscreen:");
self.with_state(|state| { self.with_state(|state| {
state.with_window(|window| { state.with_window(|window| {
let mut shared_state = window.lock_shared_state("window_did_fail_to_enter_fullscreen"); let mut shared_state =
window.lock_shared_state("window_did_fail_to_enter_fullscreen");
shared_state.in_fullscreen_transition = false; shared_state.in_fullscreen_transition = false;
shared_state.target_fullscreen = None; shared_state.target_fullscreen = None;
}); });
@ -477,11 +478,11 @@ fn window_did_fail_to_enter_fullscreen(&self, _: id) {
state.with_window(|window| window.restore_state_from_fullscreen()); state.with_window(|window| window.restore_state_from_fullscreen());
} }
}); });
} }
// Invoked when the occlusion state of the window changes // Invoked when the occlusion state of the window changes
#[sel(windowDidChangeOcclusionState:)] #[sel(windowDidChangeOcclusionState:)]
fn window_did_change_occlusion_state(&self, _: id) { fn window_did_change_occlusion_state(&self, _: id) {
trace_scope!("windowDidChangeOcclusionState:"); trace_scope!("windowDidChangeOcclusionState:");
unsafe { unsafe {
self.with_state(|state| { self.with_state(|state| {
@ -493,9 +494,9 @@ fn window_did_change_occlusion_state(&self, _: id) {
)) ))
}); });
} }
}
} }
} }
);
impl WinitWindowDelegate { impl WinitWindowDelegate {
// This function is definitely unsafe (&self -> &mut state), but labeling that // This function is definitely unsafe (&self -> &mut state), but labeling that