feat: update to alpha.11 (#555)

This commit is contained in:
Lucas Fernandes Nogueira
2023-08-14 14:51:20 -03:00
committed by GitHub
parent d5a7c77a8d
commit d74fc0a097
67 changed files with 628 additions and 538 deletions
+14 -15
View File
@@ -8,14 +8,7 @@
* @module
*/
declare global {
interface Window {
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
__TAURI__: {
transformCallback: <T>(cb: (payload: T) => void) => number;
};
}
}
import { invoke, Channel } from "@tauri-apps/api/tauri";
export type ShortcutHandler = (shortcut: string) => void;
@@ -38,9 +31,12 @@ async function register(
shortcut: string,
handler: ShortcutHandler,
): Promise<void> {
return await window.__TAURI_INVOKE__("plugin:globalShortcut|register", {
const h = new Channel<string>();
h.onmessage = handler;
return await invoke("plugin:globalShortcut|register", {
shortcut,
handler: window.__TAURI__.transformCallback(handler),
handler: h,
});
}
@@ -63,9 +59,12 @@ async function registerAll(
shortcuts: string[],
handler: ShortcutHandler,
): Promise<void> {
return await window.__TAURI_INVOKE__("plugin:globalShortcut|register_all", {
const h = new Channel<string>();
h.onmessage = handler;
return await invoke("plugin:globalShortcut|register_all", {
shortcuts,
handler: window.__TAURI__.transformCallback(handler),
handler: h,
});
}
@@ -85,7 +84,7 @@ async function registerAll(
* @since 2.0.0
*/
async function isRegistered(shortcut: string): Promise<boolean> {
return await window.__TAURI_INVOKE__("plugin:globalShortcut|is_registered", {
return await invoke("plugin:globalShortcut|is_registered", {
shortcut,
});
}
@@ -103,7 +102,7 @@ async function isRegistered(shortcut: string): Promise<boolean> {
* @since 2.0.0
*/
async function unregister(shortcut: string): Promise<void> {
return await window.__TAURI_INVOKE__("plugin:globalShortcut|unregister", {
return await invoke("plugin:globalShortcut|unregister", {
shortcut,
});
}
@@ -119,7 +118,7 @@ async function unregister(shortcut: string): Promise<void> {
* @since 2.0.0
*/
async function unregisterAll(): Promise<void> {
return await window.__TAURI_INVOKE__("plugin:globalShortcut|unregister_all");
return await invoke("plugin:globalShortcut|unregister_all");
}
export { register, registerAll, isRegistered, unregister, unregisterAll };
+1 -1
View File
@@ -27,6 +27,6 @@
"tslib": "^2.4.1"
},
"dependencies": {
"@tauri-apps/api": "2.0.0-alpha.5"
"@tauri-apps/api": "2.0.0-alpha.6"
}
}
+1 -1
View File
@@ -1 +1 @@
if("__TAURI__"in window){var __TAURI_GLOBALSHORTCUT__=function(_){"use strict";return _.isRegistered=async function(_){return await window.__TAURI_INVOKE__("plugin:globalShortcut|is_registered",{shortcut:_})},_.register=async function(_,t){return await window.__TAURI_INVOKE__("plugin:globalShortcut|register",{shortcut:_,handler:window.__TAURI__.transformCallback(t)})},_.registerAll=async function(_,t){return await window.__TAURI_INVOKE__("plugin:globalShortcut|register_all",{shortcuts:_,handler:window.__TAURI__.transformCallback(t)})},_.unregister=async function(_){return await window.__TAURI_INVOKE__("plugin:globalShortcut|unregister",{shortcut:_})},_.unregisterAll=async function(){return await window.__TAURI_INVOKE__("plugin:globalShortcut|unregister_all")},_}({});Object.defineProperty(window.__TAURI__,"globalShortcut",{value:__TAURI_GLOBALSHORTCUT__})}
if("__TAURI__"in window){var __TAURI_GLOBALSHORTCUT__=function(e){"use strict";var t=Object.defineProperty,n=(e,t,n)=>{if(!t.has(e))throw TypeError("Cannot "+n)},r=(e,t,r)=>(n(e,t,"read from private field"),r?r.call(e):t.get(e)),i=(e,t,r,i)=>(n(e,t,"write to private field"),i?i.call(e,r):t.set(e,r),r);function a(e,t=!1){let n=window.crypto.getRandomValues(new Uint32Array(1))[0],r=`_${n}`;return Object.defineProperty(window,r,{value:n=>(t&&Reflect.deleteProperty(window,r),e?.(n)),writable:!1,configurable:!0}),n}((e,n)=>{for(var r in n)t(e,r,{get:n[r],enumerable:!0})})({},{Channel:()=>s,PluginListener:()=>l,addPluginListener:()=>c,convertFileSrc:()=>_,invoke:()=>u,transformCallback:()=>a});var o,s=class{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,((e,t,n)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,n)})(this,o,(()=>{})),this.id=a((e=>{r(this,o).call(this,e)}))}set onmessage(e){i(this,o,e)}get onmessage(){return r(this,o)}toJSON(){return`__CHANNEL__:${this.id}`}};o=new WeakMap;var l=class{constructor(e,t,n){this.plugin=e,this.event=t,this.channelId=n}async unregister(){return u(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function c(e,t,n){let r=new s;return r.onmessage=n,u(`plugin:${e}|register_listener`,{event:t,handler:r}).then((()=>new l(e,t,r.id)))}async function u(e,t={},n){return new Promise(((r,i)=>{let o=a((e=>{r(e),Reflect.deleteProperty(window,`_${s}`)}),!0),s=a((e=>{i(e),Reflect.deleteProperty(window,`_${o}`)}),!0);window.__TAURI_IPC__({cmd:e,callback:o,error:s,payload:t,options:n})}))}function _(e,t="asset"){return window.__TAURI__.convertFileSrc(e,t)}return e.isRegistered=async function(e){return await u("plugin:globalShortcut|is_registered",{shortcut:e})},e.register=async function(e,t){const n=new s;return n.onmessage=t,await u("plugin:globalShortcut|register",{shortcut:e,handler:n})},e.registerAll=async function(e,t){const n=new s;return n.onmessage=t,await u("plugin:globalShortcut|register_all",{shortcuts:e,handler:n})},e.unregister=async function(e){return await u("plugin:globalShortcut|unregister",{shortcut:e})},e.unregisterAll=async function(){return await u("plugin:globalShortcut|unregister_all")},e}({});Object.defineProperty(window.__TAURI__,"globalShortcut",{value:__TAURI_GLOBALSHORTCUT__})}
+19 -30
View File
@@ -23,7 +23,7 @@ use std::{
pub use global_hotkey::hotkey::{Code, HotKey as Shortcut, Modifiers};
use global_hotkey::{GlobalHotKeyEvent, GlobalHotKeyManager};
use tauri::{
api::ipc::CallbackFn,
ipc::Channel,
plugin::{Builder as PluginBuilder, TauriPlugin},
AppHandle, Manager, Runtime, State, Window,
};
@@ -35,21 +35,15 @@ type Result<T> = std::result::Result<T, Error>;
type HotKeyId = u32;
type HandlerFn = Box<dyn Fn(&Shortcut) + Send + Sync + 'static>;
enum ShortcutSource<R: Runtime> {
Ipc {
window: Window<R>,
handler: CallbackFn,
},
enum ShortcutSource {
Ipc(Channel),
Rust,
}
impl<R: Runtime> Clone for ShortcutSource<R> {
impl Clone for ShortcutSource {
fn clone(&self) -> Self {
match self {
Self::Ipc { window, handler } => Self::Ipc {
window: window.clone(),
handler: *handler,
},
Self::Ipc(channel) => Self::Ipc(channel.clone()),
Self::Rust => Self::Rust,
}
}
@@ -70,8 +64,8 @@ impl TryFrom<&str> for ShortcutWrapper {
}
}
struct RegisteredShortcut<R: Runtime> {
source: ShortcutSource<R>,
struct RegisteredShortcut {
source: ShortcutSource,
shortcut: (Shortcut, Option<String>),
}
@@ -79,14 +73,14 @@ pub struct GlobalShortcut<R: Runtime> {
#[allow(dead_code)]
app: AppHandle<R>,
manager: std::result::Result<GlobalHotKeyManager, global_hotkey::Error>,
shortcuts: Arc<Mutex<HashMap<HotKeyId, RegisteredShortcut<R>>>>,
shortcuts: Arc<Mutex<HashMap<HotKeyId, RegisteredShortcut>>>,
}
impl<R: Runtime> GlobalShortcut<R> {
fn register_internal(
&self,
shortcut: (Shortcut, Option<String>),
source: ShortcutSource<R>,
source: ShortcutSource,
) -> Result<()> {
let id = shortcut.0.id();
acquire_manager(&self.manager)?.register(shortcut.0)?;
@@ -100,7 +94,7 @@ impl<R: Runtime> GlobalShortcut<R> {
fn register_all_internal<S: IntoIterator<Item = (Shortcut, Option<String>)>>(
&self,
shortcuts: S,
source: ShortcutSource<R>,
source: ShortcutSource,
) -> Result<()> {
let hotkeys = shortcuts
.into_iter()
@@ -218,29 +212,29 @@ where
#[tauri::command]
fn register<R: Runtime>(
window: Window<R>,
_window: Window<R>,
global_shortcut: State<'_, GlobalShortcut<R>>,
shortcut: String,
handler: CallbackFn,
handler: Channel,
) -> Result<()> {
global_shortcut.register_internal(
(parse_shortcut(&shortcut)?, Some(shortcut)),
ShortcutSource::Ipc { window, handler },
ShortcutSource::Ipc(handler),
)
}
#[tauri::command]
fn register_all<R: Runtime>(
window: Window<R>,
_window: Window<R>,
global_shortcut: State<'_, GlobalShortcut<R>>,
shortcuts: Vec<String>,
handler: CallbackFn,
handler: Channel,
) -> Result<()> {
let mut hotkeys = Vec::new();
for shortcut in shortcuts {
hotkeys.push((parse_shortcut(&shortcut)?, Some(shortcut)));
}
global_shortcut.register_all_internal(hotkeys, ShortcutSource::Ipc { window, handler })
global_shortcut.register_all_internal(hotkeys, ShortcutSource::Ipc(handler))
}
#[tauri::command]
@@ -303,19 +297,14 @@ impl Builder {
])
.setup(move |app, _api| {
let shortcuts =
Arc::new(Mutex::new(HashMap::<HotKeyId, RegisteredShortcut<R>>::new()));
Arc::new(Mutex::new(HashMap::<HotKeyId, RegisteredShortcut>::new()));
let shortcuts_ = shortcuts.clone();
GlobalHotKeyEvent::set_event_handler(Some(move |e: GlobalHotKeyEvent| {
if let Some(shortcut) = shortcuts_.lock().unwrap().get(&e.id) {
match &shortcut.source {
ShortcutSource::Ipc { window, handler } => {
let callback_string = tauri::api::ipc::format_callback(
*handler,
&shortcut.shortcut.1,
)
.expect("unable to serialize shortcut string to json");
let _ = window.eval(callback_string.as_str());
ShortcutSource::Ipc(channel) => {
let _ = channel.send(&shortcut.shortcut.1);
}
ShortcutSource::Rust => {
if let Some(handler) = &handler {