index.html 69 KB

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