fix(theme): 深色遮罩交给服务端,避免双重压暗

实测发现深色下 body 的 background-image 仍是亮色的 70% 白遮罩(壁纸被洗白)。
遮罩由服务端 applyWallpaperBlock 写进 conf.yml,因此在那里补一条深色规则
(同一张壁纸 + rgba(6,12,24,dim+0.15) 遮罩),并移除 navi.css 里那层
body::before 遮罩,避免壁纸被双重压暗。加单测断言深浅两条规则共存、
且移除壁纸时一并清除。
This commit is contained in:
HomeDash Bot
2026-09-12 07:49:34 +08:00
parent 1257164c3f
commit 064f986a7f
3 changed files with 121 additions and 0 deletions

View File

@ -78,6 +78,13 @@ func applyWallpaperBlock(css, url string, dim float64) string {
dim = 0.9
}
scrim := strconv.FormatFloat(dim, 'f', 2, 64)
// 深色下必须换一层深色遮罩:否则亮色的 70% 白遮罩会把壁纸洗白
// (实测:深色模式里 background-image 仍是 rgba(255,255,255,.7) 那层)。
// 用同一个 dim 作为深色遮罩强度,深浅两种模式观感一致。
dark := strconv.FormatFloat(dim+0.15, 'f', 2, 64)
if dim+0.15 > 0.9 {
dark = "0.90"
}
return css + "\n\n" + wallpaperBegin + "\n" +
"html[data-theme] body{\n" +
" background-image:\n" +
@ -87,6 +94,17 @@ func applyWallpaperBlock(css, url string, dim float64) string {
" background-position: center center !important;\n" +
" background-attachment: fixed !important;\n" +
" background-repeat: no-repeat !important;\n" +
"}\n" +
// 深色:同一张壁纸 + 深色遮罩(选择器特异度更高,与注入顺序无关)
"html.navi-dark body.navi-dark,\n" +
"html[data-theme].navi-dark body{\n" +
" background-image:\n" +
" linear-gradient(rgba(6,12,24," + dark + "), rgba(6,12,24," + dark + ")),\n" +
" url(\"" + url + "\") !important;\n" +
" background-size: cover !important;\n" +
" background-position: center center !important;\n" +
" background-attachment: fixed !important;\n" +
" background-repeat: no-repeat !important;\n" +
"}\n" + wallpaperEnd + "\n"
}

View File

@ -242,3 +242,22 @@ sections:
t.Fatalf("删除/改名后分组 = %v; want %v", names, want)
}
}
// 深色模式必须换一层深色遮罩,否则亮色遮罩会把壁纸洗白。
func TestApplyWallpaperBlockEmitsDarkScrim(t *testing.T) {
out := applyWallpaperBlock("", "/wallpapers/w.jpg", 0.5)
if !strings.Contains(out, "linear-gradient(rgba(255,255,255,0.50)") {
t.Fatalf("缺少亮色遮罩:\n%s", out)
}
if !strings.Contains(out, "html.navi-dark body.navi-dark") ||
!strings.Contains(out, "rgba(6,12,24,0.65)") {
t.Fatalf("缺少深色遮罩规则:\n%s", out)
}
if strings.Count(out, "/wallpapers/w.jpg") != 2 {
t.Fatalf("深浅两种模式都应引用同一张壁纸:\n%s", out)
}
// 移除壁纸时应把两种规则一起清掉
if got := applyWallpaperBlock(out, "", 0.5); strings.Contains(got, "navi-dark body") {
t.Fatalf("移除壁纸后仍残留深色规则:\n%s", got)
}
}