简体   繁体   English

移除 NSStatusBarButton 上的苍白效果

[英]Remove pale-effect on NSStatusBarButton

I'm looking for a way to remove the pale effect from the NSStatusBarButton.我正在寻找一种方法来消除 NSStatusBarButton 的苍白效果。 This is a picture of how it currently looks:这是它目前的样子的图片:

带有苍白图像的 NSStatusBarButton

This is how it should look:这是它的外观:

带有原始图像的 NSStatusBarButton

After looking at Apple's documentation, I found a solution to the problem.查看Apple的文档后,我找到了解决问题的方法。 If you set the appearance of the button directly (eg Aqua or DarkAqua), the pale effect disappears:如果直接设置按钮的外观(例如 Aqua 或 DarkAqua),苍白效果消失:

if let button = statusBarItem.button {
    ...
    button.appearance = NSAppearance.current // or aqua / darkAqua
}

But the problem is when the user changes the interface theme (eg from dark mode to light mode), the NSStatusBarButton does not change its appearance automatically:但问题是当用户改变界面主题(例如从暗模式到亮模式)时,NSStatusBarButton 不会自动改变其外观:

NSStatusBarButton 不会自动改变外观

I could monitor AppleInterfaceThemeChangedNotification and then change the appearance, but that's not a clean solution and I'm not happy with it.我可以监视AppleInterfaceThemeChangedNotification然后更改外观,但这不是一个干净的解决方案,我对此并不满意。

Is there an elegant solution to this?有没有优雅的解决方案? The image in the NSStatusBarButton should simply be displayed without changes (eg pale). NSStatusBarButton 中的图像应该简单地显示而不改变(例如苍白)。 Because I offer all flags of the world, I only have the images in png format, no PDF images.因为我提供世界上所有的旗帜,所以我只有 png 格式的图像,没有 PDF 图像。

Since the solution of vadian, unfortunately, did not work for me, I would like to show my alternative solution here.不幸的是,由于 vadian 的解决方案对我不起作用,我想在这里展示我的替代解决方案。 Maybe it helps somebody else.也许它可以帮助别人。

Create the NSStatusItem and customize NSStatusBarButton :创建NSStatusItem并自定义NSStatusBarButton

...
if let button = statusBarItem.button {
    ...
    button.appearance = NSAppearance.current // removes the pale effect
}
...

Write an extension for Notification.Name to react on AppleInterfaceThemeChangedNotification :Notification.Name编写一个扩展以对AppleInterfaceThemeChangedNotification做出反应:

extension Notification.Name {
    static let AppleInterfaceThemeChangedNotification = Notification.Name("AppleInterfaceThemeChangedNotification")
}

Add an Observer to the new notification name:将观察者添加到新的通知名称:

DistributedNotificationCenter.default.addObserver(self, selector: #selector(interfaceChanged), name: .AppleInterfaceThemeChangedNotification, object: nil)

Respond to the change between light/dark mode:响应亮/暗模式之间的变化:

@objc private static func interfaceChanged() {
    // change button.appearance
}

Make sure that button.appearance is only changed if the necessary macOS version is available:确保button.appearance仅在必要的 macOS 版本可用时才更改:

guard #available(OSX 10.14, *) else {
    return
}

I am sure that there is a cleaner solution.我相信有一个更清洁的解决方案。 If anyone has an idea, please tell me.如果有人有想法,请告诉我。

Write an extension of NSStatusBarButton and override viewDidChangeEffectiveAppearance .编写NSStatusBarButton的扩展并覆盖viewDidChangeEffectiveAppearance

In the body of the method change the appearance explicitly在方法的主体中显式更改外观

extension NSStatusBarButton { 
    @available(macOS 10.14, *)
    override public func viewDidChangeEffectiveAppearance() {
        print("viewDidChangeEffectiveAppearance")
        self.appearance = .current 
    }
}

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

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