Skip to content

Commit

Permalink
Merge pull request #42 from ryanrixxh/19-saved-hotkey-does-not-trigge…
Browse files Browse the repository at this point in the history
…r-a-request-after-exiting-the-home-component-and-re-entering

Fixed hotkey registration by waiting for unregister to pop
  • Loading branch information
ryanrixxh authored Jul 5, 2023
2 parents 3a22ae9 + 0b57fa7 commit 7040a47
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/components/Marker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect, useMemo } from "react"
import { invoke } from '@tauri-apps/api/tauri'
import { register, unregister } from '@tauri-apps/api/globalShortcut'
import { register, unregisterAll } from '@tauri-apps/api/globalShortcut'
import {writeTextFile, BaseDirectory, exists, createDir } from '@tauri-apps/api/fs'
import { appLocalDataDir } from '@tauri-apps/api/path'
import { postMarker } from "../utils/api"
Expand Down Expand Up @@ -95,23 +95,16 @@ function Marker(props) {
}

async function changeShortcut(newHotkey: string) {
await unregister(hotkey)
await unregisterAll()
.finally(() => {
register(newHotkey, () => {
// This logic is ran when the hotkey is pressed
setCount(count => count + 1)
})
})
let current_hotkey = newHotkey
try {
await register(newHotkey, async () => {
// This logic is ran when the hotkey is pressed
setCount(count => count + 1)
})
} catch(e) {
if(typeof e === 'string' && e.includes('already registered')) {
} else {
throw e
}
}

setHotkey(current_hotkey)
saveHotkey(current_hotkey)

setHkPrompt(current_hotkey)
}

Expand All @@ -125,7 +118,6 @@ function Marker(props) {
const savedHotkey: string = (val.value !== null) ? val.value : ''
setHotkey(savedHotkey)
changeShortcut(savedHotkey)

setHkPrompt(savedHotkey)
}

Expand Down

0 comments on commit 7040a47

Please sign in to comment.