hiss/old.src/view/input.js
2025-02-04 14:21:03 -07:00

212 lines
3.5 KiB
JavaScript

const app = window.app ??= {}
const view = window.view ??= {}
const holdIntervals = 3
const holdIntervalLength = 200
import * as browser from './browser.js'
import * as player from './player.js'
import * as queue from './queue.js'
import * as main from '../main.js'
{
window.addEventListener('keydown', OnKeydown)
window.addEventListener('keyup', OnKeyup)
}
export function Render() {
Actions()
}
export function Actions() {
let a = view.actions = {}
a['EndCall'] = {
name: 'Close Hiss',
up() {
main.saveState().then(() => window.close())
}
}
a['ArrowRight'] = {
name: 'Next Panel',
up() {
mode.Scroll(1)
}
}
a['ArrowLeft'] = {
name: 'Prev Panel',
up() {
mode.Scroll(-1)
}
}
a['0'] = {
}
a['c'] = a.pausePlay = {
hint: 'Pause/Play',
up: player.TogglePausePlay
}
a['s'] = a.skip = {
hint: 'Next',
up: player.Next
}
a['+'] = a.volumeUp = {
hint: 'Volume Up',
down() {
navigator.volumeUp()
}
}
a['-'] = a.volumeDown = {
hint: 'Volume Down',
down() {
navigator.volumeDown()
}
}
switch(app.mode) {
case mode.states.PLAYER:
a[' '] = a['Enter'] = player.State() == player.states.PLAYING ?
{
hint: 'Play',
up: player.Play
} :
{
hint: 'Pause',
up: player.Pause
}
a['ArrowUp'] = a.volumeUp
a['ArrowDown'] = a.volumeDown
a['SoftRight'] = a.skip
break
case mode.states.BROWSER:
a['q'] = {
down: browser.Queue
}
a[' '] = a['Enter'] = {
hint: 'Queue',
press() {
browser.Cd() && browser.Queue()
},
holdHint: 'Queue All',
hold: browser.QueueAllBelow
}
a['4'] = a['g'] = {
up: browser.GoToDirectory
}
a['Backspace'] = a['Escape'] = {
up: browser.Back
}
listActions(browser.list)
break
case mode.states.QUEUE:
listActions(queue.list)
a[' '] = a['Enter'] = {
hint: 'Remove',
press: queue.Remove,
holdHint: 'Remove All',
hold: queue.RemoveAllBelow
}
a['SoftLeft'] = a['['] = a['ArrowUp'].shift = {
down() {
queue.Move(-1)
}
}
a['SoftRight'] = a[']'] = a['ArrowDown'].shift = {
down() {
queue.Move(1)
}
}
a['4'] = a['g'] = {
up() {
browser.GoToDirectory(queue.Focused())
mode.Set(mode.states.BROWSER)
}
}
break
}
}
function listActions(list) {
let a = view.actions
a['ArrowUp'] = {
down() {
list.Scroll(-1)
}
}
a['ArrowDown'] = {
down() {
list.Scroll(1)
}
}
}
export function OnKeydown(event) {
let a = view.actions[event.key]
if(a == null) return
ActionDown(a.shift && event.shiftKey ? a.shift : a)
&& event.preventDefault()
}
export function ActionDown(action) {
if(action.holdTimeout != null) {
return true
}
if(action.down) {
return action.down()
}
if(action.hold) {
Hold(action)
} else if(action.press) {
return action.press()
}
}
export function OnKeyup(event) {
let a = view.actions[event.key]
if(a == null) return
ActionUp(a.shift && event.shiftKey ? a.shift : a)
&& event.preventDefault()
}
export function ActionUp(action) {
if(action.holdTimeout != null) {
clearTimeout(action.holdTimeout)
action.holdTimeout = null
}
if(action.up) {
return action.up()
}
if(action.heldCount > 0) {
return action.press()
}
}
export function Hold(aAction) {
aAction.heldCount = holdIntervals
createHoldTimeout(aAction)
}
export function createHoldTimeout(aAction) {
aAction.holdTimeout = setTimeout(() => {
aAction.heldCount--
if(aAction.heldCount == 0) {
aAction.hold()
} else {
createHoldTimeout(aAction)
}
}, holdIntervalLength)
}