-
Notifications
You must be signed in to change notification settings - Fork 49
feat: add keyboard control to sliders #115
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,13 +11,18 @@ export interface VolumeSliderState extends SliderState { | |||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| export class VolumeSlider extends Slider { | ||||||||||||||||||||||||||||||||||||||
| constructor() { | ||||||||||||||||||||||||||||||||||||||
| super(); | ||||||||||||||||||||||||||||||||||||||
| this.setStepSize(0.1); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| getState(): VolumeSliderState { | ||||||||||||||||||||||||||||||||||||||
| const state = super.getState() as VolumeSliderState; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // When dragging, use pointer position for immediate feedback; | ||||||||||||||||||||||||||||||||||||||
| // When dragging or keying, use pointer position for immediate feedback; | ||||||||||||||||||||||||||||||||||||||
| // Otherwise, use current volume; | ||||||||||||||||||||||||||||||||||||||
| let _fillWidth = 0; | ||||||||||||||||||||||||||||||||||||||
| if (state._dragging) { | ||||||||||||||||||||||||||||||||||||||
| if (state._dragging || state._keying) { | ||||||||||||||||||||||||||||||||||||||
| _fillWidth = state._pointerRatio * 100; | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| _fillWidth = state.muted ? 0 : (state.volume || 0) * 100; | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -28,6 +33,15 @@ export class VolumeSlider extends Slider { | |||||||||||||||||||||||||||||||||||||
| return { ...state, _fillWidth, _volumeText }; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| setState(state: Partial<VolumeSliderState>): void { | ||||||||||||||||||||||||||||||||||||||
| // When not dragging or keying, set pointer ratio to current volume. | ||||||||||||||||||||||||||||||||||||||
| if (!state._dragging && !state._keying && state.volume) { | ||||||||||||||||||||||||||||||||||||||
| super.setState({ ...state, _pointerRatio: state.volume }); | ||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| super.setState(state); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| handleEvent(event: Event): void { | ||||||||||||||||||||||||||||||||||||||
| const { type } = event; | ||||||||||||||||||||||||||||||||||||||
| switch (type) { | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -40,6 +54,9 @@ export class VolumeSlider extends Slider { | |||||||||||||||||||||||||||||||||||||
| case 'pointerup': | ||||||||||||||||||||||||||||||||||||||
| this.#handlePointerUp(event as PointerEvent); | ||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||
| case 'keydown': | ||||||||||||||||||||||||||||||||||||||
| this.#handleKeyDown(event as KeyboardEvent); | ||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||
| super.handleEvent(event); | ||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -72,6 +89,13 @@ export class VolumeSlider extends Slider { | |||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| super.handleEvent(event); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| #handleKeyDown(event: KeyboardEvent) { | ||||||||||||||||||||||||||||||||||||||
| super.handleEvent(event); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const { _pointerRatio, requestVolumeChange } = super.getState() as VolumeSliderState; | ||||||||||||||||||||||||||||||||||||||
| requestVolumeChange(_pointerRatio); | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+94
to
+97
|
||||||||||||||||||||||||||||||||||||||
| super.handleEvent(event); | |
| const { _pointerRatio, requestVolumeChange } = super.getState() as VolumeSliderState; | |
| requestVolumeChange(_pointerRatio); | |
| // Compute the new pointer ratio synchronously based on the key event | |
| const state = super.getState() as VolumeSliderState; | |
| let newPointerRatio = state._pointerRatio; | |
| // Example logic: adjust ratio based on arrow keys | |
| if (event.key === 'ArrowLeft' || event.key === 'ArrowDown') { | |
| newPointerRatio = Math.max(0, newPointerRatio - this.getStepSize()); | |
| } else if (event.key === 'ArrowRight' || event.key === 'ArrowUp') { | |
| newPointerRatio = Math.min(1, newPointerRatio + this.getStepSize()); | |
| } | |
| const { requestVolumeChange } = state; | |
| requestVolumeChange(newPointerRatio); | |
| // Optionally, update the pointer ratio in state if needed | |
| // (depends on parent class implementation) | |
| // super.setPointerRatio(newPointerRatio); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export interface StateOwners { | ||
| media?: HTMLMediaElement; | ||
| container?: HTMLElement; | ||
| } |
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.
Same issue as in VolumeSlider: calling
getState()immediately aftersuper.handleEvent(event)may not capture the updated_pointerRatiovalue if state updates are asynchronous. This aligns with the PR description's noted bug about 'relative value bug in time slider and volume slider'.