Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating 1Password-like macOS menubar app

I am trying to create a macOS menubar app which will have a text field as the first item. The textfield will serve as a search bar for filtering other items which will be displayed below it.

It should look very similar to 1password:

1password

This is what I managed to do:

mine

I have accomplished this by creating a Status menu with three items and creating a custom view for the first item in the menu.

However, this approach does not seem to solve my issue. When pressing cmd + A in the search field, the focus jumps to the next item in the menu. This is the default behaviour for NSMenu.

So, my question is: Is this the right approach to create a 1Password-like app or is there a better one?

like image 525
Jerguš Lejko Avatar asked Oct 20 '17 22:10

Jerguš Lejko


1 Answers

Basically the approach is correct.

But you have to catch the edit key events explicitly. Subclass NSTextField and override performKeyEquivalent

class AXCVTextField: NSTextField {

    override func performKeyEquivalent(with event: NSEvent) -> Bool {
        if event.modifierFlags.contains(.command),
          let key = event.charactersIgnoringModifiers {
            var action : String?
            switch key {
            case "x": action = "cut:"
            case "c": action = "copy:"
            case "v": action = "paste:"
            case "a": action = "selectAll:"
            default:
                break
            }
            if let action = action {
                return NSApp.sendAction(Selector(action), to:self.window!.firstResponder, from:self)
            }
        }
        return super.performKeyEquivalent(with: event)
    }
}
like image 64
vadian Avatar answered Sep 30 '22 12:09

vadian