Skip to content

Commit 381a466

Browse files
refactor(global-shortcut): enhance un/register to accept an array, 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]>
1 parent a665493 commit 381a466

File tree

9 files changed

+98
-120
lines changed

9 files changed

+98
-120
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"global-shortcut": "patch"
3+
---
4+
5+
Refactored the Rust APIs:
6+
7+
- Renamed `GlobalShortcut::on_all_shortcuts` to `GlobalShortcut::on_shortcuts`
8+
- Renamed `GlobalShortcut::register_all` to `GlobalShortcut::register_multiple`
9+
- Changed `GlobalShortcut::unregister_all` behavior to remove all registerd shortcuts.
10+
- Added `GlobalShortcut::unregister_multiple` to register a list of shortcuts (old behavior of `unregister_all`).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"global-shortcut-js": "patch"
3+
---
4+
5+
Refactored the JS APIs:
6+
7+
- Enhanced `register` and `unregister` to take either a single shortcut or an array.
8+
- Removed `registerAll` instead use `register` with an array.

examples/api/src-tauri/gen/schemas/desktop-schema.json

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4850,13 +4850,6 @@
48504850
"global-shortcut:allow-register"
48514851
]
48524852
},
4853-
{
4854-
"description": "global-shortcut:allow-register-all -> Enables the register_all command without any pre-configured scope.",
4855-
"type": "string",
4856-
"enum": [
4857-
"global-shortcut:allow-register-all"
4858-
]
4859-
},
48604853
{
48614854
"description": "global-shortcut:allow-unregister -> Enables the unregister command without any pre-configured scope.",
48624855
"type": "string",
@@ -4885,13 +4878,6 @@
48854878
"global-shortcut:deny-register"
48864879
]
48874880
},
4888-
{
4889-
"description": "global-shortcut:deny-register-all -> Denies the register_all command without any pre-configured scope.",
4890-
"type": "string",
4891-
"enum": [
4892-
"global-shortcut:deny-register-all"
4893-
]
4894-
},
48954881
{
48964882
"description": "global-shortcut:deny-unregister -> Denies the unregister command without any pre-configured scope.",
48974883
"type": "string",

examples/api/src/views/Shortcuts.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<script>
2-
import { writable } from "svelte/store";
2+
import { writable, get } from "svelte/store";
33
import {
44
register as registerShortcut,
55
unregister as unregisterShortcut,
6-
unregisterAll as unregisterAllShortcuts,
76
} from "@tauri-apps/plugin-global-shortcut";
87
98
export let onMessage;
@@ -35,7 +34,7 @@
3534
}
3635
3736
function unregisterAll() {
38-
unregisterAllShortcuts()
37+
unregisterShortcut(get(shortcuts))
3938
.then(() => {
4039
shortcuts.update(() => []);
4140
onMessage(`Unregistered all shortcuts`);

plugins/global-shortcut/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/global-shortcut/build.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
const COMMANDS: &[&str] = &[
6-
"register",
7-
"register_all",
8-
"unregister",
9-
"unregister_all",
10-
"is_registered",
11-
];
5+
const COMMANDS: &[&str] = &["register", "unregister", "unregister_all", "is_registered"];
126

137
fn main() {
148
tauri_plugin::Builder::new(COMMANDS)

plugins/global-shortcut/guest-js/index.ts

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,28 @@ export interface ShortcutEvent {
1919
export type ShortcutHandler = (event: ShortcutEvent) => void;
2020

2121
/**
22-
* Register a global shortcut.
22+
* Register a global shortcut or a list of shortcuts.
23+
*
24+
* The handler is called when any of the registered shortcuts are pressed by the user.
25+
*
26+
* If the shortcut is already taken by another application, the handler will not be triggered.
27+
* Make sure the shortcut is as unique as possible while still taking user experience into consideration.
28+
*
2329
* @example
2430
* ```typescript
2531
* import { register } from '@tauri-apps/plugin-global-shortcut';
32+
*
33+
* // register a single hotkey
2634
* await register('CommandOrControl+Shift+C', (event) => {
2735
* if (event.state === "Pressed") {
2836
* console.log('Shortcut triggered');
2937
* }
3038
* });
39+
*
40+
* // or register multiple hotkeys at once
41+
* await register(['CommandOrControl+Shift+C', 'Alt+A'], (event) => {
42+
* console.log(`Shortcut ${event.shortcut} triggered`);
43+
* });
3144
* ```
3245
*
3346
* @param shortcut Shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
@@ -36,97 +49,75 @@ export type ShortcutHandler = (event: ShortcutEvent) => void;
3649
* @since 2.0.0
3750
*/
3851
async function register(
39-
shortcut: string,
52+
shortcuts: string | string[],
4053
handler: ShortcutHandler,
4154
): Promise<void> {
4255
const h = new Channel<ShortcutEvent>();
4356
h.onmessage = handler;
4457

45-
await invoke("plugin:global-shortcut|register", {
46-
shortcut,
58+
return await invoke("plugin:global-shortcut|register", {
59+
shortcuts: Array.isArray(shortcuts) ? shortcuts : [shortcuts],
4760
handler: h,
4861
});
4962
}
5063

5164
/**
52-
* Register a collection of global shortcuts.
65+
* Unregister a global shortcut or a list of shortcuts.
66+
*
5367
* @example
5468
* ```typescript
55-
* import { registerAll } from '@tauri-apps/plugin-global-shortcut';
56-
* await registerAll(['CommandOrControl+Shift+C', 'Ctrl+Alt+F12'], (event) => {
57-
* console.log(`Shortcut ${event.shortcut} ${event.state}`);
58-
* });
69+
* import { unregister } from '@tauri-apps/plugin-global-shortcut';
70+
*
71+
* // unregister a single hotkey
72+
* await unregister('CmdOrControl+Space');
73+
*
74+
* // or unregister multiple hotkeys at the same time
75+
* await unregister(['CmdOrControl+Space', 'Alt+A']);
5976
* ```
6077
*
61-
* @param shortcuts Array of shortcut definitions, modifiers and key separated by "+" e.g. CmdOrControl+Q
62-
* @param handler Shortcut handler callback - takes the triggered shortcut as argument
78+
* @param shortcut shortcut definition (modifiers and key separated by "+" e.g. CmdOrControl+Q), also accepts a list of shortcuts
6379
*
6480
* @since 2.0.0
6581
*/
66-
async function registerAll(
67-
shortcuts: string[],
68-
handler: ShortcutHandler,
69-
): Promise<void> {
70-
const h = new Channel<ShortcutEvent>();
71-
h.onmessage = handler;
72-
73-
await invoke("plugin:global-shortcut|register_all", {
74-
shortcuts,
75-
handler: h,
82+
async function unregister(shortcuts: string | string[]): Promise<void> {
83+
return await invoke("plugin:global-shortcut|unregister", {
84+
shortcuts: Array.isArray(shortcuts) ? shortcuts : [shortcuts],
7685
});
7786
}
7887

7988
/**
80-
* Determines whether the given shortcut is registered by this application or not.
81-
*
82-
* If the shortcut is registered by another application, it will still return `false`.
89+
* Unregister all global shortcuts.
8390
*
8491
* @example
8592
* ```typescript
86-
* import { isRegistered } from '@tauri-apps/plugin-global-shortcut';
87-
* const isRegistered = await isRegistered('CommandOrControl+P');
93+
* import { unregisterAll } from '@tauri-apps/plugin-global-shortcut';
94+
* await unregisterAll();
8895
* ```
89-
*
90-
* @param shortcut shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
91-
*
9296
* @since 2.0.0
9397
*/
94-
async function isRegistered(shortcut: string): Promise<boolean> {
95-
return await invoke("plugin:global-shortcut|is_registered", {
96-
shortcut,
97-
});
98+
async function unregisterAll(): Promise<void> {
99+
return await invoke("plugin:global-shortcut|unregister_all", {});
98100
}
99101

100102
/**
101-
* Unregister a global shortcut.
103+
* Determines whether the given shortcut is registered by this application or not.
104+
*
105+
* If the shortcut is registered by another application, it will still return `false`.
106+
*
102107
* @example
103108
* ```typescript
104-
* import { unregister } from '@tauri-apps/plugin-global-shortcut';
105-
* await unregister('CmdOrControl+Space');
109+
* import { isRegistered } from '@tauri-apps/plugin-global-shortcut';
110+
* const isRegistered = await isRegistered('CommandOrControl+P');
106111
* ```
107112
*
108113
* @param shortcut shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
109114
*
110115
* @since 2.0.0
111116
*/
112-
async function unregister(shortcut: string): Promise<void> {
113-
await invoke("plugin:global-shortcut|unregister", {
117+
async function isRegistered(shortcut: string): Promise<boolean> {
118+
return await invoke("plugin:global-shortcut|is_registered", {
114119
shortcut,
115120
});
116121
}
117122

118-
/**
119-
* Unregisters all shortcuts registered by the application.
120-
* @example
121-
* ```typescript
122-
* import { unregisterAll } from '@tauri-apps/plugin-global-shortcut';
123-
* await unregisterAll();
124-
* ```
125-
*
126-
* @since 2.0.0
127-
*/
128-
async function unregisterAll(): Promise<void> {
129-
await invoke("plugin:global-shortcut|unregister_all");
130-
}
131-
132-
export { register, registerAll, isRegistered, unregister, unregisterAll };
123+
export { register, unregister, unregisterAll, isRegistered };

plugins/global-shortcut/permissions/default.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[default]
33
description = """
44
No features are enabled by default, as we believe
5-
the shortcuts can be inherently dangerous and it is
5+
the shortcuts can be inherently dangerous and it is
66
application specific if specific shortcuts should be
77
registered or unregistered.
88
"""

0 commit comments

Comments
 (0)