Is the settings window supposed to be a popover as well? Or is that just a separate window that opens when the setting button is clicked?
I would try
1) Adding a popover instance to the AppDelegate
2) Add the main view controller instance to the new popover in the applicationDidFinishLaunching method
Something like this (This is untested!)
importCocoaimportHotKeyimportCarbon@NSApplicationMainclassAppDelegate:NSObject,NSApplicationDelegate{letstatusItem=NSStatusBar.system.statusItem(withLength:NSStatusItem.variableLength)// Add mainPopover and eventMonitor as an instance variableletmainPopoverView=NSPopover()vareventMonitor:EventMonitor?funcapplicationDidFinishLaunching(_aNotification:Notification){ifStorage.fileExists("globalKeybind.json",in:.documents){letglobalKeybinds=Storage.retrieve("globalKeybind.json",from:.documents,as:GlobalKeybindPreferences.self)hotKey=HotKey(keyCombo:KeyCombo(carbonKeyCode:globalKeybinds.keyCode,carbonModifiers:globalKeybinds.carbonFlags))}statusItem.button?.title="β "statusItem.button?.target=selfstatusItem.button?.action=#selector(showSettings)// Fresh controller instance - use your view controller heremainPopoverView.contentViewController=MenuViewController.freshController()// Check if mouse is clicked outside of menu - if it is then close the popover (optional)eventMonitor=EventMonitor(mask:[.leftMouseDown,.rightMouseDown]){[weakself]eventinifletstrongSelf=self,strongSelf.mainPopoverView.isShown{print("is shown")strongSelf.closePopover(sender:event)}}}funcapplicationWillTerminate(_aNotification:Notification){// Insert code here to tear down your application}// Add in the toggling methods@objcfunctogglePopover(_sender:Any?){ifmainPopoverView.isShown{closePopover(sender:sender)}else{showPopover(sender:sender)}}funcshowPopover(sender:Any?){ifletbutton=statusItem.button{mainPopoverView.show(relativeTo:button.bounds,of:button,preferredEdge:NSRectEdge.minY)}eventMonitor?.start()}funcclosePopover(sender:Any?){mainPopoverView.performClose(sender)eventMonitor?.stop()}@objcfuncshowSettings(){letstoryboard=NSStoryboard(name:"Main",bundle:nil)guardletvc=storyboard.instantiateController(withIdentifier:"ViewController")as?NSViewControllerelse{fatalError("ViewController in Storyboard nicht gefunden.")}guardletbutton=statusItem.buttonelse{fatalError("Button des Status-Items konnte nicht gefunden werden")}letpopoverView=NSPopover()popoverView.contentViewController=vcpopoverView.behavior=.transientpopoverView.show(relativeTo:button.bounds,of:button,preferredEdge:.maxY)}publicvarhotKey:HotKey?{didSet{guardlethotKey=hotKeyelse{return}// Toggle popover hotKey.keyDownHandler={[weakself]inifletstrongSelf=self{strongSelf.togglePopover(nil)}}}}}
Mildly related, I recommend reading this Ray Wenderlich tutorial on menu bar apps, it helped me understand them better - raywenderlich.com/450-menus-and-po...
There's no such thing... 'bio'. As a developer I'd describe myself to be even far beyond an amateur or beginner. Auto-didactically started coding in Swift in late 2017. HTML/CSS since 2002 though.
Thank you! I guess this is far above my skill level. I'm already out on "event monitor" and "instance variable", "fresh controller instance". Funny thing: the moment you sent the reply I was on that exact same site of Ray Wenderlich to find out how to use an image as menu bar icon instead of that mathematical "sum" icon I had used before. I think I will keep learning stuff and get back to this in about 5 years, haha :D
But again: thank you very much. It's doesn't happen too often that people just offer their time to help out in this huge form. And: it shows that there will be a solution in the future when I'm able to understand these things and alter the necessary "things" (Objects? Items? I have no idea at all! :D)
There's no such thing... 'bio'. As a developer I'd describe myself to be even far beyond an amateur or beginner. Auto-didactically started coding in Swift in late 2017. HTML/CSS since 2002 though.
Thanks. And... I'm a fool. I stopped reading the page at "Bet youβre feeling smarter already!" with that little fella flexing. Thought "OK, page end" and quit. I reopened the site to re-read a detail I had missed and saw that there's an entire "Event Monitoring" part following that little dude. NOW I know what you were talking about in the comments of your code.
Well... time for an awkward confession: I'm learning Swift since late 2017 and I even have a few iOS Apps on the App Store. But I am totally new to that thing on macOS. It's very similar in the basics but there're many differences in actions and objects and members of objects etc.
I like Swift a lot. I always like things that show your progress immediately (or tell you what or THAT something went wrong). And as a Mac and iPhone user I can do things for myself. Things I need or always wanted. Yes, it's great. And the day will come when this super small app is done :D
Yeah MacOS is a completely different beast! and unfortunately it's really hard to find good tutorials and resources for it which makes it that much harder to build for.
Not only that, you'll often have to rely on old APIs that are really awful to write/read. iOS is much nicer in that sense!
Glad you've got the event monitoring stuff figured out π
There's no such thing... 'bio'. As a developer I'd describe myself to be even far beyond an amateur or beginner. Auto-didactically started coding in Swift in late 2017. HTML/CSS since 2002 though.
Not only figured that out. I got the shortcut thing AND the window/popup issues sorted out and finally both up and running now. And that's only because of your help. Thank you. Again and again :)
There are only 2-3 little things left.
Another good thing is that not everything is different from iOS. I just learned that UserDefaults work the exact same way. At least this was a hattrick scored within seconds, hehe.
Phew... by the way: if you don't understand any things I write feel free to ask. Just a German digging in pale memories of English lessons from over 25 years ago, haha.
Is the settings window supposed to be a popover as well? Or is that just a separate window that opens when the setting button is clicked?
I would try
1) Adding a popover instance to the AppDelegate
2) Add the main view controller instance to the new popover in the
applicationDidFinishLaunching
methodSomething like this (This is untested!)
Mildly related, I recommend reading this Ray Wenderlich tutorial on menu bar apps, it helped me understand them better - raywenderlich.com/450-menus-and-po...
Thank you! I guess this is far above my skill level. I'm already out on "event monitor" and "instance variable", "fresh controller instance". Funny thing: the moment you sent the reply I was on that exact same site of Ray Wenderlich to find out how to use an image as menu bar icon instead of that mathematical "sum" icon I had used before. I think I will keep learning stuff and get back to this in about 5 years, haha :D
But again: thank you very much. It's doesn't happen too often that people just offer their time to help out in this huge form. And: it shows that there will be a solution in the future when I'm able to understand these things and alter the necessary "things" (Objects? Items? I have no idea at all! :D)
My pleasure Patrick,
The trick is to keep pushing yourself to try different things in your code. The fact that you're already doing this is a great start.
I hope you continue to pursue Swift. It's truly a wonderful language, especially with the new SwiftUI stuff that's arrived recently!
Thanks. And... I'm a fool. I stopped reading the page at "Bet youβre feeling smarter already!" with that little fella flexing. Thought "OK, page end" and quit. I reopened the site to re-read a detail I had missed and saw that there's an entire "Event Monitoring" part following that little dude. NOW I know what you were talking about in the comments of your code.
Well... time for an awkward confession: I'm learning Swift since late 2017 and I even have a few iOS Apps on the App Store. But I am totally new to that thing on macOS. It's very similar in the basics but there're many differences in actions and objects and members of objects etc.
I like Swift a lot. I always like things that show your progress immediately (or tell you what or THAT something went wrong). And as a Mac and iPhone user I can do things for myself. Things I need or always wanted. Yes, it's great. And the day will come when this super small app is done :D
Thanks for motivation - I'll keep practicing :)
Yeah MacOS is a completely different beast! and unfortunately it's really hard to find good tutorials and resources for it which makes it that much harder to build for.
Not only that, you'll often have to rely on old APIs that are really awful to write/read. iOS is much nicer in that sense!
Glad you've got the event monitoring stuff figured out π
Not only figured that out. I got the shortcut thing AND the window/popup issues sorted out and finally both up and running now. And that's only because of your help. Thank you. Again and again :)
There are only 2-3 little things left.
Another good thing is that not everything is different from iOS. I just learned that UserDefaults work the exact same way. At least this was a hattrick scored within seconds, hehe.
Phew... by the way: if you don't understand any things I write feel free to ask. Just a German digging in pale memories of English lessons from over 25 years ago, haha.
That's great to hear! And yeah being able to share code between iOS and MacOS is really handy :).
Don't worry your English seems pretty good to me, I didn't even noticed until the screenshot you sent had German in it, haha :D.