试图使菜单图标适应莫哈韦沙漠黑暗模式 [英] Trying to adapt Menu Icon to Mojave dark mode

查看:69
本文介绍了试图使菜单图标适应莫哈韦沙漠黑暗模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到莫哈韦沙漠后,我试图在模式更改时调整菜单图标. 我的应用程序:应用程序是代理(UIElement)"最初没有Windows.

After switching to mojave I´m trying to adapt the menu icon when the mode has changed. My app: "Application is agent (UIElement)" doesn´t have windows initially.

目前,我正在使用NSMenuDelegate函数 menuWillOpen ,该函数到目前为止已经可以使用,但是用户必须打开菜单才能更改图标.

At the moment I´m using the NSMenuDelegate function menuWillOpen which works so far but the user has to open the menu to get the icon changed.

我想知道是否有一种方法可以在不打开菜单的情况下检测出外观是否发生了变化.我已经从NSApplicationDelegate尝试过applicationDidChangeScreenParameters,但没有成功.

I wonder if there is a way to detect that the appearance has changed whithout opening the menu. I already tried applicationDidChangeScreenParameters from NSApplicationDelegate without success.

//这来自NSMenuDelegate

//this comes from NSMenuDelegate

- (void)menuWillOpen:(NSMenu *)menu { 
    [self adaptToDarkMode];
}

//这可以处理菜单图标的更改

// this handles the menu icon change

 - (void) adaptMenuIcon {
    BOOL  darkModeFlag  = [self psGetDarkMode];
    NSString *iconName  = @"MenuIconBlack";

    if(darkModeFlag) {
        iconName  = @"MenuIconWhite";
    }

    NSString *filePath  = [self psBundlePathToFolder:@""];
    NSString *finalPath = [NSString stringWithFormat:@"%@%@.png", filePath, iconName];

    NSImage  *image     = [[NSImage alloc] initWithContentsOfFile:finalPath];
    pathToMenuIcons     = [self psBundlePathToFolder:@"MenuIcons"];

    [statusItem setMenu:statusMenu];
    [statusItem setTitle:@"➜"];
    [statusItem setImage: image];
    [statusItem setHighlightMode:YES];
}

推荐答案

您可以阅读全局首选项:

You can read global preferences:

NSString *appearance = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];

这将在黑暗模式下返回Dark.

This will return Dark in dark mode.

您也可以使用NSDistributedNotificationCenter

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

-(void)onAppearanceChanged:(NSNotification *)notificaton
{
 // read appearance
}

这篇关于试图使菜单图标适应莫哈韦沙漠黑暗模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆