2022-09-08 16:45:29 +02:00
use std ::{ boxed ::Box , collections ::VecDeque , os ::raw ::* , ptr , str , sync ::Mutex } ;
2019-05-01 17:03:30 -06:00
2022-09-08 16:45:29 +02:00
use objc2 ::declare ::{ Ivar , IvarDrop } ;
use objc2 ::foundation ::{
NSArray , NSAttributedString , NSAttributedStringKey , NSCopying , NSMutableAttributedString ,
NSObject , NSPoint , NSRange , NSRect , NSSize , NSString , NSUInteger ,
2019-06-21 11:33:15 -04:00
} ;
2022-09-08 16:45:29 +02:00
use objc2 ::rc ::{ Id , Owned , Shared } ;
use objc2 ::runtime ::{ Object , Sel } ;
use objc2 ::{ class , declare_class , msg_send , msg_send_id , sel , ClassType } ;
2019-05-01 17:03:30 -06:00
2022-09-08 16:45:29 +02:00
use super ::appkit ::{
NSApp , NSCursor , NSEvent , NSEventModifierFlags , NSEventPhase , NSResponder , NSTrackingRectTag ,
NSView ,
} ;
2019-06-18 02:27:00 +08:00
use crate ::{
2022-05-23 13:53:07 -06:00
dpi ::{ LogicalPosition , LogicalSize } ,
2019-05-01 17:03:30 -06:00
event ::{
2022-05-07 05:29:25 +03:00
DeviceEvent , ElementState , Event , Ime , KeyboardInput , ModifiersState , MouseButton ,
2019-12-12 22:48:32 +01:00
MouseScrollDelta , TouchPhase , VirtualKeyCode , WindowEvent ,
2019-06-21 11:33:15 -04:00
} ,
platform_impl ::platform ::{
app_state ::AppState ,
event ::{
2022-09-08 16:45:29 +02:00
char_to_keycode , check_function_keys , event_mods , modifier_event , scancode_to_keycode ,
EventWrapper ,
2019-06-21 11:33:15 -04:00
} ,
2022-09-08 16:45:29 +02:00
util ,
window ::WinitWindow ,
2019-06-21 11:33:15 -04:00
DEVICE_ID ,
2019-05-01 17:03:30 -06:00
} ,
window ::WindowId ,
} ;
2022-09-08 16:45:29 +02:00
#[ derive(Debug) ]
2020-01-03 09:34:14 +09:00
pub struct CursorState {
pub visible : bool ,
2022-09-02 21:02:40 +02:00
pub ( super ) cursor : Id < NSCursor , Shared > ,
2020-01-03 09:34:14 +09:00
}
impl Default for CursorState {
fn default ( ) -> Self {
Self {
visible : true ,
cursor : Default ::default ( ) ,
}
}
}
2022-07-21 22:23:22 +03:00
#[ derive(Debug, Eq, PartialEq) ]
2022-05-07 05:29:25 +03:00
enum ImeState {
2022-07-21 22:23:22 +03:00
/// The IME events are disabled, so only `ReceivedCharacter` is being sent to the user.
2022-05-07 05:29:25 +03:00
Disabled ,
2022-07-21 22:23:22 +03:00
/// The IME events are enabled.
2022-05-07 05:29:25 +03:00
Enabled ,
2022-07-21 22:23:22 +03:00
/// The IME is in preedit.
2022-05-07 05:29:25 +03:00
Preedit ,
2022-07-21 22:23:22 +03:00
/// The text was just commited, so the next input from the keyboard must be ignored.
Commited ,
2022-05-07 05:29:25 +03:00
}
2022-09-08 16:45:29 +02:00
#[ derive(Debug) ]
Move `ModifiersChanged` variant to `WindowEvent` (#1381)
* Move `ModifiersChanged` variant to `WindowEvent`
* macos: Fix flags_changed for ModifiersChanged variant move
I haven't look too deep at what this does internally, but at least
cargo-check is fully happy now. :)
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Fire a ModifiersChanged event on window_did_resign_key
From debugging, I determined that macOS' emission of a flagsChanged
around window switching is inconsistent. It is fair to assume, I think,
that when the user switches windows, they do not expect their former
modifiers state to remain effective; so I think it's best to clear that
state by sending a ModifiersChanged(ModifiersState::empty()).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Fix build
I don't know enough about the code to implement the fix as it is done on
this branch, but this commit at least fixes the build.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Send ModifiersChanged(ModifiersState::empty) on KILLFOCUS
Very similar to the changes made in [1], as focus is lost, send an event
to the window indicating that the modifiers have been released.
It's unclear to me (without a Windows device to test this on) whether
this is necessary, but it certainly ensures that unfocused windows will
have at least received this event, which is an improvement.
[1]: f79f21641a31da3e4039d41be89047cdcc6028f7
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Add a hook to update stale modifiers
Sometimes, `ViewState` and `event` might have different values for their
stored `modifiers` flags. These are internally stored as a bitmask in
the latter and an enum in the former.
We can check to see if they differ, and if they do, automatically
dispatch an event to update consumers of modifier state as well as the
stored `state.modifiers`. That's what the hook does.
This hook is then called in the key_down, mouse_entered, mouse_exited,
mouse_click, scroll_wheel, and pressure_change_with_event callbacks,
which each will contain updated modifiers.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Only call event_mods once when determining whether to update state
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* flags_changed: Memoize window_id collection
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_did_resign_key: Remove synthetic ModifiersChanged event
We no longer need to emit this event, since we are checking the state of
our modifiers before emitting most other events.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Add a call to update_potentially_stale_modifiers
Now, cover all events (that I can think of, at least) where stale
modifiers might affect how user programs behave. Effectively, every
human-interface event (keypress, mouse click, keydown, etc.) will cause
a ModifiersChanged event to be fired if something has changed.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* key_up: Add a call to update_potentially_stale_modifiers
We also want to make sure modifiers state is synchronized here, too.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Remove update_potentially_stale_modifiers invocation
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Retry CI
* ViewState: Promote visibility of modifiers to the macos impl
This is so that we can interact with the ViewState directly from the
WindowDelegate.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_delegate: Synthetically set modifiers state to empty on resignKey
This logic is implemented similarly on other platforms, so we wish to
regain parity here. Originally this behavior was implemented to always
fire an event with ModifiersState::empty(), but that was not the best as
it was not necessarily correct and could be a duplicate event.
This solution is perhaps the most elegant possible to implement the
desired behavior of sending a synthetic empty modifiers event when a
window loses focus, trading some safety for interoperation between the
NSWindowDelegate and the NSView (as the objc runtime must now be
consulted in order to acquire access to the ViewState which is "owned"
by the NSView).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Check for modifiers change in window events
* Fix modifier changed on macOS
Since the `mouse_entered` function was generating a mouse motion, which
updates the modifier state, a modifiers changed event was incorrectly
generated.
The updating of the modifier state has also been changed to make sure it
consistently happens before events that have a modifier state attached
to it, without happening on any other event.
This of course means that no `CursorMoved` event is generated anymore
when the user enters the window without it being focused, however I'd
say that is consistent with how winit should behave.
* Fix unused variable warning
* Move changelog entry into `Unreleased` section
Co-authored-by: Freya Gentz <zegentzy@protonmail.com>
Co-authored-by: Kristofer Rye <kristofer.rye@gmail.com>
Co-authored-by: Christian Duerr <contact@christianduerr.com>
2020-03-06 15:43:55 -07:00
pub ( super ) struct ViewState {
2022-09-02 21:02:40 +02:00
pub cursor_state : Mutex < CursorState > ,
2022-03-18 14:50:24 +01:00
ime_position : LogicalPosition < f64 > ,
Move `ModifiersChanged` variant to `WindowEvent` (#1381)
* Move `ModifiersChanged` variant to `WindowEvent`
* macos: Fix flags_changed for ModifiersChanged variant move
I haven't look too deep at what this does internally, but at least
cargo-check is fully happy now. :)
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Fire a ModifiersChanged event on window_did_resign_key
From debugging, I determined that macOS' emission of a flagsChanged
around window switching is inconsistent. It is fair to assume, I think,
that when the user switches windows, they do not expect their former
modifiers state to remain effective; so I think it's best to clear that
state by sending a ModifiersChanged(ModifiersState::empty()).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Fix build
I don't know enough about the code to implement the fix as it is done on
this branch, but this commit at least fixes the build.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Send ModifiersChanged(ModifiersState::empty) on KILLFOCUS
Very similar to the changes made in [1], as focus is lost, send an event
to the window indicating that the modifiers have been released.
It's unclear to me (without a Windows device to test this on) whether
this is necessary, but it certainly ensures that unfocused windows will
have at least received this event, which is an improvement.
[1]: f79f21641a31da3e4039d41be89047cdcc6028f7
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Add a hook to update stale modifiers
Sometimes, `ViewState` and `event` might have different values for their
stored `modifiers` flags. These are internally stored as a bitmask in
the latter and an enum in the former.
We can check to see if they differ, and if they do, automatically
dispatch an event to update consumers of modifier state as well as the
stored `state.modifiers`. That's what the hook does.
This hook is then called in the key_down, mouse_entered, mouse_exited,
mouse_click, scroll_wheel, and pressure_change_with_event callbacks,
which each will contain updated modifiers.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Only call event_mods once when determining whether to update state
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* flags_changed: Memoize window_id collection
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_did_resign_key: Remove synthetic ModifiersChanged event
We no longer need to emit this event, since we are checking the state of
our modifiers before emitting most other events.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Add a call to update_potentially_stale_modifiers
Now, cover all events (that I can think of, at least) where stale
modifiers might affect how user programs behave. Effectively, every
human-interface event (keypress, mouse click, keydown, etc.) will cause
a ModifiersChanged event to be fired if something has changed.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* key_up: Add a call to update_potentially_stale_modifiers
We also want to make sure modifiers state is synchronized here, too.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Remove update_potentially_stale_modifiers invocation
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Retry CI
* ViewState: Promote visibility of modifiers to the macos impl
This is so that we can interact with the ViewState directly from the
WindowDelegate.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_delegate: Synthetically set modifiers state to empty on resignKey
This logic is implemented similarly on other platforms, so we wish to
regain parity here. Originally this behavior was implemented to always
fire an event with ModifiersState::empty(), but that was not the best as
it was not necessarily correct and could be a duplicate event.
This solution is perhaps the most elegant possible to implement the
desired behavior of sending a synthetic empty modifiers event when a
window loses focus, trading some safety for interoperation between the
NSWindowDelegate and the NSView (as the objc runtime must now be
consulted in order to acquire access to the ViewState which is "owned"
by the NSView).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Check for modifiers change in window events
* Fix modifier changed on macOS
Since the `mouse_entered` function was generating a mouse motion, which
updates the modifier state, a modifiers changed event was incorrectly
generated.
The updating of the modifier state has also been changed to make sure it
consistently happens before events that have a modifier state attached
to it, without happening on any other event.
This of course means that no `CursorMoved` event is generated anymore
when the user enters the window without it being focused, however I'd
say that is consistent with how winit should behave.
* Fix unused variable warning
* Move changelog entry into `Unreleased` section
Co-authored-by: Freya Gentz <zegentzy@protonmail.com>
Co-authored-by: Kristofer Rye <kristofer.rye@gmail.com>
Co-authored-by: Christian Duerr <contact@christianduerr.com>
2020-03-06 15:43:55 -07:00
pub ( super ) modifiers : ModifiersState ,
2022-09-08 16:45:29 +02:00
tracking_rect : Option < NSTrackingRectTag > ,
2022-05-07 05:29:25 +03:00
ime_state : ImeState ,
input_source : String ,
/// True iff the application wants IME events.
///
/// Can be set using `set_ime_allowed`
ime_allowed : bool ,
/// True if the current key event should be forwarded
/// to the application, even during IME
forward_key_to_app : bool ,
2018-05-17 21:28:30 -04:00
}
2022-09-08 16:45:29 +02:00
fn get_characters ( event : & NSEvent , ignore_modifiers : bool ) -> String {
if ignore_modifiers {
event . charactersIgnoringModifiers ( )
} else {
event . characters ( )
2022-09-02 18:46:18 +02:00
}
2022-09-08 16:45:29 +02:00
. expect ( " expected characters to be non-null " )
. to_string ( )
2022-09-02 18:46:18 +02:00
}
// As defined in: https://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/CORPCHAR.TXT
fn is_corporate_character ( c : char ) -> bool {
matches! ( c ,
'\u{F700}' ..= '\u{F747}'
| '\u{F802}' ..= '\u{F84F}'
| '\u{F850}'
| '\u{F85C}'
| '\u{F85D}'
| '\u{F85F}'
| '\u{F860}' ..= '\u{F86B}'
| '\u{F870}' ..= '\u{F8FF}'
)
}
// Retrieves a layout-independent keycode given an event.
2022-09-08 16:45:29 +02:00
fn retrieve_keycode ( event : & NSEvent ) -> Option < VirtualKeyCode > {
2022-09-02 18:46:18 +02:00
#[ inline ]
2022-09-08 16:45:29 +02:00
fn get_code ( ev : & NSEvent , raw : bool ) -> Option < VirtualKeyCode > {
2022-09-02 18:46:18 +02:00
let characters = get_characters ( ev , raw ) ;
characters . chars ( ) . next ( ) . and_then ( char_to_keycode )
}
// Cmd switches Roman letters for Dvorak-QWERTY layout, so we try modified characters first.
// If we don't get a match, then we fall back to unmodified characters.
let code = get_code ( event , false ) . or_else ( | | get_code ( event , true ) ) ;
// We've checked all layout related keys, so fall through to scancode.
// Reaching this code means that the key is layout-independent (e.g. Backspace, Return).
//
// We're additionally checking here for F21-F24 keys, since their keycode
// can vary, but we know that they are encoded
// in characters property.
code . or_else ( | | {
2022-09-08 16:45:29 +02:00
let scancode = event . scancode ( ) ;
2022-09-02 18:46:18 +02:00
scancode_to_keycode ( scancode ) . or_else ( | | check_function_keys ( & get_characters ( event , true ) ) )
} )
}
2022-09-02 19:38:32 +02:00
declare_class! (
2022-09-02 18:46:18 +02:00
#[ derive(Debug) ]
#[ allow(non_snake_case) ]
2022-09-08 16:45:29 +02:00
pub ( super ) struct WinitView {
_ns_window : IvarDrop < Id < WinitWindow , Shared > > ,
pub ( super ) state : IvarDrop < Box < ViewState > > ,
marked_text : IvarDrop < Id < NSMutableAttributedString , Owned > > ,
2022-09-02 18:46:18 +02:00
}
unsafe impl ClassType for WinitView {
#[ inherits(NSResponder, NSObject) ]
2022-09-08 16:45:29 +02:00
type Super = NSView ;
2022-09-02 18:46:18 +02:00
}
unsafe impl WinitView {
2022-09-08 16:45:29 +02:00
#[ sel(initWithId:) ]
fn init_with_id ( & mut self , window : * mut WinitWindow ) -> Option < & mut Self > {
let this : Option < & mut Self > = unsafe { msg_send! [ super ( self ) , init ] } ;
this . map ( | this | {
let state = ViewState {
cursor_state : Default ::default ( ) ,
ime_position : LogicalPosition ::new ( 0.0 , 0.0 ) ,
modifiers : Default ::default ( ) ,
tracking_rect : None ,
ime_state : ImeState ::Disabled ,
input_source : String ::new ( ) ,
ime_allowed : false ,
forward_key_to_app : false ,
} ;
Ivar ::write (
& mut this . _ns_window ,
unsafe { Id ::retain ( window ) } . expect ( " non-null window " ) ,
) ;
Ivar ::write ( & mut this . state , Box ::new ( state ) ) ;
Ivar ::write ( & mut this . marked_text , NSMutableAttributedString ::new ( ) ) ;
2018-05-17 21:28:30 -04:00
2022-09-08 16:45:29 +02:00
this . setPostsFrameChangedNotifications ( true ) ;
2022-09-02 19:38:32 +02:00
let notification_center : & Object =
2022-09-08 16:45:29 +02:00
unsafe { msg_send! [ class! ( NSNotificationCenter ) , defaultCenter ] } ;
2022-09-02 19:38:32 +02:00
// About frame change
let frame_did_change_notification_name =
2022-09-08 16:45:29 +02:00
NSString ::from_str ( " NSViewFrameDidChangeNotification " ) ;
#[ allow(clippy::let_unit_value) ]
unsafe {
let _ : ( ) = msg_send! [
notification_center ,
addObserver : & * this ,
selector : sel ! ( frameDidChange :) ,
name : & * frame_did_change_notification_name ,
object : & * this ,
] ;
}
this . state . input_source = this . current_input_source ( ) ;
2022-09-02 19:38:32 +02:00
this
} )
}
2022-09-02 18:46:18 +02:00
}
unsafe impl WinitView {
2022-09-02 19:38:32 +02:00
#[ sel(viewDidMoveToWindow) ]
2022-09-08 16:45:29 +02:00
fn view_did_move_to_window ( & mut self ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " viewDidMoveToWindow " ) ;
2022-09-08 16:45:29 +02:00
if let Some ( tracking_rect ) = self . state . tracking_rect . take ( ) {
self . removeTrackingRect ( tracking_rect ) ;
2022-09-02 19:38:32 +02:00
}
2022-09-08 16:45:29 +02:00
let rect = self . visibleRect ( ) ;
let tracking_rect = self . add_tracking_rect ( rect , false ) ;
self . state . tracking_rect = Some ( tracking_rect ) ;
2019-12-31 05:32:37 +09:00
}
2022-09-02 19:38:32 +02:00
#[ sel(frameDidChange:) ]
2022-09-08 16:45:29 +02:00
fn frame_did_change ( & mut self , _event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " frameDidChange: " ) ;
2022-09-08 16:45:29 +02:00
if let Some ( tracking_rect ) = self . state . tracking_rect . take ( ) {
self . removeTrackingRect ( tracking_rect ) ;
2022-09-02 19:38:32 +02:00
}
2022-09-08 16:45:29 +02:00
let rect = self . visibleRect ( ) ;
let tracking_rect = self . add_tracking_rect ( rect , false ) ;
self . state . tracking_rect = Some ( tracking_rect ) ;
// 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.
// 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 size = logical_size . to_physical ::< u32 > ( self . scale_factor ( ) ) ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::Resized ( size ) ,
} ) ) ;
2019-12-31 05:32:37 +09:00
}
2022-09-02 19:38:32 +02:00
#[ sel(drawRect:) ]
2022-09-08 16:45:29 +02:00
fn draw_rect ( & mut self , rect : NSRect ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " drawRect: " ) ;
2019-12-31 05:32:37 +09:00
2022-09-08 16:45:29 +02:00
AppState ::handle_redraw ( self . window_id ( ) ) ;
2018-12-27 15:16:58 -05:00
2022-09-08 16:45:29 +02:00
#[ allow(clippy::let_unit_value) ]
unsafe {
2022-09-02 19:38:32 +02:00
let _ : ( ) = msg_send! [ super ( self ) , drawRect : rect ] ;
}
}
2019-05-01 17:03:30 -06:00
2022-09-02 19:38:32 +02:00
#[ sel(acceptsFirstResponder) ]
fn accepts_first_responder ( & self ) -> bool {
trace_scope! ( " acceptsFirstResponder " ) ;
true
}
2019-05-01 17:03:30 -06:00
2022-09-02 19:38:32 +02:00
// 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
// TODO: Add an API extension for using `NSTouchBar`
#[ sel(touchBar) ]
fn touch_bar ( & self ) -> bool {
trace_scope! ( " touchBar " ) ;
false
}
2018-12-28 15:29:29 -05:00
2022-09-02 19:38:32 +02:00
#[ sel(resetCursorRects) ]
fn reset_cursor_rects ( & self ) {
trace_scope! ( " resetCursorRects " ) ;
2022-09-02 21:02:40 +02:00
let bounds = self . bounds ( ) ;
2022-09-08 16:45:29 +02:00
let cursor_state = self . state . cursor_state . lock ( ) . unwrap ( ) ;
// We correctly invoke `addCursorRect` only from inside `resetCursorRects`
2022-09-02 21:02:40 +02:00
if cursor_state . visible {
self . addCursorRect ( bounds , & cursor_state . cursor ) ;
} else {
self . addCursorRect ( bounds , & NSCursor ::invisible ( ) ) ;
2022-09-02 19:38:32 +02:00
}
}
2022-09-02 18:46:18 +02:00
}
2018-12-28 15:29:29 -05:00
2022-09-02 18:46:18 +02:00
unsafe impl Protocol < NSTextInputClient > for WinitView {
2022-09-02 19:38:32 +02:00
#[ sel(hasMarkedText) ]
fn has_marked_text ( & self ) -> bool {
trace_scope! ( " hasMarkedText " ) ;
2022-09-08 16:45:29 +02:00
self . marked_text . len_utf16 ( ) > 0
2022-09-02 19:38:32 +02:00
}
2022-09-02 18:46:18 +02:00
2022-09-02 19:38:32 +02:00
#[ sel(markedRange) ]
fn marked_range ( & self ) -> NSRange {
trace_scope! ( " markedRange " ) ;
2022-09-08 16:45:29 +02:00
let length = self . marked_text . len_utf16 ( ) ;
if length > 0 {
NSRange ::new ( 0 , length )
} else {
util ::EMPTY_RANGE
2022-09-02 19:38:32 +02:00
}
}
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
#[ sel(selectedRange) ]
fn selected_range ( & self ) -> NSRange {
trace_scope! ( " selectedRange " ) ;
2018-05-17 21:28:30 -04:00
util ::EMPTY_RANGE
}
2022-09-02 19:38:32 +02:00
#[ sel(setMarkedText:selectedRange:replacementRange:) ]
fn set_marked_text (
& mut self ,
2022-09-08 16:45:29 +02:00
string : & NSObject ,
2022-09-02 19:38:32 +02:00
_selected_range : NSRange ,
_replacement_range : NSRange ,
) {
trace_scope! ( " setMarkedText:selectedRange:replacementRange: " ) ;
2018-05-17 21:28:30 -04:00
2022-09-08 16:45:29 +02:00
// SAFETY: This method is guaranteed to get either a `NSString` or a `NSAttributedString`.
let ( marked_text , preedit_string ) = if string . is_kind_of ::< NSAttributedString > ( ) {
let string : * const NSObject = string ;
let string : * const NSAttributedString = string . cast ( ) ;
let string = unsafe { & * string } ;
(
NSMutableAttributedString ::from_attributed_nsstring ( string ) ,
string . string ( ) . to_string ( ) ,
)
} else {
let string : * const NSObject = string ;
let string : * const NSString = string . cast ( ) ;
let string = unsafe { & * string } ;
(
NSMutableAttributedString ::from_nsstring ( string ) ,
string . to_string ( ) ,
)
} ;
2022-05-07 05:29:25 +03:00
2022-09-08 16:45:29 +02:00
// Update marked text
* self . marked_text = marked_text ;
2022-05-07 05:29:25 +03:00
2022-09-08 16:45:29 +02:00
// Notify IME is active if application still doesn't know it.
if self . state . ime_state = = ImeState ::Disabled {
self . state . input_source = self . current_input_source ( ) ;
2022-09-02 19:38:32 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
2022-09-08 16:45:29 +02:00
window_id : self . window_id ( ) ,
event : WindowEvent ::Ime ( Ime ::Enabled ) ,
2022-09-02 19:38:32 +02:00
} ) ) ;
}
2022-09-08 16:45:29 +02:00
// Don't update self.state to preedit when we've just commited a string, since the following
// preedit string will be None anyway.
if self . state . ime_state ! = ImeState ::Commited {
self . state . ime_state = ImeState ::Preedit ;
}
// Empty string basically means that there's no preedit, so indicate that by sending
// `None` cursor range.
let cursor_range = if preedit_string . is_empty ( ) {
None
} else {
Some ( ( preedit_string . len ( ) , preedit_string . len ( ) ) )
} ;
// Send WindowEvent for updating marked text
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::Ime ( Ime ::Preedit ( preedit_string , cursor_range ) ) ,
} ) ) ;
2022-07-21 22:23:22 +03:00
}
2022-09-02 19:38:32 +02:00
#[ sel(unmarkText) ]
2022-09-08 16:45:29 +02:00
fn unmark_text ( & mut self ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " unmarkText " ) ;
2022-09-08 16:45:29 +02:00
* self . marked_text = NSMutableAttributedString ::new ( ) ;
let input_context = self . inputContext ( ) . expect ( " input context " ) ;
input_context . discardMarkedText ( ) ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::Ime ( Ime ::Preedit ( String ::new ( ) , None ) ) ,
} ) ) ;
if self . is_ime_enabled ( ) {
// Leave the Preedit self.state
self . state . ime_state = ImeState ::Enabled ;
} else {
warn! ( " Expected to have IME enabled when receiving unmarkText " ) ;
2022-09-02 19:38:32 +02:00
}
2022-05-07 05:29:25 +03:00
}
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
#[ sel(validAttributesForMarkedText) ]
2022-09-08 16:45:29 +02:00
fn valid_attributes_for_marked_text ( & self ) -> * const NSArray < NSAttributedStringKey > {
2022-09-02 19:38:32 +02:00
trace_scope! ( " validAttributesForMarkedText " ) ;
2022-09-08 16:45:29 +02:00
Id ::autorelease_return ( NSArray ::new ( ) )
2022-09-02 19:38:32 +02:00
}
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
#[ sel(attributedSubstringForProposedRange:actualRange:) ]
fn attributed_substring_for_proposed_range (
& self ,
_range : NSRange ,
_actual_range : * mut c_void , // *mut NSRange
2022-09-08 16:45:29 +02:00
) -> * const NSAttributedString {
2022-09-02 19:38:32 +02:00
trace_scope! ( " attributedSubstringForProposedRange:actualRange: " ) ;
2022-09-08 16:45:29 +02:00
ptr ::null ( )
2022-09-02 19:38:32 +02:00
}
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
#[ sel(characterIndexForPoint:) ]
fn character_index_for_point ( & self , _point : NSPoint ) -> NSUInteger {
trace_scope! ( " characterIndexForPoint: " ) ;
0
}
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
#[ sel(firstRectForCharacterRange:actualRange:) ]
fn first_rect_for_character_range (
& self ,
_range : NSRange ,
_actual_range : * mut c_void , // *mut NSRange
) -> NSRect {
trace_scope! ( " firstRectForCharacterRange:actualRange: " ) ;
2022-09-08 16:45:29 +02:00
let window = self . window ( ) ;
let content_rect = window . contentRectForFrameRect ( window . frame ( ) ) ;
let base_x = content_rect . origin . x as f64 ;
let base_y = ( content_rect . origin . y + content_rect . size . height ) as f64 ;
let x = base_x + self . state . ime_position . x ;
let y = base_y - self . state . ime_position . y ;
// This is not ideal: We _should_ return a different position based on
// the currently selected character (which varies depending on the type
// and size of the character), but in the current `winit` API there is
// no way to express this. Same goes for the `NSSize`.
NSRect ::new ( NSPoint ::new ( x as _ , y as _ ) , NSSize ::new ( 0.0 , 0.0 ) )
2022-09-02 19:38:32 +02:00
}
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
#[ sel(insertText:replacementRange:) ]
2022-09-08 16:45:29 +02:00
fn insert_text ( & mut self , string : & NSObject , _replacement_range : NSRange ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " insertText:replacementRange: " ) ;
2018-05-17 21:28:30 -04:00
2022-09-08 16:45:29 +02:00
// SAFETY: This method is guaranteed to get either a `NSString` or a `NSAttributedString`.
let string = if string . is_kind_of ::< NSAttributedString > ( ) {
let string : * const NSObject = string ;
let string : * const NSAttributedString = string . cast ( ) ;
unsafe { & * string } . string ( ) . to_string ( )
} else {
let string : * const NSObject = string ;
let string : * const NSString = string . cast ( ) ;
unsafe { & * string } . to_string ( )
} ;
2018-05-17 21:28:30 -04:00
2022-09-08 16:45:29 +02:00
let is_control = string . chars ( ) . next ( ) . map_or ( false , | c | c . is_control ( ) ) ;
2018-05-17 21:28:30 -04:00
2022-09-08 16:45:29 +02:00
if self . is_ime_enabled ( ) & & ! is_control {
2022-09-11 00:48:24 +03:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::Ime ( Ime ::Preedit ( String ::new ( ) , None ) ) ,
} ) ) ;
2022-09-08 16:45:29 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::Ime ( Ime ::Commit ( string ) ) ,
} ) ) ;
self . state . ime_state = ImeState ::Commited ;
2022-09-02 19:38:32 +02:00
}
2018-05-17 21:28:30 -04:00
}
2022-09-08 16:45:29 +02:00
// 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.
2022-09-02 19:38:32 +02:00
#[ sel(doCommandBySelector:) ]
2022-09-08 16:45:29 +02:00
fn do_command_by_selector ( & mut self , _command : Sel ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " doCommandBySelector: " ) ;
2022-09-08 16:45:29 +02:00
// We shouldn't forward any character from just commited text, since we'll end up sending
// it twice with some IMEs like Korean one. We'll also always send `Enter` in that case,
// which is not desired given it was used to confirm IME input.
if self . state . ime_state = = ImeState ::Commited {
return ;
}
2022-07-21 22:23:22 +03:00
2022-09-08 16:45:29 +02:00
self . state . forward_key_to_app = true ;
2018-05-17 21:28:30 -04:00
2022-09-08 16:45:29 +02:00
if self . hasMarkedText ( ) & & self . state . ime_state = = ImeState ::Preedit {
// Leave preedit so that we also report the keyup for this key
self . state . ime_state = ImeState ::Enabled ;
2022-09-02 19:38:32 +02:00
}
2022-05-07 05:29:25 +03:00
}
2018-05-17 21:28:30 -04:00
}
2019-02-24 06:41:55 +10:00
2022-09-02 18:46:18 +02:00
unsafe impl WinitView {
2022-09-02 19:38:32 +02:00
#[ sel(keyDown:) ]
2022-09-08 16:45:29 +02:00
fn key_down ( & mut self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " keyDown: " ) ;
2022-09-08 16:45:29 +02:00
let window_id = self . window_id ( ) ;
let input_source = self . current_input_source ( ) ;
if self . state . input_source ! = input_source & & self . is_ime_enabled ( ) {
self . state . ime_state = ImeState ::Disabled ;
self . state . input_source = input_source ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id ,
event : WindowEvent ::Ime ( Ime ::Disabled ) ,
} ) ) ;
}
let was_in_preedit = self . state . ime_state = = ImeState ::Preedit ;
let characters = get_characters ( event , false ) ;
self . state . forward_key_to_app = false ;
// The `interpretKeyEvents` function might call
// `setMarkedText`, `insertText`, and `doCommandBySelector`.
// It's important that we call this before queuing the KeyboardInput, because
// we must send the `KeyboardInput` event during IME if it triggered
// `doCommandBySelector`. (doCommandBySelector means that the keyboard input
// is not handled by IME and should be handled by the application)
let mut text_commited = false ;
if self . state . ime_allowed {
let events_for_nsview = NSArray ::from_slice ( & [ event . copy ( ) ] ) ;
unsafe { self . interpretKeyEvents ( & events_for_nsview ) } ;
// If the text was commited we must treat the next keyboard event as IME related.
if self . state . ime_state = = ImeState ::Commited {
self . state . ime_state = ImeState ::Enabled ;
text_commited = true ;
2022-09-02 19:38:32 +02:00
}
2022-09-08 16:45:29 +02:00
}
Move `ModifiersChanged` variant to `WindowEvent` (#1381)
* Move `ModifiersChanged` variant to `WindowEvent`
* macos: Fix flags_changed for ModifiersChanged variant move
I haven't look too deep at what this does internally, but at least
cargo-check is fully happy now. :)
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Fire a ModifiersChanged event on window_did_resign_key
From debugging, I determined that macOS' emission of a flagsChanged
around window switching is inconsistent. It is fair to assume, I think,
that when the user switches windows, they do not expect their former
modifiers state to remain effective; so I think it's best to clear that
state by sending a ModifiersChanged(ModifiersState::empty()).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Fix build
I don't know enough about the code to implement the fix as it is done on
this branch, but this commit at least fixes the build.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Send ModifiersChanged(ModifiersState::empty) on KILLFOCUS
Very similar to the changes made in [1], as focus is lost, send an event
to the window indicating that the modifiers have been released.
It's unclear to me (without a Windows device to test this on) whether
this is necessary, but it certainly ensures that unfocused windows will
have at least received this event, which is an improvement.
[1]: f79f21641a31da3e4039d41be89047cdcc6028f7
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Add a hook to update stale modifiers
Sometimes, `ViewState` and `event` might have different values for their
stored `modifiers` flags. These are internally stored as a bitmask in
the latter and an enum in the former.
We can check to see if they differ, and if they do, automatically
dispatch an event to update consumers of modifier state as well as the
stored `state.modifiers`. That's what the hook does.
This hook is then called in the key_down, mouse_entered, mouse_exited,
mouse_click, scroll_wheel, and pressure_change_with_event callbacks,
which each will contain updated modifiers.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Only call event_mods once when determining whether to update state
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* flags_changed: Memoize window_id collection
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_did_resign_key: Remove synthetic ModifiersChanged event
We no longer need to emit this event, since we are checking the state of
our modifiers before emitting most other events.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Add a call to update_potentially_stale_modifiers
Now, cover all events (that I can think of, at least) where stale
modifiers might affect how user programs behave. Effectively, every
human-interface event (keypress, mouse click, keydown, etc.) will cause
a ModifiersChanged event to be fired if something has changed.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* key_up: Add a call to update_potentially_stale_modifiers
We also want to make sure modifiers state is synchronized here, too.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Remove update_potentially_stale_modifiers invocation
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Retry CI
* ViewState: Promote visibility of modifiers to the macos impl
This is so that we can interact with the ViewState directly from the
WindowDelegate.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_delegate: Synthetically set modifiers state to empty on resignKey
This logic is implemented similarly on other platforms, so we wish to
regain parity here. Originally this behavior was implemented to always
fire an event with ModifiersState::empty(), but that was not the best as
it was not necessarily correct and could be a duplicate event.
This solution is perhaps the most elegant possible to implement the
desired behavior of sending a synthetic empty modifiers event when a
window loses focus, trading some safety for interoperation between the
NSWindowDelegate and the NSView (as the objc runtime must now be
consulted in order to acquire access to the ViewState which is "owned"
by the NSView).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Check for modifiers change in window events
* Fix modifier changed on macOS
Since the `mouse_entered` function was generating a mouse motion, which
updates the modifier state, a modifiers changed event was incorrectly
generated.
The updating of the modifier state has also been changed to make sure it
consistently happens before events that have a modifier state attached
to it, without happening on any other event.
This of course means that no `CursorMoved` event is generated anymore
when the user enters the window without it being focused, however I'd
say that is consistent with how winit should behave.
* Fix unused variable warning
* Move changelog entry into `Unreleased` section
Co-authored-by: Freya Gentz <zegentzy@protonmail.com>
Co-authored-by: Kristofer Rye <kristofer.rye@gmail.com>
Co-authored-by: Christian Duerr <contact@christianduerr.com>
2020-03-06 15:43:55 -07:00
2022-09-08 16:45:29 +02:00
let now_in_preedit = self . state . ime_state = = ImeState ::Preedit ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
let scancode = event . scancode ( ) as u32 ;
let virtual_keycode = retrieve_keycode ( event ) ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
self . update_potentially_stale_modifiers ( event ) ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
let ime_related = was_in_preedit | | now_in_preedit | | text_commited ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
if ! ime_related | | self . state . forward_key_to_app | | ! self . state . ime_allowed {
#[ allow(deprecated) ]
let window_event = Event ::WindowEvent {
window_id ,
event : WindowEvent ::KeyboardInput {
device_id : DEVICE_ID ,
input : KeyboardInput {
state : ElementState ::Pressed ,
scancode ,
virtual_keycode ,
modifiers : event_mods ( event ) ,
2022-09-02 19:38:32 +02:00
} ,
2022-09-08 16:45:29 +02:00
is_synthetic : false ,
} ,
} ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
for character in characters . chars ( ) . filter ( | c | ! is_corporate_character ( * c ) ) {
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id ,
event : WindowEvent ::ReceivedCharacter ( character ) ,
} ) ) ;
2022-09-02 19:38:32 +02:00
}
}
2022-05-07 05:29:25 +03:00
}
2022-09-02 19:38:32 +02:00
#[ sel(keyUp:) ]
2022-09-08 16:45:29 +02:00
fn key_up ( & mut self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " keyUp: " ) ;
2022-09-08 16:45:29 +02:00
let scancode = event . scancode ( ) as u32 ;
let virtual_keycode = retrieve_keycode ( event ) ;
self . update_potentially_stale_modifiers ( event ) ;
// We want to send keyboard input when we are not currently in preedit
if self . state . ime_state ! = ImeState ::Preedit {
#[ allow(deprecated) ]
let window_event = Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::KeyboardInput {
device_id : DEVICE_ID ,
input : KeyboardInput {
state : ElementState ::Released ,
scancode ,
virtual_keycode ,
modifiers : event_mods ( event ) ,
2022-09-02 19:38:32 +02:00
} ,
2022-09-08 16:45:29 +02:00
is_synthetic : false ,
} ,
} ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
2022-07-21 22:23:22 +03:00
}
2022-05-07 05:29:25 +03:00
}
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
#[ sel(flagsChanged:) ]
2022-09-08 16:45:29 +02:00
fn flags_changed ( & mut self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " flagsChanged: " ) ;
2022-09-08 16:45:29 +02:00
let mut events = VecDeque ::with_capacity ( 4 ) ;
2018-09-12 20:04:16 +03:00
2022-09-08 16:45:29 +02:00
if let Some ( window_event ) = modifier_event (
event ,
NSEventModifierFlags ::NSShiftKeyMask ,
self . state . modifiers . shift ( ) ,
) {
self . state . modifiers . toggle ( ModifiersState ::SHIFT ) ;
events . push_back ( window_event ) ;
}
2019-05-01 17:03:30 -06:00
2022-09-08 16:45:29 +02:00
if let Some ( window_event ) = modifier_event (
event ,
NSEventModifierFlags ::NSControlKeyMask ,
self . state . modifiers . ctrl ( ) ,
) {
self . state . modifiers . toggle ( ModifiersState ::CTRL ) ;
events . push_back ( window_event ) ;
}
Move `ModifiersChanged` variant to `WindowEvent` (#1381)
* Move `ModifiersChanged` variant to `WindowEvent`
* macos: Fix flags_changed for ModifiersChanged variant move
I haven't look too deep at what this does internally, but at least
cargo-check is fully happy now. :)
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Fire a ModifiersChanged event on window_did_resign_key
From debugging, I determined that macOS' emission of a flagsChanged
around window switching is inconsistent. It is fair to assume, I think,
that when the user switches windows, they do not expect their former
modifiers state to remain effective; so I think it's best to clear that
state by sending a ModifiersChanged(ModifiersState::empty()).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Fix build
I don't know enough about the code to implement the fix as it is done on
this branch, but this commit at least fixes the build.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Send ModifiersChanged(ModifiersState::empty) on KILLFOCUS
Very similar to the changes made in [1], as focus is lost, send an event
to the window indicating that the modifiers have been released.
It's unclear to me (without a Windows device to test this on) whether
this is necessary, but it certainly ensures that unfocused windows will
have at least received this event, which is an improvement.
[1]: f79f21641a31da3e4039d41be89047cdcc6028f7
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Add a hook to update stale modifiers
Sometimes, `ViewState` and `event` might have different values for their
stored `modifiers` flags. These are internally stored as a bitmask in
the latter and an enum in the former.
We can check to see if they differ, and if they do, automatically
dispatch an event to update consumers of modifier state as well as the
stored `state.modifiers`. That's what the hook does.
This hook is then called in the key_down, mouse_entered, mouse_exited,
mouse_click, scroll_wheel, and pressure_change_with_event callbacks,
which each will contain updated modifiers.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Only call event_mods once when determining whether to update state
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* flags_changed: Memoize window_id collection
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_did_resign_key: Remove synthetic ModifiersChanged event
We no longer need to emit this event, since we are checking the state of
our modifiers before emitting most other events.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Add a call to update_potentially_stale_modifiers
Now, cover all events (that I can think of, at least) where stale
modifiers might affect how user programs behave. Effectively, every
human-interface event (keypress, mouse click, keydown, etc.) will cause
a ModifiersChanged event to be fired if something has changed.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* key_up: Add a call to update_potentially_stale_modifiers
We also want to make sure modifiers state is synchronized here, too.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Remove update_potentially_stale_modifiers invocation
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Retry CI
* ViewState: Promote visibility of modifiers to the macos impl
This is so that we can interact with the ViewState directly from the
WindowDelegate.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_delegate: Synthetically set modifiers state to empty on resignKey
This logic is implemented similarly on other platforms, so we wish to
regain parity here. Originally this behavior was implemented to always
fire an event with ModifiersState::empty(), but that was not the best as
it was not necessarily correct and could be a duplicate event.
This solution is perhaps the most elegant possible to implement the
desired behavior of sending a synthetic empty modifiers event when a
window loses focus, trading some safety for interoperation between the
NSWindowDelegate and the NSView (as the objc runtime must now be
consulted in order to acquire access to the ViewState which is "owned"
by the NSView).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Check for modifiers change in window events
* Fix modifier changed on macOS
Since the `mouse_entered` function was generating a mouse motion, which
updates the modifier state, a modifiers changed event was incorrectly
generated.
The updating of the modifier state has also been changed to make sure it
consistently happens before events that have a modifier state attached
to it, without happening on any other event.
This of course means that no `CursorMoved` event is generated anymore
when the user enters the window without it being focused, however I'd
say that is consistent with how winit should behave.
* Fix unused variable warning
* Move changelog entry into `Unreleased` section
Co-authored-by: Freya Gentz <zegentzy@protonmail.com>
Co-authored-by: Kristofer Rye <kristofer.rye@gmail.com>
Co-authored-by: Christian Duerr <contact@christianduerr.com>
2020-03-06 15:43:55 -07:00
2022-09-08 16:45:29 +02:00
if let Some ( window_event ) = modifier_event (
event ,
NSEventModifierFlags ::NSCommandKeyMask ,
self . state . modifiers . logo ( ) ,
) {
self . state . modifiers . toggle ( ModifiersState ::LOGO ) ;
events . push_back ( window_event ) ;
}
2022-07-21 22:23:22 +03:00
2022-09-08 16:45:29 +02:00
if let Some ( window_event ) = modifier_event (
event ,
NSEventModifierFlags ::NSAlternateKeyMask ,
self . state . modifiers . alt ( ) ,
) {
self . state . modifiers . toggle ( ModifiersState ::ALT ) ;
events . push_back ( window_event ) ;
}
2018-05-17 21:28:30 -04:00
2022-09-08 16:45:29 +02:00
let window_id = self . window_id ( ) ;
2019-05-01 17:03:30 -06:00
2022-09-08 16:45:29 +02:00
for event in events {
2022-05-07 05:29:25 +03:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id ,
2022-09-08 16:45:29 +02:00
event ,
2022-05-07 05:29:25 +03:00
} ) ) ;
}
2022-09-08 16:45:29 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id ,
event : WindowEvent ::ModifiersChanged ( self . state . modifiers ) ,
} ) ) ;
2018-05-17 21:28:30 -04:00
}
2022-09-02 19:38:32 +02:00
#[ sel(insertTab:) ]
2022-09-08 16:45:29 +02:00
fn insert_tab ( & self , _sender : * const Object ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " insertTab: " ) ;
2022-09-08 16:45:29 +02:00
let window = self . window ( ) ;
if let Some ( first_responder ) = window . firstResponder ( ) {
if * first_responder = = * * * self {
window . selectNextKeyView ( Some ( self ) )
2022-09-02 19:38:32 +02:00
}
}
}
2018-09-12 20:04:16 +03:00
2022-09-02 19:38:32 +02:00
#[ sel(insertBackTab:) ]
2022-09-08 16:45:29 +02:00
fn insert_back_tab ( & self , _sender : * const Object ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " insertBackTab: " ) ;
2022-09-08 16:45:29 +02:00
let window = self . window ( ) ;
if let Some ( first_responder ) = window . firstResponder ( ) {
if * first_responder = = * * * self {
window . selectPreviousKeyView ( Some ( self ) )
2022-09-02 19:38:32 +02:00
}
}
}
Move `ModifiersChanged` variant to `WindowEvent` (#1381)
* Move `ModifiersChanged` variant to `WindowEvent`
* macos: Fix flags_changed for ModifiersChanged variant move
I haven't look too deep at what this does internally, but at least
cargo-check is fully happy now. :)
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Fire a ModifiersChanged event on window_did_resign_key
From debugging, I determined that macOS' emission of a flagsChanged
around window switching is inconsistent. It is fair to assume, I think,
that when the user switches windows, they do not expect their former
modifiers state to remain effective; so I think it's best to clear that
state by sending a ModifiersChanged(ModifiersState::empty()).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Fix build
I don't know enough about the code to implement the fix as it is done on
this branch, but this commit at least fixes the build.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Send ModifiersChanged(ModifiersState::empty) on KILLFOCUS
Very similar to the changes made in [1], as focus is lost, send an event
to the window indicating that the modifiers have been released.
It's unclear to me (without a Windows device to test this on) whether
this is necessary, but it certainly ensures that unfocused windows will
have at least received this event, which is an improvement.
[1]: f79f21641a31da3e4039d41be89047cdcc6028f7
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Add a hook to update stale modifiers
Sometimes, `ViewState` and `event` might have different values for their
stored `modifiers` flags. These are internally stored as a bitmask in
the latter and an enum in the former.
We can check to see if they differ, and if they do, automatically
dispatch an event to update consumers of modifier state as well as the
stored `state.modifiers`. That's what the hook does.
This hook is then called in the key_down, mouse_entered, mouse_exited,
mouse_click, scroll_wheel, and pressure_change_with_event callbacks,
which each will contain updated modifiers.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Only call event_mods once when determining whether to update state
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* flags_changed: Memoize window_id collection
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_did_resign_key: Remove synthetic ModifiersChanged event
We no longer need to emit this event, since we are checking the state of
our modifiers before emitting most other events.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Add a call to update_potentially_stale_modifiers
Now, cover all events (that I can think of, at least) where stale
modifiers might affect how user programs behave. Effectively, every
human-interface event (keypress, mouse click, keydown, etc.) will cause
a ModifiersChanged event to be fired if something has changed.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* key_up: Add a call to update_potentially_stale_modifiers
We also want to make sure modifiers state is synchronized here, too.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Remove update_potentially_stale_modifiers invocation
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Retry CI
* ViewState: Promote visibility of modifiers to the macos impl
This is so that we can interact with the ViewState directly from the
WindowDelegate.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_delegate: Synthetically set modifiers state to empty on resignKey
This logic is implemented similarly on other platforms, so we wish to
regain parity here. Originally this behavior was implemented to always
fire an event with ModifiersState::empty(), but that was not the best as
it was not necessarily correct and could be a duplicate event.
This solution is perhaps the most elegant possible to implement the
desired behavior of sending a synthetic empty modifiers event when a
window loses focus, trading some safety for interoperation between the
NSWindowDelegate and the NSView (as the objc runtime must now be
consulted in order to acquire access to the ViewState which is "owned"
by the NSView).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Check for modifiers change in window events
* Fix modifier changed on macOS
Since the `mouse_entered` function was generating a mouse motion, which
updates the modifier state, a modifiers changed event was incorrectly
generated.
The updating of the modifier state has also been changed to make sure it
consistently happens before events that have a modifier state attached
to it, without happening on any other event.
This of course means that no `CursorMoved` event is generated anymore
when the user enters the window without it being focused, however I'd
say that is consistent with how winit should behave.
* Fix unused variable warning
* Move changelog entry into `Unreleased` section
Co-authored-by: Freya Gentz <zegentzy@protonmail.com>
Co-authored-by: Kristofer Rye <kristofer.rye@gmail.com>
Co-authored-by: Christian Duerr <contact@christianduerr.com>
2020-03-06 15:43:55 -07:00
2022-09-02 19:38:32 +02:00
// Allows us to receive Cmd-. (the shortcut for closing a dialog)
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=300620#c6
#[ sel(cancelOperation:) ]
2022-09-08 16:45:29 +02:00
fn cancel_operation ( & mut self , _sender : * const Object ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " cancelOperation: " ) ;
2022-09-08 16:45:29 +02:00
let scancode = 0x2f ;
let virtual_keycode = scancode_to_keycode ( scancode ) ;
debug_assert_eq! ( virtual_keycode , Some ( VirtualKeyCode ::Period ) ) ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
let event = NSApp ( )
. currentEvent ( )
. expect ( " could not find current event " ) ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
self . update_potentially_stale_modifiers ( & event ) ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
#[ allow(deprecated) ]
let window_event = Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::KeyboardInput {
device_id : DEVICE_ID ,
input : KeyboardInput {
state : ElementState ::Pressed ,
scancode : scancode as _ ,
virtual_keycode ,
modifiers : event_mods ( & event ) ,
2022-05-07 05:29:25 +03:00
} ,
2022-09-08 16:45:29 +02:00
is_synthetic : false ,
} ,
} ;
2018-05-17 21:28:30 -04:00
2022-09-08 16:45:29 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
2022-05-07 05:29:25 +03:00
}
2019-05-01 17:03:30 -06:00
2022-09-02 19:38:32 +02:00
#[ sel(mouseDown:) ]
2022-09-08 16:45:29 +02:00
fn mouse_down ( & mut self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " mouseDown: " ) ;
2022-09-08 16:45:29 +02:00
self . mouse_motion ( event ) ;
self . mouse_click ( event , MouseButton ::Left , ElementState ::Pressed ) ;
2019-05-01 17:03:30 -06:00
}
2022-09-02 19:38:32 +02:00
#[ sel(mouseUp:) ]
2022-09-08 16:45:29 +02:00
fn mouse_up ( & mut self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " mouseUp: " ) ;
2022-09-08 16:45:29 +02:00
self . mouse_motion ( event ) ;
self . mouse_click ( event , MouseButton ::Left , ElementState ::Released ) ;
2019-05-01 17:03:30 -06:00
}
2022-09-02 19:38:32 +02:00
#[ sel(rightMouseDown:) ]
2022-09-08 16:45:29 +02:00
fn right_mouse_down ( & mut self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " rightMouseDown: " ) ;
2022-09-08 16:45:29 +02:00
self . mouse_motion ( event ) ;
self . mouse_click ( event , MouseButton ::Right , ElementState ::Pressed ) ;
2019-05-01 17:03:30 -06:00
}
2022-09-02 19:38:32 +02:00
#[ sel(rightMouseUp:) ]
2022-09-08 16:45:29 +02:00
fn right_mouse_up ( & mut self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " rightMouseUp: " ) ;
2022-09-08 16:45:29 +02:00
self . mouse_motion ( event ) ;
self . mouse_click ( event , MouseButton ::Right , ElementState ::Released ) ;
2019-05-01 17:03:30 -06:00
}
2022-09-02 19:38:32 +02:00
#[ sel(otherMouseDown:) ]
2022-09-08 16:45:29 +02:00
fn other_mouse_down ( & mut self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " otherMouseDown: " ) ;
2022-09-08 16:45:29 +02:00
self . mouse_motion ( event ) ;
self . mouse_click ( event , MouseButton ::Middle , ElementState ::Pressed ) ;
2022-09-02 19:38:32 +02:00
}
Move `ModifiersChanged` variant to `WindowEvent` (#1381)
* Move `ModifiersChanged` variant to `WindowEvent`
* macos: Fix flags_changed for ModifiersChanged variant move
I haven't look too deep at what this does internally, but at least
cargo-check is fully happy now. :)
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Fire a ModifiersChanged event on window_did_resign_key
From debugging, I determined that macOS' emission of a flagsChanged
around window switching is inconsistent. It is fair to assume, I think,
that when the user switches windows, they do not expect their former
modifiers state to remain effective; so I think it's best to clear that
state by sending a ModifiersChanged(ModifiersState::empty()).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Fix build
I don't know enough about the code to implement the fix as it is done on
this branch, but this commit at least fixes the build.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* windows: Send ModifiersChanged(ModifiersState::empty) on KILLFOCUS
Very similar to the changes made in [1], as focus is lost, send an event
to the window indicating that the modifiers have been released.
It's unclear to me (without a Windows device to test this on) whether
this is necessary, but it certainly ensures that unfocused windows will
have at least received this event, which is an improvement.
[1]: f79f21641a31da3e4039d41be89047cdcc6028f7
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* macos: Add a hook to update stale modifiers
Sometimes, `ViewState` and `event` might have different values for their
stored `modifiers` flags. These are internally stored as a bitmask in
the latter and an enum in the former.
We can check to see if they differ, and if they do, automatically
dispatch an event to update consumers of modifier state as well as the
stored `state.modifiers`. That's what the hook does.
This hook is then called in the key_down, mouse_entered, mouse_exited,
mouse_click, scroll_wheel, and pressure_change_with_event callbacks,
which each will contain updated modifiers.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Only call event_mods once when determining whether to update state
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* flags_changed: Memoize window_id collection
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_did_resign_key: Remove synthetic ModifiersChanged event
We no longer need to emit this event, since we are checking the state of
our modifiers before emitting most other events.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Add a call to update_potentially_stale_modifiers
Now, cover all events (that I can think of, at least) where stale
modifiers might affect how user programs behave. Effectively, every
human-interface event (keypress, mouse click, keydown, etc.) will cause
a ModifiersChanged event to be fired if something has changed.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* key_up: Add a call to update_potentially_stale_modifiers
We also want to make sure modifiers state is synchronized here, too.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* mouse_motion: Remove update_potentially_stale_modifiers invocation
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Retry CI
* ViewState: Promote visibility of modifiers to the macos impl
This is so that we can interact with the ViewState directly from the
WindowDelegate.
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* window_delegate: Synthetically set modifiers state to empty on resignKey
This logic is implemented similarly on other platforms, so we wish to
regain parity here. Originally this behavior was implemented to always
fire an event with ModifiersState::empty(), but that was not the best as
it was not necessarily correct and could be a duplicate event.
This solution is perhaps the most elegant possible to implement the
desired behavior of sending a synthetic empty modifiers event when a
window loses focus, trading some safety for interoperation between the
NSWindowDelegate and the NSView (as the objc runtime must now be
consulted in order to acquire access to the ViewState which is "owned"
by the NSView).
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
* Check for modifiers change in window events
* Fix modifier changed on macOS
Since the `mouse_entered` function was generating a mouse motion, which
updates the modifier state, a modifiers changed event was incorrectly
generated.
The updating of the modifier state has also been changed to make sure it
consistently happens before events that have a modifier state attached
to it, without happening on any other event.
This of course means that no `CursorMoved` event is generated anymore
when the user enters the window without it being focused, however I'd
say that is consistent with how winit should behave.
* Fix unused variable warning
* Move changelog entry into `Unreleased` section
Co-authored-by: Freya Gentz <zegentzy@protonmail.com>
Co-authored-by: Kristofer Rye <kristofer.rye@gmail.com>
Co-authored-by: Christian Duerr <contact@christianduerr.com>
2020-03-06 15:43:55 -07:00
2022-09-02 19:38:32 +02:00
#[ sel(otherMouseUp:) ]
2022-09-08 16:45:29 +02:00
fn other_mouse_up ( & mut self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " otherMouseUp: " ) ;
2022-09-08 16:45:29 +02:00
self . mouse_motion ( event ) ;
self . mouse_click ( event , MouseButton ::Middle , ElementState ::Released ) ;
2018-05-17 21:28:30 -04:00
}
2019-12-12 22:48:32 +01:00
2022-09-02 19:38:32 +02:00
// No tracing on these because that would be overly verbose
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
#[ sel(mouseMoved:) ]
2022-09-08 16:45:29 +02:00
fn mouse_moved ( & mut self , event : & NSEvent ) {
self . mouse_motion ( event ) ;
2018-05-17 21:28:30 -04:00
}
2022-09-02 19:38:32 +02:00
#[ sel(mouseDragged:) ]
2022-09-08 16:45:29 +02:00
fn mouse_dragged ( & mut self , event : & NSEvent ) {
self . mouse_motion ( event ) ;
2018-05-17 21:28:30 -04:00
}
2018-06-11 11:16:39 -04:00
2022-09-02 19:38:32 +02:00
#[ sel(rightMouseDragged:) ]
2022-09-08 16:45:29 +02:00
fn right_mouse_dragged ( & mut self , event : & NSEvent ) {
self . mouse_motion ( event ) ;
2022-09-02 19:38:32 +02:00
}
2020-04-26 20:42:45 +00:00
2022-09-02 19:38:32 +02:00
#[ sel(otherMouseDragged:) ]
2022-09-08 16:45:29 +02:00
fn other_mouse_dragged ( & mut self , event : & NSEvent ) {
self . mouse_motion ( event ) ;
2022-09-02 19:38:32 +02:00
}
2020-07-26 22:16:21 +00:00
2022-09-02 19:38:32 +02:00
#[ sel(mouseEntered:) ]
2022-09-08 16:45:29 +02:00
fn mouse_entered ( & self , _event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " mouseEntered: " ) ;
2022-09-08 16:45:29 +02:00
let enter_event = Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::CursorEntered {
device_id : DEVICE_ID ,
} ,
} ;
2022-08-10 18:28:19 +02:00
2022-09-08 16:45:29 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( enter_event ) ) ;
2022-09-02 19:38:32 +02:00
}
2019-05-01 17:03:30 -06:00
2022-09-02 19:38:32 +02:00
#[ sel(mouseExited:) ]
2022-09-08 16:45:29 +02:00
fn mouse_exited ( & self , _event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " mouseExited: " ) ;
2022-09-08 16:45:29 +02:00
let window_event = Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::CursorLeft {
device_id : DEVICE_ID ,
} ,
} ;
2019-05-01 17:03:30 -06:00
2022-09-08 16:45:29 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
2022-09-02 19:38:32 +02:00
}
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
#[ sel(scrollWheel:) ]
2022-09-08 16:45:29 +02:00
fn scroll_wheel ( & mut self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " scrollWheel: " ) ;
2022-09-08 16:45:29 +02:00
self . mouse_motion ( event ) ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
let delta = {
let ( x , y ) = ( event . scrollingDeltaX ( ) , event . scrollingDeltaY ( ) ) ;
if event . hasPreciseScrollingDeltas ( ) {
let delta = LogicalPosition ::new ( x , y ) . to_physical ( self . scale_factor ( ) ) ;
MouseScrollDelta ::PixelDelta ( delta )
} else {
MouseScrollDelta ::LineDelta ( x as f32 , y as f32 )
}
} ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
// The "momentum phase," if any, has higher priority than touch phase (the two should
// 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
// report the touch phase.
let phase = match event . momentumPhase ( ) {
NSEventPhase ::NSEventPhaseMayBegin | NSEventPhase ::NSEventPhaseBegan = > {
TouchPhase ::Started
}
NSEventPhase ::NSEventPhaseEnded | NSEventPhase ::NSEventPhaseCancelled = > {
TouchPhase ::Ended
}
_ = > match event . phase ( ) {
NSEventPhase ::NSEventPhaseMayBegin | NSEventPhase ::NSEventPhaseBegan = > {
TouchPhase ::Started
}
NSEventPhase ::NSEventPhaseEnded | NSEventPhase ::NSEventPhaseCancelled = > {
TouchPhase ::Ended
}
_ = > TouchPhase ::Moved ,
} ,
} ;
2022-08-16 18:20:06 +03:00
2022-09-08 16:45:29 +02:00
let device_event = Event ::DeviceEvent {
device_id : DEVICE_ID ,
event : DeviceEvent ::MouseWheel { delta } ,
} ;
2022-08-16 18:20:06 +03:00
2022-09-08 16:45:29 +02:00
self . update_potentially_stale_modifiers ( event ) ;
2022-08-16 18:20:06 +03:00
2022-09-08 16:45:29 +02:00
let window_event = Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::MouseWheel {
device_id : DEVICE_ID ,
delta ,
phase ,
modifiers : event_mods ( event ) ,
} ,
} ;
2022-08-16 18:20:06 +03:00
2022-09-08 16:45:29 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( device_event ) ) ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
2022-09-02 19:38:32 +02:00
}
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
#[ sel(magnifyWithEvent:) ]
2022-09-08 16:45:29 +02:00
fn magnify_with_event ( & self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " magnifyWithEvent: " ) ;
2022-09-08 16:45:29 +02:00
let delta = event . magnification ( ) ;
let phase = match event . phase ( ) {
NSEventPhase ::NSEventPhaseBegan = > TouchPhase ::Started ,
NSEventPhase ::NSEventPhaseChanged = > TouchPhase ::Moved ,
NSEventPhase ::NSEventPhaseCancelled = > TouchPhase ::Cancelled ,
NSEventPhase ::NSEventPhaseEnded = > TouchPhase ::Ended ,
_ = > return ,
} ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
let window_event = Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::TouchpadMagnify {
device_id : DEVICE_ID ,
delta ,
phase ,
} ,
} ;
2022-08-16 18:20:06 +03:00
2022-09-08 16:45:29 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
2022-09-02 19:38:32 +02:00
}
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
#[ sel(rotateWithEvent:) ]
2022-09-08 16:45:29 +02:00
fn rotate_with_event ( & self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " rotateWithEvent: " ) ;
2022-09-08 16:45:29 +02:00
let delta = event . rotation ( ) ;
let phase = match event . phase ( ) {
NSEventPhase ::NSEventPhaseBegan = > TouchPhase ::Started ,
NSEventPhase ::NSEventPhaseChanged = > TouchPhase ::Moved ,
NSEventPhase ::NSEventPhaseCancelled = > TouchPhase ::Cancelled ,
NSEventPhase ::NSEventPhaseEnded = > TouchPhase ::Ended ,
_ = > return ,
} ;
2022-09-02 19:38:32 +02:00
2022-09-08 16:45:29 +02:00
let window_event = Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::TouchpadRotate {
device_id : DEVICE_ID ,
delta ,
phase ,
} ,
} ;
2022-08-16 18:20:06 +03:00
2022-09-08 16:45:29 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
2022-09-02 19:38:32 +02:00
}
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
#[ sel(pressureChangeWithEvent:) ]
2022-09-08 16:45:29 +02:00
fn pressure_change_with_event ( & mut self , event : & NSEvent ) {
2022-09-02 19:38:32 +02:00
trace_scope! ( " pressureChangeWithEvent: " ) ;
2020-04-26 20:42:45 +00:00
2022-09-08 16:45:29 +02:00
self . mouse_motion ( event ) ;
2019-05-01 17:03:30 -06:00
2022-09-08 16:45:29 +02:00
let pressure = event . pressure ( ) ;
let stage = event . stage ( ) ;
2019-05-01 17:03:30 -06:00
2022-09-08 16:45:29 +02:00
let window_event = Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::TouchpadPressure {
device_id : DEVICE_ID ,
pressure ,
stage : stage as i64 ,
} ,
} ;
2019-05-01 17:03:30 -06:00
2022-09-08 16:45:29 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
2022-09-02 19:38:32 +02:00
}
2019-05-01 17:03:30 -06:00
2022-09-02 19:38:32 +02:00
// Allows us to receive Ctrl-Tab and Ctrl-Esc.
// 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
#[ sel(_wantsKeyDownForEvent:) ]
2022-09-08 16:45:29 +02:00
fn wants_key_down_for_event ( & self , _event : & NSEvent ) -> bool {
2022-09-02 19:38:32 +02:00
trace_scope! ( " _wantsKeyDownForEvent: " ) ;
true
}
2021-04-30 17:30:09 +08:00
2022-09-02 19:38:32 +02:00
#[ sel(acceptsFirstMouse:) ]
2022-09-08 16:45:29 +02:00
fn accepts_first_mouse ( & self , _event : & NSEvent ) -> bool {
2022-09-02 19:38:32 +02:00
trace_scope! ( " acceptsFirstMouse: " ) ;
true
}
2022-09-02 18:46:18 +02:00
}
2022-09-02 19:38:32 +02:00
) ;
2022-09-02 18:46:18 +02:00
impl WinitView {
2022-09-08 16:45:29 +02:00
pub ( super ) fn new ( window : & WinitWindow ) -> Id < Self , Shared > {
unsafe { msg_send_id! [ msg_send_id! [ Self ::class ( ) , alloc ] , initWithId : window ] }
}
fn window ( & self ) -> Id < WinitWindow , Shared > {
// TODO: Simply use `window` property on `NSView`.
// That only returns a window _after_ the view has been attached though!
// (which is incompatible with `frameDidChange:`)
//
// unsafe { msg_send_id![self, window] }
( * self . _ns_window ) . clone ( )
}
fn window_id ( & self ) -> WindowId {
WindowId ( self . _ns_window . id ( ) )
}
fn scale_factor ( & self ) -> f64 {
self . window ( ) . backingScaleFactor ( ) as f64
}
fn is_ime_enabled ( & self ) -> bool {
! matches! ( self . state . ime_state , ImeState ::Disabled )
}
2022-09-02 18:46:18 +02:00
fn current_input_source ( & self ) -> String {
2022-09-08 16:45:29 +02:00
self . inputContext ( )
. expect ( " input context " )
. selectedKeyboardInputSource ( )
. map ( | input_source | input_source . to_string ( ) )
. unwrap_or_else ( String ::new )
}
pub ( super ) fn set_ime_allowed ( & mut self , ime_allowed : bool ) {
if self . state . ime_allowed = = ime_allowed {
return ;
}
self . state . ime_allowed = ime_allowed ;
if self . state . ime_allowed {
return ;
}
// Clear markedText
* self . marked_text = NSMutableAttributedString ::new ( ) ;
if self . state . ime_state ! = ImeState ::Disabled {
self . state . ime_state = ImeState ::Disabled ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::Ime ( Ime ::Disabled ) ,
} ) ) ;
}
}
pub ( super ) fn set_ime_position ( & mut self , position : LogicalPosition < f64 > ) {
self . state . ime_position = position ;
let input_context = self . inputContext ( ) . expect ( " input context " ) ;
input_context . invalidateCharacterCoordinates ( ) ;
}
// Update `state.modifiers` if `event` has something different
fn update_potentially_stale_modifiers ( & mut self , event : & NSEvent ) {
let event_modifiers = event_mods ( event ) ;
if self . state . modifiers ! = event_modifiers {
self . state . modifiers = event_modifiers ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::ModifiersChanged ( self . state . modifiers ) ,
} ) ) ;
}
}
fn mouse_click ( & mut self , event : & NSEvent , button : MouseButton , button_state : ElementState ) {
self . update_potentially_stale_modifiers ( event ) ;
let window_event = Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::MouseInput {
device_id : DEVICE_ID ,
state : button_state ,
button ,
modifiers : event_mods ( event ) ,
} ,
} ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
}
fn mouse_motion ( & mut self , event : & NSEvent ) {
let window_point = event . locationInWindow ( ) ;
let view_point = self . convertPoint_fromView ( window_point , None ) ;
let view_rect = self . frame ( ) ;
if view_point . x . is_sign_negative ( )
| | view_point . y . is_sign_negative ( )
| | view_point . x > view_rect . size . width
| | view_point . y > view_rect . size . height
{
let mouse_buttons_down = NSEvent ::pressedMouseButtons ( ) ;
if mouse_buttons_down = = 0 {
// Point is outside of the client area (view) and no buttons are pressed
return ;
}
}
let x = view_point . x as f64 ;
let y = view_rect . size . height as f64 - view_point . y as f64 ;
let logical_position = LogicalPosition ::new ( x , y ) ;
self . update_potentially_stale_modifiers ( event ) ;
let window_event = Event ::WindowEvent {
window_id : self . window_id ( ) ,
event : WindowEvent ::CursorMoved {
device_id : DEVICE_ID ,
position : logical_position . to_physical ( self . scale_factor ( ) ) ,
modifiers : event_mods ( event ) ,
} ,
} ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
2022-09-02 18:46:18 +02:00
}
2021-04-30 17:30:09 +08:00
}