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

hs.alert.show: how to place this alert on different place on screen (center?) #3541

Closed
muescha opened this issue Sep 29, 2023 · 8 comments
Closed

Comments

@muescha
Copy link
Contributor

muescha commented Sep 29, 2023

How can I position the 'hs.alert.show' alert at a different location on the screen?

I'd prefer to have it centered within a child window of an app that's not in full-screen mode.

@latenitefilms
Copy link
Contributor

There's no way to change position (aside from choosing a screen) currently in the extension, but the extension is all Lua, so you could copy and modify as needed:

local showAlert = function(message, image, style, screenObj, duration)

@muescha
Copy link
Contributor Author

muescha commented Sep 29, 2023

thanks for the hint. i tried to fake an screen object from a window object to get it to the alert as "Screenobject" - the only thing missing was the :fullFrame()

local screenFrame = screenObj:fullFrame()

but this patch from chat gpt not working :(

	local win = hs.window.focusedWindow()
	local extendedWindowMetatable = {
        __index = {
            fullFrame = function(self)
                return self:frame()
            end
        }
    }

    setmetatable(win, extendedWindowMetatable)

	hs.alert.show("Hello World!", win)

i have done it with this patch in alert.lua:

-   local screenFrame = screenObj:fullFrame()
+   local screenFrame = screenObj.frame and screenObj:frame() or screenObj:fullFrame()

muescha/dot_hammerspoon@7a95c28

now i can call it with

	hs.alert.show("Hello World!", win)

@muescha
Copy link
Contributor Author

muescha commented Sep 29, 2023

one idea was to allow a hs.geomety object as parameter, but this was not possible to test with getmetatable(frame) == hs.getObjectMetatable('hs.geometry')

@muescha
Copy link
Contributor Author

muescha commented Sep 30, 2023

a little bug: :frame() exists also on the screen so I have to test for .allFrame:

-   local screenFrame = screenObj.frame and screenObj:frame() or screenObj:fullFrame()
+   local screenFrame = screenObj.fullFrame and screenObj:fullFrame() or screenObj:frame()

@muescha
Copy link
Contributor Author

muescha commented Sep 30, 2023

opened:

@cmsj
Copy link
Member

cmsj commented Aug 6, 2024

PR is merged, so I'm going to close this.

@cmsj cmsj closed this as completed Aug 6, 2024
@Rhys-T
Copy link

Rhys-T commented Aug 6, 2024

one idea was to allow a hs.geomety object as parameter, but this was not possible to test with getmetatable(frame) == hs.getObjectMetatable('hs.geometry')

For what it's worth: hs.getObjectMetatable seems to only work for modules that put their metatables into the registry, which currently means just the ones that are implemented in native code/Objective-C. hs.geometry is written in pure Lua and doesn't register itself, so hs.getObjectMetatable('hs.geometry') just returns nil. However, hs.geometry actually uses the module itself as the metatable, so you can instead do getmetatable(frame) == hs.geometry. I don't know if that fact is considered 'public API' though, so if you'd prefer, you can do:

local geometryMT = getmetatable(hs.geometry '0x0')
if getmetatable(frame) == geometryMT then
	--
end

@asmagill
Copy link
Member

asmagill commented Aug 6, 2024

FWIW, lua only code can add its metatable to the registry so hs.getObjectMetatable works with the following code:

debug.getregistry()[userdata_tag] = metatable

Then, hs.getObjectMetatable(userdata_tag) will work; it also means that we can assign the metatable from C/Swift side code, even though the metatable is pure lua code.

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

No branches or pull requests

5 participants