Skip to content

Commit 855effa

Browse files
authored
feat(core): globalShortcut API (#1232)
1 parent 772d83e commit 855effa

File tree

22 files changed

+481
-15
lines changed

22 files changed

+481
-15
lines changed

.changes/shortcut-api.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"api": minor
3+
"tauri-api": minor
4+
"tauri": minor
5+
---
6+
7+
Adds a global shortcut API.

api/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"./notification": "./dist/notification.js",
1515
"./tauri": "./dist/tauri.js",
1616
"./window": "./dist/window.js",
17-
"./shell": "./dist/shell.js"
17+
"./shell": "./dist/shell.js",
18+
"./globalShortcut": "./dist/globalShortcut.js"
1819
},
1920
"funding": {
2021
"type": "opencollective",

api/rollup.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export default [
2020
tauri: './src/tauri.ts',
2121
window: './src/window.ts',
2222
cli: './src/cli.ts',
23-
notification: './src/notification.ts'
23+
notification: './src/notification.ts',
24+
globalShortcut: './src/globalShortcut.ts'
2425
},
2526
treeshake: true,
2627
perf: true,

api/src/bundle.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as shell from './shell'
99
import * as tauri from './tauri'
1010
import * as window from './window'
1111
import * as notification from './notification'
12+
import * as globalShortcut from './globalShortcut'
1213

1314
export {
1415
cli,
@@ -20,5 +21,6 @@ export {
2021
shell,
2122
tauri,
2223
window,
23-
notification
24+
notification,
25+
globalShortcut
2426
}

api/src/globalShortcut.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { promisified, transformCallback } from './tauri'
2+
3+
/**
4+
* register a global shortcut
5+
* @param shortcut shortcut definition, modifiers and key separated by "+" e.g. Alt+Q
6+
* @param handler shortcut handler callback
7+
*/
8+
async function registerShortcut(
9+
shortcut: string,
10+
handler: () => void
11+
): Promise<void> {
12+
return await promisified({
13+
module: 'GlobalShortcut',
14+
message: {
15+
cmd: 'register',
16+
shortcut,
17+
handler: transformCallback(handler)
18+
}
19+
})
20+
}
21+
22+
/**
23+
* unregister a global shortcut
24+
* @param shortcut shortcut definition, modifiers and key separated by "+" e.g. Alt+Q
25+
*/
26+
async function unregisterShortcut(shortcut: string): Promise<void> {
27+
return await promisified({
28+
module: 'GlobalShortcut',
29+
message: {
30+
cmd: 'unregister',
31+
shortcut
32+
}
33+
})
34+
}
35+
36+
export {
37+
registerShortcut,
38+
unregisterShortcut
39+
}

api/src/http.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,4 @@ async function deleteRequest<T>(
155155
})
156156
}
157157

158-
export {
159-
request,
160-
get,
161-
post,
162-
put,
163-
patch,
164-
deleteRequest as httpDelete,
165-
}
158+
export { request, get, post, put, patch, deleteRequest as httpDelete }

api/src/window.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function resize(width: number, height: number): void {
187187
message: {
188188
cmd: 'resize',
189189
width,
190-
height,
190+
height
191191
}
192192
})
193193
}

tauri-api/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ tauri-utils = { version = "0.5", path = "../tauri-utils" }
3535
clap = { version = "=3.0.0-beta.2", optional = true }
3636
notify-rust = { version = "4.2.2", optional = true }
3737
once_cell = "1.5.2"
38+
tauri-hotkey = { git = "https://github.com/tauri-apps/tauri-hotkey-rs", branch = "dev", optional = true }
3839

3940
[dev-dependencies]
4041
quickcheck = "1.0.3"
@@ -43,3 +44,4 @@ quickcheck_macros = "1.0.0"
4344
[features]
4445
cli = [ "clap" ]
4546
notification = [ "notify-rust" ]
47+
global-shortcut = [ "tauri-hotkey" ]

tauri-api/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ pub enum Error {
5353
#[cfg(feature = "cli")]
5454
#[error("failed to parse CLI arguments: {0}")]
5555
ParseCliArguments(#[from] clap::Error),
56+
/// Shortcut error.
57+
#[cfg(feature = "global-shortcut")]
58+
#[error("shortcut error: {0}")]
59+
Shortcut(#[from] tauri_hotkey::Error),
5660
}
5761

5862
impl From<attohttpc::StatusCode> for Error {

tauri-api/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ pub mod cli;
3030
#[macro_use]
3131
extern crate clap;
3232

33+
/// Global shortcuts interface.
34+
#[cfg(feature = "global-shortcut")]
35+
pub mod shortcuts;
36+
3337
/// The desktop notifications API module.
3438
#[cfg(feature = "notification")]
3539
pub mod notification;

0 commit comments

Comments
 (0)