macos: change to NSMenu extension
parent
f612e4632c
commit
dc641c7861
|
|
@ -299,23 +299,8 @@ class TerminalWindow: NSWindow {
|
|||
item.identifier = Self.closeTabsOnRightMenuItemIdentifier
|
||||
item.target = nil
|
||||
item.isEnabled = true
|
||||
|
||||
// Remove any previously configured items, because the menu is
|
||||
// cached across different tab targets.
|
||||
if let existing = menu.items.first(where: { $0.identifier == item.identifier }) {
|
||||
menu.removeItem(existing)
|
||||
}
|
||||
|
||||
// Insert it wherever we can
|
||||
if let idx = menu.items.firstIndex(where: {
|
||||
$0.action == NSSelectorFromString("performCloseOtherTabs:")
|
||||
}) {
|
||||
menu.insertItem(item, at: idx + 1)
|
||||
} else if let idx = menu.items.firstIndex(where: {
|
||||
$0.action == NSSelectorFromString("performClose:")
|
||||
}) {
|
||||
menu.insertItem(item, at: idx + 1)
|
||||
} else {
|
||||
if !menu.insertItem(item, after: NSSelectorFromString("performCloseOtherTabs:")) &&
|
||||
!menu.insertItem(item, after: NSSelectorFromString("performClose:")) {
|
||||
menu.addItem(item)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
import AppKit
|
||||
|
||||
extension NSMenu {
|
||||
/// Inserts a menu item after an existing item with the specified action selector.
|
||||
///
|
||||
/// If an item with the same identifier already exists, it is removed first to avoid duplicates.
|
||||
/// This is useful when menus are cached and reused across different targets.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - item: The menu item to insert.
|
||||
/// - action: The action selector to search for. The new item will be inserted after the first
|
||||
/// item with this action.
|
||||
/// - Returns: `true` if the item was inserted after the specified action, `false` if the action
|
||||
/// was not found and the item was not inserted.
|
||||
@discardableResult
|
||||
func insertItem(_ item: NSMenuItem, after action: Selector) -> Bool {
|
||||
if let identifier = item.identifier,
|
||||
let existing = items.first(where: { $0.identifier == identifier }) {
|
||||
removeItem(existing)
|
||||
}
|
||||
|
||||
guard let idx = items.firstIndex(where: { $0.action == action }) else {
|
||||
return false
|
||||
}
|
||||
|
||||
insertItem(item, at: idx + 1)
|
||||
return true
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue