概要
- アクアとダークモードでメニューバーアイコンを切り替える必要がでた。
-
(Apple)Supporting Dark Mode in Your Interfaceにあるように、Viewであれば
drawRectなどで切り替えれば良さそう。 - ただし今回は
NSObject内で操作したいため再描画のトリガがない。よってNSNotificationで実装を行った。
参考
- objective c - macOS 1014でダークモードを検出するにはどうすればよいですか?
- Swift:macOSでテーマの切り替えを検出する
- NSNotificationを使って設定パネルを実装する
- [(Apple)Supporting Dark Mode in Your Interface]
- View関連は載っているけれどNSNotificationに関しては言及が無い
- 「Supporting Dark Mode in Your Interface」ではViewの描画時のことに言及している。
- 基本はNSNotificationば使わないのだろうか。今回みたいにメニューバーアイコンを考えなくていいなら、描画時に変更するので事足りるのか。
Code
[NSDistributedNotificationCenter.defaultCenter addObserver:self
selector:@selector(themeChanged:)
name:@"AppleInterfaceThemeChangedNotification"
object: nil];
-(void)themeChanged:(NSNotification *) notification {
NSLog (@"%@", notification);
}
/**
@brief 現在ダークモードかどうか
*/
- (BOOL)isDarkMode {
if (@available(*, macOS 10.14)) {
NSAppearance *appearance = NSApp.effectiveAppearance;
NSLog(@"%@", NSAppearance.currentAppearance);
return appearance.name == NSAppearanceNameDarkAqua;
}
return NO;
}
NSAppearance *appearance = NSApp.effectiveAppearance;
NSAppearanceのとり方に注意。
NSAppearance *appearance = NSAppearance.currentAppearance;
- これだと
NSAppearanceがappearance.nameが初回起動時のテーマに固定されて値が帰ってくるので不適。 - currentAppearance