将NSToolbarItems与NSSplitView列对齐

时间:2016-12-29 02:11:16

标签: cocoa nssplitview nstoolbar nstoolbaritem nssplitviewcontroller

Finder和Notes有一种我想要重现的特殊行为。 NSToolbar中的“灵活空间”似乎考虑了拆分视图的尺寸。例如,第一组按钮在左侧与侧栏的右侧对齐。第二组图标与第一列的右侧对齐。当我扩展侧边栏时,工具栏项随之移动。

有可能重现这个吗?

enter image description here

解决方案

使用@KenThomases提供的解决方案,我已经实现了如下:

final class MainWindowController: NSWindowController {
    override func windowDidLoad() {
        super.windowDidLoad()
        window?.toolbar?.delegate = self
        // Make sure that tracking is enabled when the toolbar is completed
        DispatchQueue.main.async {
            self.trackSplitViewForFirstFlexibleToolbarItem()
        }
    }
}

extension MainWindowController: NSToolbarDelegate {
    func toolbarWillAddItem(_ notification: Notification) {
        // Make sure that tracking is evaluated only after the item was added
        DispatchQueue.main.async {
            self.trackSplitViewForFirstFlexibleToolbarItem()
        }
    }

    func toolbarDidRemoveItem(_ notification: Notification) {
        trackSplitViewForFirstFlexibleToolbarItem()
    }

    /// - Warning: This is a private Apple method and may break in the future.
    func toolbarDidReorderItem(_ notification: Notification) {
        trackSplitViewForFirstFlexibleToolbarItem()
    }

    /// - Warning: This method uses private Apple methods that may break in the future.
    fileprivate func trackSplitViewForFirstFlexibleToolbarItem() {
        guard var toolbarItems = self.window?.toolbar?.items, let splitView = (contentViewController as? NSSplitViewController)?.splitView else {
            return
        }

        // Add tracking to the first flexible space and remove it from the group
        if let firstFlexibleToolbarItem = toolbarItems.first, firstFlexibleToolbarItem.itemIdentifier == NSToolbarFlexibleSpaceItemIdentifier {
            _ = firstFlexibleToolbarItem.perform(Selector(("setTrackedSplitView:")), with: splitView)
            toolbarItems.removeFirst()
        }

        // Remove tracking from other flexible spaces
        for flexibleToolbarItem in toolbarItems.filter({ $0.itemIdentifier == NSToolbarFlexibleSpaceItemIdentifier }) {
            _ = flexibleToolbarItem.perform(Selector(("setTrackedSplitView:")), with: nil)
        }
    }
}

2 个答案:

答案 0 :(得分:3)

在使用macOS 11或更高版本时,您可以在工具栏中插入NSTrackingSeparatorToolbarItem项,这将把您的工具栏分成与NSSplitView对象的分隔线对齐的部分。

本示例将新的分隔符项目添加到已经包含其余按钮的工具栏,这些按钮已在Interface Builder或代码中配置。目标拆分视图涉及3个拆分视图的标准配置,包括侧边栏面板。

class WindowController: NSWindowController, NSToolbarDelegate {

    let mainPanelSeparatorIdentifier = NSToolbarItem.Identifier(rawValue: "MainPanel")

    override func windowDidLoad() {
        super.windowDidLoad()

        self.window?.toolbar?.delegate = self
    
        // Calling the inserts async gives more time to bind with the split viewer, and prevents crashes
        DispatchQueue.main.async {

            // The .sidebarTrackingSeparator is a built-in tracking separator which always aligns with the sidebar splitview
            self.window?.toolbar?.insertItem(withItemIdentifier: .sidebarTrackingSeparator, at: 0)
        
            // Example of a custom mainPanelSeparatorIdentifier
            // Index at '3' means that there are 3 toolbar items at the left side
            // of this separator, including the first tracking separator
            self.window?.toolbar?.insertItem(withItemIdentifier: mainPanelSeparatorIdentifier at: 3)
        }
    }

    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
    
        if let splitView = (self.contentViewController as? NSSplitViewController)?.splitView {
        
            // You must implement this for custom separator identifiers, to connect the separator with a split view divider
            if itemIdentifier == mainPanelSeparatorIdentifier {
                return NSTrackingSeparatorToolbarItem(identifier: itemIdentifier, splitView: splitView, dividerIndex: 1)
            }
        }
        return nil
    }
}

答案 1 :(得分:2)

您可以使用Apple私有方法执行此操作,但App Store中不允许这样做。

-setTrackedSplitView:上有私人方法NSToolbarItem。它需要NSSplitView*作为参数。您需要在要跟踪拆分视图的灵活空间工具栏项上调用它,并将它应该跟踪的拆分视图传递给它。为了防止Apple移除该方法,您应该在尝试使用之前检查NSToolbarItem是否响应该方法。

由于用户可以自定义和重新排序工具栏,因此通常需要枚举窗口工具栏的项目。对于标识符为NSToolbarFlexibleSpaceItemIdentifier的第一个,您可以设置它应跟踪的拆分视图。对于所有其他灵活空间项目,您清除(设置为nil)要拆分的拆分视图。您需要在首次设置窗口时再次使用工具栏委托-toolbarWillAddItem:-toolbarDidRemoveItem:方法执行此操作。还有另一个未记录的委托方法-toolbarDidReorderItem:,我发现更新工具栏很有用。