Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): globalShortcut API (#1232)
- Loading branch information
1 parent
772d83e
commit 855effa
Showing
22 changed files
with
481 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"api": minor | ||
"tauri-api": minor | ||
"tauri": minor | ||
--- | ||
|
||
Adds a global shortcut API. |
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
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,39 @@ | ||
import { promisified, transformCallback } from './tauri' | ||
|
||
/** | ||
* register a global shortcut | ||
* @param shortcut shortcut definition, modifiers and key separated by "+" e.g. Alt+Q | ||
* @param handler shortcut handler callback | ||
*/ | ||
async function registerShortcut( | ||
shortcut: string, | ||
handler: () => void | ||
): Promise<void> { | ||
return await promisified({ | ||
module: 'GlobalShortcut', | ||
message: { | ||
cmd: 'register', | ||
shortcut, | ||
handler: transformCallback(handler) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* unregister a global shortcut | ||
* @param shortcut shortcut definition, modifiers and key separated by "+" e.g. Alt+Q | ||
*/ | ||
async function unregisterShortcut(shortcut: string): Promise<void> { | ||
return await promisified({ | ||
module: 'GlobalShortcut', | ||
message: { | ||
cmd: 'unregister', | ||
shortcut | ||
} | ||
}) | ||
} | ||
|
||
export { | ||
registerShortcut, | ||
unregisterShortcut | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use tauri_hotkey::{parse_hotkey, HotkeyManager}; | ||
|
||
/// The shortcut manager builder. | ||
#[derive(Default)] | ||
pub struct ShortcutManager(HotkeyManager); | ||
|
||
impl ShortcutManager { | ||
/// Initializes a new instance of the shortcut manager. | ||
pub fn new() -> Self { | ||
Default::default() | ||
} | ||
|
||
/// Registers a new shortcut handler. | ||
pub fn register_shortcut<H: FnMut() + Send + 'static>( | ||
&mut self, | ||
shortcut: String, | ||
handler: H, | ||
) -> crate::Result<()> { | ||
let hotkey = parse_hotkey(&shortcut.to_uppercase().replace(" ", ""))?; | ||
self.0.register(hotkey, handler)?; | ||
Ok(()) | ||
} | ||
|
||
/// Unregister a previously registered shortcut handler. | ||
pub fn unregister_shortcut(&mut self, shortcut: String) -> crate::Result<()> { | ||
let hotkey = parse_hotkey(&shortcut.to_uppercase().replace(" ", ""))?; | ||
self.0.unregister(&hotkey)?; | ||
Ok(()) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,41 @@ | ||
<script> | ||
import { writable } from 'svelte/store' | ||
import { registerShortcut, unregisterShortcut } from '@tauri-apps/api/globalShortcut' | ||
export let onMessage | ||
const shortcuts = writable([]) | ||
let shortcut = 'CTRL+X' | ||
function register() { | ||
const shortcut_ = shortcut | ||
registerShortcut(shortcut_, () => { | ||
onMessage(`Shortcut ${shortcut_} triggered`) | ||
}).then(() => { | ||
shortcuts.update(shortcuts_ => [...shortcuts_, shortcut_]) | ||
onMessage(`Shortcut ${shortcut_} registered successfully`) | ||
}).catch(onMessage) | ||
} | ||
function unregister(shortcut) { | ||
const shortcut_ = shortcut | ||
unregisterShortcut(shortcut_).then(() => { | ||
shortcuts.update(shortcuts_ => shortcuts_.filter(s => s !== shortcut_)) | ||
onMessage(`Shortcut ${shortcut_} unregistered`) | ||
}).catch(onMessage) | ||
} | ||
</script> | ||
|
||
<div style="margin-top: 24px"> | ||
<div> | ||
<input placeholder="Type a shortcut with '+' as separator..." bind:value={shortcut}> | ||
<button type="button" on:click={register}>Register</button> | ||
</div> | ||
<div> | ||
{#each $shortcuts as savedShortcut} | ||
<div> | ||
{savedShortcut} | ||
<button type="button" on:click={()=> unregister(savedShortcut)}>Unregister</button> | ||
</div> | ||
{/each} | ||
</div> | ||
</div> |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.