mirror of
https://github.com/tauri-apps/tauri-plugin-app.git
synced 2026-02-04 02:41:21 +01:00
feat(plugins): inject API on window.__TAURI__ (#383)
Committed via a GitHub action: https://github.com/tauri-apps/plugins-workspace/actions/runs/5060238742 Co-authored-by: lucasfernog <lucasfernog@users.noreply.github.com>
This commit is contained in:
committed by
tauri-bot
parent
3e7b08b81a
commit
1f291d46a4
10
dist-js/index.d.ts
vendored
10
dist-js/index.d.ts
vendored
@@ -1,3 +1,13 @@
|
||||
/**
|
||||
* Get application metadata.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
declare global {
|
||||
interface Window {
|
||||
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Gets the application version.
|
||||
* @example
|
||||
|
||||
16
dist-js/index.min.js
vendored
16
dist-js/index.min.js
vendored
@@ -1,8 +1,6 @@
|
||||
var f$1=Object.defineProperty;var g=(a,b)=>{for(var c in b)f$1(a,c,{get:b[c],enumerable:!0});};var e=(a,b,c)=>{if(!b.has(a))throw TypeError("Cannot "+c)};var h$1=(a,b,c)=>(e(a,b,"read from private field"),c?c.call(a):b.get(a)),i=(a,b,c)=>{if(b.has(a))throw TypeError("Cannot add the same private member more than once");b instanceof WeakSet?b.add(a):b.set(a,c);},j=(a,b,c,d)=>(e(a,b,"write to private field"),d?d.call(a,c):b.set(a,c),c);
|
||||
|
||||
var f={};g(f,{Channel:()=>a,PluginListener:()=>c,addPluginListener:()=>v,convertFileSrc:()=>m,invoke:()=>u,transformCallback:()=>o});function h(){return window.crypto.getRandomValues(new Uint32Array(1))[0]}function o(t,e=!1){let n=h(),r=`_${n}`;return Object.defineProperty(window,r,{value:i=>(e&&Reflect.deleteProperty(window,r),t?.(i)),writable:!1,configurable:!0}),n}var s,a=class{constructor(){this.__TAURI_CHANNEL_MARKER__=!0;i(this,s,()=>{});this.id=o(e=>{h$1(this,s).call(this,e);});}set onmessage(e){j(this,s,e);}get onmessage(){return h$1(this,s)}toJSON(){return `__CHANNEL__:${this.id}`}};s=new WeakMap;var c=class{constructor(e,n,r){this.plugin=e,this.event=n,this.channelId=r;}async unregister(){return u(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function v(t,e,n){let r=new a;return r.onmessage=n,u(`plugin:${t}|register_listener`,{event:e,handler:r}).then(()=>new c(t,e,r.id))}async function u(t,e={}){return new Promise((n,r)=>{let i=o(d=>{n(d),Reflect.deleteProperty(window,`_${g}`);},!0),g=o(d=>{r(d),Reflect.deleteProperty(window,`_${i}`);},!0);window.__TAURI_IPC__({cmd:t,callback:i,error:g,...e});})}function m(t,e="asset"){let n=encodeURIComponent(t);return navigator.userAgent.includes("Windows")?`https://${e}.localhost/${n}`:`${e}://localhost/${n}`}
|
||||
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
/**
|
||||
* Gets the application version.
|
||||
* @example
|
||||
@@ -14,7 +12,7 @@ var f={};g(f,{Channel:()=>a,PluginListener:()=>c,addPluginListener:()=>v,convert
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getVersion() {
|
||||
return u("plugin:app|version");
|
||||
return window.__TAURI_INVOKE__("plugin:app|version");
|
||||
}
|
||||
/**
|
||||
* Gets the application name.
|
||||
@@ -27,7 +25,7 @@ async function getVersion() {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getName() {
|
||||
return u("plugin:app|name");
|
||||
return window.__TAURI_INVOKE__("plugin:app|name");
|
||||
}
|
||||
/**
|
||||
* Gets the Tauri version.
|
||||
@@ -41,7 +39,7 @@ async function getName() {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getTauriVersion() {
|
||||
return u("plugin:app|tauri_version");
|
||||
return window.__TAURI_INVOKE__("plugin:app|tauri_version");
|
||||
}
|
||||
/**
|
||||
* Shows the application on macOS. This function does not automatically focus any specific app window.
|
||||
@@ -55,7 +53,7 @@ async function getTauriVersion() {
|
||||
* @since 1.2.0
|
||||
*/
|
||||
async function show() {
|
||||
return u("plugin:app|show");
|
||||
return window.__TAURI_INVOKE__("plugin:app|show");
|
||||
}
|
||||
/**
|
||||
* Hides the application on macOS.
|
||||
@@ -69,7 +67,7 @@ async function show() {
|
||||
* @since 1.2.0
|
||||
*/
|
||||
async function hide() {
|
||||
return u("plugin:app|hide");
|
||||
return window.__TAURI_INVOKE__("plugin:app|hide");
|
||||
}
|
||||
|
||||
export { getName, getTauriVersion, getVersion, hide, show };
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,6 +1,6 @@
|
||||
import { invoke } from '@tauri-apps/api/tauri';
|
||||
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
/**
|
||||
* Gets the application version.
|
||||
* @example
|
||||
@@ -12,7 +12,7 @@ import { invoke } from '@tauri-apps/api/tauri';
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getVersion() {
|
||||
return invoke("plugin:app|version");
|
||||
return window.__TAURI_INVOKE__("plugin:app|version");
|
||||
}
|
||||
/**
|
||||
* Gets the application name.
|
||||
@@ -25,7 +25,7 @@ async function getVersion() {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getName() {
|
||||
return invoke("plugin:app|name");
|
||||
return window.__TAURI_INVOKE__("plugin:app|name");
|
||||
}
|
||||
/**
|
||||
* Gets the Tauri version.
|
||||
@@ -39,7 +39,7 @@ async function getName() {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getTauriVersion() {
|
||||
return invoke("plugin:app|tauri_version");
|
||||
return window.__TAURI_INVOKE__("plugin:app|tauri_version");
|
||||
}
|
||||
/**
|
||||
* Shows the application on macOS. This function does not automatically focus any specific app window.
|
||||
@@ -53,7 +53,7 @@ async function getTauriVersion() {
|
||||
* @since 1.2.0
|
||||
*/
|
||||
async function show() {
|
||||
return invoke("plugin:app|show");
|
||||
return window.__TAURI_INVOKE__("plugin:app|show");
|
||||
}
|
||||
/**
|
||||
* Hides the application on macOS.
|
||||
@@ -67,7 +67,7 @@ async function show() {
|
||||
* @since 1.2.0
|
||||
*/
|
||||
async function hide() {
|
||||
return invoke("plugin:app|hide");
|
||||
return window.__TAURI_INVOKE__("plugin:app|hide");
|
||||
}
|
||||
|
||||
export { getName, getTauriVersion, getVersion, hide, show };
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"version":3,"file":"index.mjs","sources":["../guest-js/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;AAYA;;;;;;;;;AASG;AACH,eAAe,UAAU,GAAA;AACvB,IAAA,OAAO,MAAM,CAAC,oBAAoB,CAAC,CAAC;AACtC,CAAC;AAED;;;;;;;;;AASG;AACH,eAAe,OAAO,GAAA;AACpB,IAAA,OAAO,MAAM,CAAC,iBAAiB,CAAC,CAAC;AACnC,CAAC;AAED;;;;;;;;;;AAUG;AACH,eAAe,eAAe,GAAA;AAC5B,IAAA,OAAO,MAAM,CAAC,0BAA0B,CAAC,CAAC;AAC5C,CAAC;AAED;;;;;;;;;;AAUG;AACH,eAAe,IAAI,GAAA;AACjB,IAAA,OAAO,MAAM,CAAC,iBAAiB,CAAC,CAAC;AACnC,CAAC;AAED;;;;;;;;;;AAUG;AACH,eAAe,IAAI,GAAA;AACjB,IAAA,OAAO,MAAM,CAAC,iBAAiB,CAAC,CAAC;AACnC;;;;"}
|
||||
{"version":3,"file":"index.mjs","sources":["../guest-js/index.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;AACA;AACA;AAcA;;;;;;;;;AASG;AACH,eAAe,UAAU,GAAA;AACvB,IAAA,OAAO,MAAM,CAAC,gBAAgB,CAAC,oBAAoB,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;;;;AASG;AACH,eAAe,OAAO,GAAA;AACpB,IAAA,OAAO,MAAM,CAAC,gBAAgB,CAAC,iBAAiB,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;;;;;AAUG;AACH,eAAe,eAAe,GAAA;AAC5B,IAAA,OAAO,MAAM,CAAC,gBAAgB,CAAC,0BAA0B,CAAC,CAAC;AAC7D,CAAC;AAED;;;;;;;;;;AAUG;AACH,eAAe,IAAI,GAAA;AACjB,IAAA,OAAO,MAAM,CAAC,gBAAgB,CAAC,iBAAiB,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;;;;;AAUG;AACH,eAAe,IAAI,GAAA;AACjB,IAAA,OAAO,MAAM,CAAC,gBAAgB,CAAC,iBAAiB,CAAC,CAAC;AACpD;;;;"}
|
||||
@@ -8,7 +8,11 @@
|
||||
* @module
|
||||
*/
|
||||
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
declare global {
|
||||
interface Window {
|
||||
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the application version.
|
||||
@@ -21,7 +25,7 @@ import { invoke } from "@tauri-apps/api/tauri";
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getVersion(): Promise<string> {
|
||||
return invoke("plugin:app|version");
|
||||
return window.__TAURI_INVOKE__("plugin:app|version");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,7 +39,7 @@ async function getVersion(): Promise<string> {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getName(): Promise<string> {
|
||||
return invoke("plugin:app|name");
|
||||
return window.__TAURI_INVOKE__("plugin:app|name");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,7 +54,7 @@ async function getName(): Promise<string> {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function getTauriVersion(): Promise<string> {
|
||||
return invoke("plugin:app|tauri_version");
|
||||
return window.__TAURI_INVOKE__("plugin:app|tauri_version");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,7 +69,7 @@ async function getTauriVersion(): Promise<string> {
|
||||
* @since 1.2.0
|
||||
*/
|
||||
async function show(): Promise<void> {
|
||||
return invoke("plugin:app|show");
|
||||
return window.__TAURI_INVOKE__("plugin:app|show");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +84,7 @@ async function show(): Promise<void> {
|
||||
* @since 1.2.0
|
||||
*/
|
||||
async function hide(): Promise<void> {
|
||||
return invoke("plugin:app|hide");
|
||||
return window.__TAURI_INVOKE__("plugin:app|hide");
|
||||
}
|
||||
|
||||
export { getName, getVersion, getTauriVersion, show, hide };
|
||||
|
||||
1
src/api-iife.js
Normal file
1
src/api-iife.js
Normal file
@@ -0,0 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_APP__=function(_){"use strict";return _.getName=async function(){return window.__TAURI_INVOKE__("plugin:app|name")},_.getTauriVersion=async function(){return window.__TAURI_INVOKE__("plugin:app|tauri_version")},_.getVersion=async function(){return window.__TAURI_INVOKE__("plugin:app|version")},_.hide=async function(){return window.__TAURI_INVOKE__("plugin:app|hide")},_.show=async function(){return window.__TAURI_INVOKE__("plugin:app|show")},_}({});Object.defineProperty(window.__TAURI__,"app",{value:__TAURI_APP__})}
|
||||
@@ -11,6 +11,7 @@ mod commands;
|
||||
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
Builder::new("app")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::version,
|
||||
commands::name,
|
||||
|
||||
Reference in New Issue
Block a user