Creating 1Password-like macOS menubar app
Asked Answered
I

1

6

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?

Iyar answered 20/10, 2017 at 22:33 Comment(0)
D
3

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)
    }
}
Dravidian answered 22/10, 2017 at 18:9 Comment(7)
I have created a subclass class SearchField: NSTextField {...} and set it as NSTextField's class. However, the method is not triggered when I press cmd + A. Also, I just realised that sometimes when I run the app, I cannot click on the input. It becomes unresponsive but other NSMenuItems work properly.Ganja
Of course you have to design your views that the text field is in the responder chain to receive the key events.Dravidian
Care to explain in a bit more detail? I'm having trouble googling itGanja
To be able to receive (key) events the view must be in a window which can become the key window. I guess 1Password uses a separate window controller rather than a simple NSMenuDravidian
would you suggest that I replace NSMenu with a custom window that shows up in place where NSMenu does by default and implement everything in that window?Ganja
There are probably a few solutions, as mentioned in any case you need a key windowDravidian
Let us continue this discussion in chat.Ganja

© 2022 - 2025 — McMap. All rights reserved.