Reposition webview upon each showing. Added a few comments.

This commit is contained in:
Markus Birth 2021-12-26 21:22:40 +00:00
parent b135be0f01
commit c3362579f4

View File

@ -11,24 +11,31 @@ obj.version = "0.1"
obj.author = "Markus Birth <markus@birth-online.de>" obj.author = "Markus Birth <markus@birth-online.de>"
function obj:init() function obj:init()
self.webView = hs.webview.new({x=0, y=0, w=900, h=716}) -- prepare variable for menubar item/icon
self.mbIcon = nil
-- prepare variable for media key handler, but don't assign just yet
self.eventTap = nil self.eventTap = nil
-- prepare logger
self.log = hs.logger.new(self.name, "verbose") self.log = hs.logger.new(self.name, "verbose")
end end
function obj:start() function obj:start()
self.log.v('Start') self.log.v('Start')
-- create global webview to keep it running/playing the whole time
self.webView = hs.webview.new()
self.webView:allowNewWindows(false) self.webView:allowNewWindows(false)
self.webView:windowCallback(self.webViewHide)
self.webView:navigationCallback(self.webViewMods) self.webView:navigationCallback(self.webViewMods)
-- self.webView:magnification(1)
self.webView:url("https://www.bbc.co.uk/sounds/play/live:bbc_radio_one_dance") self.webView:url("https://www.bbc.co.uk/sounds/play/live:bbc_radio_one_dance")
-- self.webView:magnification(1)
-- load menubar icon and prepare menubar item
local iconImage = hs.image.imageFromPath("Spoons/BbcSounds.spoon/icon_16.png") local iconImage = hs.image.imageFromPath("Spoons/BbcSounds.spoon/icon_16.png")
self.mbIcon = hs.menubar.new() self.mbIcon = hs.menubar.new()
self.mbIcon:setIcon(iconImage) self.mbIcon:setIcon(iconImage)
self.mbIcon:setClickCallback(self.showHide) self.mbIcon:setClickCallback(self.showHide)
local mbPos = self.mbIcon:frame()
local newPos = hs.geometry.point(mbPos.y, mbPos.x) -- create eventtap for handling media keys
self.webView:topLeft(newPos)
self.eventTap = hs.eventtap.new({hs.eventtap.event.types.systemDefined}, self.handleMediaKey) self.eventTap = hs.eventtap.new({hs.eventtap.event.types.systemDefined}, self.handleMediaKey)
self.eventTap:start() self.eventTap:start()
self.log.v('Startup complete.') self.log.v('Startup complete.')
@ -58,6 +65,13 @@ function obj.handleMediaKey(event)
return delete, nil return delete, nil
end end
function obj.webViewHide(action, webview, state)
obj.log.v("webViewHide: " .. action)
if action == "focusChange" and not state then
obj.webView:hide(0.3)
end
end
function obj.webViewMods(action, webView, navID, error) function obj.webViewMods(action, webView, navID, error)
if action == "didFinishNavigation" then if action == "didFinishNavigation" then
obj.hideHeader() obj.hideHeader()
@ -65,6 +79,7 @@ function obj.webViewMods(action, webView, navID, error)
end end
function obj.hideHeader() function obj.hideHeader()
-- Hide top navbar on BBC page
obj.webView:evaluateJavaScript("document.getElementById(\"orb-banner\").style.display = \"none\"") obj.webView:evaluateJavaScript("document.getElementById(\"orb-banner\").style.display = \"none\"")
end end
@ -84,8 +99,23 @@ function obj.showHide(modifierKeys)
if obj.webView:isVisible() then if obj.webView:isVisible() then
obj.webView:hide(0.3) obj.webView:hide(0.3)
else else
-- get position of menubar item and move webview there, then show it
local mbPos = obj.mbIcon:frame()
mbPos.w = 900
mbPos.h = 716
-- check if out of bounds
local desktopFrame = hs.window.desktop():frame()
if mbPos.x + mbPos.w > desktopFrame.w then
obj.log.w("webView width larger than desktop. Adjusting position.")
mbPos.x = desktopFrame.w - mbPos.w
end
if mbPos.y + mbPos.h > desktopFrame.h then
obj.log.w("webView height larger than desktop. Adjusting position.")
mbPos.y = desktopFrame.h - mbPos.h
end
obj.webView:frame(mbPos)
obj.webView:show(0.3) obj.webView:show(0.3)
obj.webView:bringToFront(true) obj.webView:bringToFront()
end end
end end