Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically update NSMenuItem from an NSTimer while the menu is currently open in swift?

So here's my situation, I have a little app that will keep track of how long you have been working and let you know when it's time to take a break. I have an NSTimer that fires every second and updates some numbers (the remaining time). The app lives in the status bar, and I want the remaining time to be show in a menu item from the status menu dropdown menu. Right now I have it Updating to an NSMenuItem and it works but when you click on the icon in the menu bar to get at the menu, the time won't update dynamically while you're looking at it. If you change focus and then go back to the menu, the time has kept correctly, but again, when the menu is open it doesn't update.

Is there anyway I can get that MenuItem to update even while the menu is open in swift? There are several other threads with objective C solutions but I haven't been able to adapt them for swift.

like image 443
Spencer Wood Avatar asked Jan 11 '15 08:01

Spencer Wood


1 Answers

You have to add your Timer to NSRunLoop.

NSRunLoop.currentRunLoop().addTimer(yourTimer, forMode: NSRunLoopCommonModes)
like image 199
ixany Avatar answered Oct 29 '22 17:10

ixany