release / linux-x64 (push) Failing after 59s
ci / linux-x64 (push) Failing after 1m9s
ci / linux-arm64-cross (push) Failing after 4m15s
release / linux-arm64 (push) Successful in 4m36s
release / appimage-startup (push) Skipped
release / publish (push) Skipped
149 lines
7.3 KiB
JavaScript
149 lines
7.3 KiB
JavaScript
// Runs key binding actions on the app's real operations (official hotkey handler, main.js 6.0.0 @2283500).
|
||
// Nothing here clicks DOM controls: audio state and disconnect go through hooks the shell (app.js) supplies,
|
||
// everything else calls the module that owns the operation.
|
||
//
|
||
// Lifecycle per binding: down(binding, scope) once, then up(id, { synthetic }). `hold` actions end on any up;
|
||
// `trigger` actions run only on a real (non-synthetic) up, so focus loss, a rebind or a crashed helper
|
||
// never toggles anything; `press` actions run on down.
|
||
import { ACTION_KIND } from './keybind-catalog.js';
|
||
import { callOn } from './core.js';
|
||
import { connectionList, inputConnection, viewedConnection, isConnected, ownClient } from './state.js';
|
||
import { setPtt, setInputDeactivated } from './voice.js';
|
||
import { pressWhisper, releaseWhisper } from './whisper.js';
|
||
import { openBroadcast } from './broadcast.js';
|
||
import { recordingState, startRecording, stopRecording, togglePause } from './recording.js';
|
||
import { openAdminAction } from './admin.js';
|
||
import { toast } from './toast.js';
|
||
import { errorText } from './text.js';
|
||
|
||
/** Official FindAudioInputServerConnectionHandler, else the server on screen. */
|
||
const audioConnection = () => inputConnection() ?? viewedConnection();
|
||
/** Official recording target: focusedServerConnectionHandler ?? FindAudioInputServerConnectionHandler. */
|
||
const focusedConnection = () => viewedConnection() ?? inputConnection();
|
||
|
||
/**
|
||
* hooks.setSelfAudio(key: 'input_muted'|'output_muted', value: boolean|'toggle') – all servers, also the
|
||
* default for servers opened later (official setCaptureState / setPlaybackState); 'toggle' flips that default.
|
||
* hooks.disconnect(conn) – the shell's disconnect (same path as the toolbar Disconnect button).
|
||
*/
|
||
export function createKeybindActions(hooks) {
|
||
const held = new Map(); // binding id → { binding, scope }
|
||
|
||
const run = (work) => {
|
||
Promise.resolve().then(work).catch((err) => toast(() => errorText(err), 'error'));
|
||
};
|
||
|
||
const awayReference = () => Boolean(ownClient(audioConnection())?.away);
|
||
|
||
async function setAway(away) {
|
||
// Official setAwayState applies to every server connection.
|
||
await Promise.all(connectionList().filter((c) => isConnected(c) && Boolean(ownClient(c)?.away) !== away)
|
||
.map((c) => callOn(c.id, 'set_away', { away })));
|
||
}
|
||
|
||
async function requestTalkPower() {
|
||
const conn = audioConnection();
|
||
if (!isConnected(conn)) return;
|
||
const me = ownClient(conn);
|
||
if (!me || me.is_talker || me.talk_request) return;
|
||
await callOn(conn.id, 'talk_request', { request: true });
|
||
}
|
||
|
||
/** Grants talk power to the longest-waiting requester in the own channel (Server Info → talk queue path). */
|
||
async function grantNextTalkPower() {
|
||
const conn = audioConnection();
|
||
if (!isConnected(conn)) return;
|
||
const snap = conn.snap;
|
||
const next = snap.clients
|
||
.filter((c) => c.channel_id === snap.own_channel_id && c.id !== snap.own_client_id && c.talk_request && !c.is_talker && c.uid)
|
||
.sort((a, b) => (a.talk_request.time_unix ?? Infinity) - (b.talk_request.time_unix ?? Infinity))[0];
|
||
if (!next) return;
|
||
const context = await callOn(conn.id, 'admin_context', {});
|
||
if (!Number.isSafeInteger(context?.generation) || context.generation < 1) return;
|
||
await callOn(conn.id, 'talk_power', { generation: context.generation, client_id: next.id, uid: next.uid, grant: true });
|
||
}
|
||
|
||
function trigger(action) {
|
||
switch (action) {
|
||
case 'microphone_toggle': return run(() => hooks.setSelfAudio('input_muted', 'toggle'));
|
||
case 'microphone_mute': return run(() => hooks.setSelfAudio('input_muted', true));
|
||
case 'microphone_unmute': return run(() => hooks.setSelfAudio('input_muted', false));
|
||
case 'speaker_toggle': return run(() => hooks.setSelfAudio('output_muted', 'toggle'));
|
||
case 'speaker_mute': return run(() => hooks.setSelfAudio('output_muted', true));
|
||
case 'speaker_unmute': return run(() => hooks.setSelfAudio('output_muted', false));
|
||
case 'away_toggle': return run(() => setAway(!awayReference()));
|
||
case 'away_online': return run(() => setAway(false));
|
||
case 'away_set': return run(() => setAway(true));
|
||
case 'away_toggle_message':
|
||
// Going away needs the message: the existing away form (message + Save) opens for the server on screen.
|
||
return run(() => (awayReference() ? setAway(false) : openAdminAction('away')));
|
||
case 'recording_start': return run(() => {
|
||
const conn = focusedConnection();
|
||
const state = recordingState(conn);
|
||
if (state.recording) return state.paused ? togglePause(conn) : undefined;
|
||
return startRecording(conn);
|
||
});
|
||
case 'recording_stop': return run(() => stopRecording(focusedConnection()));
|
||
case 'recording_toggle_pause': return run(() => togglePause(focusedConnection()));
|
||
case 'disconnect_current': return run(() => {
|
||
const conn = audioConnection();
|
||
if (conn && conn.conn.state !== 'disconnected') return hooks.disconnect(conn);
|
||
return undefined;
|
||
});
|
||
case 'disconnect_all': return run(() => Promise.all(connectionList().filter((c) => c.conn.state !== 'disconnected').map((c) => hooks.disconnect(c))));
|
||
case 'talk_power_request': return run(requestTalkPower);
|
||
case 'talk_power_grant_next': return run(grantNextTalkPower);
|
||
default: return undefined;
|
||
}
|
||
}
|
||
|
||
function start(binding, scope) {
|
||
switch (binding.action) {
|
||
case 'push_to_talk': setPtt(true, `keybind:${binding.id}:${scope}`); break;
|
||
case 'push_to_mute': setInputDeactivated(`keybind:${binding.id}`, true); break;
|
||
case 'whisper_list':
|
||
case 'quick_whisper':
|
||
case 'reply_whisper': void pressWhisper({ id: binding.id, action: binding.action, target: binding.target }); break;
|
||
// Official StartStream lists windows and streams the single active one; TeamsPeach cannot know the
|
||
// focused application, so the existing broadcast chooser opens and the user picks the source.
|
||
case 'go_live_current': run(openBroadcast); break;
|
||
default: break;
|
||
}
|
||
}
|
||
|
||
function end(binding, scope) {
|
||
switch (binding.action) {
|
||
case 'push_to_talk': setPtt(false, `keybind:${binding.id}:${scope}`); break;
|
||
case 'push_to_mute': setInputDeactivated(`keybind:${binding.id}`, false); break;
|
||
case 'whisper_list':
|
||
case 'quick_whisper':
|
||
case 'reply_whisper': releaseWhisper(binding.id); break;
|
||
default: break;
|
||
}
|
||
}
|
||
|
||
return {
|
||
/** scope: 'local' (focused window) or 'global' (helper). */
|
||
down(binding, scope) {
|
||
if (held.has(binding.id)) return;
|
||
held.set(binding.id, { binding, scope });
|
||
const kind = ACTION_KIND[binding.action];
|
||
if (kind === 'hold' || kind === 'press') start(binding, scope);
|
||
},
|
||
up(id, { synthetic = false } = {}) {
|
||
const entry = held.get(id);
|
||
if (!entry) return;
|
||
held.delete(id);
|
||
const kind = ACTION_KIND[entry.binding.action];
|
||
if (kind === 'hold') end(entry.binding, entry.scope);
|
||
else if (kind === 'trigger' && !synthetic) trigger(entry.binding.action);
|
||
},
|
||
isHeld: (id) => held.has(id),
|
||
held: () => [...held.values()],
|
||
/** Safety release of everything (no trigger runs). `filter(entry)` limits it, e.g. to local scope. */
|
||
releaseAll(filter = () => true) {
|
||
for (const [id, entry] of [...held]) if (filter(entry)) this.up(id, { synthetic: true });
|
||
},
|
||
};
|
||
}
|