index.html 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  1. <!DOCTYPE html>
  2. <html lang="zh-Hans-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>PageOS 用户桌面</title>
  7. <style>
  8. /* 全局 */
  9. * {
  10. color: var(--secondary-font-color);
  11. font-family: "Maple Mono CN", "Courier New", Courier, monospace;
  12. box-sizing: border-box;
  13. transition: all 0.2s ease;
  14. }
  15. ::selection {
  16. background: var(--accent-bg-color);
  17. color: var(--accent-font-color);
  18. }
  19. :root,
  20. body {
  21. height: 100%;
  22. margin: 0;
  23. overflow: hidden;
  24. --accent-color: #64ffda; /* 强调色:边框 */
  25. --secondary-color: #00695c; /* 次要色:未激活边框 */
  26. --danger-color: #ff6d00; /* 着重色:边框 */
  27. --accent-bg-color: #009688;
  28. --secondary-bg-color: #616161;
  29. --content-bg-color: #424242;
  30. --accent-font-color: #f5f5f5;
  31. --secondary-font-color: #eeeeee;
  32. --margin-padding: 0.5rem;
  33. --border-thickness: 0.2rem;
  34. --border-radius: 0.5rem;
  35. --title-bar-height: 2rem;
  36. --title-bar-justify-content: left;
  37. --task-bar-height: 3rem;
  38. --shadow: 0.6rem 0.6rem 0.1rem 0.1rem rgba(0, 0, 0, 0.2);
  39. --focused-bg-color: rgba(255, 255, 255, 0.1);
  40. --favicon: url("data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMiIgaGVpZ2h0PSIzMiIgdmlld0JveD0iMCAwIDMyIDMyIj4KICA8IS0tIOWkluahhuefqeW9oiAtLT4KICA8cmVjdCB4PSIyIiB5PSIyIiB3aWR0aD0iMjgiIGhlaWdodD0iMjgiIHJ4PSIxMCIgcnk9IjEwIgogICAgZmlsbD0iIzQyNDI0MiIKICAgIHN0cm9rZT0iIzAwNjk1YyIKICAgIHN0cm9rZS13aWR0aD0iNCIvPgoKICA8IS0tIOW3puecvO+8muWwj+WchueCuSAtLT4KICA8Y2lyY2xlIGN4PSIxMiIgY3k9IjE0IiByPSIxLjUiIGZpbGw9IiNmNWY1ZjUiIC8+CgogIDwhLS0g5Y+z55y877ya5bCP5ZyG54K5IC0tPgogIDxjaXJjbGUgY3g9IjIwIiBjeT0iMTQiIHI9IjEuNSIgZmlsbD0iI2Y1ZjVmNSIgLz4KCiAgPCEtLSDpvLvlrZDvvJrnq5bnur8gLS0+CiAgPGxpbmUgeDE9IjE2IiB5MT0iMTYiIHgyPSIxNiIgeTI9IjE4IiBzdHJva2U9IiNmNWY1ZjUiIHN0cm9rZS13aWR0aD0iMS41IiAvPgoKICA8IS0tIOW+rueskeeahOWYtO+8muS4iuaJrOeahOW8p+e6vyAtLT4KICA8cGF0aCBkPSJNIDE0IDIwIFEgMTYgMjIgMTggMjAiIAogICAgICAgIGZpbGw9Im5vbmUiIAogICAgICAgIHN0cm9rZT0iI2Y1ZjVmNSIgCiAgICAgICAgc3Ryb2tlLXdpZHRoPSIxLjUiIAogICAgICAgIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgLz4KPC9zdmc+");
  41. }
  42. button {
  43. background-color: var(--secondary-bg-color);
  44. background-position: center;
  45. background-repeat: no-repeat;
  46. border: none;
  47. display: flex;
  48. align-self: center;
  49. justify-content: center;
  50. &:hover,
  51. &:focus {
  52. background-color: var(--focused-bg-color);
  53. }
  54. }
  55. .warning,
  56. .error {
  57. color: var(--danger-color);
  58. }
  59. </style>
  60. <style>
  61. /* 窗口样式 */
  62. #windows-container {
  63. position: fixed;
  64. top: 0;
  65. left: 0;
  66. right: 0;
  67. bottom: 0;
  68. pointer-events: none;
  69. z-index: 1000;
  70. }
  71. .window {
  72. color: var(--secondary-font-color);
  73. position: absolute;
  74. display: flex;
  75. flex-direction: column;
  76. background: var(--content-bg-color);
  77. box-sizing: border-box;
  78. border: var(--border-thickness) solid var(--secondary-color);
  79. border-radius: var(--border-radius);
  80. overflow: hidden;
  81. pointer-events: auto;
  82. &.dragging {
  83. transition: left 0s, right 0s;
  84. }
  85. &.resizing {
  86. transition: width 0s, height 0s;
  87. }
  88. &.focused {
  89. --secondary-font-color: var(--accent-font-color);
  90. --secondary-color: var(--accent-color);
  91. box-shadow: var(--shadow);
  92. }
  93. &.dragging,
  94. &.resizing {
  95. --secondary-color: var(--danger-color);
  96. iframe {
  97. display: none;
  98. }
  99. }
  100. &.borderless {
  101. border: none;
  102. .window-title-bar {
  103. height: 0;
  104. display: none;
  105. }
  106. }
  107. &.minimized {
  108. /* max-height: var(--title-bar-height); */
  109. opacity: 0;
  110. transform: scale(0.8);
  111. pointer-events: none;
  112. }
  113. &.fullscreen {
  114. top: 0 !important;
  115. left: 0 !important;
  116. width: 100% !important;
  117. height: 100% !important;
  118. border: none !important;
  119. border-radius: 0;
  120. box-shadow: none !important;
  121. }
  122. &.borderless,
  123. &.fullscreen {
  124. .resize-handle {
  125. display: none;
  126. }
  127. }
  128. .resize-handle {
  129. border-radius: calc(0.5 * var(--border-radius)) 0 0 0;
  130. position: absolute;
  131. bottom: 0;
  132. right: 0;
  133. width: calc(2 * var(--border-thickness));
  134. height: calc(2 * var(--border-thickness));
  135. background: var(--secondary-color);
  136. cursor: nwse-resize;
  137. }
  138. }
  139. .window-title-bar {
  140. height: var(--title-bar-height);
  141. background: var(--accent-bg-color);
  142. display: flex;
  143. align-items: center;
  144. justify-content: flex-start;
  145. cursor: move;
  146. user-select: none;
  147. white-space: nowrap;
  148. & > *:first-child {
  149. margin: 0 var(--margin-padding);
  150. min-width: calc(var(--title-bar-height) - var(--margin-padding));
  151. }
  152. }
  153. .window-title {
  154. flex: 1;
  155. justify-content: var(--title-bar-justify-content);
  156. font-weight: bold;
  157. overflow: hidden;
  158. text-overflow: ellipsis;
  159. }
  160. .window-icon {
  161. display: flex;
  162. align-items: center;
  163. justify-content: center;
  164. }
  165. .window-controls {
  166. display: flex;
  167. flex-shrink: 0;
  168. }
  169. .window-controls button {
  170. color: var(--secondary-font-color);
  171. background: none;
  172. border: none;
  173. width: var(--title-bar-height);
  174. height: var(--title-bar-height);
  175. cursor: pointer;
  176. font-size: 16px;
  177. display: flex;
  178. align-items: center;
  179. justify-content: center;
  180. &:hover,
  181. &:focus {
  182. background: var(--focused-bg-color);
  183. }
  184. &.close:hover,
  185. &.close:focus {
  186. background-color: var(--danger-color);
  187. }
  188. }
  189. .window-tab-bar {
  190. display: flex;
  191. background: var(--content-bg-color);
  192. }
  193. .tab {
  194. font-size: small;
  195. padding: var(--margin-padding);
  196. cursor: pointer;
  197. border-bottom: var(--border-thickness) solid transparent;
  198. user-select: none;
  199. &:hover {
  200. background: var(--focused-bg-color);
  201. }
  202. &.active {
  203. background: var(--secondary-bg-color);
  204. border-bottom: var(--border-thickness) solid var(--secondary-color);
  205. }
  206. }
  207. .window-content {
  208. flex: 1;
  209. overflow: auto;
  210. display: flex;
  211. .tab-content {
  212. flex: 1;
  213. display: none;
  214. padding: var(--margin-padding);
  215. &.active {
  216. display: flex;
  217. flex-direction: column;
  218. gap: var(--margin-padding);
  219. }
  220. }
  221. }
  222. </style>
  223. <style>
  224. /* 背景 */
  225. bg {
  226. position: fixed;
  227. top: 0;
  228. left: 0;
  229. right: 0;
  230. bottom: 0;
  231. pointer-events: none;
  232. overflow: hidden;
  233. z-index: -1;
  234. background: url("assets/pageos_文字壁纸.svg");
  235. background-size: cover;
  236. background-position: center;
  237. background-attachment: fixed;
  238. }
  239. </style>
  240. <style>
  241. /* 任务栏 */
  242. taskbar-container {
  243. position: fixed;
  244. bottom: 0;
  245. left: 0;
  246. right: 0;
  247. height: var(--margin-padding); /* 底部触发区域高度 */
  248. z-index: 3100; /* 确保任务栏始终在最顶层 */
  249. pointer-events: auto;
  250. &:hover {
  251. height: var(--task-bar-height);
  252. }
  253. &:hover task-bar {
  254. bottom: var(--margin-padding);
  255. opacity: 1;
  256. pointer-events: auto;
  257. }
  258. }
  259. task-bar {
  260. position: absolute;
  261. background-color: var(--content-bg-color);
  262. border-radius: var(--border-radius);
  263. border: var(--border-thickness) solid var(--secondary-color);
  264. bottom: var(--margin-padding);
  265. height: var(--task-bar-height);
  266. left: 50%;
  267. transform: translateX(-50%);
  268. display: flex;
  269. justify-content: space-between;
  270. align-items: center;
  271. width: 90%;
  272. max-width: 1024px;
  273. overflow: hidden;
  274. &.hide {
  275. bottom: calc(var(--task-bar-height) * -1);
  276. opacity: 0;
  277. pointer-events: none;
  278. }
  279. &:hover {
  280. box-shadow: var(--shadow);
  281. --secondary-color: var(--accent-color);
  282. --secondary-font-color: var(--accent-font-color);
  283. }
  284. & > *:first-child {
  285. border-left: none;
  286. }
  287. & > * {
  288. height: 100%;
  289. border-left: var(--border-thickness) solid var(--secondary-color);
  290. min-width: var(--task-bar-height);
  291. display: flex;
  292. overflow: hidden;
  293. & > * {
  294. cursor: pointer;
  295. padding: var(--margin-padding);
  296. height: 100%;
  297. min-width: var(--task-bar-height);
  298. display: flex;
  299. align-items: center;
  300. justify-content: center;
  301. border-radius: calc(var(--border-radius) - var(--margin-padding));
  302. background-color: var(--content-bg-color);
  303. }
  304. }
  305. }
  306. window-buttons {
  307. flex: 1;
  308. & > * {
  309. border-top: var(--border-thickness) solid transparent;
  310. border-bottom: var(--border-thickness) solid transparent;
  311. font-size: large;
  312. & > * {
  313. pointer-events: none;
  314. }
  315. }
  316. & > *.focused {
  317. border-bottom: var(--border-thickness) solid var(--accent-color);
  318. }
  319. }
  320. bar-space > * {
  321. font-size: larger;
  322. }
  323. .app-launcher {
  324. background-image: var(--favicon);
  325. }
  326. .power {
  327. display: none;
  328. }
  329. #translate {
  330. position: relative;
  331. .translateSelectLanguage {
  332. background-color: var(--content-bg-color);
  333. color: inherit;
  334. opacity: 0;
  335. cursor: pointer;
  336. position: absolute;
  337. top: 0;
  338. right: 0;
  339. bottom: 0;
  340. left: 0;
  341. & > * {
  342. border: var(--border-thickness) solid var(--secondary-color);
  343. border-radius: var(--border-radius);
  344. background-color: var(--content-bg-color);
  345. color: inherit;
  346. }
  347. }
  348. }
  349. </style>
  350. <link
  351. rel="icon"
  352. type="image/svg+xml"
  353. href="data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMiIgaGVpZ2h0PSIzMiIgdmlld0JveD0iMCAwIDMyIDMyIj4KICA8IS0tIOWkluahhuefqeW9oiAtLT4KICA8cmVjdCB4PSIyIiB5PSIyIiB3aWR0aD0iMjgiIGhlaWdodD0iMjgiIHJ4PSIxMCIgcnk9IjEwIgogICAgZmlsbD0iIzQyNDI0MiIKICAgIHN0cm9rZT0iIzAwNjk1YyIKICAgIHN0cm9rZS13aWR0aD0iNCIvPgoKICA8IS0tIOW3puecvO+8muWwj+WchueCuSAtLT4KICA8Y2lyY2xlIGN4PSIxMiIgY3k9IjE0IiByPSIxLjUiIGZpbGw9IiNmNWY1ZjUiIC8+CgogIDwhLS0g5Y+z55y877ya5bCP5ZyG54K5IC0tPgogIDxjaXJjbGUgY3g9IjIwIiBjeT0iMTQiIHI9IjEuNSIgZmlsbD0iI2Y1ZjVmNSIgLz4KCiAgPCEtLSDpvLvlrZDvvJrnq5bnur8gLS0+CiAgPGxpbmUgeDE9IjE2IiB5MT0iMTYiIHgyPSIxNiIgeTI9IjE4IiBzdHJva2U9IiNmNWY1ZjUiIHN0cm9rZS13aWR0aD0iMS41IiAvPgoKICA8IS0tIOW+rueskeeahOWYtO+8muS4iuaJrOeahOW8p+e6vyAtLT4KICA8cGF0aCBkPSJNIDE0IDIwIFEgMTYgMjIgMTggMjAiIAogICAgICAgIGZpbGw9Im5vbmUiIAogICAgICAgIHN0cm9rZT0iI2Y1ZjVmNSIgCiAgICAgICAgc3Ryb2tlLXdpZHRoPSIxLjUiIAogICAgICAgIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgLz4KPC9zdmc+"
  354. />
  355. <script
  356. src="https://lib.baomitu.com/translate.js/3.17.0/translate.js"
  357. type="text/javascript"
  358. ></script>
  359. </head>
  360. <body>
  361. <bg></bg>
  362. <div id="windows-container"></div>
  363. <taskbar-container>
  364. <task-bar>
  365. <bar-space>
  366. <button class="app-launcher" title="应用程序启动器"></button>
  367. <button class="search" title="全局搜索">
  368. <!-- Copyright (c) 2018-2025 Tabler -->
  369. <svg
  370. xmlns="http://www.w3.org/2000/svg"
  371. width="24"
  372. height="24"
  373. viewBox="0 0 24 24"
  374. fill="none"
  375. stroke="currentColor"
  376. stroke-width="2"
  377. stroke-linecap="round"
  378. stroke-linejoin="round"
  379. class="icon icon-tabler icons-tabler-outline icon-tabler-search"
  380. >
  381. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  382. <path d="M10 10m-7 0a7 7 0 1 0 14 0a7 7 0 1 0 -14 0" />
  383. <path d="M21 21l-6 -6" /></svg
  384. ></button>
  385. </bar-space>
  386. <window-buttons></window-buttons>
  387. <status-space>
  388. <button id="translate" class="language-tools" title="语言工具">
  389. <!-- Copyright (c) 2018-2025 Tabler -->
  390. <svg
  391. xmlns="http://www.w3.org/2000/svg"
  392. width="24"
  393. height="24"
  394. viewBox="0 0 24 24"
  395. fill="none"
  396. stroke="currentColor"
  397. stroke-width="2"
  398. stroke-linecap="round"
  399. stroke-linejoin="round"
  400. class="icon icon-tabler icons-tabler-outline icon-tabler-world"
  401. >
  402. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  403. <path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0" />
  404. <path d="M3.6 9h16.8" />
  405. <path d="M3.6 15h16.8" />
  406. <path d="M11.5 3a17 17 0 0 0 0 18" />
  407. <path d="M12.5 3a17 17 0 0 1 0 18" />
  408. </svg>
  409. </button>
  410. <button class="power" title="电源状态"></button>
  411. <button class="date-time" title="日期时间">0/0/0 0:0</button>
  412. </status-space>
  413. <bar-space>
  414. <button class="exit" title="退出">
  415. <!-- Copyright (c) 2018-2025 Tabler -->
  416. <svg
  417. xmlns="http://www.w3.org/2000/svg"
  418. width="24"
  419. height="24"
  420. viewBox="0 0 24 24"
  421. fill="none"
  422. stroke="currentColor"
  423. stroke-width="2"
  424. stroke-linecap="round"
  425. stroke-linejoin="round"
  426. class="icon icon-tabler icons-tabler-outline icon-tabler-logout"
  427. >
  428. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  429. <path
  430. d="M14 8v-2a2 2 0 0 0 -2 -2h-7a2 2 0 0 0 -2 2v12a2 2 0 0 0 2 2h7a2 2 0 0 0 2 -2v-2"
  431. />
  432. <path d="M9 12h12l-3 -3" />
  433. <path d="M18 15l3 -3" />
  434. </svg>
  435. </button>
  436. </bar-space>
  437. </task-bar>
  438. </taskbar-container>
  439. <script type="text/js-worker" id="processWorker">
  440. // 进程 Worker 脚本
  441. class WorkerProcess {
  442. constructor(id, parentId) {
  443. this.id = id;
  444. this.parentId = parentId;
  445. this.command = "";
  446. this.settings = {};
  447. this.active = false;
  448. }
  449. // 添加设置项
  450. addSetting(key, defaultValue) {
  451. this.settings[key] = defaultValue;
  452. }
  453. // 更新设置项
  454. updateSetting(key, value) {
  455. if (this.settings.hasOwnProperty(key)) {
  456. this.settings[key] = value;
  457. }
  458. }
  459. // 安全命令执行(白名单机制)
  460. safeExecute(commandName) {
  461. const commandMap = {
  462. log: (msg) => console.log(msg),
  463. calculate: (expr) => eval(expr), // 仅允许计算表达式
  464. };
  465. if (commandMap[commandName]) {
  466. return commandMap[commandName](...arguments);
  467. } else {
  468. console.error(`未知命令: ${commandName}`);
  469. }
  470. }
  471. // 发送消息到主线程
  472. sendMessage(targetId, messageType, data) {
  473. self.postMessage({
  474. type: "process-message",
  475. from: this.id,
  476. targetId,
  477. messageType,
  478. data,
  479. });
  480. }
  481. // 处理主线程消息
  482. handleMessage(event) {
  483. const { type, data } = event.data;
  484. if (type === "execute") {
  485. const [cmd, ...args] = data.command.split(" ");
  486. return this.safeExecute(cmd, ...args);
  487. } else if (type === "update-setting") {
  488. this.updateSetting(data.key, data.value);
  489. } else if (type === "message") {
  490. // 处理其他进程消息
  491. console.log(`进程 ${this.id} 收到消息:`, data);
  492. }
  493. }
  494. }
  495. // Worker 入口
  496. self.addEventListener("message", (event) => {
  497. if (event.data.type === "init") {
  498. const { id, parentId } = event.data;
  499. const process = new WorkerProcess(id, parentId);
  500. // 处理后续消息
  501. self.addEventListener("message", (e) => {
  502. process.handleMessage(e);
  503. });
  504. self.postMessage({ type: "ready", id });
  505. }
  506. });
  507. </script>
  508. <script>
  509. (function () {
  510. // 加载 CSS
  511. function loadCSS(url) {
  512. return new Promise((resolve, reject) => {
  513. //// 检查是否已加载
  514. const existingLinks = document.querySelectorAll(
  515. `link[href="${url}"]`
  516. );
  517. if (existingLinks.length > 0) {
  518. return resolve(url);
  519. }
  520. //// 创建新的 link 元素
  521. const link = document.createElement("link");
  522. link.rel = "stylesheet";
  523. link.type = "text/css";
  524. link.href = url;
  525. //// 成功加载处理
  526. link.onload = () => {
  527. link.onload = null;
  528. link.onerror = null;
  529. resolve(url);
  530. };
  531. //// 加载失败处理
  532. link.onerror = () => {
  533. link.onerror = null;
  534. link.onload = null;
  535. document.head.removeChild(link);
  536. reject(new Error(`无法加载 CSS 文件: ${url}`));
  537. };
  538. //// 添加到文档头部
  539. document.head.appendChild(link);
  540. });
  541. }
  542. // translate-js 翻译系统
  543. //// 初始化翻译系统
  544. function initTranslate() {
  545. try {
  546. translate.service.use("client.edge");
  547. translate.request.listener.start();
  548. translate.setAutoDiscriminateLocalLanguage();
  549. translate.language.setUrlParamControl();
  550. translate.ignore.class.push("notTranslate");
  551. translate.execute();
  552. } catch (e) {
  553. console.error("翻译系统出错:" + e);
  554. }
  555. }
  556. //// 刷新翻译
  557. function refreshTranslate() {
  558. try {
  559. translate.selectLanguageTag.refreshRender();
  560. } catch (e) {
  561. console.error("刷新翻译出错:" + e);
  562. }
  563. }
  564. //// 翻译元素 [element, ……]
  565. function translateElement(elements) {
  566. try {
  567. translate.execute(elements);
  568. } catch (e) {
  569. console.error("翻译元素出错:" + e);
  570. }
  571. }
  572. // 初始化
  573. function initialize() {
  574. initTranslate(); //// 翻译系统
  575. loadCSS(
  576. "https://chinese-fonts-cdn.deno.dev/packages/maple-mono-cn/dist/MapleMono-CN-Regular/result.css"
  577. ); //// 加载字体 CSS
  578. }
  579. // 触发器
  580. //// 网页加载完毕后触发初始化
  581. window.addEventListener("DOMContentLoaded", () => initialize());
  582. // 统一焦点管理器(合并输入管理和焦点管理)
  583. class FocusManager {
  584. constructor() {
  585. // 原FocusManager属性
  586. this.focusedWindowId = null;
  587. this.focusedElement = null;
  588. // 原InputManager属性
  589. this.windowStack = []; // 窗口堆叠顺序(最近使用在前)
  590. this.captureInputProcess = null; // 捕获输入的进程ID
  591. }
  592. // 任务栏窗口按钮样式更新
  593. updateWindowButtons(windowId) {
  594. const winBtns = document.querySelectorAll(
  595. "task-bar window-buttons button"
  596. );
  597. winBtns.forEach((winBtn) => {
  598. winBtn.classList.toggle(
  599. "focused",
  600. winBtn.dataset.windowId === windowId
  601. );
  602. });
  603. }
  604. // 设置焦点窗口
  605. focusWindow(windowId) {
  606. if (this.focusedWindowId === windowId) return;
  607. // 移除旧窗口的焦点
  608. if (this.focusedWindowId) {
  609. const prevWindow = wManager.windows.get(this.focusedWindowId);
  610. if (prevWindow) prevWindow.blur();
  611. }
  612. // 更新窗口堆叠顺序(原 InputManager 逻辑)
  613. this.windowStack = this.windowStack.filter((id) => id !== windowId);
  614. this.windowStack.unshift(windowId);
  615. // 设置新焦点窗口
  616. this.focusedWindowId = windowId;
  617. this.releaseInput(); // 窗口聚焦时释放输入捕获
  618. const window = wManager.windows.get(windowId);
  619. if (window) {
  620. window.focus();
  621. this.updateWindowButtons(windowId);
  622. }
  623. }
  624. // 设置捕获输入的进程(原 InputManager 方法)
  625. captureInput(processId) {
  626. this.captureInputProcess = processId;
  627. }
  628. // 释放输入捕获(原 InputManager 方法)
  629. releaseInput() {
  630. this.captureInputProcess = null;
  631. }
  632. // 移除已关闭窗口的状态(合并原 InputManager 和 FocusManager 逻辑)
  633. removeWindow(windowId) {
  634. // 原 InputManager 窗口移除逻辑
  635. this.windowStack = this.windowStack.filter((id) => id !== windowId);
  636. if (this.focusedWindowId === windowId) {
  637. this.focusedWindowId = null;
  638. if (this.windowStack.length > 0) {
  639. this.focusWindow(this.windowStack[0]);
  640. }
  641. }
  642. // 原 FocusManager 窗口移除逻辑
  643. if (this.focusedWindowId === windowId) {
  644. this.focusedWindowId = null;
  645. }
  646. if (
  647. this.focusedElement &&
  648. this.focusedElement.closest(`[data-window-id="${windowId}"]`)
  649. ) {
  650. this.focusedElement = null;
  651. }
  652. }
  653. // 切换到下一个窗口 (Alt+Tab)(原 InputManager 方法)
  654. nextWindow() {
  655. if (this.windowStack.length < 2) return;
  656. // 将当前窗口移到末尾
  657. const current = this.windowStack.shift();
  658. this.windowStack.push(current);
  659. // 聚焦新窗口
  660. this.focusedWindowId = this.windowStack[0];
  661. return this.focusedWindowId;
  662. }
  663. // 设置焦点元素(原 FocusManager 方法)
  664. focusElement(element, windowId) {
  665. if (this.focusedElement) {
  666. this.focusedElement.blur();
  667. }
  668. this.focusedElement = element;
  669. this.focusWindow(windowId);
  670. element.focus();
  671. }
  672. // 清除焦点(原 FocusManager 方法)
  673. clearFocus() {
  674. // 清除当前焦点元素
  675. if (this.focusedElement) {
  676. this.focusedElement.blur();
  677. this.focusedElement = null;
  678. }
  679. // 清除当前焦点窗口的样式
  680. if (this.focusedWindowId) {
  681. const prevWindow = wManager.windows.get(this.focusedWindowId);
  682. if (prevWindow) prevWindow.blur();
  683. this.focusedWindowId = null;
  684. }
  685. this.updateWindowButtons(this.focusedWindowId);
  686. }
  687. }
  688. // 层级管理器
  689. class LayerManager {
  690. static LAYERS = {
  691. BACKGROUND: { zIndex: 100, maxWindows: 10 }, // 背景层
  692. WINDOW: { zIndex: 1000, maxWindows: 100 }, // 窗口层
  693. TOP: { zIndex: 3000, maxWindows: 10 }, // 顶层(弹窗、菜单等)
  694. };
  695. constructor() {
  696. this.windowLayers = new Map(); // windowId -> layerType
  697. this.layerStacks = {
  698. BACKGROUND: [],
  699. WINDOW: [],
  700. TOP: [],
  701. };
  702. }
  703. // 添加窗口到指定层
  704. addToLayer(windowId, layerType = "WINDOW") {
  705. if (!LayerManager.LAYERS[layerType]) {
  706. console.error(`无效的层级类型: ${layerType}`);
  707. return;
  708. }
  709. // 从原有层移除
  710. this.removeWindow(windowId);
  711. // 添加到新层
  712. this.windowLayers.set(windowId, layerType);
  713. this.layerStacks[layerType].push(windowId);
  714. // 确保不超过最大窗口数
  715. const maxWindows = LayerManager.LAYERS[layerType].maxWindows;
  716. if (this.layerStacks[layerType].length > maxWindows) {
  717. const removedId = this.layerStacks[layerType].shift();
  718. this.windowLayers.delete(removedId);
  719. }
  720. // 层级变更时更新所有窗口
  721. this.updateAllWindows();
  722. }
  723. // 获取窗口所在层
  724. getWindowLayer(windowId) {
  725. return this.windowLayers.get(windowId) || "WINDOW";
  726. }
  727. // 提升窗口到层内最前
  728. bringToFront(windowId) {
  729. const layerType = this.getWindowLayer(windowId);
  730. const stack = this.layerStacks[layerType];
  731. // 从原位置移除
  732. const index = stack.indexOf(windowId);
  733. if (index > -1) {
  734. stack.splice(index, 1);
  735. }
  736. // 添加到栈顶
  737. stack.push(windowId);
  738. // 层级变更时更新所有窗口
  739. this.updateAllWindows();
  740. }
  741. // 移除窗口
  742. removeWindow(windowId) {
  743. const layerType = this.getWindowLayer(windowId);
  744. if (layerType) {
  745. const stack = this.layerStacks[layerType];
  746. const index = stack.indexOf(windowId);
  747. if (index > -1) {
  748. stack.splice(index, 1);
  749. }
  750. }
  751. this.windowLayers.delete(windowId);
  752. // 层级变更时更新所有窗口
  753. this.updateAllWindows();
  754. }
  755. // 遍历更新所有窗口层级(新增方法)
  756. updateAllWindows() {
  757. // 遍历所有层
  758. for (const [layerType, stack] of Object.entries(this.layerStacks)) {
  759. const baseZIndex = LayerManager.LAYERS[layerType].zIndex;
  760. // 遍历层内所有窗口
  761. stack.forEach((windowId, index) => {
  762. const window = wManager.windows.get(windowId);
  763. if (window) {
  764. // 计算z-index: 基础值 + 在层内的位置
  765. window.element.style.zIndex = baseZIndex + index;
  766. }
  767. });
  768. }
  769. }
  770. }
  771. // 全局焦点管理器实例
  772. const fManager = new FocusManager();
  773. // 窗口管理类
  774. class WindowManager {
  775. constructor() {
  776. this.windows = new Map();
  777. this.layerManager = new LayerManager();
  778. this.windowIdCounter = 0;
  779. this.windowsContainer =
  780. document.getElementById("windows-container");
  781. // 全屏窗口计数
  782. this.fullscreenCount = 0;
  783. this.taskBar = document.querySelector("task-bar");
  784. }
  785. // 创建新窗口
  786. createWindow(processId, config = {}) {
  787. const windowId = `window-${this.windowIdCounter++}`; // 使用递增ID
  788. const win = new PageWin(windowId, processId, config);
  789. translateElement([win.element]);
  790. this.windows.set(windowId, win);
  791. this.windowsContainer.appendChild(win.element);
  792. // 如果窗口初始为全屏,则增加计数
  793. if (config.fullscreen) {
  794. this.updateFullscreenCount(1);
  795. }
  796. // 任务栏窗口按钮
  797. const taskbar = document.querySelector("task-bar window-buttons");
  798. const windowBtn = document.createElement("button");
  799. windowBtn.innerHTML = win.config.icon
  800. ? win.config.icon
  801. : win.config.title.charAt(0).toUpperCase();
  802. windowBtn.title = win.config.title;
  803. windowBtn.dataset.windowId = windowId;
  804. windowBtn.addEventListener("click", () => {
  805. fManager.focusWindow(windowId);
  806. this.focusWindow(windowId);
  807. });
  808. // 设置右键菜单选项
  809. const menuOptions = [
  810. { label: "移动窗口", action: "move" },
  811. { label: "最小化", action: "minimize" },
  812. { label: "最大化", action: "maximize" },
  813. { label: "关闭", action: "close" },
  814. ];
  815. windowBtn.dataset.menuOptions = JSON.stringify(menuOptions);
  816. translateElement([windowBtn]);
  817. taskbar.appendChild(windowBtn);
  818. return windowId;
  819. }
  820. // TODO: 是否删除:聚焦窗口
  821. focusWindow(windowId) {
  822. const win = this.windows.get(windowId);
  823. if (win) {
  824. // 使用层级管理器设置z-index
  825. const zIndex = this.layerManager.bringToFront(windowId);
  826. win.element.style.zIndex = zIndex;
  827. win.focus(); // 通知窗口获得焦点
  828. }
  829. }
  830. // 关闭窗口的统一入口
  831. closeWindow(windowId) {
  832. const win = this.windows.get(windowId);
  833. if (win) {
  834. // 如果窗口是全屏状态,则减少计数
  835. if (win.config.fullscreen) {
  836. this.updateFullscreenCount(-1);
  837. }
  838. win.cleanup();
  839. this.windows.delete(windowId);
  840. win.element.remove();
  841. // 删除任务栏窗口按钮
  842. const taskbar = document.querySelector("task-bar window-buttons");
  843. taskbar.querySelector(`[data-window-id="${windowId}"]`).remove();
  844. // 更新焦点管理器和层级管理器
  845. fManager.removeWindow(windowId);
  846. this.layerManager.removeWindow(windowId);
  847. }
  848. }
  849. moveWindow(windowId) {
  850. const win = this.windows.get(windowId);
  851. if (win) {
  852. // 激活窗口
  853. this.focusWindow(windowId);
  854. fManager.focusWindow(windowId);
  855. win.element.classList.add("dragging");
  856. win.isDragging = true;
  857. }
  858. }
  859. minimizeWindow(windowId) {
  860. const win = this.windows.get(windowId);
  861. if (win) {
  862. win.toggleMinimize();
  863. }
  864. }
  865. maximizeWindow(windowId) {
  866. const win = this.windows.get(windowId);
  867. if (win) {
  868. win.toggleMaximize();
  869. }
  870. }
  871. // 通过进程 ID 获取窗口
  872. getWindowsByProcess(processId) {
  873. return Array.from(this.windows.values()).filter(
  874. (w) => w.processId === processId
  875. );
  876. }
  877. // 更新全屏窗口计数
  878. updateFullscreenCount(change) {
  879. this.fullscreenCount += change;
  880. if (this.fullscreenCount < 0) {
  881. this.fullscreenCount = 0;
  882. }
  883. if (this.fullscreenCount > 0) {
  884. this.taskBar.classList.add("hide");
  885. } else {
  886. this.taskBar.classList.remove("hide");
  887. }
  888. }
  889. }
  890. // 窗口类
  891. class PageWin {
  892. constructor(id, processId, config) {
  893. var i = document.createElement("i");
  894. i.textContent = "空内容";
  895. this.id = id;
  896. this.processId = processId;
  897. this.config = {
  898. css: {},
  899. borderless: false,
  900. fullscreen: false,
  901. minimized: false,
  902. icon: '<!-- Copyright (c) 2018-2025 Tabler --><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-app-window"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M3 5m0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v10a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z" /><path d="M6 8h.01" /><path d="M9 8h.01" /></svg>',
  903. title: "窗口",
  904. width: 400,
  905. height: 300,
  906. x: 50,
  907. y: 50,
  908. tabs: [
  909. {
  910. title: "标签页",
  911. css: {},
  912. content: i,
  913. },
  914. ], // { title: string, css: string }[]
  915. ...config,
  916. };
  917. this.activeTabIndex = 0;
  918. this.isDragging = false;
  919. this.dragOffset = { x: 0, y: 0 };
  920. this.isResizing = false;
  921. this.resizeDirection = null;
  922. this.originalSize = null;
  923. this.originalPosition = null;
  924. this.preMaximizeState = null;
  925. this.createDOM();
  926. this.setupEventListeners();
  927. }
  928. // 创建窗口DOM结构
  929. createDOM() {
  930. this.element = document.createElement("div");
  931. this.element.dataset.windowId = this.id;
  932. for (var property in this.config.css) {
  933. this.element.style[property] = this.config.css[property];
  934. }
  935. this.element.style.width = `${this.config.width}px`;
  936. this.element.style.height = `${this.config.height}px`;
  937. this.element.style.left = `${this.config.x}px`;
  938. this.element.style.top = `${this.config.y}px`;
  939. this.element.tabIndex = -1; // 使窗口可聚焦
  940. this.element.classList.add("window");
  941. if (this.config.borderless) {
  942. this.element.classList.add("borderless");
  943. }
  944. if (this.config.fullscreen) {
  945. this.element.classList.add("fullscreen");
  946. }
  947. if (this.config.minimized) {
  948. this.element.classList.add("minimized");
  949. }
  950. // 标题栏
  951. const titleBar = document.createElement("div");
  952. titleBar.className = "window-title-bar";
  953. titleBar.innerHTML = `<div class="window-icon">${
  954. this.config.icon
  955. ? this.config.icon
  956. : this.config.title.charAt(0).toUpperCase()
  957. }</div>
  958. <div class="window-title">${this.config.title}</div>
  959. <div class="window-controls">
  960. <button class="minimize">
  961. <!-- Copyright (c) 2018-2025 Tabler -->
  962. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-chevron-compact-down"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M4 11l8 3l8 -3" /></svg>
  963. </button>
  964. <button class="maximize">
  965. <!-- Copyright (c) 2018-2025 Tabler -->
  966. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-maximize"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M4 8v-2a2 2 0 0 1 2 -2h2" /><path d="M4 16v2a2 2 0 0 0 2 2h2" /><path d="M16 4h2a2 2 0 0 1 2 2v2" /><path d="M16 20h2a2 2 0 0 0 2 -2v-2" /></svg>
  967. </button>
  968. <button class="close">
  969. <!-- Copyright (c) 2018-2025 Tabler -->
  970. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-square-x"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M3 5a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v14a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2v-14z" /><path d="M9 9l6 6m0 -6l-6 6" /></svg>
  971. </button>
  972. </div>`;
  973. // 标签页栏
  974. const tabBar = document.createElement("div");
  975. tabBar.className = "window-tab-bar";
  976. this.config.tabs.forEach((tab, index) => {
  977. const tabEl = document.createElement("div");
  978. tabEl.className = `tab ${
  979. index === this.activeTabIndex ? "active" : ""
  980. }`;
  981. tabEl.textContent = tab.title;
  982. tabEl.dataset.tabIndex = index;
  983. tabBar.appendChild(tabEl);
  984. });
  985. // 内容区
  986. const content = document.createElement("div");
  987. content.className = "window-content";
  988. this.config.tabs.forEach((tab, index) => {
  989. const tabConEl = document.createElement("div");
  990. tabConEl.className = `tab-content ${
  991. index === this.activeTabIndex ? "active" : ""
  992. }`;
  993. if (tab.content) {
  994. try {
  995. tabConEl.appendChild(tab.content);
  996. } catch (e) {
  997. var p = document.createElement("p");
  998. p.style.color = "var(--danger-color)";
  999. p.textContent = e;
  1000. tabConEl.appendChild(p);
  1001. }
  1002. } else {
  1003. var i = document.createElement("i");
  1004. i.textContent = "空内容";
  1005. tabConEl.appendChild(i);
  1006. }
  1007. tabConEl.dataset.tabContentIndex = index;
  1008. for (var property in tab.css) {
  1009. tabConEl.style[property] = tab.css[property];
  1010. }
  1011. content.appendChild(tabConEl);
  1012. });
  1013. this.element.appendChild(titleBar);
  1014. if (this.config.tabs.length > 1) {
  1015. this.element.appendChild(tabBar);
  1016. }
  1017. this.element.appendChild(content);
  1018. // 调整大小手柄
  1019. const resizeHandle = document.createElement("div");
  1020. resizeHandle.className = "resize-handle";
  1021. this.element.appendChild(resizeHandle);
  1022. this.switchTab(0);
  1023. }
  1024. // 设置事件监听
  1025. setupEventListeners() {
  1026. const titleBar = this.element.querySelector(".window-title-bar");
  1027. const minimizeBtn = this.element.querySelector(".minimize");
  1028. const maximizeBtn = this.element.querySelector(".maximize");
  1029. const closeBtn = this.element.querySelector(".close");
  1030. const tabs = this.element.querySelectorAll(".tab");
  1031. const resizeHandle = this.element.querySelector(".resize-handle");
  1032. this.element.addEventListener("mousedown", (e) => {
  1033. wManager.focusWindow(this.id);
  1034. fManager.focusWindow(this.id);
  1035. });
  1036. // 拖动窗口
  1037. titleBar.addEventListener("mousedown", (e) => {
  1038. if (
  1039. e.target === minimizeBtn ||
  1040. e.target === maximizeBtn ||
  1041. e.target === closeBtn
  1042. ) {
  1043. return;
  1044. }
  1045. this.isDragging = true;
  1046. this.element.classList.add("dragging");
  1047. if (this.config.fullscreen) {
  1048. this.dragOffset = {
  1049. x: (this.config.width / window.innerWidth) * e.clientX,
  1050. y: e.clientY,
  1051. };
  1052. this.element.style.left = `${e.clientX - this.dragOffset.x}px`;
  1053. this.element.style.top = `${e.clientY - this.dragOffset.y}px`;
  1054. this.toggleMaximize();
  1055. } else {
  1056. this.dragOffset = {
  1057. x: e.clientX - this.element.offsetLeft,
  1058. y: e.clientY - this.element.offsetTop,
  1059. };
  1060. }
  1061. });
  1062. this.handleDocumentMouseMove = (e) => {
  1063. if (this.isDragging) {
  1064. // 计算边界,确保窗口不会移出屏幕
  1065. let newX = e.clientX - this.dragOffset.x;
  1066. let newY = e.clientY - this.dragOffset.y;
  1067. // newX = Math.max(
  1068. // 0,
  1069. // Math.min(newX, window.innerWidth - this.element.offsetWidth)
  1070. // );
  1071. // newY = Math.max(
  1072. // 0,
  1073. // Math.min(newY, window.innerHeight - this.element.offsetHeight)
  1074. // );
  1075. // 设置新的位置
  1076. this.element.style.left = newX + "px";
  1077. this.element.style.top = newY + "px";
  1078. }
  1079. };
  1080. this.handleDocumentMouseUp = () => {
  1081. this.isDragging = false;
  1082. this.element.classList.remove("dragging");
  1083. };
  1084. // 添加触摸事件支持
  1085. titleBar.addEventListener("touchstart", (e) => {
  1086. if (
  1087. e.target === minimizeBtn ||
  1088. e.target === maximizeBtn ||
  1089. e.target === closeBtn
  1090. ) {
  1091. return;
  1092. }
  1093. this.isDragging = true;
  1094. this.element.classList.add("dragging");
  1095. if (this.config.fullscreen) {
  1096. this.dragOffset = {
  1097. x:
  1098. (this.config.width / window.innerWidth) *
  1099. e.touches[0].clientX,
  1100. y: e.touches[0].clientY,
  1101. };
  1102. this.element.style.left = `${
  1103. e.touches[0].clientX - this.dragOffset.x
  1104. }px`;
  1105. this.element.style.top = `${
  1106. e.touches[0].clientY - this.dragOffset.y
  1107. }px`;
  1108. this.toggleMaximize();
  1109. } else {
  1110. this.dragOffset = {
  1111. x: e.touches[0].clientX - this.element.offsetLeft,
  1112. y: e.touches[0].clientY - this.element.offsetTop,
  1113. };
  1114. }
  1115. e.preventDefault(); // 防止触摸滚动
  1116. });
  1117. this.handleDocumentTouchMove = (e) => {
  1118. if (this.isDragging) {
  1119. // 计算边界,确保窗口不会移出屏幕
  1120. let newX = e.touches[0].clientX - this.dragOffset.x;
  1121. let newY = e.touches[0].clientY - this.dragOffset.y;
  1122. // newX = Math.max(
  1123. // 0,
  1124. // Math.min(newX, window.innerWidth - this.element.offsetWidth)
  1125. // );
  1126. // newY = Math.max(
  1127. // 0,
  1128. // Math.min(newY, window.innerHeight - this.element.offsetHeight)
  1129. // );
  1130. // 设置新的位置
  1131. this.element.style.left = newX + "px";
  1132. this.element.style.top = newY + "px";
  1133. e.preventDefault(); // 防止触摸滚动
  1134. }
  1135. };
  1136. this.handleDocumentTouchEnd = () => {
  1137. this.isDragging = false;
  1138. this.element.classList.remove("dragging");
  1139. };
  1140. document.addEventListener(
  1141. "touchmove",
  1142. this.handleDocumentTouchMove,
  1143. { passive: false }
  1144. );
  1145. document.addEventListener("touchend", this.handleDocumentTouchEnd);
  1146. document.addEventListener(
  1147. "mousemove",
  1148. this.handleDocumentMouseMove
  1149. );
  1150. document.addEventListener("mouseup", this.handleDocumentMouseUp);
  1151. // 窗口控制按钮
  1152. minimizeBtn.addEventListener("click", () => this.toggleMinimize());
  1153. maximizeBtn.addEventListener("click", () => this.toggleMaximize());
  1154. closeBtn.addEventListener("click", () =>
  1155. wManager.closeWindow(this.id)
  1156. );
  1157. // 标签页切换
  1158. tabs.forEach((tab) => {
  1159. tab.addEventListener("click", (e) => {
  1160. const tabIndex = parseInt(e.target.dataset.tabIndex);
  1161. this.switchTab(tabIndex);
  1162. fManager.focusWindow(this.id);
  1163. });
  1164. });
  1165. // 调整大小事件
  1166. resizeHandle.addEventListener("mousedown", this.handleResizeStart);
  1167. }
  1168. // 窗口获得焦点
  1169. focus() {
  1170. if (this.config.minimized) {
  1171. this.config.minimized = !this.config.minimized;
  1172. this.element.classList.remove("minimized");
  1173. }
  1174. this.element.classList.add("focused");
  1175. }
  1176. // 窗口失去焦点
  1177. blur() {
  1178. this.element.classList.remove("focused");
  1179. }
  1180. // 切换标签页
  1181. switchTab(index) {
  1182. this.activeTabIndex = index;
  1183. const tabs = this.element.querySelectorAll(".tab");
  1184. const contents = this.element.querySelectorAll(".tab-content");
  1185. tabs.forEach((tab, i) => {
  1186. tab.classList.toggle("active", i === index);
  1187. });
  1188. contents.forEach((content, i) => {
  1189. content.classList.toggle("active", i === index);
  1190. });
  1191. }
  1192. // 最小化/恢复窗口
  1193. toggleMinimize() {
  1194. if (this.config.fullscreen) {
  1195. this.config.fullscreen = !this.config.fullscreen;
  1196. this.element.classList.remove("fullscreen");
  1197. wManager.updateFullscreenCount(-1);
  1198. }
  1199. fManager.clearFocus();
  1200. this.config.minimized = !this.config.minimized;
  1201. this.element.classList.toggle("minimized", this.config.minimized);
  1202. }
  1203. // 最大化/恢复窗口
  1204. toggleMaximize() {
  1205. if (this.config.minimized) {
  1206. this.config.minimized = !this.config.minimized;
  1207. this.element.classList.remove("minimized");
  1208. }
  1209. const wasFullscreen = this.config.fullscreen;
  1210. this.config.fullscreen = !this.config.fullscreen;
  1211. const isNowFullscreen = this.config.fullscreen;
  1212. this.element.classList.toggle("fullscreen", this.config.fullscreen);
  1213. // 更新全屏计数
  1214. if (wasFullscreen && !isNowFullscreen) {
  1215. wManager.updateFullscreenCount(-1);
  1216. } else if (!wasFullscreen && isNowFullscreen) {
  1217. wManager.updateFullscreenCount(1);
  1218. }
  1219. }
  1220. // 关闭窗口
  1221. cleanup() {
  1222. // 移除所有事件监听器
  1223. document.removeEventListener(
  1224. "mousemove",
  1225. this.handleDocumentMouseMove
  1226. );
  1227. document.removeEventListener("mouseup", this.handleDocumentMouseUp);
  1228. document.removeEventListener("mousemove", this.handleResize);
  1229. document.removeEventListener("mouseup", this.handleResizeEnd);
  1230. // 从窗口元素移除 win-close 监听器
  1231. if (this.closeHandler) {
  1232. this.element.removeEventListener("win-close", this.closeHandler);
  1233. }
  1234. // 通知管理器移除窗口状态(统一管理)
  1235. fManager.removeWindow(this.id);
  1236. }
  1237. // 设置关闭处理器
  1238. setCloseHandler(handler) {
  1239. this.closeHandler = handler;
  1240. this.element.addEventListener("win-close", handler);
  1241. }
  1242. close() {
  1243. // 触发关闭事件
  1244. this.element.dispatchEvent(
  1245. new CustomEvent("win-close", { bubbles: true })
  1246. );
  1247. // 清理窗口
  1248. this.cleanup();
  1249. // 从DOM和管理器中移除
  1250. this.element.remove();
  1251. wManager.windows.delete(this.id);
  1252. }
  1253. // 调整大小功能
  1254. handleResizeStart = (e) => {
  1255. e.stopPropagation();
  1256. this.isResizing = true;
  1257. this.element.classList.add("resizing");
  1258. this.resizeStartX = e.clientX;
  1259. this.resizeStartY = e.clientY;
  1260. this.originalWidth = this.element.offsetWidth;
  1261. this.originalHeight = this.element.offsetHeight;
  1262. document.addEventListener("mousemove", this.handleResize);
  1263. document.addEventListener("mouseup", this.handleResizeEnd);
  1264. wManager.focusWindow(this.id);
  1265. fManager.focusWindow(this.id);
  1266. };
  1267. handleResize = (e) => {
  1268. if (!this.isResizing) return;
  1269. const dx = e.clientX - this.resizeStartX;
  1270. const dy = e.clientY - this.resizeStartY;
  1271. const minWidth = 200;
  1272. const minHeight = 150;
  1273. const newWidth = Math.max(minWidth, this.originalWidth + dx);
  1274. const newHeight = Math.max(minHeight, this.originalHeight + dy);
  1275. this.element.style.width = `${newWidth}px`;
  1276. this.element.style.height = `${newHeight}px`;
  1277. };
  1278. handleResizeEnd = () => {
  1279. this.isResizing = false;
  1280. this.element.classList.remove("resizing");
  1281. document.removeEventListener("mousemove", this.handleResize);
  1282. document.removeEventListener("mouseup", this.handleResizeEnd);
  1283. };
  1284. }
  1285. // 全局窗口管理器实例
  1286. const wManager = new WindowManager();
  1287. // 拦截全局输入事件
  1288. document.addEventListener("keydown", (e) => {
  1289. // 只有焦点窗口能接收键盘事件
  1290. if (!fManager.focusedWindowId) return;
  1291. // 阻止事件冒泡到非焦点窗口
  1292. const targetWindow = e.target.closest(".window");
  1293. if (
  1294. !targetWindow ||
  1295. targetWindow.dataset.windowId !== fManager.focusedWindowId
  1296. ) {
  1297. e.stopPropagation();
  1298. }
  1299. });
  1300. document.addEventListener("mousedown", (e) => {
  1301. const windowElement = e.target.closest(".window");
  1302. if (windowElement) {
  1303. // 点击窗口时设置焦点
  1304. fManager.focusWindow(windowElement.dataset.windowId);
  1305. } else {
  1306. // 点击窗口外区域清除焦点
  1307. fManager.clearFocus();
  1308. }
  1309. });
  1310. class ProcessManager {
  1311. constructor() {
  1312. this.workers = new Map();
  1313. this.registry = new Map(); // 权限注册表 {进程 ID: 允许通信的目标 ID 集合}
  1314. }
  1315. // 创建新进程
  1316. async createProcess(id, parentId = null) {
  1317. const workerScript =
  1318. document.querySelector("#processWorker").textContent;
  1319. const blob = new Blob([workerScript], {
  1320. type: "application/javascript",
  1321. });
  1322. const worker = new Worker(URL.createObjectURL(blob));
  1323. this.workers.set(id, worker);
  1324. // 初始化权限
  1325. this.registry.set(id, new Set());
  1326. if (parentId) {
  1327. this.allowCommunication(parentId, id);
  1328. }
  1329. // 处理 Worker 消息
  1330. worker.onmessage = (event) => {
  1331. if (event.data.type === "process-message") {
  1332. const { targetId, messageType, data, from } = event.data;
  1333. this.sendMessage(from, targetId, messageType, data);
  1334. }
  1335. };
  1336. // 初始化 Worker
  1337. worker.postMessage({
  1338. type: "init",
  1339. id,
  1340. parentId,
  1341. });
  1342. return new Promise((resolve) => {
  1343. worker.addEventListener(
  1344. "message",
  1345. function ready(e) {
  1346. if (e.data.type === "ready") {
  1347. resolve();
  1348. }
  1349. },
  1350. { once: true }
  1351. );
  1352. });
  1353. }
  1354. // 允许进程通信
  1355. allowCommunication(sourceId, targetId) {
  1356. if (this.registry.has(sourceId)) {
  1357. this.registry.get(sourceId).add(targetId);
  1358. }
  1359. }
  1360. // 发送进程消息
  1361. sendMessage(sourceId, targetId, messageType, data) {
  1362. if (!this.registry.get(sourceId)?.has(targetId)) {
  1363. console.error(`进程 ${sourceId} 无权向 ${targetId} 发送消息`);
  1364. return;
  1365. }
  1366. const worker = this.workers.get(targetId);
  1367. if (worker) {
  1368. worker.postMessage({
  1369. type: "message",
  1370. data: {
  1371. type: messageType,
  1372. data,
  1373. from: sourceId,
  1374. },
  1375. });
  1376. }
  1377. }
  1378. // 执行进程命令
  1379. executeCommand(processId, command) {
  1380. const worker = this.workers.get(processId);
  1381. if (worker) {
  1382. worker.postMessage({
  1383. type: "execute",
  1384. data: { command },
  1385. });
  1386. }
  1387. }
  1388. // 更新进程设置
  1389. updateSetting(processId, key, value) {
  1390. const worker = this.workers.get(processId);
  1391. if (worker) {
  1392. worker.postMessage({
  1393. type: "update-setting",
  1394. data: { key, value },
  1395. });
  1396. }
  1397. }
  1398. // 终止进程
  1399. terminateProcess(processId) {
  1400. const worker = this.workers.get(processId);
  1401. if (worker) {
  1402. worker.terminate();
  1403. this.workers.delete(processId);
  1404. this.registry.delete(processId);
  1405. }
  1406. }
  1407. }
  1408. // 初始化进程管理器
  1409. const pManager = new ProcessManager();
  1410. // 自定义右键菜单功能
  1411. function initOptionMenu(doc) {
  1412. // 初始化样式
  1413. var style = doc.createElement("style");
  1414. style.type = "text/css";
  1415. style.innerHTML = `
  1416. #custom-context-menu {
  1417. --accent-color: #64ffda; /* 强调色:边框 */
  1418. --secondary-color: #00695c; /* 次要色:未激活边框 */
  1419. --danger-color: #ff6d00; /* 着重色:边框 */
  1420. --accent-bg-color: #009688;
  1421. --secondary-bg-color: #616161;
  1422. --content-bg-color: #424242;
  1423. --accent-font-color: #f5f5f5;
  1424. --secondary-font-color: #eeeeee;
  1425. --margin-padding: 0.5rem;
  1426. --border-thickness: 0.2rem;
  1427. --border-radius: 0.5rem;
  1428. --title-bar-height: 2rem;
  1429. --title-bar-justify-content: left;
  1430. --task-bar-height: 3rem;
  1431. --shadow: 0.6rem 0.6rem 0.1rem 0.1rem rgba(0, 0, 0, 0.2);
  1432. --focused-bg-color: rgba(255, 255, 255, 0.1);
  1433. font-size: small;
  1434. user-select: none;
  1435. position: absolute;
  1436. background-color: var(--content-bg-color);
  1437. border: var(--border-thickness) solid var(--secondary-color);
  1438. border-radius: var(--border-radius);
  1439. padding: 0;
  1440. opacity: 0;
  1441. transform: scale(0.8);
  1442. pointer-events: none;
  1443. z-index: 4000; /* 确保在顶层 */
  1444. min-width: 150px;
  1445. &.show {
  1446. display: block;
  1447. opacity: 1;
  1448. transform: scale(1);
  1449. pointer-events: auto;
  1450. }
  1451. &:hover {
  1452. box-shadow: var(--shadow);
  1453. --secondary-color: var(--accent-color);
  1454. }
  1455. }
  1456. #custom-context-menu ul {
  1457. list-style: none;
  1458. margin: 0;
  1459. padding: 0;
  1460. }
  1461. #custom-context-menu li {
  1462. padding: var(--margin-padding);
  1463. cursor: pointer;
  1464. list-style-type: none; /* 去除列表项标记 */
  1465. }
  1466. #custom-context-menu li:hover {
  1467. background-color: var(--focused-bg-color);
  1468. }`;
  1469. style.id = "addStyle";
  1470. doc.getElementsByTagName("HEAD").item(0).appendChild(style);
  1471. // 初始化元素
  1472. const contextMenu = doc.createElement("ul");
  1473. contextMenu.id = "custom-context-menu";
  1474. doc.body.appendChild(contextMenu);
  1475. // 监听全局右键点击事件
  1476. doc.addEventListener("contextmenu", function (e) {
  1477. // 阻止默认右键菜单
  1478. e.preventDefault();
  1479. // 获取被点击的元素
  1480. const target = e.target;
  1481. const selection = window.getSelection();
  1482. const hasSelection =
  1483. selection && selection.toString().trim().length > 0;
  1484. // 检查事件目标是否在选区内
  1485. let inSelection = false;
  1486. if (hasSelection && selection.rangeCount > 0) {
  1487. const range = selection.getRangeAt(0);
  1488. inSelection = range.intersectsNode(e.target);
  1489. }
  1490. // 如果有文本被选中且右键点击在选区内,显示默认文本操作菜单
  1491. if (hasSelection && inSelection) {
  1492. const defaultTextOptions = [
  1493. { label: "复制", action: "copy" },
  1494. { label: "剪切", action: "cut" },
  1495. { label: "粘贴", action: "paste" },
  1496. { label: "全选", action: "selectAll" },
  1497. ];
  1498. // 清空菜单
  1499. contextMenu.innerHTML = "";
  1500. // 生成菜单项
  1501. defaultTextOptions.forEach((option) => {
  1502. const li = doc.createElement("li");
  1503. li.textContent = option.label;
  1504. li.onclick = function () {
  1505. if (option.action === "copy") {
  1506. doc.execCommand("copy");
  1507. } else if (option.action === "cut") {
  1508. doc.execCommand("cut");
  1509. } else if (option.action === "paste") {
  1510. doc.execCommand("paste");
  1511. } else if (option.action === "selectAll") {
  1512. doc.execCommand("selectAll");
  1513. }
  1514. contextMenu.classList.remove("show");
  1515. };
  1516. contextMenu.appendChild(li);
  1517. });
  1518. // 显示菜单(确保在屏幕内)
  1519. translateElement([contextMenu]);
  1520. contextMenu.classList.add("show");
  1521. // 获取菜单尺寸
  1522. const menuWidth = contextMenu.offsetWidth;
  1523. const menuHeight = contextMenu.offsetHeight;
  1524. // 计算调整后的位置
  1525. let adjustedLeft = e.pageX;
  1526. let adjustedTop = e.pageY;
  1527. // 如果靠近右边缘,向左偏移
  1528. if (window.innerWidth - e.pageX < menuWidth) {
  1529. adjustedLeft = e.pageX - menuWidth;
  1530. }
  1531. // 如果靠近下边缘,向上偏移
  1532. if (window.innerHeight - e.pageY < menuHeight) {
  1533. adjustedTop = e.pageY - menuHeight;
  1534. }
  1535. contextMenu.style.left = adjustedLeft + "px";
  1536. contextMenu.style.top = adjustedTop + "px";
  1537. }
  1538. // 否则检查元素的 data-menu-options 属性
  1539. else if (target.dataset.menuOptions) {
  1540. try {
  1541. // 解析 JSON 字符串
  1542. const menuOptions = JSON.parse(target.dataset.menuOptions);
  1543. // 清空菜单
  1544. contextMenu.innerHTML = "";
  1545. // 动态生成菜单项
  1546. menuOptions.forEach((option) => {
  1547. const li = doc.createElement("li");
  1548. li.textContent = option.label;
  1549. li.onclick = function () {
  1550. // 执行action
  1551. if (option.action) {
  1552. // 触发自定义事件
  1553. const event = new CustomEvent(
  1554. "custom-contextmenu-action",
  1555. {
  1556. detail: {
  1557. action: option.action,
  1558. target: target,
  1559. },
  1560. }
  1561. );
  1562. doc.dispatchEvent(event);
  1563. }
  1564. // 隐藏菜单
  1565. contextMenu.classList.remove("show");
  1566. };
  1567. contextMenu.appendChild(li);
  1568. });
  1569. // 显示菜单(确保在屏幕内)
  1570. translateElement([contextMenu]);
  1571. contextMenu.classList.add("show");
  1572. // 获取菜单尺寸
  1573. const menuWidth = contextMenu.offsetWidth;
  1574. const menuHeight = contextMenu.offsetHeight;
  1575. // 计算调整后的位置
  1576. let adjustedLeft = e.pageX;
  1577. let adjustedTop = e.pageY;
  1578. // 如果靠近右边缘,向左偏移
  1579. if (window.innerWidth - e.pageX < menuWidth) {
  1580. adjustedLeft = e.pageX - menuWidth;
  1581. }
  1582. // 如果靠近下边缘,向上偏移
  1583. if (window.innerHeight - e.pageY < menuHeight) {
  1584. adjustedTop = e.pageY - menuHeight;
  1585. }
  1586. contextMenu.style.left = adjustedLeft + "px";
  1587. contextMenu.style.top = adjustedTop + "px";
  1588. } catch (error) {
  1589. console.error("解析菜单选项失败", error);
  1590. }
  1591. } else {
  1592. // 如果没有定义菜单选项,则隐藏菜单
  1593. contextMenu.classList.remove("show");
  1594. }
  1595. });
  1596. // 点击其他地方隐藏菜单
  1597. doc.addEventListener("click", function () {
  1598. contextMenu.classList.remove("show");
  1599. });
  1600. // 阻止菜单内部的点击事件冒泡
  1601. contextMenu.addEventListener("click", function (e) {
  1602. e.stopPropagation();
  1603. });
  1604. // 处理任务栏窗口按钮的右键菜单动作
  1605. doc.addEventListener("custom-contextmenu-action", function (e) {
  1606. const { action, target } = e.detail;
  1607. const windowId = target.dataset.windowId;
  1608. if (!windowId) return;
  1609. if (action === "move") {
  1610. wManager.moveWindow(windowId);
  1611. } else if (action === "minimize") {
  1612. wManager.minimizeWindow(windowId);
  1613. } else if (action === "maximize") {
  1614. wManager.maximizeWindow(windowId);
  1615. } else if (action === "close") {
  1616. wManager.closeWindow(windowId);
  1617. }
  1618. });
  1619. }
  1620. initOptionMenu(document);
  1621. (async function init() {
  1622. // (async function desktop() {
  1623. // // 为桌面进程创建全屏无边框窗口(桌面背景)
  1624. // const desktopWin = wManager.createWindow("desktop", {
  1625. // css: {
  1626. // "z-index": "-1",
  1627. // "pointer-events": "none",
  1628. // },
  1629. // borderless: true,
  1630. // fullscreen: true,
  1631. // title: "桌面背景",
  1632. // tabs: [
  1633. // {
  1634. // title: "桌面",
  1635. // css: {
  1636. // position: "absolute",
  1637. // top: "0",
  1638. // left: "0",
  1639. // width: "100%",
  1640. // height: "100%",
  1641. // background: "linear-gradient(#EEE, #666)",
  1642. // "z-index": "-1",
  1643. // overflow: "hidden",
  1644. // },
  1645. // content: " ",
  1646. // },
  1647. // ],
  1648. // });
  1649. // })();
  1650. (async function taskBar() {
  1651. const taskBarProcess = await pManager.createProcess("taskBar");
  1652. const taskbarContainer =
  1653. document.querySelector("taskbar-container");
  1654. const taskBar = taskbarContainer.querySelector("task-bar");
  1655. (async function time() {
  1656. // 日期时间显示功能
  1657. const updateDateTime = () => {
  1658. const now = new Date();
  1659. const dateStr = now.toLocaleDateString("zh-CN", {
  1660. year: "numeric",
  1661. month: "2-digit",
  1662. day: "2-digit",
  1663. });
  1664. const timeStr = now.toLocaleTimeString("zh-CN", {
  1665. hour: "2-digit",
  1666. minute: "2-digit",
  1667. });
  1668. taskBar.querySelector(
  1669. ".date-time"
  1670. ).textContent = `${dateStr} ${timeStr}`;
  1671. };
  1672. // 初始更新并设置定时器
  1673. updateDateTime();
  1674. setInterval(updateDateTime, 1000);
  1675. })();
  1676. (async function power() {
  1677. // 电池状态功能(https://developer.mozilla.org/zh-CN/docs/Web/API/Battery_Status_API)
  1678. const powerButton = taskBar.querySelector(".power");
  1679. async function updateBatteryStatus() {
  1680. try {
  1681. if (!navigator.getBattery) {
  1682. powerButton.title = "电池状态不可用";
  1683. powerButton.textContent = "󰂃"; // 感叹号图标
  1684. console.warn("浏览器不支持电池状态 API");
  1685. return;
  1686. }
  1687. const battery = await navigator.getBattery();
  1688. const level = Math.floor(battery.level * 100);
  1689. let statusText = `电量: ${level}%`;
  1690. let batteryIcon = "󰂃"; // 默认感叹号图标
  1691. // 充电状态使用充电图标
  1692. if (battery.charging) {
  1693. batteryIcon = "󰂄";
  1694. statusText += " (充电中)";
  1695. if (battery.chargingTime < Infinity) {
  1696. statusText += ` | 充满时间: ${Math.floor(
  1697. battery.chargingTime / 60
  1698. )}分钟`;
  1699. }
  1700. }
  1701. // 非充电状态根据电量选择图标
  1702. else {
  1703. const icons = [
  1704. "󰁺", // 10%
  1705. "󰁻", // 20%
  1706. "󰁼", // 30%
  1707. "󰁽", // 40%
  1708. "󰁾", // 50%
  1709. "󰁿", // 60%
  1710. "󰂀", // 70%
  1711. "󰂁", // 80%
  1712. "󰂂", // 90%
  1713. "󰁹", // 100%
  1714. ];
  1715. const index = Math.min(Math.floor(level / 10), 9);
  1716. batteryIcon = level <= 10 ? "󰂃" : icons[index];
  1717. if (battery.dischargingTime < Infinity) {
  1718. statusText += ` | 剩余时间: ${Math.floor(
  1719. battery.dischargingTime / 60
  1720. )}分钟`;
  1721. }
  1722. }
  1723. powerButton.textContent = batteryIcon;
  1724. powerButton.title = statusText;
  1725. powerButton.style.display = "block";
  1726. // 添加事件监听器
  1727. battery.addEventListener(
  1728. "chargingchange",
  1729. updateBatteryStatus
  1730. );
  1731. battery.addEventListener("levelchange", updateBatteryStatus);
  1732. battery.addEventListener(
  1733. "chargingtimechange",
  1734. updateBatteryStatus
  1735. );
  1736. battery.addEventListener(
  1737. "dischargingtimechange",
  1738. updateBatteryStatus
  1739. );
  1740. } catch (error) {
  1741. powerButton.textContent = "󰂃";
  1742. powerButton.title = "电池状态检测失败";
  1743. console.warn("获取电池状态失败:", error);
  1744. }
  1745. }
  1746. // 点击显示详细状态
  1747. powerButton.addEventListener("click", async () => {
  1748. try {
  1749. if (navigator.getBattery) {
  1750. const battery = await navigator.getBattery();
  1751. const statusDetails = [
  1752. `电量: ${Math.floor(battery.level * 100)}%`,
  1753. `充电状态: ${battery.charging ? "充电中" : "未充电"}`,
  1754. `充电时间: ${
  1755. battery.chargingTime === Infinity
  1756. ? "未知"
  1757. : Math.floor(battery.chargingTime / 60) + "分钟"
  1758. }`,
  1759. `放电时间: ${
  1760. battery.dischargingTime === Infinity
  1761. ? "未知"
  1762. : Math.floor(battery.dischargingTime / 60) + "分钟"
  1763. }`,
  1764. ].join("\n");
  1765. var e = document.createElement("pre");
  1766. e.textContent = statusDetails;
  1767. // 创建电池状态窗口
  1768. const batteryWinId = wManager.createWindow("taskBar", {
  1769. title: "电池状态",
  1770. width: 300,
  1771. height: 200,
  1772. x: window.screen.width / 2 - 150,
  1773. y: window.screen.height / 2 - 100,
  1774. tabs: [
  1775. {
  1776. title: "详细信息",
  1777. content: e,
  1778. },
  1779. ],
  1780. });
  1781. }
  1782. } catch (error) {
  1783. var e = document.createElement("p");
  1784. e.style.color = "var(--danger-color)";
  1785. e.textContent = "无法获取电池详细信息" + error;
  1786. // 创建错误提示窗口
  1787. const errorWinId = wManager.createWindow("taskBar", {
  1788. title: "错误",
  1789. width: 250,
  1790. height: 100,
  1791. x: window.screen.width / 2 - 125,
  1792. y: window.screen.height / 2 - 50,
  1793. tabs: [
  1794. {
  1795. title: "提示",
  1796. content: e,
  1797. },
  1798. ],
  1799. });
  1800. }
  1801. });
  1802. // 初始化电池状态
  1803. updateBatteryStatus();
  1804. })();
  1805. })();
  1806. (function apps() {
  1807. // 应用程序:创建一个进程,该进程可创建多个窗口,当所有该进程的窗口都关闭时,进程自动退出
  1808. async function application(appProcessId, windowConfigs) {
  1809. await pManager.createProcess(appProcessId);
  1810. var count = 0;
  1811. // 跟踪窗口关闭状态
  1812. let windows = [];
  1813. windowConfigs.forEach((windowConfig) => {
  1814. // 创建窗口
  1815. const windowId = wManager.createWindow(
  1816. appProcessId,
  1817. windowConfig
  1818. );
  1819. // 焦点
  1820. fManager.focusWindow(windowId);
  1821. wManager.focusWindow(windowId);
  1822. // 通过事件监听来监控窗口关闭
  1823. const windowObj = { windowId: windowId, closed: false };
  1824. windows[count] = windowObj;
  1825. // 监听窗口关闭事件
  1826. const closeHandler = () => {
  1827. windowObj.closed = true;
  1828. // 检查是否所有窗口都已关闭
  1829. const allClosed = windows.every((w) => w.closed);
  1830. if (allClosed) {
  1831. // 所有窗口都已关闭,终止进程
  1832. pManager.terminateProcess(appProcessId);
  1833. }
  1834. };
  1835. // 设置关闭处理器
  1836. const windowInstance = wManager.windows.get(windowId);
  1837. if (windowInstance) {
  1838. windowInstance.setCloseHandler(closeHandler);
  1839. } else {
  1840. console.warn(`未找到窗口实例: ${windowId}`);
  1841. }
  1842. count++;
  1843. });
  1844. }
  1845. document
  1846. .querySelector("taskbar-container bar-space .app-launcher")
  1847. .addEventListener("click", async () => {
  1848. const launcherPath = "../../../packages/pageos-app-launcher/";
  1849. const response = await fetch(launcherPath + "versions.txt");
  1850. const versions = await response.text();
  1851. const version = versions.split("\n").pop();
  1852. const iframe = document.createElement("iframe");
  1853. iframe.style.cssText =
  1854. "width: 100%; height: 100%; border: none;";
  1855. iframe.src = `${launcherPath + version}/index.html`;
  1856. // 监听菜单 iframe 加载完成事件
  1857. iframe.addEventListener("load", function () {
  1858. // 获取 iframe 内部文档对象
  1859. const iframeDoc =
  1860. iframe.contentDocument || iframe.contentWindow.document;
  1861. // 初始化右键菜单
  1862. // initOptionMenu(iframeDoc);
  1863. // 在 iframe 内部文档上绑定点击事件
  1864. iframeDoc.addEventListener("click", async function (event) {
  1865. const anchor = event.target.closest('a[data-pageos="app"]');
  1866. if (
  1867. anchor &&
  1868. anchor.href &&
  1869. anchor.href.endsWith("metadata.json")
  1870. ) {
  1871. try {
  1872. event.preventDefault();
  1873. const response = await fetch(anchor.href);
  1874. const metadata = await response.json();
  1875. const newIframe = document.createElement("iframe");
  1876. newIframe.style.cssText =
  1877. "width: 100%; height: 100%; border: none;";
  1878. newIframe.src = new URL(
  1879. metadata.entry,
  1880. anchor.href
  1881. ).href;
  1882. application(metadata.id, [
  1883. {
  1884. icon: metadata.icon
  1885. ? `<img src="${
  1886. new URL(metadata.icon, anchor.href).href
  1887. }" />`
  1888. : metadata.id[0],
  1889. title: metadata.name,
  1890. width: window.screen.width * 0.6,
  1891. height: window.screen.height * 0.6,
  1892. x: window.screen.width * 0.2,
  1893. y: window.screen.height * 0.2,
  1894. tabs: [
  1895. {
  1896. css: { padding: 0 },
  1897. title: metadata.name,
  1898. content: newIframe,
  1899. },
  1900. ],
  1901. },
  1902. ]);
  1903. } catch (error) {
  1904. console.error("加载 metadata.json 失败:", error);
  1905. }
  1906. }
  1907. });
  1908. });
  1909. application("pageos-app-launcher", [
  1910. {
  1911. icon: `<img src="${
  1912. launcherPath + version
  1913. }assets/icon.svg" />`,
  1914. title: "PageOS 应用启动器",
  1915. width:
  1916. window.screen.width * 0.9 > 1024
  1917. ? 1024
  1918. : window.screen.width * 0.9,
  1919. height: window.screen.height * 0.6,
  1920. x:
  1921. (window.screen.width -
  1922. (window.screen.width * 0.9 > 1024
  1923. ? 1024
  1924. : window.screen.width * 0.9)) *
  1925. 0.5,
  1926. y: window.screen.height * 0.25,
  1927. tabs: [
  1928. {
  1929. css: { padding: 0 },
  1930. title: "PageOS 应用启动器",
  1931. content: iframe,
  1932. },
  1933. ],
  1934. },
  1935. ]);
  1936. });
  1937. })();
  1938. })();
  1939. })();
  1940. </script>
  1941. </body>
  1942. </html>