Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua

Home Page:http://www.hammerspoon.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

switcher.ui.highlight rounded corners?

R-7200 opened this issue · comments

Good evening!

Is it possible to include rounded corners for switcher.ui.highlight? Right now I'm stuck with the following appearance:

Bildschirmfoto 2023-12-16 um 17 51 07

I'd be glad about mentioned improvement - have a nice weekend, all of you :-)

Best regards.

It doesn't look like there's currently any official way to make those corners rounded, but it could probably be added.

As a workaround for now, if you're okay poking around inside hs.window.switcher's implementation details, you can get the hs.drawing object being used to draw the highlight rectangle and set its corner radius manually using the setRoundedRectRadii method:

ws = hs.window.switcher.new( --[[ whatever arguments ]] )
-- any ws.ui configuration goes here
local highlightRadius = 10 -- or whatever
ws.drawings.highlightRect:setRoundedRectRadii(highlightRadius, highlightRadius)

Make sure you finish modifying that switcher's ui object before you set the radius. Any changes to ws.ui will cause the highlight drawing to be recreated and lose that change. (Changes to the global hs.window.switcher.ui object won't do that, but they also won't properly update switchers you've already created.)

If you want to avoid losing the radius whenever you change `ws.ui`…

It's possible, but requires messing with hs.window.switcher's internals even more using Lua's debug functions:

local function findUpvalue(f, name)
	local i = 0
	local n, v
	repeat
		i = i + 1
		n, v = debug.getupvalue(f, i)
	until not n or n == name
	if not n then return nil end
	return i, v
end

local setUiPrefsUpvalueIndex, oldSetUiPrefs = findUpvalue(hs.window.switcher.new, 'setUiPrefs')
if not setUiPrefsUpvalueIndex then
	error "Couldn't find hs.window.switcher's setUiPrefs function"
end
debug.setupvalue(hs.window.switcher.new, setUiPrefsUpvalueIndex, function(self)
	oldSetUiPrefs(self)
	local highlightRadius = self.ui.highlightRadius or 0
	self.drawings.highlightRect:setRoundedRectRadii(highlightRadius, highlightRadius)
end)

-- Now you can do:
hs.window.switcher.ui.highlightRadius = 10 -- or whatever
ws = hs.window.switcher.new( --[[ whatever arguments ]] )

-- or for a specific switcher:
ws.ui.highlightRadius = 15 -- or whatever
If you want to make it match the size of the existing rounded corners…

There are three radii that are computed and used by hs.window.switcher, so it depends which one you're trying to match:

  • ui.titleRectRadius: Used for the window titles in the main switcher area. Defined as 1/4 of ui.textSize (which defaults to 16, making this radius 4).
  • selectedRectRadius: Used for the large preview box for the selected window, as well as the title box at the top of that preview. Defined as 1/4 of the height of an uppercase O in the selected-window title font (with the odd effect of not rounding those corners if you turn that title off). With default settings, that seems to work out to 8.
  • padding: Used for the corners of the switcher itself. Also has some non-corner-radius uses: It's the amount of space between two thumbnails, or between the first/last thumbnail and the corresponding edge of the switcher. Defined in a complicated way based on ui.thumbnailSize, the number of windows, and the available screen width. If you're trying to match the switcher's corners, it will probably take a more involved patch to actually find that value.

Hi!

Works as described :-) I mistook the declaration of

switcher = hs.window.switcher.new()

for a general function (switcher.ui.highlight). Sorry for that - I was tired...

Thank you for clarifying; marked as solved!

Have a nice day!