Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
11.96k stars 579 forks source link

Support ASCII: style hs.menubar:setIcon() in hs.menubar:setMenu() menu table #2659

Open bviefhues opened 3 years ago

bviefhues commented 3 years ago

hs.menubar.setIcon() supports "a string beginning with ASCII: which signifies that the rest of the string is interpreted as a special form of ASCII diagram, which will be rendered to an image and used as the icon." This is especially useful since it takes care of light mode vs dark mode rendering.

hs.menubar.setMenu() does support images for menu items in a menu table. However, for ASCII images, this requires piping though hs.image.imageFromASCII(). And that requires a light/dark mode specificcontext to adapt image to light or dark mode.

Would be great if menu table would support the same icon functionality as setIcon() does.

asmagill commented 3 years ago

I assume you mean for individual menu items within the menu? That's a little harder than modifying new and setIcon... in the later case, we can easily wrap the functions on the lua side and easily check the image argument and if it's a string pass it through hs.image.fromASCII for you... in the case of the menu table, it's passed through unmodified (or returned by a function) and isn't actually parsed until the menu itself opens... it's a good idea, though, so I'll give it some thought and see what might be the best way to achieve it.

bviefhues commented 3 years ago

@asmagill Indeed, for individual menu items within the menu. My use case is showing a mode-dependent icon on the menubar and showing the modes as menu items, with icons attached.

I found a workaround: render the icon on the menubar and grab the image.

local function iconFromASCII(ascii)
    local menubar = hs.menubar:new(false):setIcon(ascii)
    local icon = menubar:icon() -- hs.image object
    menubar:delete()
    return icon
end

The returned image fits to the light/dark mode OS setting.