Sample app and underlying machinery - my observations #1
Replies: 4 comments 4 replies
-
For this particular library, 95% of it I had to hack out on my own. It took days to find solutions and most of it came from hunches from past experience. As far as NSStatusItem or MenuBarExtra, there isn't much out there apart from Apple's docs. Google is scant to say the least. Which is why I decided to take the deep dive and work out some of the missing functionality.
That's tricky and may only be possible with NSStatusItem currently. MenuBarExtra is very strict in limiting the menu bar button to being an image, image and text, or text. No custom UI or formatting is allowed.
That might happen if you left its index unchanged. The indexes must be supplied starting from 0 in the order in which the MenuBarExtras are listed. If there's only one MenuBarExtra, you can omit the index because it defaults to 0. For two or more MenuBarExtra, you need explicit indexes for them. As an aside: Within the library, ID and Index are separate things. ID was an experiment that I left in while trying to identify a MenuBarExtra/NSStatusItem instance with a String, but there is no trivial way to achieve it (thus far). So it's not actually used. I decided to settle on Index because it was a little more reliable.
Not sure what you're referring to here. As I mentioned, you have to supply the index. I would love it to be more automatic, but it's the simplest I could get it while maintaining compatibility with multiple MenuBarExtra.
I've never seen this, so not sure what to make of it. |
Beta Was this translation helpful? Give feedback.
-
Right. Silly I. To the right of my thing you can see an another item. It has icon, two lines of text and a tooltip. And is updating all the time. |
Beta Was this translation helpful? Give feedback.
-
Just further to this - if you want to get dangerous and poke around at the NSStatusItem, I have pushed changes to See this post for details: |
Beta Was this translation helpful? Give feedback.
-
My first impression after looking at the code is WOW! I learned a lot how the problems can be solved, far beyond my intuition and imagination 😢. Well, I seriously started to learn Swift/SwiftUI just two months ago... I wonder where is all that knowledge coming from. Apple documentation is, let's say, poor. StackOverflow? Or just Google? Cannot believe, one must first know what to look for. For example I didn't know that window has something like class name, that its descendants can be iterated and so on.
Working on some app for macOS, that shall live in MenuBar, I found two ways: working directly with (1) NSStatusBar (old school) or use (2) MenuBarExtra. I want to have in StatusItem something more complicated than just an Image, and so I have come to this Project. Thanks Google, of course. I've found an example how to attach a View to the button, dealing with NSStatusItem directly but SwiftUI hides it, as we know. So, I started to play with Demo app accompanying all the machinery. I removed all the StatusItems but the third - and it stopped working. Digging deeper I noticed the Id of StatusItem is used only to distinguish between things created. The question is, which app could possibly want to have many of them? Apples say StatusItem is a scarce thing, many of them is a StatusBar orgy. Maybe I lack the imagination, but cannot guess a proper use of the feature (except, obviously, the Demo app).
This piece of code somehow confirms the idea:
/// Returns the underlying status items created by
MenuBarExtrafor the ///
MenuBarExtrawith the specified index. /// /// Each
MenuBarExtracreates one status item. /// /// If the
isInsertedbinding on a
MenuBarExtra` is set to false, it may not return a status/// item. This may also change its index.
static func statusItem(for ident: StatusItemIdentity? = nil) -> NSStatusItem? {
let statusItems = statusItems
`
The last 3 lines of above code exhibit the dependency on assumed numbering of the items, starting from 0, increment 1. When there's only 3, item will never be found. BTW having only one MenuBarExtra I got two status items in the array. Suspicious, isn't it?
Anyhow, I changed Id to 0 and it works like a charm. See screen recording below.
Screen.Recording.2023-04-12.at.14.12.26.mov
If you are interested in my progress, I will continue posting in this thread.
Once again, chapeaux bas! Your work is highly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions