基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-04 b3bde921167ee0a7dc21cf7e3d642840a0fcc11e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Audio Visualizer WS Test</title>
  <style>
    :root {
      color-scheme: dark;
      --hot: #ff2fb3;
      --cool: #29d8ff;
      --accent: #f8fbff;
      --panel: rgba(9, 12, 18, 0.72);
      --line: rgba(255, 255, 255, 0.14);
      --text: #edf4ff;
      --muted: #8ea2b8;
      --ok: #42f5b0;
      --warn: #ffd166;
      --bad: #ff6b6b;
    }
 
    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0;
      overflow: hidden;
      background: #05070b;
      color: var(--text);
      font-family: "Segoe UI", Arial, sans-serif;
    }
 
    body {
      display: grid;
      grid-template-rows: auto 1fr;
    }
 
    .toolbar {
      display: grid;
      grid-template-columns: minmax(220px, 1fr) auto auto;
      gap: 10px;
      align-items: center;
      padding: 10px 12px;
      background: var(--panel);
      border-bottom: 1px solid var(--line);
      box-sizing: border-box;
    }
 
    .url {
      min-width: 0;
      height: 32px;
      padding: 0 10px;
      border: 1px solid var(--line);
      border-radius: 6px;
      box-sizing: border-box;
      background: rgba(255, 255, 255, 0.06);
      color: var(--text);
      font: inherit;
    }
 
    .button {
      height: 32px;
      padding: 0 12px;
      border: 1px solid rgba(41, 216, 255, 0.45);
      border-radius: 6px;
      background: rgba(41, 216, 255, 0.14);
      color: var(--text);
      cursor: pointer;
      font: inherit;
    }
 
    .button:hover {
      background: rgba(41, 216, 255, 0.24);
    }
 
    .status {
      display: flex;
      align-items: center;
      gap: 8px;
      min-width: 132px;
      color: var(--muted);
      font-size: 13px;
      white-space: nowrap;
    }
 
    .dot {
      width: 9px;
      height: 9px;
      border-radius: 50%;
      background: var(--warn);
      box-shadow: 0 0 12px currentColor;
    }
 
    .status[data-state="open"] {
      color: var(--ok);
    }
 
    .status[data-state="closed"],
    .status[data-state="error"] {
      color: var(--bad);
    }
 
    .status[data-state="open"] .dot {
      background: var(--ok);
    }
 
    .status[data-state="closed"] .dot,
    .status[data-state="error"] .dot {
      background: var(--bad);
    }
 
    #stage {
      position: relative;
      width: 100vw;
      height: 100%;
      min-height: 0;
      overflow: hidden;
      background:
        linear-gradient(180deg, rgba(41, 216, 255, 0.08), transparent 28%),
        radial-gradient(circle at 50% 58%, rgba(255, 47, 179, 0.18), transparent 34%),
        #05070b;
      --base-y: 60%;
    }
 
    #stage::before {
      content: "";
      position: absolute;
      left: 0;
      right: 0;
      top: var(--base-y);
      height: 1px;
      background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.72), transparent);
      box-shadow: 0 0 8px rgba(255, 255, 255, 0.68), 0 0 22px rgba(255, 47, 179, 0.42);
      opacity: 0.86;
    }
 
    .bars {
      position: absolute;
      inset: 0 14px;
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
      gap: 2px;
    }
 
    .visualizer {
      position: absolute;
      inset: 0;
      width: 100%;
      height: 100%;
      display: block;
    }
 
    .bar {
      --bar-height: 1%;
      --bar-top: 59%;
      --bar-opacity: 0.24;
      --reflection-height: 1vh;
      --reflection-opacity: 0.04;
      position: relative;
      flex: 1 1 auto;
      min-width: 2px;
      max-width: 7px;
      height: var(--bar-height);
      margin-top: var(--bar-top);
      border-radius: 999px;
      background: linear-gradient(180deg, var(--accent), var(--hot) 36%, var(--cool));
      box-shadow: 0 0 5px rgba(255, 255, 255, 0.72), 0 0 13px rgba(255, 47, 179, 0.46);
      opacity: var(--bar-opacity);
      transition: height 58ms linear, margin-top 58ms linear, opacity 58ms linear;
    }
 
    .bar::after {
      content: "";
      position: absolute;
      width: inherit;
      min-width: inherit;
      max-width: inherit;
      height: var(--reflection-height);
      transform: translateY(100%) scaleY(0.72);
      transform-origin: top center;
      border-radius: inherit;
      background: linear-gradient(180deg, rgba(255, 47, 179, 0.24), rgba(41, 216, 255, 0.02));
      filter: blur(1px);
      opacity: var(--reflection-opacity);
    }
 
    .metrics {
      position: absolute;
      left: 12px;
      bottom: 12px;
      width: min(680px, calc(100vw - 24px));
      display: grid;
      grid-template-columns: repeat(4, minmax(0, 1fr));
      gap: 8px;
      padding: 10px;
      border: 1px solid var(--line);
      border-radius: 8px;
      background: rgba(9, 12, 18, 0.72);
      box-sizing: border-box;
      backdrop-filter: blur(10px);
    }
 
    .metric {
      min-width: 0;
    }
 
    .label {
      display: block;
      color: var(--muted);
      font-size: 11px;
      line-height: 1.4;
    }
 
    .value {
      display: block;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      font-size: 13px;
      line-height: 1.5;
    }
 
    .raw {
      position: absolute;
      right: 12px;
      bottom: 12px;
      width: min(460px, calc(100vw - 24px));
      max-height: 136px;
      margin: 0;
      padding: 10px;
      overflow: auto;
      border: 1px solid var(--line);
      border-radius: 8px;
      background: rgba(9, 12, 18, 0.72);
      color: #b6c7dc;
      box-sizing: border-box;
      font: 12px/1.45 Consolas, "Cascadia Mono", monospace;
      backdrop-filter: blur(10px);
    }
 
    @media (max-width: 860px) {
      .toolbar {
        grid-template-columns: 1fr auto;
      }
 
      .status {
        grid-column: 1 / -1;
      }
 
      .metrics {
        grid-template-columns: repeat(2, minmax(0, 1fr));
      }
 
      .raw {
        display: none;
      }
    }
  </style>
</head>
<body>
  <header class="toolbar">
    <input id="wsUrl" class="url" value="ws://127.0.0.1:8765/ws/events" aria-label="WebSocket URL">
    <button id="connect" class="button" type="button">Connect</button>
    <div id="status" class="status" data-state="closed">
      <span class="dot"></span>
      <span id="statusText">未连接</span>
    </div>
  </header>
  <main id="stage">
    <canvas id="visualizer" class="visualizer"></canvas>
    <div id="bars" class="bars"></div>
    <section class="metrics" aria-label="WebSocket audio metrics">
      <div class="metric">
        <span class="label">EVENT</span>
        <span id="eventName" class="value">-</span>
      </div>
      <div class="metric">
        <span class="label">FRAMES</span>
        <span id="frameCount" class="value">0</span>
      </div>
      <div class="metric">
        <span class="label">RMS</span>
        <span id="rmsValue" class="value">-</span>
      </div>
      <div class="metric">
        <span class="label">LEVELS</span>
        <span id="levelsCount" class="value">-</span>
      </div>
    </section>
    <pre id="raw" class="raw">等待 ws://127.0.0.1:8765/ws/events 数据...</pre>
  </main>
  <script>
    const barCount = 192;
    const reconnectDelayMs = 1200;
    const compressionPower = 0.52;
    const smoothingAttack = 0.48;
    const smoothingRelease = 0.18;
    const gaussianSigma = 0.42;
    const canvas = document.getElementById("visualizer");
    const context = canvas.getContext("2d");
    const barsRoot = document.getElementById("bars");
    const wsUrlInput = document.getElementById("wsUrl");
    const connectButton = document.getElementById("connect");
    const statusNode = document.getElementById("status");
    const statusText = document.getElementById("statusText");
    const eventNameNode = document.getElementById("eventName");
    const frameCountNode = document.getElementById("frameCount");
    const rmsValueNode = document.getElementById("rmsValue");
    const levelsCountNode = document.getElementById("levelsCount");
    const rawNode = document.getElementById("raw");
    const bars = Array.from({ length: barCount }, () => {
      const node = document.createElement("i");
      node.className = "bar";
      barsRoot.appendChild(node);
      return node;
    });
    let socket = null;
    let reconnectTimer = 0;
    let manualClose = false;
    let rawLevels = new Array(64).fill(0);
    let visualLevels = new Array(barCount).fill(0.01);
    let smoothedLevels = new Array(barCount).fill(0.01);
    let frames = 0;
 
    function resizeCanvas() {
      const rect = canvas.getBoundingClientRect();
      const ratio = window.devicePixelRatio || 1;
      const width = Math.max(1, Math.floor(rect.width * ratio));
      const height = Math.max(1, Math.floor(rect.height * ratio));
      if (canvas.width !== width || canvas.height !== height) {
        canvas.width = width;
        canvas.height = height;
      }
      context.setTransform(ratio, 0, 0, ratio, 0, 0);
    }
 
    function setStatus(state, text) {
      statusNode.dataset.state = state;
      statusText.textContent = text;
    }
 
    function connect() {
      manualClose = false;
      clearTimeout(reconnectTimer);
      if (socket) {
        socket.close();
      }
 
      setStatus("connecting", "连接中");
      socket = new WebSocket(wsUrlInput.value.trim() || "ws://127.0.0.1:8765/ws/events");
      socket.addEventListener("open", () => setStatus("open", "已连接"));
      socket.addEventListener("message", event => handleMessage(event.data));
      socket.addEventListener("error", () => setStatus("error", "连接错误"));
      socket.addEventListener("close", () => {
        setStatus("closed", manualClose ? "已断开" : "已断开,重连中");
        if (!manualClose) {
          reconnectTimer = window.setTimeout(connect, reconnectDelayMs);
        }
      });
    }
 
    function disconnect() {
      manualClose = true;
      clearTimeout(reconnectTimer);
      if (socket) {
        socket.close();
        socket = null;
      }
      setStatus("closed", "已断开");
    }
 
    function handleMessage(text) {
      let message;
      try {
        message = JSON.parse(text);
      } catch {
        rawNode.textContent = text;
        return;
      }
 
      eventNameNode.textContent = message.name || "-";
      rawNode.textContent = JSON.stringify(message, null, 2).slice(0, 1600);
      if (message.name !== "audio.spectrum") {
        return;
      }
 
      const payload = message.payload || {};
      const nextLevels = payload.levels || payload.Levels;
      if (!Array.isArray(nextLevels) || nextLevels.length === 0) {
        return;
      }
 
      rawLevels = nextLevels.map(value => Math.max(0, Math.min(1, Number(value) || 0)));
      visualLevels = processSpectrum(rawLevels);
      const peak = Math.max(...rawLevels);
      frames += 1;
      frameCountNode.textContent = String(frames);
      levelsCountNode.textContent = `${rawLevels.length} -> ${visualLevels.length} / peak ${peak.toFixed(3)}`;
      rmsValueNode.textContent = Number(payload.rms ?? payload.Rms ?? 0).toFixed(4);
    }
 
    function processSpectrum(input) {
      const binned = logBin(input, barCount);
      const peak = Math.max(...binned);
      const gain = peak > 0 ? Math.min(18, Math.max(2, 0.86 / peak)) : 1;
      return binned.map((value, index) => {
        const compressed = Math.pow(Math.min(1, value * gain), compressionPower);
        return Math.max(0.01, Math.min(1, compressed * gaussianEnvelope(index, binned.length)));
      });
    }
 
    function logBin(input, outputCount) {
      if (input.length === 0) {
        return new Array(outputCount).fill(0);
      }
 
      const last = input.length - 1;
      const logMax = Math.log(input.length);
      return Array.from({ length: outputCount }, (_, index) => {
        const start = Math.max(0, Math.floor(Math.exp(logMax * (index / outputCount)) - 1));
        const end = Math.min(last, Math.ceil(Math.exp(logMax * ((index + 1) / outputCount)) - 1));
        let sum = 0;
        let weight = 0;
        for (let bin = start; bin <= end; bin++) {
          const localWeight = 1 + (bin / Math.max(1, last)) * 0.35;
          sum += (input[bin] || 0) * localWeight;
          weight += localWeight;
        }
        return weight > 0 ? sum / weight : 0;
      });
    }
 
    function gaussianEnvelope(index, count) {
      const x = count > 1 ? (index / (count - 1)) * 2 - 1 : 0;
      const gaussian = Math.exp(-(x * x) / (2 * gaussianSigma * gaussianSigma));
      return 0.28 + gaussian * 0.82;
    }
 
    function sample(index) {
      const target = visualLevels[index] || 0.01;
      const current = smoothedLevels[index] || 0.01;
      const rate = target > current ? smoothingAttack : smoothingRelease;
      const value = current + (target - current) * rate;
      smoothedLevels[index] = value;
      return Math.max(0.01, Math.min(1, value));
    }
 
    function draw() {
      resizeCanvas();
      const frameLevels = visualLevels.map((_, index) => sample(index));
      drawCanvas(frameLevels);
      bars.forEach((bar, index) => {
        const centerBias = Math.sin((index / Math.max(1, bars.length - 1)) * Math.PI);
        const value = Math.min(1, frameLevels[index] * (0.72 + centerBias * 0.36));
        const height = value * 46;
        const top = Math.max(0, 60 - height);
        bar.style.setProperty("--bar-height", `${height.toFixed(2)}%`);
        bar.style.setProperty("--bar-top", `${top.toFixed(2)}%`);
        bar.style.setProperty("--bar-opacity", (0.24 + value * 0.76).toFixed(3));
        bar.style.setProperty("--reflection-height", `${(value * 26).toFixed(2)}vh`);
        bar.style.setProperty("--reflection-opacity", (value * 0.34).toFixed(3));
      });
      requestAnimationFrame(draw);
    }
 
    function drawCanvas(frameLevels) {
      const width = canvas.clientWidth;
      const height = canvas.clientHeight;
      const baseY = height * 0.6;
      context.clearRect(0, 0, width, height);
 
      const lineGradient = context.createLinearGradient(0, 0, width, 0);
      lineGradient.addColorStop(0, "rgba(255,255,255,0)");
      lineGradient.addColorStop(0.5, "rgba(255,255,255,0.72)");
      lineGradient.addColorStop(1, "rgba(255,255,255,0)");
      context.fillStyle = lineGradient;
      context.shadowBlur = 18;
      context.shadowColor = "rgba(255, 47, 179, 0.55)";
      context.fillRect(0, baseY, width, 1);
      context.shadowBlur = 0;
 
      const gap = 2;
      const barWidth = Math.max(2, Math.min(7, (width - gap * (barCount - 1)) / barCount));
      for (let index = 0; index < barCount; index++) {
        const centerBias = Math.sin((index / Math.max(1, barCount - 1)) * Math.PI);
        const value = Math.min(1, frameLevels[index] * (0.72 + centerBias * 0.36));
        const barHeight = Math.max(2, value * height * 0.48);
        const x = index * (barWidth + gap);
        const y = baseY - barHeight;
        const gradient = context.createLinearGradient(0, y, 0, baseY);
        gradient.addColorStop(0, "#f8fbff");
        gradient.addColorStop(0.36, "#ff2fb3");
        gradient.addColorStop(1, "#29d8ff");
        context.globalAlpha = 0.28 + value * 0.72;
        context.fillStyle = gradient;
        context.shadowBlur = 13;
        context.shadowColor = "rgba(255, 47, 179, 0.5)";
        roundRect(context, x, y, barWidth, barHeight, barWidth / 2);
        context.fill();
 
        context.globalAlpha = value * 0.24;
        context.shadowBlur = 0;
        context.fillStyle = "rgba(255, 47, 179, 0.38)";
        roundRect(context, x, baseY, barWidth, barHeight * 0.62, barWidth / 2);
        context.fill();
      }
      context.globalAlpha = 1;
      context.shadowBlur = 0;
    }
 
    function roundRect(target, x, y, width, height, radius) {
      const safeRadius = Math.min(radius, width / 2, height / 2);
      target.beginPath();
      target.moveTo(x + safeRadius, y);
      target.lineTo(x + width - safeRadius, y);
      target.quadraticCurveTo(x + width, y, x + width, y + safeRadius);
      target.lineTo(x + width, y + height - safeRadius);
      target.quadraticCurveTo(x + width, y + height, x + width - safeRadius, y + height);
      target.lineTo(x + safeRadius, y + height);
      target.quadraticCurveTo(x, y + height, x, y + height - safeRadius);
      target.lineTo(x, y + safeRadius);
      target.quadraticCurveTo(x, y, x + safeRadius, y);
      target.closePath();
    }
 
    connectButton.addEventListener("click", () => {
      if (socket && socket.readyState === WebSocket.OPEN) {
        disconnect();
        connectButton.textContent = "Connect";
        return;
      }
 
      connect();
      connectButton.textContent = "Disconnect";
    });
 
    wsUrlInput.addEventListener("keydown", event => {
      if (event.key === "Enter") {
        connect();
        connectButton.textContent = "Disconnect";
      }
    });
 
    window.addEventListener("resize", resizeCanvas);
    draw();
    connect();
    connectButton.textContent = "Disconnect";
  </script>
</body>
</html>