Skip to content

Commit

Permalink
refactor(global-shortcut): enhance un/register to accept an array, …
Browse files Browse the repository at this point in the history
…remove `un/registerAll` (#1117)

* refactor(shell): enhance `un/register` to accept an array, remove `un/registerAll`

closes #1101

* Update lib.rs

* remove permissions, cleanup docs

* bring back unregister_all

* fmt

* fix build

* bundle

---------

Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
amrbashir and lucasfernog authored Jul 8, 2024
1 parent a665493 commit 381a466
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 120 deletions.
10 changes: 10 additions & 0 deletions .changes/global-shortcut-js-apis-refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"global-shortcut": "patch"
---

Refactored the Rust APIs:

- Renamed `GlobalShortcut::on_all_shortcuts` to `GlobalShortcut::on_shortcuts`
- Renamed `GlobalShortcut::register_all` to `GlobalShortcut::register_multiple`
- Changed `GlobalShortcut::unregister_all` behavior to remove all registerd shortcuts.
- Added `GlobalShortcut::unregister_multiple` to register a list of shortcuts (old behavior of `unregister_all`).
8 changes: 8 additions & 0 deletions .changes/global-shortcut-rust-apis-refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"global-shortcut-js": "patch"
---

Refactored the JS APIs:

- Enhanced `register` and `unregister` to take either a single shortcut or an array.
- Removed `registerAll` instead use `register` with an array.
14 changes: 0 additions & 14 deletions examples/api/src-tauri/gen/schemas/desktop-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4850,13 +4850,6 @@
"global-shortcut:allow-register"
]
},
{
"description": "global-shortcut:allow-register-all -> Enables the register_all command without any pre-configured scope.",
"type": "string",
"enum": [
"global-shortcut:allow-register-all"
]
},
{
"description": "global-shortcut:allow-unregister -> Enables the unregister command without any pre-configured scope.",
"type": "string",
Expand Down Expand Up @@ -4885,13 +4878,6 @@
"global-shortcut:deny-register"
]
},
{
"description": "global-shortcut:deny-register-all -> Denies the register_all command without any pre-configured scope.",
"type": "string",
"enum": [
"global-shortcut:deny-register-all"
]
},
{
"description": "global-shortcut:deny-unregister -> Denies the unregister command without any pre-configured scope.",
"type": "string",
Expand Down
5 changes: 2 additions & 3 deletions examples/api/src/views/Shortcuts.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script>
import { writable } from "svelte/store";
import { writable, get } from "svelte/store";
import {
register as registerShortcut,
unregister as unregisterShortcut,
unregisterAll as unregisterAllShortcuts,
} from "@tauri-apps/plugin-global-shortcut";
export let onMessage;
Expand Down Expand Up @@ -35,7 +34,7 @@
}
function unregisterAll() {
unregisterAllShortcuts()
unregisterShortcut(get(shortcuts))
.then(() => {
shortcuts.update(() => []);
onMessage(`Unregistered all shortcuts`);
Expand Down
2 changes: 1 addition & 1 deletion plugins/global-shortcut/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions plugins/global-shortcut/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

const COMMANDS: &[&str] = &[
"register",
"register_all",
"unregister",
"unregister_all",
"is_registered",
];
const COMMANDS: &[&str] = &["register", "unregister", "unregister_all", "is_registered"];

fn main() {
tauri_plugin::Builder::new(COMMANDS)
Expand Down
97 changes: 44 additions & 53 deletions plugins/global-shortcut/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,28 @@ export interface ShortcutEvent {
export type ShortcutHandler = (event: ShortcutEvent) => void;

/**
* Register a global shortcut.
* Register a global shortcut or a list of shortcuts.
*
* The handler is called when any of the registered shortcuts are pressed by the user.
*
* If the shortcut is already taken by another application, the handler will not be triggered.
* Make sure the shortcut is as unique as possible while still taking user experience into consideration.
*
* @example
* ```typescript
* import { register } from '@tauri-apps/plugin-global-shortcut';
*
* // register a single hotkey
* await register('CommandOrControl+Shift+C', (event) => {
* if (event.state === "Pressed") {
* console.log('Shortcut triggered');
* }
* });
*
* // or register multiple hotkeys at once
* await register(['CommandOrControl+Shift+C', 'Alt+A'], (event) => {
* console.log(`Shortcut ${event.shortcut} triggered`);
* });
* ```
*
* @param shortcut Shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
Expand All @@ -36,97 +49,75 @@ export type ShortcutHandler = (event: ShortcutEvent) => void;
* @since 2.0.0
*/
async function register(
shortcut: string,
shortcuts: string | string[],
handler: ShortcutHandler,
): Promise<void> {
const h = new Channel<ShortcutEvent>();
h.onmessage = handler;

await invoke("plugin:global-shortcut|register", {
shortcut,
return await invoke("plugin:global-shortcut|register", {
shortcuts: Array.isArray(shortcuts) ? shortcuts : [shortcuts],
handler: h,
});
}

/**
* Register a collection of global shortcuts.
* Unregister a global shortcut or a list of shortcuts.
*
* @example
* ```typescript
* import { registerAll } from '@tauri-apps/plugin-global-shortcut';
* await registerAll(['CommandOrControl+Shift+C', 'Ctrl+Alt+F12'], (event) => {
* console.log(`Shortcut ${event.shortcut} ${event.state}`);
* });
* import { unregister } from '@tauri-apps/plugin-global-shortcut';
*
* // unregister a single hotkey
* await unregister('CmdOrControl+Space');
*
* // or unregister multiple hotkeys at the same time
* await unregister(['CmdOrControl+Space', 'Alt+A']);
* ```
*
* @param shortcuts Array of shortcut definitions, modifiers and key separated by "+" e.g. CmdOrControl+Q
* @param handler Shortcut handler callback - takes the triggered shortcut as argument
* @param shortcut shortcut definition (modifiers and key separated by "+" e.g. CmdOrControl+Q), also accepts a list of shortcuts
*
* @since 2.0.0
*/
async function registerAll(
shortcuts: string[],
handler: ShortcutHandler,
): Promise<void> {
const h = new Channel<ShortcutEvent>();
h.onmessage = handler;

await invoke("plugin:global-shortcut|register_all", {
shortcuts,
handler: h,
async function unregister(shortcuts: string | string[]): Promise<void> {
return await invoke("plugin:global-shortcut|unregister", {
shortcuts: Array.isArray(shortcuts) ? shortcuts : [shortcuts],
});
}

/**
* Determines whether the given shortcut is registered by this application or not.
*
* If the shortcut is registered by another application, it will still return `false`.
* Unregister all global shortcuts.
*
* @example
* ```typescript
* import { isRegistered } from '@tauri-apps/plugin-global-shortcut';
* const isRegistered = await isRegistered('CommandOrControl+P');
* import { unregisterAll } from '@tauri-apps/plugin-global-shortcut';
* await unregisterAll();
* ```
*
* @param shortcut shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
*
* @since 2.0.0
*/
async function isRegistered(shortcut: string): Promise<boolean> {
return await invoke("plugin:global-shortcut|is_registered", {
shortcut,
});
async function unregisterAll(): Promise<void> {
return await invoke("plugin:global-shortcut|unregister_all", {});
}

/**
* Unregister a global shortcut.
* Determines whether the given shortcut is registered by this application or not.
*
* If the shortcut is registered by another application, it will still return `false`.
*
* @example
* ```typescript
* import { unregister } from '@tauri-apps/plugin-global-shortcut';
* await unregister('CmdOrControl+Space');
* import { isRegistered } from '@tauri-apps/plugin-global-shortcut';
* const isRegistered = await isRegistered('CommandOrControl+P');
* ```
*
* @param shortcut shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
*
* @since 2.0.0
*/
async function unregister(shortcut: string): Promise<void> {
await invoke("plugin:global-shortcut|unregister", {
async function isRegistered(shortcut: string): Promise<boolean> {
return await invoke("plugin:global-shortcut|is_registered", {
shortcut,
});
}

/**
* Unregisters all shortcuts registered by the application.
* @example
* ```typescript
* import { unregisterAll } from '@tauri-apps/plugin-global-shortcut';
* await unregisterAll();
* ```
*
* @since 2.0.0
*/
async function unregisterAll(): Promise<void> {
await invoke("plugin:global-shortcut|unregister_all");
}

export { register, registerAll, isRegistered, unregister, unregisterAll };
export { register, unregister, unregisterAll, isRegistered };
2 changes: 1 addition & 1 deletion plugins/global-shortcut/permissions/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[default]
description = """
No features are enabled by default, as we believe
the shortcuts can be inherently dangerous and it is
the shortcuts can be inherently dangerous and it is
application specific if specific shortcuts should be
registered or unregistered.
"""
Expand Down
Loading

0 comments on commit 381a466

Please sign in to comment.