Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski
Created December 24, 2020 23:14
Show Gist options
  • Save KevinGutowski/5e38e090d3e693c8e2a6451707ebc86e to your computer and use it in GitHub Desktop.
Save KevinGutowski/5e38e090d3e693c8e2a6451707ebc86e to your computer and use it in GitHub Desktop.
Popupmenu Plan
import Cocoa
class SMBMenu: NSMenu {
var button: NSStatusBarButton?
required init(coder: NSCoder) {
super.init(coder: coder)
}
override init(title: String) {
super.init(title: title)
}
init(with button:NSStatusBarButton) {
super.init(title: "")
self.button = button
let search = NSTextField()
search.frame = NSMakeRect(0, 0, 180, 24)
search.placeholderString = "Search..."
let searchMenu = NSMenuItem()
searchMenu.view = search
self.addItem(searchMenu)
self.addItem(.separator())
self.addItem(NSMenuItem(title: "Item 1", action: #selector(AppDelegate.copyCode), keyEquivalent: ""))
self.addItem(NSMenuItem(title: "Item 2", action: #selector(AppDelegate.copyCode), keyEquivalent: ""))
self.addItem(NSMenuItem(title: "Item 3", action: #selector(AppDelegate.copyCode), keyEquivalent: ""))
self.addItem(.separator())
self.addItem(NSMenuItem(title: "Quit", action: #selector(AppDelegate.quit), keyEquivalent: ""))
self.minimumWidth = 200
}
deinit {
button?.highlight(false)
}
}
extension NSStatusBarButton {
public override func mouseDown(with event: NSEvent) {
NSApp.activate(ignoringOtherApps: true)
self.highlight(true)
let menu = SMBMenu(with: self) //pass in this instance so we can remove highlighting when this object is deinit
DispatchQueue.main.async {
menu.popUp(positioning: menu.item(at: 0), at:NSMakePoint(self.frame.minX, self.frame.maxY+4), in: self)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment