Skip to content

Commit

Permalink
Removing unnecessary menu items from the address bar field context me…
Browse files Browse the repository at this point in the history
…nu (#384)

* Removing unnecessary menu items from the address bar field context menu
  • Loading branch information
tomasstrba authored Jan 11, 2022
1 parent 5388f9f commit ccbd795
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions DuckDuckGo/NavigationBar/View/AddressBarTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,45 @@ extension AddressBarTextField: SuggestionViewControllerDelegate {
}

extension AddressBarTextField: NSTextViewDelegate {

func textView(_ textView: NSTextView, willChangeSelectionFromCharacterRange _: NSRange, toCharacterRange range: NSRange) -> NSRange {
return self.filterSuffix(fromSelectionRange: range, for: textView.string)
}

func textView(_ view: NSTextView, menu: NSMenu, for event: NSEvent, at charIndex: Int) -> NSMenu? {
return removingAttributeChangingMenuItems(from: menu)
}

private static var selectorsToRemove: Set<Selector> = Set([
Selector(("_makeLinkFromMenu:")),
Selector(("_searchWithGoogleFromMenu:")),
#selector(NSFontManager.orderFrontFontPanel(_:)),
#selector(NSText.showGuessPanel(_:)),
Selector(("replaceQuotesInSelection:")),
#selector(NSStandardKeyBindingResponding.uppercaseWord(_:)),
#selector(NSTextView.startSpeaking(_:)),
#selector(NSTextView.changeLayoutOrientation(_:))
])

private func removingAttributeChangingMenuItems(from menu: NSMenu) -> NSMenu {
menu.items.reversed().forEach { menuItem in
if let action = menuItem.action, Self.selectorsToRemove.contains(action) {
menu.removeItem(menuItem)
} else {
if let submenu = menuItem.submenu, submenu.items.first(where: { submenuItem in
if let submenuAction = submenuItem.action, Self.selectorsToRemove.contains(submenuAction) {
return true
} else {
return false
}
}) != nil {
menu.removeItem(menuItem)
}
}
}
return menu
}

}

final class AddressBarTextEditor: NSTextView {
Expand Down

0 comments on commit ccbd795

Please sign in to comment.