package com.pixelized.desktop.lwa.utils.extention

import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type

fun Modifier.onPreviewEscape(
    escape: () -> Unit,
    enter: () -> Unit,
): Modifier = this.onPreviewKeyEvent {
    when (it.key) {
        Key.Escape -> {
            if (it.type != KeyEventType.KeyDown) escape()
            true
        }
        Key.NumPadEnter, Key.Enter -> {
            if (it.type != KeyEventType.KeyDown) enter()
            true
        }
        Key.Spacebar -> {
            true
        }
        else -> false
    }
}