-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
1 parent
3aefe2c
commit fa918d6
Showing
6 changed files
with
199 additions
and
30 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
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
75 changes: 46 additions & 29 deletions
75
apps/stage-tamagotchi/src/renderer/src/composables/window-shortcuts.ts
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 |
---|---|---|
@@ -1,39 +1,56 @@ | ||
import { onMounted, onUnmounted } from 'vue' | ||
import type { EffectScope } from 'vue' | ||
|
||
import { useShortcutsStore } from '@renderer/stores/shortcuts' | ||
import { useMagicKeys, whenever } from '@vueuse/core' | ||
import { storeToRefs } from 'pinia' | ||
import { computed, effectScope, watch } from 'vue' | ||
|
||
import { useWindowControlStore } from '../stores/window-controls' | ||
import { WindowControlMode } from '../types/window-controls' | ||
|
||
export function useWindowShortcuts() { | ||
const windowStore = useWindowControlStore() | ||
const magicKeys = useMagicKeys() | ||
|
||
function handleKeydown(event: KeyboardEvent) { | ||
// Ctrl/Cmd + Shift + D for debug mode | ||
if ((event.ctrlKey || event.metaKey) && event.shiftKey && event.key === 'd') { | ||
windowStore.setMode(WindowControlMode.DEBUG) | ||
windowStore.toggleControl() | ||
} | ||
// Ctrl/Cmd + M for move mode | ||
if ((event.ctrlKey || event.metaKey) && event.key === 'm') { | ||
windowStore.setMode(WindowControlMode.MOVE) | ||
windowStore.toggleControl() | ||
} | ||
// Ctrl/Cmd + R for resize mode | ||
if ((event.ctrlKey || event.metaKey) && event.key === 'r') { | ||
windowStore.setMode(WindowControlMode.RESIZE) | ||
windowStore.toggleControl() | ||
} | ||
// Escape to exit any mode | ||
if (event.key === 'Escape') { | ||
windowStore.setMode(WindowControlMode.DEFAULT) | ||
windowStore.toggleControl() | ||
} | ||
} | ||
const { shortcuts } = storeToRefs(useShortcutsStore()) | ||
const handlers = computed(() => [ | ||
{ | ||
handle: () => { | ||
windowStore.setMode(WindowControlMode.MOVE) | ||
windowStore.toggleControl() | ||
}, | ||
shortcut: shortcuts.value.find(shortcut => shortcut.type === 'move')?.shortcut, | ||
}, | ||
{ | ||
handle: () => { | ||
windowStore.setMode(WindowControlMode.RESIZE) | ||
windowStore.toggleControl() | ||
}, | ||
shortcut: shortcuts.value.find(shortcut => shortcut.type === 'resize')?.shortcut, | ||
}, | ||
{ | ||
handle: () => { | ||
windowStore.setMode(WindowControlMode.DEBUG) | ||
windowStore.toggleControl() | ||
}, | ||
shortcut: shortcuts.value.find(shortcut => shortcut.type === 'debug')?.shortcut, | ||
}, | ||
]) | ||
|
||
onMounted(() => { | ||
window.addEventListener('keydown', handleKeydown) | ||
}) | ||
let currentScope: EffectScope | null = null | ||
watch(handlers, () => { | ||
if (currentScope) { | ||
currentScope.stop() | ||
} | ||
|
||
onUnmounted(() => { | ||
window.removeEventListener('keydown', handleKeydown) | ||
}) | ||
currentScope = effectScope() | ||
currentScope.run(() => { | ||
handlers.value.forEach((handler) => { | ||
if (!handler.shortcut) { | ||
return | ||
} | ||
whenever(magicKeys[handler.shortcut], handler.handle) | ||
}) | ||
}) | ||
}, { immediate: true }) | ||
} |
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
30 changes: 30 additions & 0 deletions
30
apps/stage-tamagotchi/src/renderer/src/stores/shortcuts.ts
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,30 @@ | ||
import { useLocalStorage } from '@vueuse/core' | ||
import { defineStore } from 'pinia' | ||
import { ref } from 'vue' | ||
|
||
export const useShortcutsStore = defineStore('shortcuts', () => { | ||
const shortcuts = ref([ | ||
{ | ||
name: 'settings.shortcuts.window.move', | ||
shortcut: useLocalStorage('shortcuts/window/move', 'Ctrl+M'), | ||
group: 'window', | ||
type: 'move', | ||
}, | ||
{ | ||
name: 'settings.shortcuts.window.resize', | ||
shortcut: useLocalStorage('shortcuts/window/resize', 'Ctrl+R'), | ||
group: 'window', | ||
type: 'resize', | ||
}, | ||
{ | ||
name: 'settings.shortcuts.window.debug', | ||
shortcut: useLocalStorage('shortcuts/window/debug', 'Ctrl+I'), | ||
group: 'window', | ||
type: 'debug', | ||
}, | ||
]) | ||
|
||
return { | ||
shortcuts, | ||
} | ||
}) |