You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tauri-plugins-workspace/plugins/window/src/api-iife.js

2 lines
12 KiB

if("__TAURI__"in window){var __TAURI_WINDOW__=function(e){"use strict";var n=Object.defineProperty,t=(e,t)=>{for(var i in t)n(e,i,{get:t[i],enumerable:!0})},i=(e,n,t)=>{if(!n.has(e))throw TypeError("Cannot "+t)},l=(e,n,t)=>(i(e,n,"read from private field"),t?t.call(e):n.get(e)),a=(e,n,t,l)=>(i(e,n,"write to private field"),l?l.call(e,t):n.set(e,t),t);function s(e,n=!1){let t=window.crypto.getRandomValues(new Uint32Array(1))[0],i=`_${t}`;return Object.defineProperty(window,i,{value:t=>(n&&Reflect.deleteProperty(window,i),e?.(t)),writable:!1,configurable:!0}),t}t({},{Channel:()=>o,PluginListener:()=>u,addPluginListener:()=>c,convertFileSrc:()=>w,invoke:()=>h,transformCallback:()=>s});var r,o=class{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,((e,n,t)=>{if(n.has(e))throw TypeError("Cannot add the same private member more than once");n instanceof WeakSet?n.add(e):n.set(e,t)})(this,r,(()=>{})),this.id=s((e=>{l(this,r).call(this,e)}))}set onmessage(e){a(this,r,e)}get onmessage(){return l(this,r)}toJSON(){return`__CHANNEL__:${this.id}`}};r=new WeakMap;var u=class{constructor(e,n,t){this.plugin=e,this.event=n,this.channelId=t}async unregister(){return h(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function c(e,n,t){let i=new o;return i.onmessage=t,h(`plugin:${e}|register_listener`,{event:n,handler:i}).then((()=>new u(e,n,i.id)))}async function h(e,n={}){return new Promise(((t,i)=>{let l=s((e=>{t(e),Reflect.deleteProperty(window,`_${a}`)}),!0),a=s((e=>{i(e),Reflect.deleteProperty(window,`_${l}`)}),!0);window.__TAURI_IPC__({cmd:e,callback:l,error:a,...n})}))}function w(e,n="asset"){let t=encodeURIComponent(e);return navigator.userAgent.includes("Windows")?`https://${n}.localhost/${t}`:`${n}://localhost/${t}`}async function d(e,n){await h("plugin:event|unlisten",{event:e,eventId:n})}async function p(e,n,t){return h("plugin:event|listen",{event:e,windowLabel:n,handler:s(t)}).then((n=>async()=>d(e,n)))}t({},{TauriEvent:()=>b,emit:()=>m,listen:()=>g,once:()=>v});var y,_,b=((y=b||{}).WINDOW_RESIZED="tauri://resize",y.WINDOW_MOVED="tauri://move",y.WINDOW_CLOSE_REQUESTED="tauri://close-requested",y.WINDOW_CREATED="tauri://window-created",y.WINDOW_DESTROYED="tauri://destroyed",y.WINDOW_FOCUS="tauri://focus",y.WINDOW_BLUR="tauri://blur",y.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",y.WINDOW_THEME_CHANGED="tauri://theme-changed",y.WINDOW_FILE_DROP="tauri://file-drop",y.WINDOW_FILE_DROP_HOVER="tauri://file-drop-hover",y.WINDOW_FILE_DROP_CANCELLED="tauri://file-drop-cancelled",y.MENU="tauri://menu",y);async function g(e,n){return p(e,null,n)}async function v(e,n){return async function(e,n,t){return p(e,n,(n=>{t(n),d(e,n.id).catch((()=>{}))}))}(e,null,n)}async function m(e,n){return async function(e,n,t){await h("plugin:event|emit",{event:e,windowLabel:n,payload:t})}(e,void 0,n)}async function f(e,n){await h("plugin:event|unlisten",{event:e,eventId:n})}async function E(e,n,t){return h("plugin:event|listen",{event:e,windowLabel:n,handler:s(t)}).then((n=>async()=>f(e,n)))}class W{constructor(e,n){this.type="Logical",this.width=e,this.height=n}}class D{constructor(e,n){this.type="Physical",this.width=e,this.height=n}toLogical(e){return new W(this.width/e,this.height/e)}}class I{constructor(e,n){this.type="Logical",this.x=e,this.y=n}}class A{constructor(e,n){this.type="Physical",this.x=e,this.y=n}toLogical(e){return new I(this.x/e,this.y/e)}}function O(){return window.__TAURI_METADATA__.__windows.map((e=>new C(e.label,{skip:!0})))}e.UserAttentionType=void 0,(_=e.UserAttentionType||(e.UserAttentionType={}))[_.Critical=1]="Critical",_[_.Informational=2]="Informational";const P=["tauri://created","tauri://error"];class T{constructor(e){this.label=e,this.listeners=Object.create(null)}async listen(e,n){return this._handleTauriEvent(e,n)?Promise.resolve((()=>{const t=this.listeners[e];t.splice(t.indexOf(n),1)})):E(e,this.label,n)}async once(e,n){return this._handleTauriEvent(e,n)?Promise.resolve((()=>{const t=this.listeners[e];t.splice(t.indexOf(n),1)})):async function(e,n,t){return E(e,n,(n=>{t(n),f(e,n.id).catch((()=>{}))}))}(e,this.label,n)}async emit(e,n){if(P.includes(e)){for(const t of this.listeners[e]||[])t({event:e,id:-1,windowLabel:this.label,payload:n});return Promise.resolve()}return async function(e,n,t){await h("plugin:event|emit",{event:e,windowLabel:n,payload:t})}(e,this.label,n)}_handleTauriEvent(e,n){return!!P.includes(e)&&(e in this.listeners?this.listeners[e].push(n):this.listeners[e]=[n],!0)}}class L extends T{async scaleFactor(){return h("plugin:window|scale_factor",{label:this.label})}async innerPosition(){return h("plugin:window|inner_position",{label:this.label}).then((({x:e,y:n})=>new A(e,n)))}async outerPosition(){return h("plugin:window|outer_position",{label:this.label}).then((({x:e,y:n})=>new A(e,n)))}async innerSize(){return h("plugin:window|inner_size",{label:this.label}).then((({width:e,height:n})=>new D(e,n)))}async outerSize(){return h("plugin:window|outer_size",{label:this.label}).then((({width:e,height:n})=>new D(e,n)))}async isFullscreen(){return h("plugin:window|is_fullscreen",{label:this.label})}async isMinimized(){return h("plugin:window|is_minimized",{label:this.label})}async isMaximized(){return h("plugin:window|is_maximized",{label:this.label})}async isDecorated(){return h("plugin:window|is_decorated",{label:this.label})}async isResizable(){return h("plugin:window|is_resizable",{label:this.label})}async isVisible(){return h("plugin:window|is_visible",{label:this.label})}async title(){return h("plugin:window|title",{label:this.label})}async theme(){return h("plugin:window|theme",{label:this.label})}async center(){return h("plugin:window|center",{label:this.label})}async requestUserAttention(n){let t=null;return n&&(t=n===e.UserAttentionType.Critical?{type:"Critical"}:{type:"Informational"}),h("plugin:window|request_user_attention",{label:this.label,value:t})}async setResizable(e){return h("plugin:window|set_resizable",{label:this.label,value:e})}async setTitle(e){return h("plugin:window|set_title",{label:this.label,value:e})}async maximize(){return h("plugin:window|maximize",{label:this.label})}async unmaximize(){return h("plugin:window|unmaximize",{label:this.label})}async toggleMaximize(){return h("plugin:window|toggle_maximize",{label:this.label})}async minimize(){return h("plugin:window|minimize",{label:this.label})}async unminimize(){return h("plugin:window|unminimize",{label:this.label})}async show(){return h("plugin:window|show",{label:this.label})}async hide(){return h("plugin:window|hide",{label:this.label})}async close(){return h("plugin:window|close",{label:this.label})}async setDecorations(e){return h("plugin:window|set_decorations",{label:this.label,value:e})}async setShadow(e){return h("plugin:window|set_shadow",{label:this.label,value:e})}async setAlwaysOnTop(e){return h("plugin:window|set_always_on_top",{label:this.label,value:e})}async setContentProtected(e){return h("plugin:window|set_content_protected",{label:this.label,value:e})}async setSize(e){if(!e||"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `size` argument must be either a LogicalSize or a PhysicalSize instance");return h("plugin:window|set_size",{label:this.label,value:{type:e.type,data:{width:e.width,height:e.height}}})}async setMinSize(e){if(e&&"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `size` argument must be either a LogicalSize or a PhysicalSize instance");return h("plugin:window|set_min_size",{label:this.label,value:e?{type:e.type,data:{width:e.width,height:e.height}}:null})}async setMaxSize(e){if(e&&"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `size` argument must be either a LogicalSize or a PhysicalSize instance");return h("plugin:window|set_max_size",{label:this.label,value:e?{type:e.type,data:{width:e.width,height:e.height}}:null})}async setPosition(e){if(!e||"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `position` argument must be either a LogicalPosition or a PhysicalPosition instance");return h("plugin:window|set_position",{label:this.label,value:{type:e.type,data:{x:e.x,y:e.y}}})}async setFullscreen(e){return h("plugin:window|set_fullscreen",{label:this.label,value:e})}async setFocus(){return h("plugin:window|set_focus",{label:this.label})}async setIcon(e){return h("plugin:window|set_icon",{label:this.label,value:"string"==typeof e?e:Array.from(e)})}async setSkipTaskbar(e){return h("plugin:window|set_skip_taskbar",{label:this.label,value:e})}async setCursorGrab(e){return h("plugin:window|set_cursor_grab",{label:this.label,value:e})}async setCursorVisible(e){return h("plugin:window|set_cursor_visible",{label:this.label,value:e})}async setCursorIcon(e){return h("plugin:window|set_cursor_icon",{label:this.label,value:e})}async setCursorPosition(e){if(!e||"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `position` argument must be either a LogicalPosition or a PhysicalPosition instance");return h("plugin:window|set_cursor_position",{label:this.label,value:{type:e.type,data:{x:e.x,y:e.y}}})}async setIgnoreCursorEvents(e){return h("plugin:window|set_ignore_cursor_events",{label:this.label,value:e})}async startDragging(){return h("plugin:window|start_dragging",{label:this.label})}async onResized(e){return this.listen(b.WINDOW_RESIZED,(n=>{n.payload=S(n.payload),e(n)}))}async onMoved(e){return this.listen(b.WINDOW_MOVED,(n=>{n.payload=N(n.payload),e(n)}))}async onCloseRequested(e){return this.listen(b.WINDOW_CLOSE_REQUESTED,(n=>{const t=new z(n);Promise.resolve(e(t)).then((()=>{if(!t.isPreventDefault())return this.close()}))}))}async onFocusChanged(e){const n=await this.listen(b.WINDOW_FOCUS,(n=>{e({...n,payload:!0})})),t=await this.listen(b.WINDOW_BLUR,(n=>{e({...n,payload:!1})}));return()=>{n(),t()}}async onScaleChanged(e){return this.listen(b.WINDOW_SCALE_FACTOR_CHANGED,e)}async onMenuClicked(e){return this.listen(b.MENU,e)}async onFileDropEvent(e){const n=await this.listen(b.WINDOW_FILE_DROP,(n=>{e({...n,payload:{type:"drop",paths:n.payload}})})),t=await this.listen(b.WINDOW_FILE_DROP_HOVER,(n=>{e({...n,payload:{type:"hover",paths:n.payload}})})),i=await this.listen(b.WINDOW_FILE_DROP_CANCELLED,(n=>{e({...n,payload:{type:"cancel"}})}));return()=>{n(),t(),i()}}async onThemeChanged(e){return this.listen(b.WINDOW_THEME_CHANGED,e)}}class z{constructor(e){this._preventDefault=!1,this.event=e.event,this.windowLabel=e.windowLabel,this.id=e.id}preventDefault(){this._preventDefault=!0}isPreventDefault(){return this._preventDefault}}class C extends L{constructor(e,n={}){super(e),(null==n?void 0:n.skip)||h("plugin:window|create",{options:{...n,label:e}}).then((async()=>this.emit("tauri://created"))).catch((async e=>this.emit("tauri://error",e)))}static getByLabel(e){return O().some((n=>n.label===e))?new C(e,{skip:!0}):null}}function R(e){return null===e?null:{name:e.name,scaleFactor:e.scaleFactor,position:N(e.position),size:S(e.size)}}function N(e){return new A(e.x,e.y)}function S(e){return new D(e.width,e.height)}return e.appWindow=void 0,"__TAURI_METADATA__"in window?e.appWindow=new C(window.__TAURI_METADATA__.__currentWindow.label,{skip:!0}):(console.warn('Could not find "window.__TAURI_METADATA__". The "appWindow" value will reference the "main" window label.\nNote that this is not an issue if running this frontend on a browser instead of a Tauri window.'),e.appWindow=new C("main",{skip:!0})),e.CloseRequestedEvent=z,e.LogicalPosition=I,e.LogicalSize=W,e.PhysicalPosition=A,e.PhysicalSize=D,e.WebviewWindow=C,e.WebviewWindowHandle=T,e.WindowManager=L,e.availableMonitors=async function(){return h("plugin:window|available_monitors").then((e=>e.map(R)))},e.currentMonitor=async function(){return h("plugin:window|current_monitor").then(R)},e.getAll=O,e.getCurrent=function(){return new C(window.__TAURI_METADATA__.__currentWindow.label,{skip:!0})},e.primaryMonitor=async function(){return h("plugin:window|primary_monitor").then(R)},e}({});Object.defineProperty(window.__TAURI__,"window",{value:__TAURI_WINDOW__})}