本文介绍了NSStatusItem更改图像的深色调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OSX 10.10 beta 3中,苹果发布了其深色调选项.不幸的是,这也意味着几乎所有状态栏图标(除了我所见过的Apple和Path Finder除外),包括我的图标,在深色背景上都保持深色.当应用深色调时,如何提供备用图像?

With OSX 10.10 beta 3, Apple released their dark tint option. Unfortunately, it also means that pretty much all status bar icons (with the exception of Apple's and Path Finder's that I've seen), including mine, remain dark on a dark background. How can I provide an alternate image for when dark tint is applied?

我没有看到NSStatusBarNSStatusItem上的API更改向我显示更改,我假设这是通知或响应,可以随着用户更改色彩轻松进行更改.

I don't see an API change on NSStatusBar or NSStatusItem that shows me a change, I'm assuming it's a notification or something reactive to easily make the change as the user alters the tint.

用于绘制图像的当前代码包含在NSView中:

Current code to draw the image is encased within an NSView:

- (void)drawRect:(NSRect)dirtyRect
{
    // set view background color
    if (self.isActive) {
        [[NSColor selectedMenuItemColor] setFill];
    } else {
        [[NSColor clearColor] setFill];
    }

    NSRectFill(dirtyRect);

    // set image
    NSImage *image = (self.isActive ? self.alternateImage : self.image);
    _imageView.image = image;
}

推荐答案

TL; DR:您无需在黑暗主题"中做任何特别的事情.给NSStatusItem(或NSStatusBarButton)一个模板图像,它将在任何菜单栏上下文中正确设置其样式.

TL;DR: You don't have to do anything special in Dark Theme. Give NSStatusItem (or NSStatusBarButton) a template image and it will style it correctly in any menubar context.

某些应用程序的状态项(例如PathFinder的状态项)已在Dark Theme中运行的原因是因为它们没有在StatusItem上设置自己的自定义视图,而只是在StatusItem上设置模板图像.

The reason why some apps' status items (such as PathFinder's) already work in Dark Theme is because they're not setting their own custom view on the StatusItem, but only setting a template image on the StatusItem.

类似的东西:

_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
NSImage *image = [NSImage imageNamed:@"statusItemIcon"];
[image setTemplate:YES];
[_statusItem setImage:image];

这完全符合您在Mavericks和更早版本以及Yosemite和任何将来发行版中的预期,因为它允许AppKit 根据状态项的状态对图像进行所有样式设置

This works exactly as you'd expect in Mavericks and earlier, as well as Yosemite and any future releases because it allows AppKit to do all of the styling of the image depending on the status item state.

在小牛(及更早版本)中,只有2种独特的样式.未按下并按下.这两种样式分别看起来分别是纯黑色和纯白色的. (实际上纯粹是黑色的"并不完全正确-有一个很小的影响,使它们看上去略微内陷).

In Mavericks (and earlier) there were only 2 unique styles of the items. Unpressed and Pressed. These two styles pretty much looked purely black and purely white, respectively. (Actually "purely black" isn't entirely correct -- there was a small effect that made them look slightly inset).

因为只有两种可能的状态,所以状态栏应用程序可以设置其自己的视图,并根据其突出显示的状态仅绘制黑色或白色即可轻松获得相同的外观. (但要再次注意,它并非纯粹是黑色的,因此应用程序要么必须在图像中建立效果,要么对难以置信的不适当的"图标感到满意.)

Because there were only two possible state, status bar apps could set their own view and easily get the same appearance by just drawing black or white depending on their highlighted state. (But again note that it wasn't purely black, so apps either had to build the effect in the image or be satisfied with a hardly-noticeable out of place icon).

在优胜美地,至少有32种独特的商品样式. 《黑暗主题》中的压抑"只是其中之一.应用程序没有实用(或不切实际)的方法来执行自己的项目样式,并使其在所有情况下看起来都是正确的.

In Yosemite there are at least 32 unique styling of items. Unpressed in Dark Theme is only one of those. There is no practical (or unpractical) way for an app to be able to do their own styling of items and have it look correct in all contexts.

以下是其中六个可能样式的示例:

Here are examples of six of those possible stylings:

不活动的菜单栏上的状态项现在具有特定的样式,与过去的简单不透明度更改相反.外观残障是另一种可能的变化.在这种可能性矩阵中,还有其他维度.

Status items on an inactive menubar now have a specific styling, as opposed to a simple opacity change as in the past. Disabled appearance is one other possible variation; there are also other additional dimensions to this matrix of possibilities.

设置为NSStatusItem的view属性的任意视图无法捕获所有这些变化,因此在10.10中已弃用了它(以及其他相关的API).

Arbitrary views set as NSStatusItem's view property have no way to capture all of these variations, hence it (and other related API) is deprecated in 10.10.

但是,种子3在NSStatusItem上引入了新的API:

However, seed 3 introduces new API on NSStatusItem:

@property (readonly, strong) NSStatusBarButton *button NS_AVAILABLE_MAC(10_10);

这段API有几个用途:

This piece of API has a few purposes:

  1. 应用程序现在可以在不设置其自定义视图的情况下获取状态项的屏幕位置(或从中显示弹出窗口).
  2. 不再需要NSStatusItem上的API,例如imagetitlesendActionOn:.
  3. 提供新API的类:looksDisabled.这样一来,应用程序即可获得标准的禁用/关闭样式(如关闭时为Bluetooth/Time Machine),而无需自定义图片.
  1. An app can now get the screen position (or show a popover from) a status item without setting its own custom view.
  2. Removes the need for API like image, title, sendActionOn: on NSStatusItem.
  3. Provides a class for new API: i.e. looksDisabled. This allows apps to get the standard disabled/off styling (like Bluetooth/Time Machine when off) without requiring a custom image.

如果当前(非自定义视图)API无法完成某些操作,请提出增强请求. StatusItems应该以在所有状态项目中标准化的方式提供行为或外观.

If there's something that can't be done with the current (non- custom view) API, please file an enhancement request for it. StatusItems should provide behavior or appearances in a way that it standard across all status items.

有关更多讨论,请参见 https://devforums.apple.com/thread/234839 ,尽管我已对此进行了总结这里的大多数东西.

More discussion is at https://devforums.apple.com/thread/234839, although I've summarized most everything here.

这篇关于NSStatusItem更改图像的深色调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 07:42