2018-05-18 11:28:30 +10:00
|
|
|
// This is a pretty close port of the implementation in GLFW:
|
|
|
|
// https://github.com/glfw/glfw/blob/7ef34eb06de54dd9186d3d21a401b2ef819b59e7/src/cocoa_window.m
|
|
|
|
|
|
|
|
use std::{slice, str};
|
|
|
|
use std::boxed::Box;
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
use std::os::raw::*;
|
2018-12-29 07:29:29 +11:00
|
|
|
use std::sync::{Arc, Mutex, Weak};
|
2018-05-18 11:28:30 +10:00
|
|
|
|
2018-08-03 06:26:30 +10:00
|
|
|
use cocoa::base::{id, nil};
|
2018-06-12 01:16:39 +10:00
|
|
|
use cocoa::appkit::{NSEvent, NSView, NSWindow};
|
2018-05-18 11:28:30 +10:00
|
|
|
use cocoa::foundation::{NSPoint, NSRect, NSSize, NSString, NSUInteger};
|
|
|
|
use objc::declare::ClassDecl;
|
2018-08-16 09:42:57 +10:00
|
|
|
use objc::runtime::{Class, Object, Protocol, Sel, BOOL, YES};
|
2018-05-18 11:28:30 +10:00
|
|
|
|
2018-06-12 01:16:39 +10:00
|
|
|
use {ElementState, Event, KeyboardInput, MouseButton, WindowEvent, WindowId};
|
2019-02-06 02:30:33 +11:00
|
|
|
use platform_impl::platform::event_loop::{DEVICE_ID, event_mods, Shared, to_virtual_key_code, check_additional_virtual_key_codes};
|
|
|
|
use platform_impl::platform::util;
|
|
|
|
use platform_impl::platform::ffi::*;
|
|
|
|
use platform_impl::platform::window::{get_window_id, IdRef};
|
2018-05-18 11:28:30 +10:00
|
|
|
|
|
|
|
struct ViewState {
|
|
|
|
window: id,
|
|
|
|
shared: Weak<Shared>,
|
2018-12-29 07:29:29 +11:00
|
|
|
cursor: Arc<Mutex<util::Cursor>>,
|
2018-06-15 09:42:18 +10:00
|
|
|
ime_spot: Option<(f64, f64)>,
|
2018-05-18 11:28:30 +10:00
|
|
|
raw_characters: Option<String>,
|
2018-10-18 13:03:26 +11:00
|
|
|
is_key_down: bool,
|
2018-05-18 11:28:30 +10:00
|
|
|
}
|
|
|
|
|
2018-12-29 07:29:29 +11:00
|
|
|
pub fn new_view(window: id, shared: Weak<Shared>) -> (IdRef, Weak<Mutex<util::Cursor>>) {
|
|
|
|
let cursor = Default::default();
|
|
|
|
let cursor_access = Arc::downgrade(&cursor);
|
2018-05-22 23:05:33 +10:00
|
|
|
let state = ViewState {
|
|
|
|
window,
|
|
|
|
shared,
|
2018-12-29 07:29:29 +11:00
|
|
|
cursor,
|
2018-05-22 23:05:33 +10:00
|
|
|
ime_spot: None,
|
|
|
|
raw_characters: None,
|
2018-10-18 13:03:26 +11:00
|
|
|
is_key_down: false,
|
2018-05-22 23:05:33 +10:00
|
|
|
};
|
2018-05-18 11:28:30 +10:00
|
|
|
unsafe {
|
|
|
|
// This is free'd in `dealloc`
|
|
|
|
let state_ptr = Box::into_raw(Box::new(state)) as *mut c_void;
|
|
|
|
let view: id = msg_send![VIEW_CLASS.0, alloc];
|
2018-12-29 07:29:29 +11:00
|
|
|
(IdRef::new(msg_send![view, initWithWinit:state_ptr]), cursor_access)
|
2018-05-18 11:28:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
pub fn set_ime_spot(view: id, input_context: id, x: f64, y: f64) {
|
2018-05-18 11:28:30 +10:00
|
|
|
unsafe {
|
|
|
|
let state_ptr: *mut c_void = *(*view).get_mut_ivar("winitState");
|
|
|
|
let state = &mut *(state_ptr as *mut ViewState);
|
|
|
|
let content_rect = NSWindow::contentRectForFrameRect_(
|
|
|
|
state.window,
|
|
|
|
NSWindow::frame(state.window),
|
|
|
|
);
|
2018-06-15 09:42:18 +10:00
|
|
|
let base_x = content_rect.origin.x as f64;
|
|
|
|
let base_y = (content_rect.origin.y + content_rect.size.height) as f64;
|
2018-05-18 11:28:30 +10:00
|
|
|
state.ime_spot = Some((base_x + x, base_y - y));
|
|
|
|
let _: () = msg_send![input_context, invalidateCharacterCoordinates];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ViewClass(*const Class);
|
|
|
|
unsafe impl Send for ViewClass {}
|
|
|
|
unsafe impl Sync for ViewClass {}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref VIEW_CLASS: ViewClass = unsafe {
|
2018-07-20 02:02:33 +10:00
|
|
|
let superclass = class!(NSView);
|
2018-05-18 11:28:30 +10:00
|
|
|
let mut decl = ClassDecl::new("WinitView", superclass).unwrap();
|
|
|
|
decl.add_method(sel!(dealloc), dealloc as extern fn(&Object, Sel));
|
|
|
|
decl.add_method(
|
|
|
|
sel!(initWithWinit:),
|
|
|
|
init_with_winit as extern fn(&Object, Sel, *mut c_void) -> id,
|
|
|
|
);
|
2018-12-28 07:16:58 +11:00
|
|
|
decl.add_method(
|
|
|
|
sel!(drawRect:),
|
|
|
|
draw_rect as extern fn(&Object, Sel, NSRect),
|
|
|
|
);
|
2018-12-29 07:29:29 +11:00
|
|
|
decl.add_method(
|
|
|
|
sel!(resetCursorRects),
|
|
|
|
reset_cursor_rects as extern fn(&Object, Sel),
|
|
|
|
);
|
2018-05-18 11:28:30 +10:00
|
|
|
decl.add_method(sel!(hasMarkedText), has_marked_text as extern fn(&Object, Sel) -> BOOL);
|
|
|
|
decl.add_method(
|
|
|
|
sel!(markedRange),
|
|
|
|
marked_range as extern fn(&Object, Sel) -> NSRange,
|
|
|
|
);
|
|
|
|
decl.add_method(sel!(selectedRange), selected_range as extern fn(&Object, Sel) -> NSRange);
|
|
|
|
decl.add_method(
|
|
|
|
sel!(setMarkedText:selectedRange:replacementRange:),
|
|
|
|
set_marked_text as extern fn(&mut Object, Sel, id, NSRange, NSRange),
|
|
|
|
);
|
|
|
|
decl.add_method(sel!(unmarkText), unmark_text as extern fn(&Object, Sel));
|
|
|
|
decl.add_method(
|
|
|
|
sel!(validAttributesForMarkedText),
|
|
|
|
valid_attributes_for_marked_text as extern fn(&Object, Sel) -> id,
|
|
|
|
);
|
|
|
|
decl.add_method(
|
|
|
|
sel!(attributedSubstringForProposedRange:actualRange:),
|
|
|
|
attributed_substring_for_proposed_range
|
|
|
|
as extern fn(&Object, Sel, NSRange, *mut c_void) -> id,
|
|
|
|
);
|
|
|
|
decl.add_method(
|
|
|
|
sel!(insertText:replacementRange:),
|
|
|
|
insert_text as extern fn(&Object, Sel, id, NSRange),
|
|
|
|
);
|
|
|
|
decl.add_method(
|
|
|
|
sel!(characterIndexForPoint:),
|
|
|
|
character_index_for_point as extern fn(&Object, Sel, NSPoint) -> NSUInteger,
|
|
|
|
);
|
|
|
|
decl.add_method(
|
|
|
|
sel!(firstRectForCharacterRange:actualRange:),
|
|
|
|
first_rect_for_character_range
|
|
|
|
as extern fn(&Object, Sel, NSRange, *mut c_void) -> NSRect,
|
|
|
|
);
|
|
|
|
decl.add_method(
|
|
|
|
sel!(doCommandBySelector:),
|
|
|
|
do_command_by_selector as extern fn(&Object, Sel, Sel),
|
|
|
|
);
|
|
|
|
decl.add_method(sel!(keyDown:), key_down as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(keyUp:), key_up as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(insertTab:), insert_tab as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(insertBackTab:), insert_back_tab as extern fn(&Object, Sel, id));
|
2018-06-12 01:16:39 +10:00
|
|
|
decl.add_method(sel!(mouseDown:), mouse_down as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(mouseUp:), mouse_up as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(rightMouseDown:), right_mouse_down as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(rightMouseUp:), right_mouse_up as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(otherMouseDown:), other_mouse_down as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(otherMouseUp:), other_mouse_up as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(mouseMoved:), mouse_moved as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(mouseDragged:), mouse_dragged as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(rightMouseDragged:), right_mouse_dragged as extern fn(&Object, Sel, id));
|
|
|
|
decl.add_method(sel!(otherMouseDragged:), other_mouse_dragged as extern fn(&Object, Sel, id));
|
2018-08-16 09:42:57 +10:00
|
|
|
decl.add_method(sel!(_wantsKeyDownForEvent:), wants_key_down_for_event as extern fn(&Object, Sel, id) -> BOOL);
|
2018-05-18 11:28:30 +10:00
|
|
|
decl.add_ivar::<*mut c_void>("winitState");
|
|
|
|
decl.add_ivar::<id>("markedText");
|
|
|
|
let protocol = Protocol::get("NSTextInputClient").unwrap();
|
|
|
|
decl.add_protocol(&protocol);
|
|
|
|
ViewClass(decl.register())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn dealloc(this: &Object, _sel: Sel) {
|
|
|
|
unsafe {
|
|
|
|
let state: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let marked_text: id = *this.get_ivar("markedText");
|
|
|
|
let _: () = msg_send![marked_text, release];
|
|
|
|
Box::from_raw(state as *mut ViewState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn init_with_winit(this: &Object, _sel: Sel, state: *mut c_void) -> id {
|
|
|
|
unsafe {
|
|
|
|
let this: id = msg_send![this, init];
|
|
|
|
if this != nil {
|
|
|
|
(*this).set_ivar("winitState", state);
|
|
|
|
let marked_text = <id as NSMutableAttributedString>::init(
|
|
|
|
NSMutableAttributedString::alloc(nil),
|
|
|
|
);
|
|
|
|
(*this).set_ivar("markedText", marked_text);
|
|
|
|
}
|
|
|
|
this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 07:16:58 +11:00
|
|
|
extern fn draw_rect(this: &Object, _sel: Sel, rect: NSRect) {
|
|
|
|
unsafe {
|
|
|
|
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state_ptr as *mut ViewState);
|
|
|
|
|
|
|
|
if let Some(shared) = state.shared.upgrade() {
|
|
|
|
let window_event = Event::WindowEvent {
|
|
|
|
window_id: WindowId(get_window_id(state.window)),
|
|
|
|
event: WindowEvent::Refresh,
|
|
|
|
};
|
|
|
|
shared.pending_events
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.push_back(window_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
let superclass = util::superclass(this);
|
|
|
|
let () = msg_send![super(this, superclass), drawRect:rect];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 07:29:29 +11:00
|
|
|
extern fn reset_cursor_rects(this: &Object, _sel: Sel) {
|
|
|
|
unsafe {
|
|
|
|
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state_ptr as *mut ViewState);
|
|
|
|
|
|
|
|
let bounds: NSRect = msg_send![this, bounds];
|
|
|
|
let cursor = state.cursor.lock().unwrap().load();
|
|
|
|
let _: () = msg_send![this,
|
|
|
|
addCursorRect:bounds
|
|
|
|
cursor:cursor
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
extern fn has_marked_text(this: &Object, _sel: Sel) -> BOOL {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("hasMarkedText");
|
2018-05-18 11:28:30 +10:00
|
|
|
unsafe {
|
|
|
|
let marked_text: id = *this.get_ivar("markedText");
|
|
|
|
(marked_text.length() > 0) as i8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn marked_range(this: &Object, _sel: Sel) -> NSRange {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("markedRange");
|
2018-05-18 11:28:30 +10:00
|
|
|
unsafe {
|
|
|
|
let marked_text: id = *this.get_ivar("markedText");
|
|
|
|
let length = marked_text.length();
|
|
|
|
if length > 0 {
|
|
|
|
NSRange::new(0, length - 1)
|
|
|
|
} else {
|
|
|
|
util::EMPTY_RANGE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn selected_range(_this: &Object, _sel: Sel) -> NSRange {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("selectedRange");
|
2018-05-18 11:28:30 +10:00
|
|
|
util::EMPTY_RANGE
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn set_marked_text(
|
|
|
|
this: &mut Object,
|
|
|
|
_sel: Sel,
|
|
|
|
string: id,
|
|
|
|
_selected_range: NSRange,
|
|
|
|
_replacement_range: NSRange,
|
|
|
|
) {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("setMarkedText");
|
2018-05-18 11:28:30 +10:00
|
|
|
unsafe {
|
|
|
|
let marked_text_ref: &mut id = this.get_mut_ivar("markedText");
|
|
|
|
let _: () = msg_send![(*marked_text_ref), release];
|
|
|
|
let marked_text = NSMutableAttributedString::alloc(nil);
|
2018-07-20 02:02:33 +10:00
|
|
|
let has_attr = msg_send![string, isKindOfClass:class!(NSAttributedString)];
|
2018-05-18 11:28:30 +10:00
|
|
|
if has_attr {
|
|
|
|
marked_text.initWithAttributedString(string);
|
|
|
|
} else {
|
|
|
|
marked_text.initWithString(string);
|
|
|
|
};
|
|
|
|
*marked_text_ref = marked_text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn unmark_text(this: &Object, _sel: Sel) {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("unmarkText");
|
2018-05-18 11:28:30 +10:00
|
|
|
unsafe {
|
|
|
|
let marked_text: id = *this.get_ivar("markedText");
|
|
|
|
let mutable_string = marked_text.mutableString();
|
|
|
|
let _: () = msg_send![mutable_string, setString:""];
|
|
|
|
let input_context: id = msg_send![this, inputContext];
|
|
|
|
let _: () = msg_send![input_context, discardMarkedText];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn valid_attributes_for_marked_text(_this: &Object, _sel: Sel) -> id {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("validAttributesForMarkedText");
|
2018-07-20 02:02:33 +10:00
|
|
|
unsafe { msg_send![class!(NSArray), array] }
|
2018-05-18 11:28:30 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
extern fn attributed_substring_for_proposed_range(
|
|
|
|
_this: &Object,
|
|
|
|
_sel: Sel,
|
|
|
|
_range: NSRange,
|
|
|
|
_actual_range: *mut c_void, // *mut NSRange
|
|
|
|
) -> id {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("attributedSubstringForProposedRange");
|
2018-05-18 11:28:30 +10:00
|
|
|
nil
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn character_index_for_point(_this: &Object, _sel: Sel, _point: NSPoint) -> NSUInteger {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("characterIndexForPoint");
|
2018-05-18 11:28:30 +10:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn first_rect_for_character_range(
|
|
|
|
this: &Object,
|
|
|
|
_sel: Sel,
|
|
|
|
_range: NSRange,
|
|
|
|
_actual_range: *mut c_void, // *mut NSRange
|
|
|
|
) -> NSRect {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("firstRectForCharacterRange");
|
2018-05-18 11:28:30 +10:00
|
|
|
unsafe {
|
|
|
|
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state_ptr as *mut ViewState);
|
|
|
|
let (x, y) = state.ime_spot.unwrap_or_else(|| {
|
|
|
|
let content_rect = NSWindow::contentRectForFrameRect_(
|
|
|
|
state.window,
|
|
|
|
NSWindow::frame(state.window),
|
|
|
|
);
|
|
|
|
let x = content_rect.origin.x;
|
|
|
|
let y = util::bottom_left_to_top_left(content_rect);
|
2018-06-15 09:42:18 +10:00
|
|
|
(x, y)
|
2018-05-18 11:28:30 +10:00
|
|
|
});
|
2018-05-22 23:05:33 +10:00
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
NSRect::new(
|
|
|
|
NSPoint::new(x as _, y as _),
|
|
|
|
NSSize::new(0.0, 0.0),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn insert_text(this: &Object, _sel: Sel, string: id, _replacement_range: NSRange) {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("insertText");
|
2018-05-18 11:28:30 +10:00
|
|
|
unsafe {
|
|
|
|
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state_ptr as *mut ViewState);
|
|
|
|
|
2018-07-20 02:02:33 +10:00
|
|
|
let has_attr = msg_send![string, isKindOfClass:class!(NSAttributedString)];
|
2018-05-18 11:28:30 +10:00
|
|
|
let characters = if has_attr {
|
|
|
|
// This is a *mut NSAttributedString
|
|
|
|
msg_send![string, string]
|
|
|
|
} else {
|
|
|
|
// This is already a *mut NSString
|
|
|
|
string
|
|
|
|
};
|
|
|
|
|
|
|
|
let slice = slice::from_raw_parts(
|
|
|
|
characters.UTF8String() as *const c_uchar,
|
|
|
|
characters.len(),
|
|
|
|
);
|
|
|
|
let string = str::from_utf8_unchecked(slice);
|
2018-10-18 13:03:26 +11:00
|
|
|
state.is_key_down = true;
|
2018-05-18 11:28:30 +10:00
|
|
|
|
|
|
|
// We don't need this now, but it's here if that changes.
|
2018-07-20 02:02:33 +10:00
|
|
|
//let event: id = msg_send![class!(NSApp), currentEvent];
|
2018-05-18 11:28:30 +10:00
|
|
|
|
|
|
|
let mut events = VecDeque::with_capacity(characters.len());
|
|
|
|
for character in string.chars() {
|
|
|
|
events.push_back(Event::WindowEvent {
|
|
|
|
window_id: WindowId(get_window_id(state.window)),
|
|
|
|
event: WindowEvent::ReceivedCharacter(character),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(shared) = state.shared.upgrade() {
|
|
|
|
shared.pending_events
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.append(&mut events);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn do_command_by_selector(this: &Object, _sel: Sel, command: Sel) {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("doCommandBySelector");
|
2018-05-18 11:28:30 +10: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.
|
|
|
|
unsafe {
|
|
|
|
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state_ptr as *mut ViewState);
|
|
|
|
|
|
|
|
let shared = if let Some(shared) = state.shared.upgrade() {
|
|
|
|
shared
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut events = VecDeque::with_capacity(1);
|
|
|
|
if command == sel!(insertNewline:) {
|
|
|
|
// The `else` condition would emit the same character, but I'm keeping this here both...
|
|
|
|
// 1) as a reminder for how `doCommandBySelector` works
|
2018-05-22 23:05:33 +10:00
|
|
|
// 2) to make our use of carriage return explicit
|
2018-05-18 11:28:30 +10:00
|
|
|
events.push_back(Event::WindowEvent {
|
|
|
|
window_id: WindowId(get_window_id(state.window)),
|
2018-05-22 23:05:33 +10:00
|
|
|
event: WindowEvent::ReceivedCharacter('\r'),
|
2018-05-18 11:28:30 +10:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
let raw_characters = state.raw_characters.take();
|
|
|
|
if let Some(raw_characters) = raw_characters {
|
|
|
|
for character in raw_characters.chars() {
|
|
|
|
events.push_back(Event::WindowEvent {
|
|
|
|
window_id: WindowId(get_window_id(state.window)),
|
|
|
|
event: WindowEvent::ReceivedCharacter(character),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
shared.pending_events
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.append(&mut events);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 03:04:16 +10:00
|
|
|
fn get_characters(event: id) -> Option<String> {
|
|
|
|
unsafe {
|
|
|
|
let characters: id = msg_send![event, characters];
|
|
|
|
let slice = slice::from_raw_parts(
|
|
|
|
characters.UTF8String() as *const c_uchar,
|
|
|
|
characters.len(),
|
|
|
|
);
|
|
|
|
let string = str::from_utf8_unchecked(slice);
|
|
|
|
Some(string.to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
extern fn key_down(this: &Object, _sel: Sel, event: id) {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("keyDown");
|
2018-05-18 11:28:30 +10:00
|
|
|
unsafe {
|
|
|
|
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state_ptr as *mut ViewState);
|
2018-05-22 23:05:33 +10:00
|
|
|
let window_id = WindowId(get_window_id(state.window));
|
2018-05-18 11:28:30 +10:00
|
|
|
|
2018-09-13 03:04:16 +10:00
|
|
|
state.raw_characters = get_characters(event);
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
let keycode: c_ushort = msg_send![event, keyCode];
|
2018-09-13 03:04:16 +10:00
|
|
|
// We are checking here for F21-F24 keys, since their keycode
|
|
|
|
// can vary, but we know that they are encoded
|
|
|
|
// in characters property.
|
|
|
|
let virtual_keycode = to_virtual_key_code(keycode)
|
|
|
|
.or_else(|| {
|
|
|
|
check_additional_virtual_key_codes(&state.raw_characters)
|
|
|
|
});
|
2018-05-18 11:28:30 +10:00
|
|
|
let scancode = keycode as u32;
|
2018-05-22 23:05:33 +10:00
|
|
|
let is_repeat = msg_send![event, isARepeat];
|
2018-05-18 11:28:30 +10:00
|
|
|
|
|
|
|
let window_event = Event::WindowEvent {
|
2018-05-22 23:05:33 +10:00
|
|
|
window_id,
|
2018-05-18 11:28:30 +10:00
|
|
|
event: WindowEvent::KeyboardInput {
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
input: KeyboardInput {
|
|
|
|
state: ElementState::Pressed,
|
|
|
|
scancode,
|
|
|
|
virtual_keycode,
|
|
|
|
modifiers: event_mods(event),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-10-18 13:03:26 +11:00
|
|
|
let characters: id = msg_send![event, characters];
|
|
|
|
let slice = slice::from_raw_parts(
|
|
|
|
characters.UTF8String() as *const c_uchar,
|
|
|
|
characters.len(),
|
|
|
|
);
|
|
|
|
let string = str::from_utf8_unchecked(slice);
|
|
|
|
|
|
|
|
state.raw_characters = {
|
|
|
|
Some(string.to_owned())
|
|
|
|
};
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
if let Some(shared) = state.shared.upgrade() {
|
|
|
|
shared.pending_events
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.push_back(window_event);
|
2018-05-22 23:05:33 +10:00
|
|
|
// Emit `ReceivedCharacter` for key repeats
|
2018-10-18 13:03:26 +11:00
|
|
|
if is_repeat && state.is_key_down{
|
|
|
|
for character in string.chars() {
|
2018-05-22 23:05:33 +10:00
|
|
|
let window_event = Event::WindowEvent {
|
|
|
|
window_id,
|
|
|
|
event: WindowEvent::ReceivedCharacter(character),
|
|
|
|
};
|
|
|
|
shared.pending_events
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.push_back(window_event);
|
|
|
|
}
|
2018-06-18 05:08:26 +10:00
|
|
|
} else {
|
|
|
|
// Some keys (and only *some*, with no known reason) don't trigger `insertText`, while others do...
|
|
|
|
// So, we don't give repeats the opportunity to trigger that, since otherwise our hack will cause some
|
|
|
|
// keys to generate twice as many characters.
|
2018-07-20 02:02:33 +10:00
|
|
|
let array: id = msg_send![class!(NSArray), arrayWithObject:event];
|
2018-06-18 05:08:26 +10:00
|
|
|
let (): _ = msg_send![this, interpretKeyEvents:array];
|
2018-05-22 23:05:33 +10:00
|
|
|
}
|
2018-05-18 11:28:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn key_up(this: &Object, _sel: Sel, event: id) {
|
2018-05-22 23:05:33 +10:00
|
|
|
//println!("keyUp");
|
2018-05-18 11:28:30 +10:00
|
|
|
unsafe {
|
|
|
|
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state_ptr as *mut ViewState);
|
|
|
|
|
2018-10-18 13:03:26 +11:00
|
|
|
state.is_key_down = false;
|
2018-05-22 23:05:33 +10:00
|
|
|
|
2018-09-13 03:04:16 +10:00
|
|
|
// We need characters here to check for additional keys such as
|
|
|
|
// F21-F24.
|
|
|
|
let characters = get_characters(event);
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
let keycode: c_ushort = msg_send![event, keyCode];
|
2018-09-13 03:04:16 +10:00
|
|
|
let virtual_keycode = to_virtual_key_code(keycode)
|
|
|
|
.or_else(|| {
|
|
|
|
check_additional_virtual_key_codes(&characters)
|
|
|
|
});
|
2018-05-18 11:28:30 +10:00
|
|
|
let scancode = keycode as u32;
|
|
|
|
let window_event = Event::WindowEvent {
|
|
|
|
window_id: WindowId(get_window_id(state.window)),
|
|
|
|
event: WindowEvent::KeyboardInput {
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
input: KeyboardInput {
|
|
|
|
state: ElementState::Released,
|
|
|
|
scancode,
|
|
|
|
virtual_keycode,
|
|
|
|
modifiers: event_mods(event),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(shared) = state.shared.upgrade() {
|
|
|
|
shared.pending_events
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.push_back(window_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn insert_tab(this: &Object, _sel: Sel, _sender: id) {
|
|
|
|
unsafe {
|
|
|
|
let window: id = msg_send![this, window];
|
|
|
|
let first_responder: id = msg_send![window, firstResponder];
|
|
|
|
let this_ptr = this as *const _ as *mut _;
|
|
|
|
if first_responder == this_ptr {
|
|
|
|
let (): _ = msg_send![window, selectNextKeyView:this];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn insert_back_tab(this: &Object, _sel: Sel, _sender: id) {
|
|
|
|
unsafe {
|
|
|
|
let window: id = msg_send![this, window];
|
|
|
|
let first_responder: id = msg_send![window, firstResponder];
|
|
|
|
let this_ptr = this as *const _ as *mut _;
|
|
|
|
if first_responder == this_ptr {
|
|
|
|
let (): _ = msg_send![window, selectPreviousKeyView:this];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 01:16:39 +10:00
|
|
|
|
|
|
|
fn mouse_click(this: &Object, event: id, button: MouseButton, button_state: ElementState) {
|
|
|
|
unsafe {
|
|
|
|
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
|
|
|
let state = &mut *(state_ptr as *mut ViewState);
|
|
|
|
|
|
|
|
let window_event = Event::WindowEvent {
|
|
|
|
window_id: WindowId(get_window_id(state.window)),
|
|
|
|
event: WindowEvent::MouseInput {
|
|
|
|
device_id: DEVICE_ID,
|
|
|
|
state: button_state,
|
|
|
|
button,
|
|
|
|
modifiers: event_mods(event),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(shared) = state.shared.upgrade() {
|
|
|
|
shared.pending_events
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.push_back(window_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn mouse_down(this: &Object, _sel: Sel, event: id) {
|
|
|
|
mouse_click(this, event, MouseButton::Left, ElementState::Pressed);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn mouse_up(this: &Object, _sel: Sel, event: id) {
|
|
|
|
mouse_click(this, event, MouseButton::Left, ElementState::Released);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn right_mouse_down(this: &Object, _sel: Sel, event: id) {
|
|
|
|
mouse_click(this, event, MouseButton::Right, ElementState::Pressed);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn right_mouse_up(this: &Object, _sel: Sel, event: id) {
|
|
|
|
mouse_click(this, event, MouseButton::Right, ElementState::Released);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn other_mouse_down(this: &Object, _sel: Sel, event: id) {
|
|
|
|
mouse_click(this, event, MouseButton::Middle, ElementState::Pressed);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn other_mouse_up(this: &Object, _sel: Sel, event: id) {
|
|
|
|
mouse_click(this, event, MouseButton::Middle, ElementState::Released);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mouse_motion(this: &Object, event: id) {
|
|
|
|
unsafe {
|
|
|
|
let state_ptr: *mut c_void = *this.get_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 {
|
|
|
|
// Point is outside of the client area (view)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:42:18 +10:00
|
|
|
let x = view_point.x as f64;
|
|
|
|
let y = view_rect.size.height as f64 - view_point.y as f64;
|
2018-06-12 01:16:39 +10:00
|
|
|
|
|
|
|
let window_event = Event::WindowEvent {
|
|
|
|
window_id: WindowId(get_window_id(state.window)),
|
|
|
|
event: WindowEvent::CursorMoved {
|
|
|
|
device_id: DEVICE_ID,
|
2018-06-15 09:42:18 +10:00
|
|
|
position: (x, y).into(),
|
2018-06-12 01:16:39 +10:00
|
|
|
modifiers: event_mods(event),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(shared) = state.shared.upgrade() {
|
|
|
|
shared.pending_events
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.push_back(window_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn mouse_moved(this: &Object, _sel: Sel, event: id) {
|
|
|
|
mouse_motion(this, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn mouse_dragged(this: &Object, _sel: Sel, event: id) {
|
|
|
|
mouse_motion(this, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn right_mouse_dragged(this: &Object, _sel: Sel, event: id) {
|
|
|
|
mouse_motion(this, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn other_mouse_dragged(this: &Object, _sel: Sel, event: id) {
|
|
|
|
mouse_motion(this, event);
|
|
|
|
}
|
2018-08-16 09:42:57 +10:00
|
|
|
|
|
|
|
// https://github.com/chromium/chromium/blob/a86a8a6bcfa438fa3ac2eba6f02b3ad1f8e0756f/ui/views/cocoa/bridged_content_view.mm#L816
|
|
|
|
extern fn wants_key_down_for_event(_this: &Object, _se: Sel, _event: id) -> BOOL {
|
|
|
|
YES
|
|
|
|
}
|