-
Notifications
You must be signed in to change notification settings - Fork 318
feat: add PTT, keyboard shortcut engine, persistent auxiliary settings #430
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
Open
rektdeckard
wants to merge
5
commits into
main
Choose a base branch
from
tobias/ptt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2ccb2e5
feat: add PTT, keyboard shortcut engine, persistent auxiliary settings
rektdeckard 97d9cb1
fix: relax persistence type contraints
rektdeckard d82c046
chore: cleanup
rektdeckard 782ee01
chore: skip toggle action if an update is pending
rektdeckard ae26502
chore: rename discriminator -> guard
rektdeckard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,109 @@ | ||
| 'use client'; | ||
|
|
||
| import React from 'react'; | ||
| import { Track } from 'livekit-client'; | ||
| import { useTrackToggle } from '@livekit/components-react'; | ||
| import { useSettingsState } from './SettingsContext'; | ||
| import { KeyCommand } from './types'; | ||
|
|
||
| export function KeyboardShortcuts() { | ||
| const { toggle: toggleMic } = useTrackToggle({ source: Track.Source.Microphone }); | ||
| const { state } = useSettingsState() ?? {}; | ||
| const { toggle: toggleMic, enabled: micEnabled } = useTrackToggle({ | ||
| source: Track.Source.Microphone, | ||
| }); | ||
| const { toggle: toggleCamera } = useTrackToggle({ source: Track.Source.Camera }); | ||
| const [pttHeld, setPttHeld] = React.useState(false); | ||
|
|
||
| React.useEffect(() => { | ||
| function handleShortcut(event: KeyboardEvent) { | ||
| // Toggle microphone: Cmd/Ctrl-Shift-A | ||
| if (toggleMic && event.key === 'A' && (event.ctrlKey || event.metaKey)) { | ||
| event.preventDefault(); | ||
| toggleMic(); | ||
| } | ||
|
|
||
| // Toggle camera: Cmd/Ctrl-Shift-V | ||
| if (event.key === 'V' && (event.ctrlKey || event.metaKey)) { | ||
| event.preventDefault(); | ||
| toggleCamera(); | ||
| } | ||
| } | ||
|
|
||
| window.addEventListener('keydown', handleShortcut); | ||
| return () => window.removeEventListener('keydown', handleShortcut); | ||
| }, [toggleMic, toggleCamera]); | ||
| const handlers = Object.entries(state.keybindings) | ||
| .flatMap(([command, bind]) => { | ||
| switch (command) { | ||
| case KeyCommand.PTT: | ||
| if (!state.enablePTT || !Array.isArray(bind)) return []; | ||
|
|
||
| const [enable, disable] = bind; | ||
| const t = getEventTarget(enable.target); | ||
| if (!t) return null; | ||
|
|
||
| const on = (event: KeyboardEvent) => { | ||
| if (enable.discriminator(event)) { | ||
| event.preventDefault(); | ||
| if (!micEnabled) { | ||
| setPttHeld(true); | ||
| toggleMic?.(true); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const off = (event: KeyboardEvent) => { | ||
| if (disable.discriminator(event)) { | ||
| event.preventDefault(); | ||
| if (pttHeld && micEnabled) { | ||
| setPttHeld(false); | ||
| toggleMic?.(false); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| t.addEventListener(enable.eventName, on as any); | ||
| t.addEventListener(disable.eventName, off as any); | ||
| return [ | ||
| { eventName: enable.eventName, target: t, handler: on }, | ||
| { eventName: disable.eventName, target: t, handler: off }, | ||
| ]; | ||
| case KeyCommand.ToggleMic: | ||
| if (!Array.isArray(bind)) { | ||
| const t = getEventTarget(bind.target); | ||
| if (!t) return null; | ||
|
|
||
| const handler = (event: KeyboardEvent) => { | ||
| if (bind.discriminator(event)) { | ||
| event.preventDefault(); | ||
| toggleMic?.(); | ||
| } | ||
| }; | ||
| t.addEventListener(bind.eventName, handler as any); | ||
| return { eventName: bind.eventName, target: t, handler }; | ||
| } | ||
| case KeyCommand.ToggleCamera: | ||
| if (!Array.isArray(bind)) { | ||
| const t = getEventTarget(bind.target); | ||
| if (!t) return null; | ||
|
|
||
| const handler = (event: KeyboardEvent) => { | ||
| if (bind.discriminator(event)) { | ||
| event.preventDefault(); | ||
| toggleCamera?.(); | ||
| } | ||
| }; | ||
| t.addEventListener(bind.eventName, handler as any); | ||
| return { eventName: bind.eventName, target: t, handler }; | ||
| } | ||
| default: | ||
| return []; | ||
| } | ||
| }) | ||
| .filter(Boolean) as Array<{ | ||
| target: EventTarget; | ||
| eventName: string; | ||
| handler: (event: KeyboardEvent) => void; | ||
| }>; | ||
|
|
||
| return () => { | ||
| handlers.forEach(({ target, eventName, handler }) => { | ||
| target.removeEventListener(eventName, handler as any); | ||
| }); | ||
| }; | ||
| }, [state, pttHeld, micEnabled, toggleMic]); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function getEventTarget( | ||
| target: Window | Document | HTMLElement | string = window, | ||
| ): EventTarget | null { | ||
| const targetElement = typeof target === 'string' ? document.querySelector(target) : target; | ||
| if (!targetElement) { | ||
| console.warn(`Target element not found for ${target}`); | ||
| return null; | ||
| } | ||
| return targetElement; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| 'use client'; | ||
|
|
||
| import { Toaster } from 'react-hot-toast'; | ||
| import { SettingsStateProvider } from './SettingsContext'; | ||
|
|
||
| export function Providers({ children }: React.PropsWithChildren) { | ||
| return ( | ||
| <SettingsStateProvider> | ||
| <Toaster /> | ||
| {children} | ||
| </SettingsStateProvider> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| 'use client'; | ||
|
|
||
| import React, { createContext, SetStateAction, useCallback, useContext, useMemo } from 'react'; | ||
| import type { | ||
| SettingsState, | ||
| SettingsStateContextType, | ||
| SerializedSettingsState, | ||
| KeyBindings, | ||
| } from './types'; | ||
| import { defaultKeyBindings, commonKeyBindings } from './keybindings'; | ||
| import { usePersistToLocalStorage } from './persistence'; | ||
|
|
||
| const AUXILIARY_USER_CHOICES_KEY = `lk-auxiliary-user-choices`; | ||
|
|
||
| const initialState: SettingsState = { | ||
| keybindings: defaultKeyBindings, | ||
| enablePTT: false, | ||
| }; | ||
|
|
||
| function serializeSettingsState(state: SettingsState): SerializedSettingsState { | ||
| return { | ||
| ...state, | ||
| keybindings: Object.entries(state.keybindings).reduce<Record<string, string>>( | ||
| (acc, [key, value]) => { | ||
| const commonName = Object.entries(commonKeyBindings).find(([_, v]) => v === value)?.[0]; | ||
| if (commonName) { | ||
| acc[key] = commonName; | ||
| } | ||
| return acc; | ||
| }, | ||
| {}, | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| function deserializeSettingsState(state: SerializedSettingsState): SettingsState { | ||
| return { | ||
| ...state, | ||
| keybindings: { | ||
| ...defaultKeyBindings, | ||
| ...Object.entries(state.keybindings).reduce<KeyBindings>((acc, [key, commonName]) => { | ||
| const commonBinding = commonKeyBindings[commonName as keyof typeof commonKeyBindings]; | ||
| if (commonBinding) { | ||
| acc[key as keyof typeof defaultKeyBindings] = commonBinding; | ||
| } | ||
| return acc; | ||
| }, {}), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const SettingsStateContext = createContext<SettingsStateContextType>({ | ||
| state: initialState, | ||
| set: () => { }, | ||
| }); | ||
|
|
||
| const SettingsStateProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { | ||
| const [state, set] = usePersistToLocalStorage<SerializedSettingsState>( | ||
| AUXILIARY_USER_CHOICES_KEY, | ||
| serializeSettingsState(initialState), | ||
| ); | ||
|
|
||
| const deserializedState = useMemo(() => deserializeSettingsState(state), [state]); | ||
|
|
||
| const setSettingsState = useCallback( | ||
| (dispatch: SetStateAction<SettingsState>) => { | ||
| if (typeof dispatch === 'function') { | ||
| set((prev) => { | ||
| const next = serializeSettingsState(dispatch(deserializeSettingsState(prev))); | ||
| return next; | ||
| }); | ||
| } else { | ||
| set(serializeSettingsState(dispatch)); | ||
| } | ||
| }, | ||
| [set], | ||
| ); | ||
|
|
||
| return ( | ||
| <SettingsStateContext.Provider value={{ state: deserializedState, set: setSettingsState }}> | ||
| {children} | ||
| </SettingsStateContext.Provider> | ||
| ); | ||
| }; | ||
|
|
||
| const useSettingsState = () => { | ||
| const ctx = useContext(SettingsStateContext); | ||
| if (ctx === null) { | ||
| throw new Error('useSettingsState must be used within SettingsStateProvider'); | ||
| } | ||
| return ctx!; | ||
| }; | ||
|
|
||
| export { useSettingsState, SettingsStateProvider, SettingsStateContext }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will race. The
useTrackTogglehook also returnspendingstate which is between state change and its state change trigger.Generally explicitly muting/unmuting might be a better fit for PTT than toggling ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. And now checking the pending state before firing another
toggleaction in the other cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anecdotally, I have spammed both PTT and toggle as fast as I can and don't see any unexpected behavior.