Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve system appearance mode when installing themes https://github.com/siyuan-note/siyuan/issues/16990

Danield9b2fefec205
diff --git a/app/src/config/bazaar.ts b/app/src/config/bazaar.ts
index 341e9720ba..5479c657b0 100644
--- a/app/src/config/bazaar.ts
+++ b/app/src/config/bazaar.ts
@@ -1099,6 +1099,23 @@ type="checkbox">
loadPlugin(app, response.data).then(callback);
});
},
+ _resolveThemeAppearanceMode(item: IBazaarItem) {
+ const appearance = window.siyuan.config.appearance;
+ const modes = item.modes || [];
+ const currentMode = appearance.mode;
+ const supportsCurrentMode = modes.length === 0 ||
+ modes.includes(currentMode === 0 ? "light" : "dark");
+ if (supportsCurrentMode) {
+ return {
+ mode: currentMode,
+ modeOS: appearance.modeOS,
+ };
+ }
+ return {
+ mode: modes.includes("dark") ? 1 : 0,
+ modeOS: false,
+ };
+ },
_setAppearancePackage(bazaarType: "themes" | "icons", item: IBazaarItem, enabled: boolean, callback: () => void) {
const appearance = {...window.siyuan.config.appearance};
if (bazaarType === "icons") {
@@ -1113,11 +1130,9 @@ type="checkbox">
if (supportsDark) {
appearance.themeDark = item.name;
}
- const supportsCurrentMode = appearance.mode === 0 ? supportsLight : supportsDark;
- if (!supportsCurrentMode) {
- appearance.mode = supportsDark ? 1 : 0;
- appearance.modeOS = false;
- }
+ const themeAppearanceMode = bazaar._resolveThemeAppearanceMode(item);
+ appearance.mode = themeAppearanceMode.mode;
+ appearance.modeOS = themeAppearanceMode.modeOS;
} else {
if (appearance.themeLight === item.name) {
appearance.themeLight = "daylight";
@@ -1327,12 +1342,14 @@ type="checkbox">
templates: "/api/bazaar/installBazaarTemplate",
widgets: "/api/bazaar/installBazaarWidget",
};
+ const themeAppearanceMode = pkgType === "themes" ?
+ bazaar._resolveThemeAppearanceMode(installItem) : {};
fetchPost(installAPI[pkgType], {
keyword: (bazaar.element.querySelector(`.config-bazaar__panel[data-type="${bazaar._type2tabType(pkgType)}"] .b3-text-field`) as HTMLInputElement).value,
repoURL: installItem.repoURL,
packageName: installItem.name,
repoHash: installItem.repoHash,
- mode: installItem.modes?.toString() === "dark" ? 1 : 0,
+ ...themeAppearanceMode,
frontend: getFrontend()
}, response => {
if (response.code !== 0) {
diff --git a/kernel/api/bazaar.go b/kernel/api/bazaar.go
index 2be52b4c61..2ed3970ca9 100644
--- a/kernel/api/bazaar.go
+++ b/kernel/api/bazaar.go
@@ -267,7 +267,7 @@ func installBazaarPlugin(c *gin.Context) {
) {
return
}
- err := model.InstallBazaarPackage("plugins", repoURL, repoHash, packageName, 0)
+ err := model.InstallBazaarPackage("plugins", repoURL, repoHash, packageName, nil)
if err != nil {
ret.Code = 1
ret.Msg = err.Error()
@@ -373,7 +373,7 @@ func installBazaarWidget(c *gin.Context) {
) {
return
}
- err := model.InstallBazaarPackage("widgets", repoURL, repoHash, packageName, 0)
+ err := model.InstallBazaarPackage("widgets", repoURL, repoHash, packageName, nil)
if err != nil {
ret.Code = 1
ret.Msg = err.Error()
@@ -470,7 +470,7 @@ func installBazaarIcon(c *gin.Context) {
) {
return
}
- err := model.InstallBazaarPackage("icons", repoURL, repoHash, packageName, 0)
+ err := model.InstallBazaarPackage("icons", repoURL, repoHash, packageName, nil)
if err != nil {
ret.Code = 1
ret.Msg = err.Error()
@@ -569,7 +569,7 @@ func installBazaarTemplate(c *gin.Context) {
) {
return
}
- err := model.InstallBazaarPackage("templates", repoURL, repoHash, packageName, 0)
+ err := model.InstallBazaarPackage("templates", repoURL, repoHash, packageName, nil)
if err != nil {
ret.Code = 1
ret.Msg = err.Error()
@@ -659,28 +659,48 @@ func installBazaarTheme(c *gin.Context) {
}
var keyword, repoURL, repoHash, packageName string
- var mode float64
if !util.ParseJsonArgs(arg, ret,
util.BindJsonArg("keyword", &keyword, false, false),
util.BindJsonArg("repoURL", &repoURL, true, true),
util.BindJsonArg("repoHash", &repoHash, true, true),
util.BindJsonArg("packageName", &packageName, true, true),
- util.BindJsonArg("mode", &mode, true, false),
) {
return
}
- err := model.InstallBazaarPackage("themes", repoURL, repoHash, packageName, int(mode))
+
+ _, hasMode := arg["mode"]
+ _, hasModeOS := arg["modeOS"]
+ if hasMode != hasModeOS {
+ ret.Code = -1
+ ret.Msg = "Fields [mode] and [modeOS] must be provided together"
+ return
+ }
+
+ var themeOptions *model.ThemeInstallOptions
+ if hasMode {
+ var mode float64
+ var modeOS bool
+ if !util.ParseJsonArgs(arg, ret,
+ util.BindJsonArg("mode", &mode, true, false),
+ util.BindJsonArg("modeOS", &modeOS, true, false),
+ ) {
+ return
+ }
+ if 0 != mode && 1 != mode {
+ ret.Code = -1
+ ret.Msg = "Field [mode] must be 0 or 1"
+ return
+ }
+ themeOptions = &model.ThemeInstallOptions{Mode: int(mode), ModeOS: modeOS}
+ }
+
+ err := model.InstallBazaarPackage("themes", repoURL, repoHash, packageName, themeOptions)
if err != nil {
ret.Code = 1
ret.Msg = err.Error()
return
}
- // TODO 安装新主题之后,不应该始终取消外观模式“跟随系统” https://github.com/siyuan-note/siyuan/issues/16990
- // 安装集市主题后不跟随系统切换外观模式
- model.Conf.Appearance.ModeOS = false
- model.Conf.Save()
-
util.PushMsg(model.Conf.Language(69), 3000)
ret.Data = map[string]any{
"packages": model.GetBazaarPackages("themes", "", keyword),
diff --git a/kernel/model/bazaar.go b/kernel/model/bazaar.go
index 775dc3d05c..140e7470e9 100644
--- a/kernel/model/bazaar.go
+++ b/kernel/model/bazaar.go
@@ -74,6 +74,12 @@ type batchInstallItem struct {
meta installMeta
}
+// ThemeInstallOptions 描述新安装主题后需要应用的外观模式
+type ThemeInstallOptions struct {
+ Mode int
+ ModeOS bool
+}
+
// updatePackages 更新一组集市包;同类型批量更新时,安装后处理只执行一次
func updatePackages(packages []*UpdatedPackage, pkgType string, successCount *int, planned int) {
items := make([]batchInstallItem, 0, len(packages))
@@ -89,7 +95,7 @@ func updatePackages(packages []*UpdatedPackage, pkgType string, successCount *in
*successCount++
util.PushEndlessProgress(fmt.Sprintf(Conf.language(236), *successCount, planned, pkg.Name))
}
- finishInstall(pkgType, items, 0)
+ finishInstall(pkgType, items, nil)
}
// filterUpdatableBazaarPackages 过滤出允许更新的集市包
@@ -418,8 +424,8 @@ func installBazaarPackage(pkgType, repoURL, repoHash, packageName string) (meta
// finishInstall 集市包安装后的处理(刷新外观、推送插件重载等);批量更新时同类型只执行一次
//
-// - themeMode:0 浅色 / 1 深色,仅在新安装主题(meta.update 为 false)时写入外观;批量覆盖更新不会用到
-func finishInstall(pkgType string, items []batchInstallItem, themeMode int) {
+// - themeOptions:仅在新安装主题(meta.update 为 false)时写入外观;批量覆盖更新不会用到
+func finishInstall(pkgType string, items []batchInstallItem, themeOptions *ThemeInstallOptions) {
if 1 > len(items) {
return
}
@@ -447,14 +453,31 @@ func finishInstall(pkgType string, items []batchInstallItem, themeMode int) {
}
case "themes":
for _, item := range items {
- if !item.meta.update {
+ if !item.meta.update && nil != themeOptions {
// 新安装主题时才自动切换 https://github.com/siyuan-note/siyuan/issues/4966
- if 0 == themeMode {
- Conf.Appearance.ThemeLight = item.name
- } else {
- Conf.Appearance.ThemeDark = item.name
+ applied := false
+ theme, err := bazaar.ParsePackageJSON(filepath.Join(util.ThemesPath, item.name, "theme.json"))
+ if nil == err && nil != theme && nil != theme.Modes {
+ for _, mode := range *theme.Modes {
+ switch mode {
+ case "light":
+ Conf.Appearance.ThemeLight = item.name
+ applied = true
+ case "dark":
+ Conf.Appearance.ThemeDark = item.name
+ applied = true
+ }
+ }
+ }
+ if !applied {
+ if 0 == themeOptions.Mode {
+ Conf.Appearance.ThemeLight = item.name
+ } else {
+ Conf.Appearance.ThemeDark = item.name
+ }
}
- Conf.Appearance.Mode = themeMode
+ Conf.Appearance.Mode = themeOptions.Mode
+ Conf.Appearance.ModeOS = themeOptions.ModeOS
Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, item.name, "theme.js"))
Conf.Save()
}
@@ -475,13 +498,13 @@ func finishInstall(pkgType string, items []batchInstallItem, themeMode int) {
}
}
-// InstallBazaarPackage 安装集市包,themeMode 仅在 pkgType 为 "themes" 时生效
-func InstallBazaarPackage(pkgType, repoURL, repoHash, packageName string, themeMode int) error {
+// InstallBazaarPackage 安装集市包,themeOptions 仅在 pkgType 为 "themes" 时生效
+func InstallBazaarPackage(pkgType, repoURL, repoHash, packageName string, themeOptions *ThemeInstallOptions) error {
meta, err := installBazaarPackage(pkgType, repoURL, repoHash, packageName)
if err != nil {
return err
}
- finishInstall(pkgType, []batchInstallItem{{name: packageName, meta: meta}}, themeMode)
+ finishInstall(pkgType, []batchInstallItem{{name: packageName, meta: meta}}, themeOptions)
return nil
}
@@ -501,7 +524,7 @@ func UpdateBazaarPackage(pkgType, packageName, frontend string) error {
if updated.Available.DisallowUpdate {
return errors.New("marketplace package update is not allowed")
}
- return InstallBazaarPackage(pkgType, updated.Available.RepoURL, updated.Available.RepoHash, packageName, 0)
+ return InstallBazaarPackage(pkgType, updated.Available.RepoURL, updated.Available.RepoHash, packageName, nil)
}
return errors.New("marketplace package update not found")
}