2019-05-01 17:03:30 -06:00
use std ::{
2019-06-21 11:33:15 -04:00
boxed ::Box ,
collections ::VecDeque ,
os ::raw ::* ,
2022-01-10 21:39:17 +01:00
ptr , slice , str ,
2022-05-07 05:29:25 +03:00
sync ::{
atomic ::{ compiler_fence , Ordering } ,
2022-09-02 21:02:40 +02:00
Mutex ,
2022-05-07 05:29:25 +03:00
} ,
2019-05-01 17:03:30 -06:00
} ;
use cocoa ::{
appkit ::{ NSApp , NSEvent , NSEventModifierFlags , NSEventPhase , NSView , NSWindow } ,
2019-06-21 11:33:15 -04:00
base ::{ id , nil } ,
2022-09-02 15:48:02 +02:00
foundation ::{ NSPoint , NSRect , NSSize , NSString } ,
2019-06-21 11:33:15 -04:00
} ;
2022-09-02 18:46:18 +02:00
use objc2 ::foundation ::{ NSInteger , NSObject , NSRange , NSUInteger } ;
2022-09-02 21:02:40 +02:00
use objc2 ::rc ::{ Id , Shared } ;
2022-09-02 18:46:18 +02:00
use objc2 ::runtime ::{ Bool , Object , Sel } ;
use objc2 ::{ declare_class , ClassType } ;
2019-05-01 17:03:30 -06:00
2022-09-02 21:02:40 +02:00
use super ::appkit ::{ NSCursor , NSResponder , NSView as NSViewClass } ;
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 ::{
char_to_keycode , check_function_keys , event_mods , get_scancode , modifier_event ,
2020-01-04 01:32:34 -05:00
scancode_to_keycode , EventWrapper ,
2019-06-21 11:33:15 -04:00
} ,
ffi ::* ,
2022-05-07 05:29:25 +03:00
util ::{ self , id_to_string_lossy , IdRef } ,
2019-06-21 11:33:15 -04:00
window ::get_window_id ,
DEVICE_ID ,
2019-05-01 17:03:30 -06:00
} ,
window ::WindowId ,
} ;
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
}
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 {
2019-06-11 01:09:38 +02:00
ns_window : id ,
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 ,
2019-12-31 05:32:37 +09:00
tracking_rect : Option < NSInteger > ,
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
}
2020-01-12 18:50:34 +01:00
impl ViewState {
fn get_scale_factor ( & self ) -> f64 {
( unsafe { NSWindow ::backingScaleFactor ( self . ns_window ) } ) as f64
}
2022-06-10 13:43:33 +03:00
2022-05-07 05:29:25 +03:00
fn is_ime_enabled ( & self ) -> bool {
2022-06-10 13:43:33 +03:00
! matches! ( self . ime_state , ImeState ::Disabled )
2022-05-07 05:29:25 +03:00
}
2020-01-12 18:50:34 +01:00
}
2022-09-02 21:02:40 +02:00
pub fn new_view ( ns_window : id ) -> IdRef {
2018-05-22 09:05:33 -04:00
let state = ViewState {
2019-06-11 01:09:38 +02:00
ns_window ,
2022-09-02 21:02:40 +02:00
cursor_state : Default ::default ( ) ,
2022-03-18 14:50:24 +01:00
ime_position : LogicalPosition ::new ( 0.0 , 0.0 ) ,
2019-05-01 17:03:30 -06:00
modifiers : Default ::default ( ) ,
2019-12-31 05:32:37 +09:00
tracking_rect : None ,
2022-05-07 05:29:25 +03:00
ime_state : ImeState ::Disabled ,
input_source : String ::new ( ) ,
ime_allowed : false ,
forward_key_to_app : false ,
2018-05-22 09:05:33 -04:00
} ;
2018-05-17 21:28:30 -04:00
unsafe {
// This is free'd in `dealloc`
let state_ptr = Box ::into_raw ( Box ::new ( state ) ) as * mut c_void ;
2022-09-02 18:46:18 +02:00
let ns_view : id = msg_send! [ WinitView ::class ( ) , alloc ] ;
2022-09-02 21:02:40 +02:00
IdRef ::new ( msg_send! [ ns_view , initWithWinit : state_ptr ] )
2018-05-17 21:28:30 -04:00
}
}
2022-03-18 14:50:24 +01:00
pub unsafe fn set_ime_position ( ns_view : id , position : LogicalPosition < f64 > ) {
2022-09-02 15:48:02 +02:00
let state_ptr : * mut c_void = * ( * ns_view ) . ivar_mut ( " winitState " ) ;
2019-05-01 17:03:30 -06:00
let state = & mut * ( state_ptr as * mut ViewState ) ;
2022-03-18 14:50:24 +01:00
state . ime_position = position ;
let input_context : id = msg_send! [ ns_view , inputContext ] ;
2019-05-01 17:03:30 -06:00
let _ : ( ) = msg_send! [ input_context , invalidateCharacterCoordinates ] ;
2018-05-17 21:28:30 -04:00
}
2022-05-07 05:29:25 +03:00
pub unsafe fn set_ime_allowed ( ns_view : id , ime_allowed : bool ) {
2022-09-02 15:48:02 +02:00
let state_ptr : * mut c_void = * ( * ns_view ) . ivar_mut ( " winitState " ) ;
2022-05-07 05:29:25 +03:00
let state = & mut * ( state_ptr as * mut ViewState ) ;
if state . ime_allowed = = ime_allowed {
return ;
}
state . ime_allowed = ime_allowed ;
if state . ime_allowed {
return ;
}
2022-09-02 15:48:02 +02:00
let marked_text_ref : & mut id = ( * ns_view ) . ivar_mut ( " markedText " ) ;
2022-05-07 05:29:25 +03:00
// Clear markedText
let _ : ( ) = msg_send! [ * marked_text_ref , release ] ;
let marked_text =
< id as NSMutableAttributedString > ::init ( NSMutableAttributedString ::alloc ( nil ) ) ;
* marked_text_ref = marked_text ;
if state . ime_state ! = ImeState ::Disabled {
state . ime_state = ImeState ::Disabled ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::Ime ( Ime ::Disabled ) ,
} ) ) ;
}
}
2022-09-02 18:46:18 +02:00
fn get_characters ( event : id , ignore_modifiers : bool ) -> String {
2018-05-17 21:28:30 -04:00
unsafe {
2022-09-02 18:46:18 +02:00
let characters : id = if ignore_modifiers {
msg_send! [ event , charactersIgnoringModifiers ]
} else {
msg_send! [ event , characters ]
} ;
assert_ne! ( characters , nil ) ;
let slice =
slice ::from_raw_parts ( characters . UTF8String ( ) as * const c_uchar , characters . len ( ) ) ;
let string = str ::from_utf8_unchecked ( slice ) ;
string . to_owned ( )
}
}
// 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.
fn retrieve_keycode ( event : id ) -> Option < VirtualKeyCode > {
#[ inline ]
fn get_code ( ev : id , raw : bool ) -> Option < VirtualKeyCode > {
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 ( | | {
let scancode = get_scancode ( event ) ;
scancode_to_keycode ( scancode ) . or_else ( | | check_function_keys ( & get_characters ( event , true ) ) )
} )
}
// Update `state.modifiers` if `event` has something different
fn update_potentially_stale_modifiers ( state : & mut ViewState , event : id ) {
let event_modifiers = event_mods ( event ) ;
if state . modifiers ! = event_modifiers {
state . modifiers = event_modifiers ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::ModifiersChanged ( state . modifiers ) ,
} ) ) ;
}
}
fn mouse_click ( this : & Object , event : id , button : MouseButton , button_state : ElementState ) {
unsafe {
let state_ptr : * mut c_void = * this . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
update_potentially_stale_modifiers ( state , event ) ;
let window_event = Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::MouseInput {
device_id : DEVICE_ID ,
state : button_state ,
button ,
modifiers : event_mods ( event ) ,
} ,
} ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
2018-05-17 21:28:30 -04:00
}
}
2022-09-02 18:46:18 +02:00
fn mouse_motion ( this : & Object , event : id ) {
2018-05-17 21:28:30 -04:00
unsafe {
2022-09-02 18:46:18 +02:00
let state_ptr : * mut c_void = * this . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
// We have to do this to have access to the `NSView` trait...
let view : id = this as * const _ as * mut _ ;
let window_point = event . locationInWindow ( ) ;
let view_point = view . convertPoint_fromView_ ( window_point , nil ) ;
let view_rect = NSView ::frame ( view ) ;
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 : NSUInteger = msg_send! [ class! ( NSEvent ) , pressedMouseButtons ] ;
if mouse_buttons_down = = 0 {
// Point is outside of the client area (view) and no buttons are pressed
return ;
}
2018-05-17 21:28:30 -04:00
}
2022-09-02 18:46:18 +02:00
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 ) ;
update_potentially_stale_modifiers ( state , event ) ;
let window_event = Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::CursorMoved {
device_id : DEVICE_ID ,
position : logical_position . to_physical ( state . get_scale_factor ( ) ) ,
modifiers : event_mods ( event ) ,
} ,
} ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
}
}
2022-09-02 19:38:32 +02:00
declare_class! (
2022-09-02 18:46:18 +02:00
#[ derive(Debug) ]
#[ allow(non_snake_case) ]
struct WinitView {
winitState : * mut c_void ,
markedText : id ,
}
unsafe impl ClassType for WinitView {
#[ inherits(NSResponder, NSObject) ]
type Super = NSViewClass ;
}
unsafe impl WinitView {
2022-09-02 19:38:32 +02:00
#[ sel(dealloc) ]
fn dealloc ( & mut self ) {
unsafe {
let marked_text : id = * self . ivar ( " markedText " ) ;
let _ : ( ) = msg_send! [ marked_text , release ] ;
let state : * mut c_void = * self . ivar ( " winitState " ) ;
drop ( Box ::from_raw ( state as * mut ViewState ) ) ;
}
}
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
#[ sel(initWithWinit:) ]
fn init_with_winit ( & mut self , state : * mut c_void ) -> Option < & mut Self > {
let this : Option < & mut Self > = unsafe { msg_send! [ self , init ] } ;
this . map ( | this | unsafe {
( * this ) . set_ivar ( " winitState " , state ) ;
let marked_text =
< id as NSMutableAttributedString > ::init ( NSMutableAttributedString ::alloc ( nil ) ) ;
( * this ) . set_ivar ( " markedText " , marked_text ) ;
let _ : ( ) = msg_send! [ & mut * this , setPostsFrameChangedNotifications : true ] ;
let notification_center : & Object =
msg_send! [ class! ( NSNotificationCenter ) , defaultCenter ] ;
// About frame change
let frame_did_change_notification_name =
IdRef ::new ( NSString ::alloc ( nil ) . init_str ( " NSViewFrameDidChangeNotification " ) ) ;
let _ : ( ) = msg_send! [
notification_center ,
addObserver : & * this
selector : sel ! ( frameDidChange :)
name : * frame_did_change_notification_name
object : & * this
] ;
let winit_state = & mut * ( state as * mut ViewState ) ;
winit_state . input_source = this . current_input_source ( ) ;
this
} )
}
2022-09-02 18:46:18 +02:00
}
unsafe impl WinitView {
2022-09-02 19:38:32 +02:00
#[ sel(viewDidMoveToWindow) ]
fn view_did_move_to_window ( & self ) {
trace_scope! ( " viewDidMoveToWindow " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
if let Some ( tracking_rect ) = state . tracking_rect . take ( ) {
let _ : ( ) = msg_send! [ self , removeTrackingRect : tracking_rect ] ;
}
2019-12-31 05:32:37 +09:00
2022-09-02 19:38:32 +02:00
let rect : NSRect = msg_send! [ self , visibleRect ] ;
let tracking_rect : NSInteger = msg_send! [
self ,
addTrackingRect : rect ,
owner : self ,
userData : ptr ::null_mut ::< c_void > ( ) ,
assumeInside : false ,
] ;
state . tracking_rect = Some ( tracking_rect ) ;
}
2019-12-31 05:32:37 +09:00
}
2022-09-02 19:38:32 +02:00
#[ sel(frameDidChange:) ]
fn frame_did_change ( & self , _event : id ) {
trace_scope! ( " frameDidChange: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
2019-05-01 17:03:30 -06:00
2022-09-02 19:38:32 +02:00
if let Some ( tracking_rect ) = state . tracking_rect . take ( ) {
let _ : ( ) = msg_send! [ self , removeTrackingRect : tracking_rect ] ;
}
2019-12-31 05:32:37 +09:00
2022-09-02 19:38:32 +02:00
let rect : NSRect = msg_send! [ self , visibleRect ] ;
let tracking_rect : NSInteger = msg_send! [
self ,
addTrackingRect : rect ,
owner : self ,
userData : ptr ::null_mut ::< c_void > ( ) ,
assumeInside : false ,
] ;
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 > ( state . get_scale_factor ( ) ) ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::Resized ( size ) ,
} ) ) ;
}
2019-12-31 05:32:37 +09:00
}
2022-09-02 19:38:32 +02:00
#[ sel(drawRect:) ]
fn draw_rect ( & self , rect : NSRect ) {
trace_scope! ( " drawRect: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
2019-12-31 05:32:37 +09:00
2022-09-02 19:38:32 +02:00
AppState ::handle_redraw ( WindowId ( get_window_id ( state . ns_window ) ) ) ;
2018-12-27 15:16:58 -05:00
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 state = unsafe {
2022-09-02 19:38:32 +02:00
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
2022-09-02 21:02:40 +02:00
& mut * ( state_ptr as * mut ViewState )
} ;
let bounds = self . bounds ( ) ;
let cursor_state = state . cursor_state . lock ( ) . unwrap ( ) ;
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 " ) ;
unsafe {
let marked_text : id = * self . ivar ( " markedText " ) ;
marked_text . length ( ) > 0
}
}
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 " ) ;
unsafe {
let marked_text : id = * self . ivar ( " markedText " ) ;
let length = marked_text . length ( ) ;
if length > 0 {
NSRange ::new ( 0 , length )
} else {
util ::EMPTY_RANGE
}
}
}
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 ,
string : id ,
_selected_range : NSRange ,
_replacement_range : NSRange ,
) {
trace_scope! ( " setMarkedText:selectedRange:replacementRange: " ) ;
unsafe {
// Get pre-edit text
let marked_text_ref : & mut id = self . ivar_mut ( " markedText " ) ;
// Update markedText
let _ : ( ) = msg_send! [ * marked_text_ref , release ] ;
let marked_text = NSMutableAttributedString ::alloc ( nil ) ;
let has_attr = msg_send! [ string , isKindOfClass : class ! ( NSAttributedString ) ] ;
if has_attr {
marked_text . initWithAttributedString ( string ) ;
} else {
marked_text . initWithString ( string ) ;
} ;
* marked_text_ref = marked_text ;
// Update ViewState with new marked text
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
let preedit_string = id_to_string_lossy ( string ) ;
// Notify IME is active if application still doesn't know it.
if state . ime_state = = ImeState ::Disabled {
state . input_source = self . current_input_source ( ) ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::Ime ( Ime ::Enabled ) ,
} ) ) ;
}
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
// Don't update state to preedit when we've just commited a string, since the following
// preedit string will be None anyway.
if state . ime_state ! = ImeState ::Commited {
state . ime_state = ImeState ::Preedit ;
}
2022-05-07 05:29:25 +03:00
2022-09-02 19:38:32 +02:00
// 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 ( ) ) )
} ;
2022-05-07 05:29:25 +03:00
2022-09-02 19:38:32 +02:00
// Send WindowEvent for updating marked text
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
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) ]
fn unmark_text ( & self ) {
trace_scope! ( " unmarkText " ) ;
unsafe {
let marked_text : id = * self . ivar ( " markedText " ) ;
let mutable_string = marked_text . mutableString ( ) ;
let s : id = msg_send! [ class! ( NSString ) , new ] ;
let _ : ( ) = msg_send! [ mutable_string , setString : s ] ;
let _ : ( ) = msg_send! [ s , release ] ;
let input_context : & Object = msg_send! [ self , inputContext ] ;
let _ : ( ) = msg_send! [ input_context , discardMarkedText ] ;
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::Ime ( Ime ::Preedit ( String ::new ( ) , None ) ) ,
} ) ) ;
if state . is_ime_enabled ( ) {
// Leave the Preedit state
state . ime_state = ImeState ::Enabled ;
} else {
warn! ( " Expected to have IME enabled when receiving unmarkText " ) ;
}
}
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) ]
fn valid_attributes_for_marked_text ( & self ) -> id {
trace_scope! ( " validAttributesForMarkedText " ) ;
unsafe { msg_send! [ class! ( NSArray ) , array ] }
}
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
) -> id {
trace_scope! ( " attributedSubstringForProposedRange:actualRange: " ) ;
nil
}
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: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
let content_rect = NSWindow ::contentRectForFrameRect_ (
state . ns_window ,
NSWindow ::frame ( state . ns_window ) ,
) ;
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 + state . ime_position . x ;
let y = base_y - 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 ) )
}
}
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
#[ sel(insertText:replacementRange:) ]
fn insert_text ( & self , string : id , _replacement_range : NSRange ) {
trace_scope! ( " insertText:replacementRange: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
let string = id_to_string_lossy ( string ) ;
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +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-02 19:38:32 +02:00
// We don't need this now, but it's here if that changes.
//let event: id = msg_send![NSApp(), currentEvent];
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
if state . is_ime_enabled ( ) & & ! is_control {
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::Ime ( Ime ::Commit ( string ) ) ,
} ) ) ;
state . ime_state = ImeState ::Commited ;
}
}
2018-05-17 21:28:30 -04:00
}
2022-09-02 19:38:32 +02:00
#[ sel(doCommandBySelector:) ]
fn do_command_by_selector ( & self , _command : Sel ) {
trace_scope! ( " doCommandBySelector: " ) ;
// 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.
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
// 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 state . ime_state = = ImeState ::Commited {
return ;
}
2022-07-21 22:23:22 +03:00
2022-09-02 19:38:32 +02:00
state . forward_key_to_app = true ;
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
let has_marked_text = msg_send! [ self , hasMarkedText ] ;
if has_marked_text & & state . ime_state = = ImeState ::Preedit {
// Leave preedit so that we also report the keyup for this key
state . ime_state = ImeState ::Enabled ;
}
}
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:) ]
fn key_down ( & self , event : id ) {
trace_scope! ( " keyDown: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
let window_id = WindowId ( get_window_id ( state . ns_window ) ) ;
let input_source = self . current_input_source ( ) ;
if state . input_source ! = input_source & & state . is_ime_enabled ( ) {
state . ime_state = ImeState ::Disabled ;
state . input_source = input_source ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::Ime ( Ime ::Disabled ) ,
} ) ) ;
}
let was_in_preedit = state . ime_state = = ImeState ::Preedit ;
let characters = get_characters ( event , false ) ;
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 state . ime_allowed {
let events_for_nsview : id = msg_send! [ class! ( NSArray ) , arrayWithObject : event ] ;
let _ : ( ) = msg_send! [ self , interpretKeyEvents : events_for_nsview ] ;
// Using a compiler fence because `interpretKeyEvents` might call
// into functions that modify the `ViewState`, but the compiler
// doesn't know this. Without the fence, the compiler may think that
// some of the reads (eg `state.ime_state`) that happen after this
// point are not needed.
compiler_fence ( Ordering ::SeqCst ) ;
// If the text was commited we must treat the next keyboard event as IME related.
if state . ime_state = = ImeState ::Commited {
state . ime_state = ImeState ::Enabled ;
text_commited = true ;
}
}
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
let now_in_preedit = state . ime_state = = ImeState ::Preedit ;
let scancode = get_scancode ( event ) as u32 ;
let virtual_keycode = retrieve_keycode ( event ) ;
update_potentially_stale_modifiers ( state , event ) ;
let ime_related = was_in_preedit | | now_in_preedit | | text_commited ;
if ! ime_related | | state . forward_key_to_app | | ! 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 ) ,
} ,
is_synthetic : false ,
} ,
} ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
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-05-07 05:29:25 +03:00
}
2022-09-02 19:38:32 +02:00
#[ sel(keyUp:) ]
fn key_up ( & self , event : id ) {
trace_scope! ( " keyUp: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
let scancode = get_scancode ( event ) as u32 ;
let virtual_keycode = retrieve_keycode ( event ) ;
update_potentially_stale_modifiers ( state , event ) ;
// We want to send keyboard input when we are not currently in preedit
if state . ime_state ! = ImeState ::Preedit {
#[ allow(deprecated) ]
let window_event = Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::KeyboardInput {
device_id : DEVICE_ID ,
input : KeyboardInput {
state : ElementState ::Released ,
scancode ,
virtual_keycode ,
modifiers : event_mods ( event ) ,
} ,
is_synthetic : false ,
} ,
} ;
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:) ]
fn flags_changed ( & self , event : id ) {
trace_scope! ( " flagsChanged: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
let mut events = VecDeque ::with_capacity ( 4 ) ;
if let Some ( window_event ) = modifier_event (
event ,
NSEventModifierFlags ::NSShiftKeyMask ,
state . modifiers . shift ( ) ,
) {
state . modifiers . toggle ( ModifiersState ::SHIFT ) ;
events . push_back ( window_event ) ;
}
2018-09-12 20:04:16 +03:00
2022-09-02 19:38:32 +02:00
if let Some ( window_event ) = modifier_event (
event ,
NSEventModifierFlags ::NSControlKeyMask ,
state . modifiers . ctrl ( ) ,
) {
state . modifiers . toggle ( ModifiersState ::CTRL ) ;
events . push_back ( window_event ) ;
}
2019-05-01 17:03:30 -06:00
2022-09-02 19:38:32 +02:00
if let Some ( window_event ) = modifier_event (
event ,
NSEventModifierFlags ::NSCommandKeyMask ,
state . modifiers . logo ( ) ,
) {
state . modifiers . toggle ( ModifiersState ::LOGO ) ;
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-02 19:38:32 +02:00
if let Some ( window_event ) = modifier_event (
event ,
NSEventModifierFlags ::NSAlternateKeyMask ,
state . modifiers . alt ( ) ,
) {
state . modifiers . toggle ( ModifiersState ::ALT ) ;
events . push_back ( window_event ) ;
}
2022-07-21 22:23:22 +03:00
2022-09-02 19:38:32 +02:00
let window_id = WindowId ( get_window_id ( state . ns_window ) ) ;
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +02:00
for event in events {
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id ,
event ,
} ) ) ;
}
2019-05-01 17:03:30 -06:00
2022-05-07 05:29:25 +03:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( Event ::WindowEvent {
window_id ,
2022-09-02 19:38:32 +02:00
event : WindowEvent ::ModifiersChanged ( state . modifiers ) ,
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(insertTab:) ]
fn insert_tab ( & self , _sender : id ) {
trace_scope! ( " insertTab: " ) ;
unsafe {
let window : id = msg_send! [ self , window ] ;
let first_responder : id = msg_send! [ window , firstResponder ] ;
let self_ptr = self as * const _ as * mut _ ;
if first_responder = = self_ptr {
let _ : ( ) = msg_send! [ window , selectNextKeyView : self ] ;
}
}
}
2018-09-12 20:04:16 +03:00
2022-09-02 19:38:32 +02:00
#[ sel(insertBackTab:) ]
fn insert_back_tab ( & self , _sender : id ) {
trace_scope! ( " insertBackTab: " ) ;
unsafe {
let window : id = msg_send! [ self , window ] ;
let first_responder : id = msg_send! [ window , firstResponder ] ;
let self_ptr = self as * const _ as * mut _ ;
if first_responder = = self_ptr {
let _ : ( ) = msg_send! [ window , selectPreviousKeyView : self ] ;
}
}
}
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:) ]
fn cancel_operation ( & self , _sender : id ) {
trace_scope! ( " cancelOperation: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
let scancode = 0x2f ;
let virtual_keycode = scancode_to_keycode ( scancode ) ;
debug_assert_eq! ( virtual_keycode , Some ( VirtualKeyCode ::Period ) ) ;
let event : id = msg_send! [ NSApp ( ) , currentEvent ] ;
update_potentially_stale_modifiers ( state , event ) ;
#[ allow(deprecated) ]
let window_event = Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::KeyboardInput {
device_id : DEVICE_ID ,
input : KeyboardInput {
state : ElementState ::Pressed ,
scancode : scancode as _ ,
virtual_keycode ,
modifiers : event_mods ( event ) ,
} ,
is_synthetic : false ,
2022-05-07 05:29:25 +03:00
} ,
2022-09-02 19:38:32 +02:00
} ;
2018-05-17 21:28:30 -04:00
2022-09-02 19:38:32 +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:) ]
fn mouse_down ( & self , event : id ) {
trace_scope! ( " mouseDown: " ) ;
mouse_motion ( self , event ) ;
mouse_click ( self , event , MouseButton ::Left , ElementState ::Pressed ) ;
2019-05-01 17:03:30 -06:00
}
2022-09-02 19:38:32 +02:00
#[ sel(mouseUp:) ]
fn mouse_up ( & self , event : id ) {
trace_scope! ( " mouseUp: " ) ;
mouse_motion ( self , event ) ;
mouse_click ( self , event , MouseButton ::Left , ElementState ::Released ) ;
2019-05-01 17:03:30 -06:00
}
2022-09-02 19:38:32 +02:00
#[ sel(rightMouseDown:) ]
fn right_mouse_down ( & self , event : id ) {
trace_scope! ( " rightMouseDown: " ) ;
mouse_motion ( self , event ) ;
mouse_click ( self , event , MouseButton ::Right , ElementState ::Pressed ) ;
2019-05-01 17:03:30 -06:00
}
2022-09-02 19:38:32 +02:00
#[ sel(rightMouseUp:) ]
fn right_mouse_up ( & self , event : id ) {
trace_scope! ( " rightMouseUp: " ) ;
mouse_motion ( self , event ) ;
mouse_click ( self , event , MouseButton ::Right , ElementState ::Released ) ;
2019-05-01 17:03:30 -06:00
}
2022-09-02 19:38:32 +02:00
#[ sel(otherMouseDown:) ]
fn other_mouse_down ( & self , event : id ) {
trace_scope! ( " otherMouseDown: " ) ;
mouse_motion ( self , event ) ;
mouse_click ( self , event , MouseButton ::Middle , ElementState ::Pressed ) ;
}
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:) ]
fn other_mouse_up ( & self , event : id ) {
trace_scope! ( " otherMouseUp: " ) ;
mouse_motion ( self , event ) ;
mouse_click ( self , 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:) ]
fn mouse_moved ( & self , event : id ) {
mouse_motion ( self , event ) ;
2018-05-17 21:28:30 -04:00
}
2022-09-02 19:38:32 +02:00
#[ sel(mouseDragged:) ]
fn mouse_dragged ( & self , event : id ) {
mouse_motion ( self , 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:) ]
fn right_mouse_dragged ( & self , event : id ) {
mouse_motion ( self , event ) ;
}
2020-04-26 20:42:45 +00:00
2022-09-02 19:38:32 +02:00
#[ sel(otherMouseDragged:) ]
fn other_mouse_dragged ( & self , event : id ) {
mouse_motion ( self , event ) ;
}
2020-07-26 22:16:21 +00:00
2022-09-02 19:38:32 +02:00
#[ sel(mouseEntered:) ]
fn mouse_entered ( & self , _event : id ) {
trace_scope! ( " mouseEntered: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
let enter_event = Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::CursorEntered {
device_id : DEVICE_ID ,
} ,
} ;
2022-08-10 18:28:19 +02:00
2022-09-02 19:38:32 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( enter_event ) ) ;
2019-06-24 12:14:55 -04:00
}
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:) ]
fn mouse_exited ( & self , _event : id ) {
trace_scope! ( " mouseExited: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
let window_event = Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::CursorLeft {
device_id : DEVICE_ID ,
} ,
} ;
2019-05-01 17:03:30 -06:00
2022-09-02 19:38:32 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
}
}
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
#[ sel(scrollWheel:) ]
fn scroll_wheel ( & self , event : id ) {
trace_scope! ( " scrollWheel: " ) ;
mouse_motion ( self , event ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
let delta = {
let ( x , y ) = ( event . scrollingDeltaX ( ) , event . scrollingDeltaY ( ) ) ;
if Bool ::from_raw ( event . hasPreciseScrollingDeltas ( ) ) . as_bool ( ) {
let delta =
LogicalPosition ::new ( x , y ) . to_physical ( state . get_scale_factor ( ) ) ;
MouseScrollDelta ::PixelDelta ( delta )
} else {
MouseScrollDelta ::LineDelta ( x as f32 , y as f32 )
}
} ;
// 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 ,
} ,
} ;
let device_event = Event ::DeviceEvent {
device_id : DEVICE_ID ,
event : DeviceEvent ::MouseWheel { delta } ,
} ;
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
update_potentially_stale_modifiers ( state , event ) ;
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
let window_event = Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::MouseWheel {
device_id : DEVICE_ID ,
delta ,
phase ,
modifiers : event_mods ( event ) ,
} ,
} ;
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( device_event ) ) ;
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
}
}
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
#[ sel(magnifyWithEvent:) ]
fn magnify_with_event ( & self , event : id ) {
trace_scope! ( " magnifyWithEvent: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
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 ,
} ;
let window_event = Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::TouchpadMagnify {
device_id : DEVICE_ID ,
delta ,
phase ,
} ,
} ;
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
}
}
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
#[ sel(rotateWithEvent:) ]
fn rotate_with_event ( & self , event : id ) {
trace_scope! ( " rotateWithEvent: " ) ;
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
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 ,
} ;
let window_event = Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::TouchpadRotate {
device_id : DEVICE_ID ,
delta ,
phase ,
} ,
} ;
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
}
}
2022-08-16 18:20:06 +03:00
2022-09-02 19:38:32 +02:00
#[ sel(pressureChangeWithEvent:) ]
fn pressure_change_with_event ( & self , event : id ) {
trace_scope! ( " pressureChangeWithEvent: " ) ;
2020-04-26 20:42:45 +00:00
2022-09-02 19:38:32 +02:00
mouse_motion ( self , event ) ;
2020-04-26 20:42:45 +00:00
2022-09-02 19:38:32 +02:00
unsafe {
let state_ptr : * mut c_void = * self . ivar ( " winitState " ) ;
let state = & mut * ( state_ptr as * mut ViewState ) ;
2019-05-01 17:03:30 -06:00
2022-09-02 19:38:32 +02:00
let pressure = event . pressure ( ) ;
let stage = event . stage ( ) ;
2019-05-01 17:03:30 -06:00
2022-09-02 19:38:32 +02:00
let window_event = Event ::WindowEvent {
window_id : WindowId ( get_window_id ( state . ns_window ) ) ,
event : WindowEvent ::TouchpadPressure {
device_id : DEVICE_ID ,
pressure ,
stage : stage as i64 ,
} ,
} ;
2019-05-01 17:03:30 -06:00
2022-09-02 19:38:32 +02:00
AppState ::queue_event ( EventWrapper ::StaticEvent ( window_event ) ) ;
}
}
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:) ]
fn wants_key_down_for_event ( & self , _event : id ) -> bool {
trace_scope! ( " _wantsKeyDownForEvent: " ) ;
true
}
2021-04-30 17:30:09 +08:00
2022-09-02 19:38:32 +02:00
#[ sel(acceptsFirstMouse:) ]
fn accepts_first_mouse ( & self , _event : id ) -> bool {
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 {
fn current_input_source ( & self ) -> String {
let input_context : id = unsafe { msg_send! [ self , inputContext ] } ;
let input_source : id = unsafe { msg_send! [ input_context , selectedKeyboardInputSource ] } ;
unsafe { id_to_string_lossy ( input_source ) }
}
2021-04-30 17:30:09 +08:00
}