-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
225 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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,109 @@ | ||
import { keyToStr, strToKey } from '../util/keyUtil.js' | ||
import { css, classes } from './KeybindInput.tsx.scss' | ||
|
||
const { | ||
solid: { | ||
createSignal | ||
}, | ||
ui: { | ||
Text, | ||
injectCss | ||
} | ||
} = shelter | ||
|
||
interface Props { | ||
onKeybindChange(keybind: number[]): void | ||
|
||
// style overrides | ||
style?: string | ||
disabled?: boolean | ||
} | ||
|
||
let injectedCss = false | ||
|
||
export function KeybindInput(props: Props) { | ||
if (!injectedCss) { | ||
injectedCss = true | ||
injectCss(css) | ||
} | ||
|
||
const [recording, setRecording] = createSignal(false) | ||
// numbers mean (in order) | ||
const [keybind, setKeybind] = createSignal([]) | ||
|
||
const keyDown = (e) => { | ||
// If the key is already in the keybind, don't add it again | ||
if (keybind().includes(e.key) || keybind().includes(e.key.toLowerCase())) { | ||
return | ||
} | ||
|
||
// if ctrl, alt, or shift, add it to the front of the keybind | ||
switch (e.key) { | ||
case 'Control': | ||
case 'Alt': | ||
case 'Shift': | ||
case 'Meta': | ||
setKeybind([e.key, ...keybind()]) | ||
break | ||
default: | ||
setKeybind([...keybind(), e.key.toLowerCase()]) | ||
} | ||
} | ||
|
||
const keyUp = (e) => { | ||
setKeybind(keybind().filter((k) => k !== e.key && k !== e.key.toLowerCase())) | ||
} | ||
|
||
const setRecordingState = () => { | ||
if (recording()) { | ||
// Remove all event listeners | ||
window.removeEventListener('keydown', keyDown), | ||
window.removeEventListener('keyup', keyUp) | ||
|
||
// Set the keybind | ||
props.onKeybindChange(keybind()) | ||
|
||
setRecording(false) | ||
|
||
return | ||
} | ||
|
||
// Clear the keybind | ||
setKeybind([]) | ||
|
||
// Create event listeners to set the keybind based on what is being held down | ||
// Order should be (if they are pressed): Ctrl -> Alt -> Shift -> Everything else | ||
// If a key is released, it should be removed from the keybind | ||
window.addEventListener('keydown', keyDown), | ||
window.addEventListener('keyup', keyUp) | ||
|
||
setRecording(true) | ||
} | ||
|
||
return ( | ||
<div | ||
class={classes.keybindContainer + ' ' + (recording() ? classes.recording : null)} | ||
style={props.style} | ||
> | ||
<div class={classes.keybindInput}> | ||
<Text class={!keybind().length ? classes.keybindPlaceholder : ''}> | ||
{ | ||
keybind().length ? keybind().map((k, i) => { | ||
const key = k.length > 1 ? k : k.toUpperCase() | ||
return i === keybind().length - 1 ? key : key + ' + ' | ||
}) : 'No Keybind Set' | ||
} | ||
</Text> | ||
|
||
</div> | ||
<div | ||
class={classes.keybindButton} | ||
onClick={() => { | ||
setRecordingState() | ||
}} | ||
> | ||
<Text>{recording() ? 'Stop Recording' : 'Edit Keybind'}</Text> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains 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,73 @@ | ||
.keybindContainer { | ||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
justify-content: space-between; | ||
|
||
width: 100%; | ||
height: 40px; | ||
|
||
border-radius: 4px; | ||
|
||
background: var(--input-background); | ||
color: var(--text-normal); | ||
border: 1px solid transparent; | ||
|
||
padding: 4px; | ||
|
||
transition: all 0.2s; | ||
} | ||
|
||
.keybindContainer:hover { | ||
border: 1px solid var(--status-danger); | ||
} | ||
|
||
.recording { | ||
.keybindButton { | ||
background: hsl(var(--red-400-hsl)/.1); | ||
color: var(--status-danger); | ||
} | ||
|
||
.keybindButton:hover { | ||
background: hsl(var(--red-400-hsl)/.2); | ||
} | ||
|
||
border: 1px solid var(--status-danger); | ||
} | ||
|
||
.keybindInput { | ||
background: transparent; | ||
|
||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
.keybindPlaceholder { | ||
color: var(--text-muted) !important; | ||
} | ||
|
||
.keybindButton { | ||
height: 30px; | ||
width: 50%; | ||
|
||
margin: 0; | ||
padding: 4px; | ||
|
||
border-radius: 4px; | ||
|
||
display: flex; | ||
align-items: center; | ||
|
||
background: var(--button-secondary-background); | ||
color: var(--white-500); | ||
|
||
border: 1px solid transparent; | ||
|
||
cursor: pointer; | ||
|
||
transition: all 0.2s; | ||
} | ||
|
||
.keybindButton:hover { | ||
background: var(--button-secondary-background-hover); | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -10,7 +10,6 @@ | |
.keybindArea { | ||
display: flex; | ||
flex-direction: column; | ||
align-items | ||
} | ||
|
||
.actionSection { | ||
|
This file contains 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 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 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