Touchbar和NSPopover窗口

时间:2016-11-08 17:47:37

标签: macos cocoa nspopover nstouchbar

我有一个NSPopover窗口,我想添加触摸栏支持。我已经设法为标准NSWindow实现了触摸栏支持,但是按照弹出窗口的相同步骤不会导致xCode touchbar模拟器中出现任何触摸栏项目。

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

我担心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 } } } 窗口的触摸栏项?

import re

s = '[06/Nov/2016:23:24:03 +0100] BIND REQ conn=8349228 op=0 msgID=1 version=3 type=SIMPLE dn="uid=user1,ou=Org1,ou=example,o=com"'

print re.findall(r'uid=(.*?),',s)[0]
# prints user1

2 个答案:

答案 0 :(得分:1)

查看Apple的NSTouchBar Catalog Sample。最新版本有一个确切用例的例子。它被称为背景窗口(您必须通过构建应用中的 Window 菜单显示它。)

enter image description here

他们正在故事板中加载带有NSPopover segue的BackgroundImagesViewController并创建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的示例文件。

答案 1 :(得分:0)

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

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

Controllers

呼叫代码:

@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,因此我使用这种方法来实现此功能。