RevelaOS/assets/WindowManager-2a2c4c9c.js
2024-02-14 18:14:39 +00:00

23 lines
6.4 KiB
JavaScript

import{H as r}from"./index-10f4832e.js";import{s as m}from"./utils-2c87b698.js";import{n as w}from"./application-default-icon-38d880e7.js";function v(n,t){let s=0,e=0;n.querySelector("window-header")?.addEventListener("mousedown",i);function i(o){o.preventDefault(),d(),s=o.clientX,e=o.clientY,document.onmouseup=l,document.onmousemove=h}function h(o){o.preventDefault();const c=o.clientX-s,p=o.clientY-e,y=n.offsetTop+p,u=n.offsetLeft+c,x=t.offsetWidth,g=t.offsetHeight;y>=0&&y+n.offsetHeight<=g&&(n.style.top=`${y}px`),u>=0&&u+n.offsetWidth<=x&&(n.style.left=`${u}px`),s=o.clientX,e=o.clientY}function l(){document.onmouseup=null,document.onmousemove=null,t.onmouseleave=null}function d(){l(),t.onmouseenter=null}}class f{element;header;realContent;content;maximized;minimized;width;height;isMinimized=!1;isMaximized=!1;wm;process;onClose;config;constructor(t,s,e,i=()=>{}){this.process=t,this.wm=s,this.config=e,this.onClose=i,this.element=document.createElement("window"),this.element.style.zIndex=(s.getHighestZIndex()+1).toString(),this.element.style.position="absolute",this.focus(),this.element.onmousedown=()=>{this.focus()},(e.canResize===void 0||e.canResize===null)&&(e.canResize=!0),e.canResize||(this.element.style.resize="none"),this.element.style.width=`${e.width??300}px`,this.element.style.height=`${e.height??200}px`,this.header=document.createElement("window-header"),this.header.innerHTML=`<img alt="${m(e.title)} icon" src="${m(e.icon===""?w:e.icon)}"></img> <div class="title">${m(e.title)}</div><div style="flex:1;"></div><i id="min" class='material-symbols-rounded' style="margin-bottom: 5px;">minimize</i><i id="close" class='material-symbols-rounded'>close</i>`,e.canResize&&(this.header.innerHTML=`<img alt="${m(e.title)} icon" src="${m(e.icon===""?w:e.icon)}"></img> <div class="title">${m(e.title)}</div><div style="flex:1;"></div><i id="min" class='material-symbols-rounded' style="margin-bottom: 5px;">minimize</i><i id="max" class='material-symbols-rounded' style="font-size: 20px;">square</i><i id="close" class='material-symbols-rounded'>close</i>`),this.header.querySelector("#close").onclick=()=>{this.process.kill().catch(d=>console.error(d))},this.header.querySelector("#min").onclick=()=>this.toggleMin(),e.canResize&&(this.header.querySelector("#max").onclick=()=>this.toggleMax()),this.realContent=document.createElement("window-content");const h=this.realContent.attachShadow({mode:"open"});h.innerHTML=`
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" />
<style>
.material-symbols-rounded {
font-variation-settings:
'FILL' 0,
'wght' 400,
'GRAD' 200,
'opsz' 48
}
</style>
<style>
* {
-ms-overflow-style: none;
scrollbar-width: none;
font-family: "Satoshi", sans-serif;
font-weight: 600;
color: var(--text);
}
</style>
`;const l=document.createElement("body");l.style.margin="0px",l.style.height="100%",h.appendChild(l),this.content=l,this.element.appendChild(this.header),this.element.appendChild(this.realContent),v(this.element,document.querySelector("window-area"))}toggleMin(){return this.isMinimized?(this.element.style.pointerEvents="all",this.element.style.opacity="1",this.element.style.transform="translateY(0)"):(this.element.style.pointerEvents="none",this.element.style.opacity="0",this.element.style.transform="translateY(10px)"),this.isMinimized=!this.isMinimized,this.isMinimized}prevTop;prevLeft;prevWidth;prevHeight;toggleMax(){return this.isMaximized?(this.element.style.width=this.prevWidth,this.element.style.height=this.prevHeight,this.element.style.top=this.prevTop,this.element.style.left=this.prevLeft):(this.prevTop=this.element.style.top,this.prevLeft=this.element.style.left,this.prevWidth=this.element.style.width,this.prevHeight=this.element.style.height,this.element.style.top="0",this.element.style.left="0",this.element.style.width="calc(100% - 2px)",this.element.style.height="calc(100% - 3px)"),this.isMaximized=!this.isMaximized,this.isMaximized}focus(){this.element.style.zIndex!==this.wm.getHighestZIndex().toString()&&(this.element.style.zIndex=(this.wm.getHighestZIndex()+1).toString())}close(){this.element.style.pointerEvents="none",this.element.style.opacity="0",this.element.style.transform="translateY(10px)",this.onClose(),setTimeout(()=>{this.element.remove()},200)}setTitle(t){this.header.querySelector(".title").innerText=t}}const a={config:{name:"WindowManager",type:"library",targetVer:"1.0.0-indev.0"},init:(n,t,s)=>{document.addEventListener("app_closed",e=>{a.data.windows.find(i=>i.process.token===e.detail.token?(i.close(),!0):!1)})},data:{windowArea:new r("window-area"),windows:[],getHighestZIndex:()=>{const n=a.data.windows.map(s=>parseInt(s.element.style.zIndex)),t=Math.max(...n);return t===-1/0?0:t},createWindow:(n,t)=>{const s=new f(t,a.data,n),e={detail:{token:t.token,proc:t.process,win:s}};return document.dispatchEvent(new CustomEvent("app_opened",e)),a.data.windows.push(s),a.data.windowArea.elm.appendChild(s.element),s},createModal:async(n,t,s,e)=>{const i=new f(e,a.data,{title:t,icon:"",width:350,height:150,canResize:!1}),h={detail:{token:e.token,proc:e.process,win:i}},{Button:l}=await e.loadLibrary("lib/Components");return{value:await new Promise(d=>{i.content.style.padding="10px",i.content.style.display="flex",i.content.style.flexDirection="column",i.content.style.gap="10px";const o=new r("div").style({display:"flex",gap:"10px",justifyContent:"center",height:"max-content"}).appendTo(i.content);new r("img").attr({src:e.process.config.icon??w}).style({height:"100%","aspect-ratio":"1 / 1",borderRadius:"50%",alignSelf:"center"}).appendTo(o);const c=new r("div").style({display:"flex",gap:"10px","flex-direction":"column","justify-content":"center",height:"max-content"}).appendTo(o);new r("h3").text(t).style({margin:"0"}).appendTo(c),new r("p").text(s).style({margin:"0"}).appendTo(c);const p=new r("div").style({display:"flex",gap:"10px",alignItems:"right"}).appendTo(c);n==="allow"&&document.dispatchEvent(new CustomEvent("app_opened",h)),n==="allow"?(l.new("primary").text("Allow").appendTo(p).on("click",()=>{d(!0),i.close()}),l.new().text("Deny").appendTo(p).on("click",()=>{d(!1),i.close()})):n==="ok"&&l.new("primary").text("OK").appendTo(p).on("click",()=>{i.close(),d(!0)}),a.data.windows.push(i),a.data.windowArea.elm.appendChild(i.element)}),win:i}}}};export{a as default};