简体   繁体   English

如何在Yosemite中检测暗模式以更改状态栏菜单图标

[英]How to detect dark mode in Yosemite to change the status bar menu icon

The status bar app icon has to be changed when dark mode is enabled in Yosemite. 在Yosemite中启用暗模式时,必须更改状态栏应用程序图标。 How to detect if dark mode has been enabled ? 如何检测是否启用了暗模式? Is there any notification for the same ? 是否有相同的通知?

Is it better to display another image or change the alpha value of existing Image ? 是否更好地显示另一个图像或更改现有图像的alpha值? Need inputs on which is the better way to go ?? 需要输入哪个是更好的方法?

You should make use of template images wherever possible because they allow your UI to automatically adapt to changes made by the system (at least when there's not a bug in the system... http://indiestack.com/2014/10/yosemites-dark-mode/ ). 您应该尽可能使用模板图像,因为它们允许您的UI自动适应系统所做的更改(至少在系统中没有错误的时候...... http://indiestack.com/2014/10/yosemites -dark-mode / )。 But in the case where you might use a custom view in the status bar and cannot take advantage of a template image, you can manually check for dark mode and adapt your UI accordingly. 但是,如果您可能在状态栏中使用自定义视图而无法利用模板图像,则可以手动检查暗模式并相应地调整UI。

You can check whether or not dark mode is enabled by retrieving a key from the user's global preferences, like this: 您可以通过从用户的全局首选项中检索密钥来检查是否启用了暗模式,如下所示:

NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
id style = [dict objectForKey:@"AppleInterfaceStyle"];
BOOL darkModeOn = ( style && [style isKindOfClass:[NSString class]] && NSOrderedSame == [style caseInsensitiveCompare:@"dark"] );

At least for the first release of Yosemite, the key is not present when dark mode is disabled, but the key is present and returns the string value @"Dark" when dark mode is enabled. 至少对于Yosemite的第一个版本,当禁用暗模式时键不存在,但是当启用暗模式时,该键存在并返回字符串值@“Dark”。 I added the case insensitive compare because I have seen preference keys change their case between system releases, and this adds a little insurance against that. 我添加了不区分大小写的比较,因为我看到偏好键在系统发布之间改变了它们的情况,这增加了一点保险。

To monitor the current state of the setting, you register as an observer of a distributed notification (within an appropriate method), like this: 要监视设置的当前状态,请注册为分布式通知的观察者(在适当的方法中),如下所示:

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(darkModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil];

And you create a method to act as the message selector for the notification, like this: 并且您创建了一个方法来充当通知的消息选择器,如下所示:

-(void)darkModeChanged:(NSNotification *)notif
{
    NSLog(@"Dark mode changed");
}

The Status bar icon needs to be a template image. 状态栏图标需要是模板图像。 Just set the setTemplate:Yes to NSImage. 只需将setTemplate:Yes设置为NSImage即可。 And when switched to dark mode , the vibrancy should apply. 当切换到黑暗模式时,应该应用活力。

As noted by bergdesign , you need to observe the system wide notification and read the persistent global domain. 正如bergdesign所述 ,您需要观察系统范围的通知并阅读持久的全局域

We made a class to simplify handling changes to the Dark Mode settings: https://github.com/weAreYeah/WAYTheDarkSide 我们制作了一个类来简化对暗模式设置的处理更改: https//github.com/weAreYeah/WAYTheDarkSide

It becomes as easy as... 它变得像......一样简单

[WAYTheDarkSide welcomeApplicationWithBlock:^{
    // Enabling Dark Mode
    [someWindow setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameVibrantDark]];
    [someVisualEffectView setMaterial:NSVisualEffectMaterialDark];

} immediately:YES];

and

[WAYTheDarkSide outcastApplicationWithBlock:^{
    // Disabling Dark Mode
    [someWindow setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameVibrantLight]];
    [someVisualEffectView setMaterial:NSVisualEffectMaterialLight];

} immediately:YES];

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM