Use `viewDidChangeEffectiveAppearance` to detect appearance changes

Antonio Scandurra created

Change summary

crates/gpui/src/platform/mac/platform.rs    |  2 --
crates/gpui/src/platform/mac/status_item.rs | 20 +++++---------------
crates/gpui/src/platform/mac/window.rs      | 15 ++++-----------
3 files changed, 9 insertions(+), 28 deletions(-)

Detailed changes

crates/gpui/src/platform/mac/platform.rs 🔗

@@ -54,8 +54,6 @@ const NSUTF8StringEncoding: NSUInteger = 4;
 
 #[allow(non_upper_case_globals)]
 pub const NSViewLayerContentsRedrawDuringViewResize: NSInteger = 2;
-#[allow(non_upper_case_globals)]
-pub const NSKeyValueObservingOptionNew: NSInteger = 1;
 
 const MAC_PLATFORM_IVAR: &str = "platform";
 static mut APP_CLASS: *const Class = ptr::null();

crates/gpui/src/platform/mac/status_item.rs 🔗

@@ -5,17 +5,14 @@ use crate::{
     },
     platform::{
         self,
-        mac::{
-            platform::{NSKeyValueObservingOptionNew, NSViewLayerContentsRedrawDuringViewResize},
-            renderer::Renderer,
-        },
+        mac::{platform::NSViewLayerContentsRedrawDuringViewResize, renderer::Renderer},
     },
     Event, FontSystem, Scene,
 };
 use cocoa::{
     appkit::{NSScreen, NSSquareStatusItemLength, NSStatusBar, NSStatusItem, NSView, NSWindow},
     base::{id, nil, YES},
-    foundation::{NSPoint, NSRect, NSSize, NSString},
+    foundation::{NSPoint, NSRect, NSSize},
 };
 use ctor::ctor;
 use foreign_types::ForeignTypeRef;
@@ -91,8 +88,8 @@ unsafe fn build_classes() {
             make_backing_layer as extern "C" fn(&Object, Sel) -> id,
         );
         decl.add_method(
-            sel!(observeValueForKeyPath:ofObject:change:context:),
-            appearance_changed as extern "C" fn(&Object, Sel, id, id, id, id),
+            sel!(viewDidChangeEffectiveAppearance),
+            view_did_change_effective_appearance as extern "C" fn(&Object, Sel),
         );
 
         decl.add_protocol(Protocol::get("CALayerDelegate").unwrap());
@@ -152,13 +149,6 @@ impl StatusItem {
                 native_view,
                 setLayerContentsRedrawPolicy: NSViewLayerContentsRedrawDuringViewResize
             ];
-            let _: () = msg_send![
-                button,
-                addObserver: native_view
-                forKeyPath: NSString::alloc(nil).init_str("effectiveAppearance")
-                options: NSKeyValueObservingOptionNew
-                context: nil
-            ];
 
             parent_view.addSubview_(native_view);
 
@@ -350,7 +340,7 @@ extern "C" fn display_layer(this: &Object, _: Sel, _: id) {
     }
 }
 
-extern "C" fn appearance_changed(this: &Object, _: Sel, _: id, _: id, _: id, _: id) {
+extern "C" fn view_did_change_effective_appearance(this: &Object, _: Sel) {
     unsafe {
         if let Some(state) = get_state(this).upgrade() {
             let mut state_borrow = state.as_ref().borrow_mut();

crates/gpui/src/platform/mac/window.rs 🔗

@@ -5,7 +5,7 @@ use crate::{
         vector::{vec2f, Vector2F},
     },
     keymap::Keystroke,
-    mac::platform::{NSKeyValueObservingOptionNew, NSViewLayerContentsRedrawDuringViewResize},
+    mac::platform::NSViewLayerContentsRedrawDuringViewResize,
     platform::{
         self,
         mac::{geometry::RectFExt, renderer::Renderer},
@@ -266,8 +266,8 @@ unsafe fn build_classes() {
                 as extern "C" fn(&Object, Sel, NSRange, *mut c_void) -> id,
         );
         decl.add_method(
-            sel!(observeValueForKeyPath:ofObject:change:context:),
-            appearance_changed as extern "C" fn(&Object, Sel, id, id, id, id),
+            sel!(viewDidChangeEffectiveAppearance),
+            view_did_change_effective_appearance as extern "C" fn(&Object, Sel),
         );
 
         // Suppress beep on keystrokes with modifier keys.
@@ -452,13 +452,6 @@ impl Window {
             }
 
             native_window.makeKeyAndOrderFront_(nil);
-            let _: () = msg_send![
-                native_window,
-                addObserver: native_view
-                forKeyPath: NSString::alloc(nil).init_str("effectiveAppearance")
-                options: NSKeyValueObservingOptionNew
-                context: nil
-            ];
 
             window.0.borrow().move_traffic_light();
             pool.drain();
@@ -1330,7 +1323,7 @@ extern "C" fn do_command_by_selector(this: &Object, _: Sel, _: Sel) {
     }
 }
 
-extern "C" fn appearance_changed(this: &Object, _: Sel, _: id, _: id, _: id, _: id) {
+extern "C" fn view_did_change_effective_appearance(this: &Object, _: Sel) {
     unsafe {
         let state = get_window_state(this);
         let mut state_borrow = state.as_ref().borrow_mut();