From 5a0b4dba4799849851f31214107e88081c06969f Mon Sep 17 00:00:00 2001 From: Francesca Plebani Date: Thu, 27 Dec 2018 15:16:58 -0500 Subject: [PATCH] macOS: Implement `Refresh` (#742) * macOS: Implement Refresh * drawRect should take NSRect --- CHANGELOG.md | 1 + src/platform/macos/util.rs | 6 ++++++ src/platform/macos/view.rs | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8e39de5..9e261d43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - On X11, `WindowBuilder::with_min_dimensions` and `WindowBuilder::with_max_dimensions` now correctly account for DPI. - Added support for generating dummy `DeviceId`s and `WindowId`s to better support unit testing. - On macOS, fixed unsoundness in drag-and-drop that could result in drops being rejected. +- On macOS, implemented `WindowEvent::Refresh`. # Version 0.18.0 (2018-11-07) diff --git a/src/platform/macos/util.rs b/src/platform/macos/util.rs index 2d8e4f34..b080660e 100644 --- a/src/platform/macos/util.rs +++ b/src/platform/macos/util.rs @@ -2,6 +2,7 @@ use cocoa::appkit::NSWindowStyleMask; use cocoa::base::{id, nil}; use cocoa::foundation::{NSRect, NSUInteger}; use core_graphics::display::CGDisplay; +use objc::runtime::{Class, Object}; use platform::platform::ffi; use platform::platform::window::IdRef; @@ -39,6 +40,11 @@ pub unsafe fn toggle_style_mask(window: id, view: id, mask: NSWindowStyleMask, o window.makeFirstResponder_(view); } +pub unsafe fn superclass<'a>(this: &'a Object) -> &'a Class { + let superclass: id = msg_send![this, superclass]; + &*(superclass as *const _) +} + pub unsafe fn create_input_context(view: id) -> IdRef { let input_context: id = msg_send![class!(NSTextInputContext), alloc]; let input_context: id = msg_send![input_context, initWithClient:view]; diff --git a/src/platform/macos/view.rs b/src/platform/macos/view.rs index 64296aeb..fc410486 100644 --- a/src/platform/macos/view.rs +++ b/src/platform/macos/view.rs @@ -71,6 +71,10 @@ lazy_static! { sel!(initWithWinit:), init_with_winit as extern fn(&Object, Sel, *mut c_void) -> id, ); + decl.add_method( + sel!(drawRect:), + draw_rect as extern fn(&Object, Sel, NSRect), + ); decl.add_method(sel!(hasMarkedText), has_marked_text as extern fn(&Object, Sel) -> BOOL); decl.add_method( sel!(markedRange), @@ -154,6 +158,27 @@ extern fn init_with_winit(this: &Object, _sel: Sel, state: *mut c_void) -> id { } } +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]; + } +} + extern fn has_marked_text(this: &Object, _sel: Sel) -> BOOL { //println!("hasMarkedText"); unsafe {