Update PushToTalk spoon to check list of apps when starting

These changes have been submitted as a PR against the main Spoons
repository here: https://github.com/Hammerspoon/Spoons/pull/169
This commit is contained in:
2020-05-06 20:40:19 +01:00
parent 55f96f74ca
commit 2ff0508f13
3 changed files with 32 additions and 0 deletions

View File

@@ -47,6 +47,15 @@
"signature": "PushToTalk.app_switcher",
"stripped_doc": "For example this `{ ['zoom.us'] = 'push-to-talk' }` will switch mic to `push-to-talk` state when Zoom app starts.",
"type": "Variable"
},
{
"def": "PushToTalk.detect_on_start",
"desc": "Check running applications when starting PushToTalk.",
"doc": "Check running applications when starting PushToTalk.\nDefaults to false for backwards compatibility. With this disabled, PushToTalk will only change state when applications are launched or quit while PushToTalk is already active. Enable this to look through list of running applications when PushToTalk is started. If multiple apps defined in app_switcher are running, it will set state to the first one it encounters.",
"name": "detect_on_start",
"signature": "PushToTalk.detect_on_start",
"stripped_doc": "Defaults to false for backwards compatibility. With this disabled, PushToTalk will only change state when applications are launched or quit while PushToTalk is already active. Enable this to look through list of running applications when PushToTalk is started. If multiple apps defined in app_switcher are running, it will set state to the first one it encounters.",
"type": "Variable"
}
],
"desc": "Implements push-to-talk and push-to-mute functionality with `fn` key.",

View File

@@ -31,12 +31,19 @@ obj.defaultState = 'unmute'
obj.state = obj.defaultState
obj.pushed = false
--- PushToTalk.app_switcher
--- Variable
--- Takes mapping from application name to mic state.
--- For example this `{ ['zoom.us'] = 'push-to-talk' }` will switch mic to `push-to-talk` state when Zoom app starts.
obj.app_switcher = {}
--- PushToTalk.detect_on_start
--- Variable
--- Check running applications when starting PushToTalk.
--- Defaults to false for backwards compatibility. With this disabled, PushToTalk will only change state when applications are launched or quit while PushToTalk is already active. Enable this to look through list of running applications when PushToTalk is started. If multiple apps defined in app_switcher are running, it will set state to the first one it encounters.
obj.detect_on_start = false
local function showState()
local device = hs.audiodevice.defaultInputDevice()
local muted = false
@@ -97,6 +104,20 @@ local function eventTapWatcher(event)
showState()
end
local function initialState()
local apps = hs.application.runningApplications()
for i, app in pairs(apps) do
for name, state in pairs(obj.app_switcher) do
if app:name() == name then
return state
end
end
end
return obj.defaultState
end
--- PushToTalk:init()
--- Method
--- Initial setup. It's empty currently
@@ -116,6 +137,7 @@ function obj:start()
obj.menubar = hs.menubar.new()
obj.menubar:setMenu(obj.menutable)
if obj.detect_on_start then obj.state = initialState() end
obj.setState(obj.state)
end