Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions mist-player/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 平板类 Apple Music 播放页 1.1.0

需要 API 1.12 Android 宿主。

| 平板宽屏 | 平板侧栏 |
|---|---|
| ![平板宽屏](screenshots/wide.jpg) | ![平板侧栏](screenshots/sidebar.jpg) |

- 背景:`background/mist.js` 按设计稿 CSS 复刻:拿 `state.cover`(需 `canvasCover: true`)放大 1.4 倍,`blur(6% 宽) saturate(1.6) brightness(.75)`,叠两个取自调色板的径向光晕与 18% 黑色遮罩;浅色主题整体提亮。结果缓存到离屏画布,只在封面、尺寸或配色变化时重画。
- `ui/wide.json`:平板侧栏铺开到全屏时的左栏。自定义顶栏(收起、播放来源、更多),大圆角封面带调色板主色投影(暂停时缩小、双击收藏),歌名、歌手,进度条,三个无底色大控制键,底部收藏、播放模式、评论、定时、分享、播放列表。右栏只显示歌词,加大留白、限制最大宽度。
- `ui/player.json`:竖屏同一套排版,点小歌词打开歌词页。
- `ui/lyrics.json`:全屏歌词页使用同一背景;歌词白色(深色)/深灰(浅色),左对齐、当前行放大、其余行按距离模糊。宽屏右栏的歌词颜色与样式也读取这里。
- `ui/mini.json`:迷你播放器使用封面底色到高亮色的横向渐变。

构建:`python package.py`(校验布局并打包);检查:`node test-background.cjs`。
导入 `mist-player.melodiaplugin` 后,在「设置 → 扩展 → 插件 → 播放器布局」里给各区域选择本插件。停用插件即可恢复内置外观。
132 changes: 132 additions & 0 deletions mist-player/background/mist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// 背景 API v1:按设计稿的 CSS 复刻模糊封面背景
// 封面放大 1.4 倍 → blur(6% 宽) saturate(1.6) brightness(.75) → 两个径向光晕 → 18% 黑色遮罩
(() => {
const FALLBACK = '#404040';
// 设计稿取值:光晕位置与半径均为宽高百分比
const GLOWS = [
{ slot: 0, x: 0.20, y: 0.30, rx: 0.60, ry: 0.60, light: 61, sat: 13, alpha: 0.45 },
{ slot: 1, x: 0.85, y: 0.80, rx: 0.50, ry: 0.60, light: 30, sat: 23, alpha: 0.55 }
];
const TONE = {
dark: { base: 26, baseSat: 10, filter: 'saturate(1.6) brightness(0.75)', glowShift: 0, veil: 'rgba(0, 0, 0, 0.18)' },
// 浅色主题下宿主歌词是深色字,背景整体提亮
light: { base: 88, baseSat: 14, filter: 'saturate(1.5) brightness(1.15)', glowShift: 26, veil: 'rgba(255, 255, 255, 0.24)' }
};

let imageSrc = null;
let image = null;
let cache = null;

function parse(hex) {
const color = typeof hex === 'string' && /^#[0-9a-f]{6}$/i.test(hex) ? hex : FALLBACK;
const [red, green, blue] = [1, 3, 5].map(i => parseInt(color.slice(i, i + 2), 16) / 255);
const high = Math.max(red, green, blue);
const low = Math.min(red, green, blue);
const chroma = high - low;
let hue = 0;
if (chroma > 0) {
if (high === red) hue = ((green - blue) / chroma + 6) % 6;
else if (high === green) hue = 2 + (blue - red) / chroma;
else hue = 4 + (red - green) / chroma;
}
return { hue: hue * 60, vivid: Math.min(chroma / 0.18, 1) };
}

function sources(s) {
const colors = s.colors || {};
const base = colors.coverBase ?? s.coverColor;
const list = Array.isArray(s.palette) ? s.palette.filter(c => typeof c === 'string') : [];
return [list[0] ?? base, list[1] ?? colors.coverAccent ?? base].map(parse);
}

// 封面图异步解码,就绪后清缓存,下一帧重画
function track(src) {
const next = typeof src === 'string' && src.startsWith('data:image/') ? src : null;
if (next === imageSrc) return;
imageSrc = next;
if (!next || typeof Image === 'undefined') {
image = null;
cache = null;
return;
}
const pending = new Image();
pending.onload = () => {
if (imageSrc !== next) return;
image = pending;
cache = null;
};
pending.src = next;
}

function ellipse(ctx, w, h, glow, color) {
ctx.save();
ctx.translate(w * glow.x, h * glow.y);
ctx.scale(w * glow.rx, h * glow.ry);
const g = ctx.createRadialGradient(0, 0, 0, 0, 0, 1);
g.addColorStop(0, `hsla(${color}, ${glow.alpha})`);
g.addColorStop(1, `hsla(${color}, 0)`);
ctx.fillStyle = g;
ctx.fillRect(-1, -1, 2, 2);
ctx.restore();
}

function paint(ctx, s, w, h) {
const tone = s.theme === 'dark' ? TONE.dark : TONE.light;
const [primary, secondary] = sources(s);
ctx.fillStyle = `hsl(${primary.hue}, ${tone.baseSat * (0.4 + 0.6 * primary.vivid)}%, ${tone.base}%)`;
ctx.fillRect(0, 0, w, h);

if (image && image.width > 0 && image.height > 0) {
// 等同 CSS inset:-20% 加 background-size:cover
const boxW = w * 1.4, boxH = h * 1.4;
const scale = Math.max(boxW / image.width, boxH / image.height);
const drawW = image.width * scale, drawH = image.height * scale;
ctx.filter = `blur(${(w * 0.06).toFixed(2)}px) ${tone.filter}`;
ctx.drawImage(image, (w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
ctx.filter = 'none';
}

GLOWS.forEach(glow => {
const source = glow.slot === 0 ? primary : secondary;
const sat = glow.sat * (0.4 + 0.6 * source.vivid);
ellipse(ctx, w, h, glow, `${source.hue}, ${sat}%, ${Math.min(glow.light + tone.glowShift, 92)}%`);
});
ctx.fillStyle = tone.veil;
ctx.fillRect(0, 0, w, h);
}

function offscreen(width, height) {
if (typeof document === 'undefined' || !document.createElement) return null;
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
return canvas;
}

background.onState = s => track(s && s.cover && s.cover.src);

background.render = (ctx, s, time) => {
const w = s.width, h = s.height;
if (!(w > 0) || !(h > 0)) return;
track(s.cover && s.cover.src);
const ratio = Number.isFinite(s.pixelRatio) && s.pixelRatio > 0 ? s.pixelRatio : 1;
const palette = Array.isArray(s.palette) ? s.palette.slice(0, 2).join() : '';
const colors = s.colors || {};
const key = [w, h, ratio, s.theme, palette, colors.coverBase, colors.coverAccent, image ? imageSrc : ''].join('|');

// 背景静止,模糊开销只在封面、尺寸或配色变化时付一次
if (!cache || cache.key !== key) {
const surface = offscreen(Math.max(1, Math.round(w * ratio)), Math.max(1, Math.round(h * ratio)));
if (surface) {
const octx = surface.getContext('2d');
octx.scale(ratio, ratio);
paint(octx, s, w, h);
cache = { key, surface };
} else {
cache = null;
}
}
if (cache) ctx.drawImage(cache.surface, 0, 0, w, h);
else paint(ctx, s, w, h);
};
})();
Binary file added mist-player/mist-player.melodiaplugin
Binary file not shown.
74 changes: 74 additions & 0 deletions mist-player/package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import hashlib
import json
import sys
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile

root = Path(__file__).resolve().parent
FILES = (
"plugin.json",
"ui/wide.json",
"ui/player.json",
"ui/lyrics.json",
"ui/mini.json",
"background/mist.js",
)
# 与宿主 PlayerLayout 的上限一致
MAX_DEPTH = 8
MAX_NODES = 128


def measure(node, depth=1):
if depth > MAX_DEPTH:
raise ValueError(f"层级超过 {MAX_DEPTH}")
count = 1
for child in node.get("children", []):
count += measure(child, depth + 1)
return count


def has_play_pause(node):
if node.get("type") == "controls" and "playPause" in node.get("buttons", []):
return True
return any(has_play_pause(child) for child in node.get("children", []))


def check_layouts():
manifest = json.loads((root / "plugin.json").read_text(encoding="utf-8"))
for area, file in manifest["contributes"]["playerLayouts"].items():
data = json.loads((root / file).read_text(encoding="utf-8"))
layout = data.get("layout")
if layout == "host":
extra = set(data) - {"layout", "frame"}
if extra:
raise ValueError(f"{file}: 宿主内容模式不支持 {sorted(extra)}")
continue
count = measure(layout)
if count > MAX_NODES:
raise ValueError(f"{file}: 节点超过 {MAX_NODES}")
if not has_play_pause(layout):
raise ValueError(f"{file}: 缺少 playPause")
print(f"{area}: {count} 个节点")


def main():
try:
check_layouts()
except (OSError, ValueError, KeyError, json.JSONDecodeError) as error:
print(f"布局校验失败:{error}", file=sys.stderr)
return 1

out = root / "mist-player.melodiaplugin"
with ZipFile(out, "w", ZIP_DEFLATED) as package:
for name in FILES:
package.write(root / name, name)

data = out.read_bytes()
print(out)
print(f"sha256 {hashlib.sha256(data).hexdigest()}")
print(f"size {len(data)}")
return 0


if __name__ == "__main__":
sys.exit(main())
21 changes: 21 additions & 0 deletions mist-player/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"format": 2,
"kind": "sandbox",
"id": "com.melodia.mistplayer",
"name": "平板类 Apple Music 播放页",
"version": "1.1.0",
"author": "Melodia",
"description": "宿主原生模糊封面叠调色板光斑,大圆角封面带彩色投影,歌词左对齐、当前行放大、其余行模糊;平板宽屏、竖屏、歌词页与迷你播放器统一风格。需要 API 1.12 宿主。",
"apiVersion": "1.12",
"platforms": [
"android"
],
"contributes": {
"playerLayouts": {
"android.fullPlayerWide": "ui/wide.json",
"android.fullPlayer": "ui/player.json",
"android.lyrics": "ui/lyrics.json",
"android.miniPlayer": "ui/mini.json"
}
}
}
Binary file added mist-player/screenshots/sidebar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mist-player/screenshots/wide.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
136 changes: 136 additions & 0 deletions mist-player/test-background.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const path = require('node:path');

const source = fs.readFileSync(path.join(__dirname, 'background/mist.js'), 'utf8');
const COVER = 'data:image/jpeg;base64,AAAA';

function recorder() {
const log = { fills: [], filters: [], images: [], glows: [] };
const ctx = {
filter: 'none',
save() {},
restore() {},
translate(x, y) { log.glows.push({ x, y }); },
scale() {},
fillRect() { log.fills.push(typeof this.fillStyle === 'string' ? this.fillStyle : 'gradient'); },
drawImage(img, x, y, w, h) { log.images.push({ img, x, y, w, h, filter: this.filter }); },
createRadialGradient() {
const glow = log.glows.at(-1);
return { addColorStop(position, color) { if (position === 0) glow.color = color; } };
}
};
Object.defineProperty(ctx, 'filter', {
get() { return this._filter ?? 'none'; },
set(v) { this._filter = v; log.filters.push(v); }
});
return { ctx, log };
}

function load({ withImage = true, withDocument = true } = {}) {
const surfaces = [];
const context = { background: {} };
if (withImage) {
context.Image = class {
constructor() { this.width = 128; this.height = 128; }
set src(value) { this._src = value; if (this.onload) this.onload(); }
get src() { return this._src; }
};
}
if (withDocument) {
context.document = {
createElement() {
const { ctx, log } = recorder();
const surface = { width: 0, height: 0, log, getContext: () => ctx };
surfaces.push(surface);
return surface;
}
};
}
vm.runInNewContext(source, context);
return { bg: context.background, surfaces };
}

function render(bg, state, theme = 'dark') {
const { ctx, log } = recorder();
bg.render(ctx, { width: 1280, height: 800, pixelRatio: 1, theme, ...state }, 0);
return log;
}

const palette = ['#5E6B5F', '#2A3550'];

// 封面就绪:离屏画一次(铺底、模糊封面、两个光晕、遮罩),主画布只贴图
{
const { bg, surfaces } = load();
const main = render(bg, { palette, cover: { src: COVER } });
assert.equal(surfaces.length, 1);
const off = surfaces[0].log;
assert.equal(main.images.length, 1);
assert.equal(main.images[0].img, surfaces[0]);
assert.equal(off.images.length, 1);
const drawn = off.images[0];
assert.match(drawn.filter, /^blur\(76\.80px\) saturate\(1\.6\) brightness\(0\.75\)$/);
// inset -20% + cover:方形封面按 1.4 倍宽铺满,居中
assert.equal(drawn.w, 1280 * 1.4);
assert.equal(drawn.h, 1280 * 1.4);
assert.equal(drawn.x, (1280 - 1792) / 2);
assert.equal(off.filters.at(-1), 'none');
assert.equal(off.glows.length, 2);
assert.deepEqual(off.glows.map(g => [g.x, g.y]), [[256, 240], [1088, 640]]);
assert.match(off.glows[0].color, /, 0\.45\)$/);
assert.match(off.glows[1].color, /, 0\.55\)$/);
assert.equal(off.fills.at(-1), 'rgba(0, 0, 0, 0.18)');

// 状态不变时复用缓存,不再重复模糊
render(bg, { palette, cover: { src: COVER } });
assert.equal(surfaces.length, 1);
// 换封面或换主题才重建
render(bg, { palette, cover: { src: COVER + 'B' } });
assert.equal(surfaces.length, 2);
const light = render(bg, { palette, cover: { src: COVER + 'B' } }, 'light');
assert.equal(surfaces.length, 3);
assert.equal(surfaces[2].log.fills.at(-1), 'rgba(255, 255, 255, 0.24)');
assert.ok(light.images.length === 1);
}

// 没有封面或封面不是 data 图片:只画底色、光晕与遮罩
{
const { bg, surfaces } = load();
render(bg, { palette, cover: null });
assert.equal(surfaces[0].log.images.length, 0);
assert.equal(surfaces[0].log.glows.length, 2);
render(bg, { palette, cover: { src: 'https://example.com/a.jpg' } });
assert.equal(surfaces.at(-1).log.images.length, 0);
}

// 灰色封面光晕退到低饱和;无调色板时用 coverBase / coverAccent
{
const { bg, surfaces } = load();
render(bg, { palette: ['#777777'] });
assert.match(surfaces[0].log.glows[0].color, /^hsla\(0, 5\.2%, 61%/);
render(bg, { colors: { coverBase: '#803020', coverAccent: '#2050A0' } });
assert.match(surfaces.at(-1).log.glows[0].color, /^hsla\(10/);
assert.match(surfaces.at(-1).log.glows[1].color, /^hsla\(217\.5/);
}

// 运行环境缺少 document 或 Image 时直接画到主画布,不崩溃
{
const { bg } = load({ withImage: false, withDocument: false });
const log = render(bg, { palette, cover: { src: COVER } });
assert.equal(log.images.length, 0);
assert.equal(log.glows.length, 2);
assert.equal(log.fills.at(-1), 'rgba(0, 0, 0, 0.18)');
}

// 尺寸无效时不绘制
{
const { bg, surfaces } = load();
const { ctx, log } = recorder();
bg.render(ctx, { width: 0, height: 800, theme: 'dark' }, 0);
bg.render(ctx, { width: 800, height: Number.NaN, theme: 'dark' }, 0);
assert.equal(log.fills.length + log.images.length, 0);
assert.equal(surfaces.length, 0);
}

console.log('设计稿复刻背景:模糊参数、铺满方式、光晕、缓存与降级检查通过。');
Loading
Loading