Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] Ability to have hs.menubar items retain their same position upon startup/hs.reload (or at least next to the "main" Hammerspoon menubar item) rather than always reverting back to the left-most item #2878

Closed
dmattera opened this issue May 18, 2021 · 15 comments · Fixed by #3195

Comments

@dmattera
Copy link

Not sure this is even possible given the way Hammerspoon utilizes the APIs for hs.menubars/how the system behaves but just thought I'd raise the question. This is something that would benefit me and my personal workflow quite significantly as I utilize multiple hs.menubar items heavily and have a lot of non-HS items which means they aren't all always visible and so every time I reload, I have to move them all back to an always-visible section of menubar one by one.

Steps to reproduce:

  1. Create and show a new hs.menubar item
  2. Said menubar item will be placed at the end (left-most side) of all the existing menubar items
  3. Cmd+click/drag the new item to a new position in the menubar
  4. Perform hs.reload() or restart Hammerspoon enitrely
  5. The hs.menubar item is placed back at the end of existing menubar items (unlike the "main" Hammerspoon menubar which, if you restarted Hammerspoon enitrely, will return to the position it occupied before the restart)
@DemoorBug
Copy link

Same problem, it resets to the leftmost side every time it starts, very annoying

@jeffheartmp
Copy link

bump on this!

@latenitefilms
Copy link
Contributor

latenitefilms commented Apr 21, 2022

This might be helpful?

https://github.com/shergin/NSStatusBar-MISSINGOrder

EDIT: looks like Hammerspoon already had ability to set the "priority" but this no longer works on Catalina and above.

https://apple.stackexchange.com/a/399310

Mortennn/Dozer#69

@latenitefilms
Copy link
Contributor

Maybe the issue is as simple as just not supplying the menubar items an autosavename?

https://developer.apple.com/documentation/appkit/nsstatusitem/1644022-autosavename

@asmagill
Copy link
Member

We don't use autosaveName for menus, though we do for toolbar buttons.

We can't change menu position programmatically anymore -- that was removed in Catalina.

I do wonder if setting an autosaveName for the statusItem would remember position information... I can't think of a whole lot else that it might be saving... If you can't get to it sooner, I may try this weekend and see if I can add support for it so we can test it out.

@latenitefilms
Copy link
Contributor

My guess is that it'll most likely fix things, as without an autosaveName, it won't be able to keep track of user-created menubar items. I'll try test it out today.

latenitefilms added a commit to latenitefilms/hammerspoon that referenced this issue Apr 22, 2022
- Added an optional `autosaveName` parameter to `hs.menubar.new()`, which allows you to set a unique identifier for the menubar, allowing it to save and restore it's menubar position across reloads.
- Added a method to get and set the `autosaveName` -`hs.menubar:autosaveName()`.
- Removed `hs.menubar.newWithPriority()` as it no longer works on macOS Catalina (and the minimum system requirements for Hammerspoon are now Catalina & above).
- Closes Hammerspoon#2878
@latenitefilms
Copy link
Contributor

I've just added a pull request (#3195) which seems to do the trick.

@jeffheartmp
Copy link

jeffheartmp commented Apr 22, 2022

Hey @latenitefilms thank you for doing this, really cool stuff.

I checked out your branch, compiled Hammerspoon.app in Xcode, moved it to Applications folder, allowed permissions, then updated my .hammerspoon code to include this:

caffeine = hs.menubar.new()

function setCaffeineDisplay(state)
    --add the position on each click
    caffeine:autosaveName('caffeine');
    
    if state then
        caffeine:setTitle("U")      
    else 
        caffeine:setTitle("D")
    end
end

function caffeineClicked()
    setCaffeineDisplay(hs.caffeinate.toggle("displayIdle"))
end

After I move the icon, I click it to trigger the autosaveName method.

But after I reload hammerspoon, it shoots back to the left again.

Any ideas?

@latenitefilms
Copy link
Contributor

Try setting the autosaveName when creating the menubar instead, as opposed to using the autosaveName method. I might need to tweak the method if it's not working as intended.

@latenitefilms
Copy link
Contributor

I've just pushed a fix for the hs.menubar:autosaveName() method.

Here's some example code for testing:

a = hs.menubar.new(true, "A"):setTitle("A")
b = hs.menubar.new(true, "B"):setTitle("B")
c = hs.menubar.new(true):autosaveName("C"):setTitle("C")
z = hs.menubar.new(true):setTitle("Z") -- this won't save position

To test, run the above code in the Console, change the menubar icon positions, restart Hammerspoon, then run the code again.

@jeffheartmp
Copy link

When I try to build the hammerspoon app in Xcode I get this error:

WARN: Function/Method/Constructor description for hs.menubar:autosaveName([name]) should be a single line. Other content may belong in the Notes: section.

@jeffheartmp
Copy link

I've just pushed a fix for the hs.menubar:autosaveName() method.

Here's some example code for testing:

a = hs.menubar.new(true, "A"):setTitle("A")
b = hs.menubar.new(true, "B"):setTitle("B")
c = hs.menubar.new(true):autosaveName("C"):setTitle("C")
z = hs.menubar.new(true):setTitle("Z") -- this won't save position

To test, run the above code in the Console, change the menubar icon positions, restart Hammerspoon, then run the code again.

This worked on the last build that I made, thank you for the fix!

@latenitefilms
Copy link
Contributor

Opps - I've fixed the documentation bug. Thanks!

cmsj pushed a commit that referenced this issue Apr 23, 2022
- Added an optional `autosaveName` parameter to `hs.menubar.new()`, which allows you to set a unique identifier for the menubar, allowing it to save and restore it's menubar position across reloads.
- Added a method to get and set the `autosaveName` -`hs.menubar:autosaveName()`.
- Removed `hs.menubar.newWithPriority()` as it no longer works on macOS Catalina (and the minimum system requirements for Hammerspoon are now Catalina & above).
- Closes #2878
@kusazh
Copy link

kusazh commented Jul 12, 2022

This doesn't work in the latest release. Was it included in release?

@dmattera
Copy link
Author

This doesn't work in the latest release. Was it included in release?

It was not included

Commit was pushed about a month after the latest release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants