装回NSTouchBar上的ESC按钮 [英] Replacing the ESC button on a NSTouchBar

查看:164
本文介绍了装回NSTouchBar上的ESC按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用情节提要为我的应用构建NSTouchBar.

我想用其他东西代替ESC按钮.

和往常一样,没有文档告诉您该怎么做.

我已经在网上搜索了,发现了诸如

之类的含糊信息

您可以将"esc"的内容更改为其他内容,例如 通过使用完成"或其他任何内容,甚至是图标 带NSTouchBarItem的escapeKeyReplacementItemIdentifier.

但这太模糊了,难以理解.

有什么想法吗?


这是我到目前为止所做的.

我在情节提要上的NSTouchBar中添加了一个按钮,并将其标识符更改为newESC.我以编程方式添加了此行:

self.touchBar.escapeKeyReplacementItemIdentifier = @"newESC";

当我运行该应用程序时,ESC键现在不可见,但仍然占据它在工具栏上的空间.本应替换的按钮出现在其旁边.所以那个酒吧

`ESC`, `NEW_ESC`, `BUTTON1`, `BUTTON2`, ...

现在是

`ESC` (invisible), `NEW_ESC`, `BUTTON1`, `BUTTON2`, ...

旧的ESC仍在酒吧占据其空间.

解决方案

这是通过创建一个触摸栏项目(例如,一个包含NSButtonNSCustomTouchBarItem),并将该项目与它自己的标识符相关联来完成的. >

然后使用另一个标识符执行常规逻辑,但是将先前创建的标识符添加为ESC替换.

Swift中的快速示例:

func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem? {

    switch identifier {
    case NSTouchBarItemIdentifier.identifierForESCItem:
        let item = NSCustomTouchBarItem(identifier: identifier)
        let button = NSButton(title: "Button!", target: self, action: #selector(escTapped))
        item.view = button
        return item
    case NSTouchBarItemIdentifier.yourUsualIdentifier:
        let item = NSCustomTouchBarItem(identifier: identifier)
        item.view = NSTextField(labelWithString: "Example")
        touchBar.escapeKeyReplacementItemIdentifier = .identifierForESCItem
        return item
    default:
        return nil
    }

}

func escTapped() {
    // do additional logic when user taps ESC (optional)
}

我还建议对标识符进行扩展(类别),这样可以避免使用字符串文字造成拼写错误:

@available(OSX 10.12.2, *)
extension NSTouchBarItemIdentifier {
    static let identifierForESCItem = NSTouchBarItemIdentifier("com.yourdomain.yourapp.touchBar.identifierForESCItem")
    static let yourUsualIdentifier = NSTouchBarItemIdentifier("com.yourdomain.yourapp.touchBar.yourUsualIdentifier")
}

I am building a NSTouchBar for my app using storyboard.

I want to replace the ESC button with something else.

As usual, there is no doc telling you how to do that.

I have searched the web and I have found vague informations like

You can change the content of "esc" to something else, though, like "done" or anything, even an icon, by using escapeKeyReplacementItemIdentifier with the NSTouchBarItem.

But this is too vague to understand.

Any ideas?


This is what I did so far.

I have added a button to NSTouchBar on storyboard and changed its identifier to newESC. I added this line programmatically:

self.touchBar.escapeKeyReplacementItemIdentifier = @"newESC";

When I run the App the ESC key is now invisible but still occupies its space on the bar. The button that was supposed to replace it, appears next to it. So that bar that was

`ESC`, `NEW_ESC`, `BUTTON1`, `BUTTON2`, ...

is now

`ESC` (invisible), `NEW_ESC`, `BUTTON1`, `BUTTON2`, ...

The old ESC is still occupying its space on the bar.

解决方案

This is done by creating a touch bar item, let's say a NSCustomTouchBarItem containing a NSButton, and associating this item with its own identifier.

Then with another identifier you do your usual logic but you add the previously created identifier as the ESC replacement.

Quick example in Swift:

func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem? {

    switch identifier {
    case NSTouchBarItemIdentifier.identifierForESCItem:
        let item = NSCustomTouchBarItem(identifier: identifier)
        let button = NSButton(title: "Button!", target: self, action: #selector(escTapped))
        item.view = button
        return item
    case NSTouchBarItemIdentifier.yourUsualIdentifier:
        let item = NSCustomTouchBarItem(identifier: identifier)
        item.view = NSTextField(labelWithString: "Example")
        touchBar.escapeKeyReplacementItemIdentifier = .identifierForESCItem
        return item
    default:
        return nil
    }

}

func escTapped() {
    // do additional logic when user taps ESC (optional)
}

I also suggest making an extension (category) for the identifiers, it avoids making typos with string literals:

@available(OSX 10.12.2, *)
extension NSTouchBarItemIdentifier {
    static let identifierForESCItem = NSTouchBarItemIdentifier("com.yourdomain.yourapp.touchBar.identifierForESCItem")
    static let yourUsualIdentifier = NSTouchBarItemIdentifier("com.yourdomain.yourapp.touchBar.yourUsualIdentifier")
}

这篇关于装回NSTouchBar上的ESC按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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