-
Notifications
You must be signed in to change notification settings - Fork 580
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
Add a non-blocking variant of hs.image.imageFromURL #2224
Comments
Wow thanks so much for the help! I'll try that as soon as I have time today and report back. :) Edit: Works perfectly, thank you! |
- Fix blocking nature of URL entity selection modal by using the `hs.image.imageFromURL` callback API (Hammerspoon/hammerspoon#2224) - Fix `URL.getDomain` to get domains from URLs without a www subdomain
@andweeb @latenitefilms Do you guys mind telling me how to use the callback in this case so I can get the icons of the URL asynchronously? Basically, I'm pulling the tabs from Safari and their URL's into a chooser, pretty much looks like the gif above. As mentioned by @andweeb, user experience is not great if using This is a sample code of the chooser: tabs_chooser:choices(function()
local _, result = hs.osascript.applescript(tabsScript)
local lines = {}
for line in string.gmatch(result, "(.-)\n") do
table.insert(lines, line)
end
local tabs = {}
for _, tab in pairs(lines) do
local winID, tabNumber, tabTitle, url = string.match(tab, "(.*);(.*);(.*);(.*)")
-- local img = hs.image.imageFromURL("https://www.google.com/s2/favicons?sz=64&domain_url=" .. url)
local img = hs.image.imageFromURL("https://www.google.com/s2/favicons?sz=64&domain_url=" .. url, function() end)
table.insert(tabs, {
text = tabTitle,
subText = url,
win = winID,
tab = tabNumber,
image = img,
})
end
return tabs
end)
hs.hotkey.bind("alt", "'", function()
tabs_chooser:refreshChoicesCallback(true)
tabs_chooser:show()
end) Thanks in advance! |
Here's an example: print("before")
hs.image.imageFromURL("https://latenitefilms.com/media/2013/09/latenitefilms_text.png", function(img)
print(string.format("Here's your image: %s", img))
end)
print("after") This will output: 2022-08-05 11:50:09: before
2022-08-05 11:50:09: after
2022-08-05 11:50:09: Here's your image: hs.image: (0x600000b9ddb8) Callback's obviously happen asynchronously, so you'd need to update your chooser results AFTER the image callback is triggered. |
Thanks for your reply @latenitefilms. OK, so I can use the fetched image in the callback as a parameter. Great! However, how can I update the chooser results while the chooser is visible (i.e, after being triggered with |
My use case in particular falls within the context of
hs.chooser
choices, each choice item needing an icon image from an external URL as shown in the gif below.Due to the blocking nature of
hs.image.imageFromURL
, using it blocks not only the chooser modal itself but the entire application in general, which isn't a great experience. For example attempting to load http://deelay.me/20000/https://i.imgur.com/2lrs7zF.jpg (simulated delay of 20s) blocks the whole app for the full 20 seconds.It'd be awesome if there were a way to fetch images from URLs without blocking the main application thread.
The text was updated successfully, but these errors were encountered: