3

我有一个NSPopover窗口,我想添加触摸栏支持。我已经设法实现了对标准的触摸栏支持NSWindow,但是对我的弹出框执行相同的程序不会导致任何触摸栏项目出现在 xCode 触摸栏模拟器中。

我正在实施makeTouchbar并且NSTouchBarDelegateNSViewController其中呈现为NSPopover. makeTouchBar并且正在调用委托函数,但触摸栏中没有显示任何内容(在下面的代码块中,触摸栏和提示都在记录)。

我担心 NSPopover 窗口不会触发触摸栏作为活动应用程序,即使它具有键盘焦点。如何显示NSPopover窗口的触摸栏项目?

//MARK: Touch bar
@available(OSX 10.12.1, *)
override func makeTouchBar() -> NSTouchBar? {
    Swift.print("Touchbar.")

    let touchBar                        = NSTouchBar()
    touchBar.delegate                   = self
    touchBar.customizationIdentifier    = .touchBar
    touchBar.defaultItemIdentifiers     = [.tbHint, .colourC] 

    return touchBar

}

@available(OSX 10.12.1, *)
extension RememberViewController: NSTouchBarDelegate {

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

        switch identifier{
        case NSTouchBarItemIdentifier .tbHint:
            Swift.print("hint")
            let buttonView  = NSCustomTouchBarItem(identifier: identifier)
            let button      = NSButton(title: "Hint", target: self, action: #selector(showHint))
            buttonView.view = button
            return buttonView

        case NSTouchBarItemIdentifier .colourC:
            let colorPicker: NSColorPickerTouchBarItem
            colorPicker = NSColorPickerTouchBarItem.colorPicker(withIdentifier: identifier)

            colorPicker.customizationLabel = "Color Picker"
            colorPicker.target = self
            colorPicker.action = #selector(showHint)

            return colorPicker

        default:
            return nil
        }


    }
}
4

2 回答 2

3

查看来自 Apple的NSTouchBar 目录示例。最新版本有一个完全适合您的用例的示例。它被称为背景窗口(您必须通过构建应用程序中的窗口菜单显示它)。

在此处输入图像描述

他们BackgroundImagesViewController在情节提要中加载了一个 NSPopover segue 并像这样创建 NSTouchBar:

@available(OSX 10.12.2, *)
class BackgroundImagesViewController: NSViewController {
  override func makeTouchBar() -> NSTouchBar? {
    let touchBar = NSTouchBar()
    touchBar.delegate = self
    touchBar.customizationIdentifier = .scrubberBar
    touchBar.defaultItemIdentifiers = [.imageScrubber]
    touchBar.customizationAllowedItemIdentifiers = [.imageScrubber]
    touchBar.principalItemIdentifier = .imageScrubber

    return touchBar
  }
}

最好的办法是查看 Apple 的示例文件。

于 2017-01-15T11:47:35.423 回答
0

我尝试一些测试。我覆盖了 NSPopover 和 NSViewController 的 makeTouchBar 方法,当它弹出时,它不会调用该方法。但是当我在上面添加一个 TextField 时,它获得了键盘的焦点,并且神奇地调用了 makeTouchBar 方法。触摸栏只显示一个键盘图标。因此,我认为我可以覆盖 NSTextField 的 makeTouchBar 方法,但我失败了。我用谷歌搜索,他们推荐 NSTextView,我覆盖了它,它有效!

但实际上这似乎是在欺骗系统。

控制器

调用代码:

@IBAction func addAction(_ sender: Any) {
    let addingController = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil).instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("controller_adding")) as! AddViewController
    let popover = NSPopover()
    popover.contentViewController = addingController
    popover.behavior = .semitransient

    popover.show(relativeTo: addButton.frame, of: addButton, preferredEdge: NSRectEdge.maxY)
}

这是弹出框的 ViewController

import Cocoa

class AddViewController: NSViewController {


    @IBAction func OKAction(_ sender: Any) {
        print("OK")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        let touch = MyTextView()
        touch.setController(self)
        view.addSubview(touch)  
    }
}

这是被覆盖的 NSTextView。

class MyTextView : NSTextView
{

    var controller : AddViewController?

    func setController(_ controller : AddViewController)
    {
        self.controller = controller
    }

    @objc func action()
    {
        controller?.OKAction(self)
    }

    override func makeTouchBar() -> NSTouchBar? {

        let touchbar = NSTouchBar()
        let item_add = NSTouchBarItem.Identifier("item_add")
        touchbar.customizationIdentifier = NSTouchBar.CustomizationIdentifier("add")

        touchbar.customizationAllowedItemIdentifiers = [item_add]
        touchbar.defaultItemIdentifiers = [item_add]

        touchbar.delegate = self

        return touchbar
    }

    override func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
        let item = NSCustomTouchBarItem(identifier: identifier)
        let item_add = NSTouchBarItem.Identifier("item_add")
        var itemDescription = ""
        switch identifier {
        case item_add:
            itemDescription = "Add Button"
            item.view = NSButton(image: NSImage(named: NSImage.Name("NSTrashEmpty"))!, target: self, action: #selector(action))
        default:
            print("???")
        }

        item.customizationLabel = itemDescription
        return item
    }
}

对macOS开发不熟悉,也不知道如何获取popover的焦点,所以就用这种方法来实现功能。

于 2019-06-22T08:57:03.953 回答