From 43faf97418ad41862d6cb60809769d3e200fcb4f Mon Sep 17 00:00:00 2001 From: Birh Burh Date: Mon, 12 Aug 2024 22:01:42 +0200 Subject: [PATCH] native/macos: Fixes setNeedsDisplay call on arm macs --- src/native/macos.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/native/macos.rs b/src/native/macos.rs index fa513ffe..e93984b3 100644 --- a/src/native/macos.rs +++ b/src/native/macos.rs @@ -612,7 +612,6 @@ unsafe fn view_base_decl(decl: &mut ClassDecl) { payload.modifiers = new_modifiers; } - decl.add_method( sel!(canBecomeKey), yes as extern "C" fn(&Object, Sel) -> BOOL, @@ -712,6 +711,18 @@ pub fn define_opengl_view_class() -> *const Class { } } + // apparently, its impossible to use performSelectorOnMainThread + // with primitive type argument, so the only way to pass + // YES to setNeedsDisplay - send a no argument message + // https://stackoverflow.com/questions/6120614/passing-primitives-through-performselectoronmainthread + // It seems that the same thing applies to [NSTimer timerWithTimeInterval:...] + extern "C" fn set_needs_display_hack(this: &Object, _: Sel) { + unsafe { + msg_send_![this, setNeedsDisplay: YES]; + } + } + + let superclass = class!(NSView); let mut decl: ClassDecl = ClassDecl::new("RenderViewClass", superclass).unwrap(); unsafe { @@ -720,6 +731,10 @@ pub fn define_opengl_view_class() -> *const Class { sel!(drawRect:), draw_rect as extern "C" fn(&Object, Sel, ObjcId), ); + decl.add_method( + sel!(setNeedsDisplayHack), + set_needs_display_hack as extern "C" fn(&Object, Sel), + ); view_base_decl(&mut decl); } @@ -1118,13 +1133,13 @@ where let timer = match conf.platform.apple_gfx_api { AppleGfxApi::OpenGl => msg_send_![class!(NSTimer), timerWithTimeInterval:0.016 // ~60FPS target:view - selector:sel!(setNeedsDisplay:) - userInfo:YES + selector:sel!(setNeedsDisplayHack) + userInfo:nil repeats:YES], AppleGfxApi::Metal => msg_send_![class!(NSTimer), timerWithTimeInterval:0.016 // ~60FPS target:view selector:sel!(draw) - userInfo:YES + userInfo:nil repeats:YES], }; msg_send_![current_runloop, addTimer:timer forMode:NSEventTrackingRunLoopMode];