Use viewDidChangeEffectiveAppearance to detect appearance changes

This commit is contained in:
Antonio Scandurra 2022-09-14 15:49:08 +02:00
parent d10f6f60ad
commit c1f448d8a8
3 changed files with 9 additions and 28 deletions

View file

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

View file

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

View file

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