macos: clean up setting up the tab menu by using an NSMenu extension
parent
a0089702f1
commit
f559bccc38
|
|
@ -330,14 +330,9 @@ class TerminalWindow: NSWindow {
|
||||||
item.identifier = Self.closeTabsOnRightMenuItemIdentifier
|
item.identifier = Self.closeTabsOnRightMenuItemIdentifier
|
||||||
item.target = targetController
|
item.target = targetController
|
||||||
item.setImageIfDesired(systemSymbolName: "xmark")
|
item.setImageIfDesired(systemSymbolName: "xmark")
|
||||||
let insertionIndex: UInt
|
if menu.insertItem(item, after: NSSelectorFromString("performCloseOtherTabs:")) == nil,
|
||||||
if let idx = menu.insertItem(item, after: NSSelectorFromString("performCloseOtherTabs:")) {
|
menu.insertItem(item, after: NSSelectorFromString("performClose:")) == nil {
|
||||||
insertionIndex = idx
|
|
||||||
} else if let idx = menu.insertItem(item, after: NSSelectorFromString("performClose:")) {
|
|
||||||
insertionIndex = idx
|
|
||||||
} else {
|
|
||||||
menu.addItem(item)
|
menu.addItem(item)
|
||||||
insertionIndex = UInt(menu.items.count - 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Other close items should have the xmark to match Safari on macOS 26
|
// Other close items should have the xmark to match Safari on macOS 26
|
||||||
|
|
@ -348,8 +343,7 @@ class TerminalWindow: NSWindow {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
removeTabColorSection(from: menu)
|
appendTabColorSection(to: menu, target: targetController)
|
||||||
appendTabColorSection(to: menu)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func isTabContextMenu(_ menu: NSMenu) -> Bool {
|
private func isTabContextMenu(_ menu: NSMenu) -> Bool {
|
||||||
|
|
@ -367,33 +361,20 @@ class TerminalWindow: NSWindow {
|
||||||
return !selectorNames.isDisjoint(with: tabContextSelectors)
|
return !selectorNames.isDisjoint(with: tabContextSelectors)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func appendTabColorSection(to menu: NSMenu, target: TerminalController?) {
|
||||||
private func removeTabColorSection(from menu: NSMenu) {
|
menu.removeItems(withIdentifiers: [
|
||||||
let identifiers: Set<NSUserInterfaceItemIdentifier> = [
|
|
||||||
Self.tabColorSeparatorIdentifier,
|
Self.tabColorSeparatorIdentifier,
|
||||||
Self.tabColorHeaderIdentifier,
|
Self.tabColorHeaderIdentifier,
|
||||||
Self.tabColorPaletteIdentifier
|
Self.tabColorPaletteIdentifier
|
||||||
]
|
])
|
||||||
|
|
||||||
for (index, item) in menu.items.enumerated().reversed() {
|
|
||||||
guard let identifier = item.identifier else { continue }
|
|
||||||
if identifiers.contains(identifier) {
|
|
||||||
menu.removeItem(at: index)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func appendTabColorSection(to menu: NSMenu) {
|
|
||||||
guard let terminalController else { return }
|
|
||||||
|
|
||||||
let separator = NSMenuItem.separator()
|
let separator = NSMenuItem.separator()
|
||||||
separator.identifier = Self.tabColorSeparatorIdentifier
|
separator.identifier = Self.tabColorSeparatorIdentifier
|
||||||
menu.addItem(separator)
|
menu.addItem(separator)
|
||||||
|
|
||||||
let headerTitle = NSLocalizedString("Tab Color", comment: "Tab color context menu section title")
|
|
||||||
let headerItem = NSMenuItem()
|
let headerItem = NSMenuItem()
|
||||||
headerItem.identifier = Self.tabColorHeaderIdentifier
|
headerItem.identifier = Self.tabColorHeaderIdentifier
|
||||||
headerItem.title = headerTitle
|
headerItem.title = "Tab Color"
|
||||||
headerItem.isEnabled = false
|
headerItem.isEnabled = false
|
||||||
headerItem.setImageIfDesired(systemSymbolName: "eyedropper")
|
headerItem.setImageIfDesired(systemSymbolName: "eyedropper")
|
||||||
menu.addItem(headerItem)
|
menu.addItem(headerItem)
|
||||||
|
|
@ -402,8 +383,8 @@ class TerminalWindow: NSWindow {
|
||||||
paletteItem.identifier = Self.tabColorPaletteIdentifier
|
paletteItem.identifier = Self.tabColorPaletteIdentifier
|
||||||
paletteItem.view = makeTabColorPaletteView(
|
paletteItem.view = makeTabColorPaletteView(
|
||||||
selectedColor: tabColorSelection
|
selectedColor: tabColorSelection
|
||||||
) { [weak terminalController] color in
|
) { [weak target] color in
|
||||||
terminalController?.setTabColor(color)
|
target?.setTabColor(color)
|
||||||
}
|
}
|
||||||
menu.addItem(paletteItem)
|
menu.addItem(paletteItem)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,4 +27,16 @@ extension NSMenu {
|
||||||
insertItem(item, at: insertionIndex)
|
insertItem(item, at: insertionIndex)
|
||||||
return UInt(insertionIndex)
|
return UInt(insertionIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes all menu items whose identifier is in the given set.
|
||||||
|
///
|
||||||
|
/// - Parameter identifiers: The set of identifiers to match for removal.
|
||||||
|
func removeItems(withIdentifiers identifiers: Set<NSUserInterfaceItemIdentifier>) {
|
||||||
|
for (index, item) in items.enumerated().reversed() {
|
||||||
|
guard let identifier = item.identifier else { continue }
|
||||||
|
if identifiers.contains(identifier) {
|
||||||
|
removeItem(at: index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue