mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-30 16:20:33 +08:00
1 parent
385d7aa531
commit
8d435fe109
11 files changed
+648
-188
No files matched your search
@@ -0,0 +1,130 @@
|
||||
// Copyright 2025-2026 肖其顿 (XIAO QI DUN)
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build !js || !wasm
|
||||
|
||||
package webview
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/xiaoqidun/guigo"
|
||||
webuiassets "github.com/xiaoqidun/ofdgo/assets/webui"
|
||||
)
|
||||
|
||||
// applicationTheme 保存应用主题偏好及最后一次解析的系统外观
|
||||
type applicationTheme struct {
|
||||
Mode string `json:"mode"`
|
||||
Dark bool `json:"dark"`
|
||||
}
|
||||
|
||||
// themeColors 返回应用对应外观的原生标题栏配色
|
||||
// 入参: dark 是否使用深色
|
||||
// 返回: guigo.TitleBarColors 标题栏配色
|
||||
func themeColors(dark bool) guigo.TitleBarColors {
|
||||
if dark {
|
||||
return guigo.TitleBarColors{Background: "#20272b", Foreground: "#e0e7eb", InactiveBackground: "#242b30", InactiveForeground: "#9aa8b1"}
|
||||
}
|
||||
return guigo.TitleBarColors{Background: "#e8edee", Foreground: "#263238", InactiveBackground: "#eef1f3", InactiveForeground: "#728087"}
|
||||
}
|
||||
|
||||
// loadTheme 在创建窗口前读取用户目录中的主题偏好
|
||||
// 返回: error 读取或解析错误
|
||||
func (h *Host) loadTheme() error {
|
||||
h.theme = applicationTheme{Mode: "system"}
|
||||
data, err := os.ReadFile(h.themePath)
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.Unmarshal(data, &h.theme); err != nil {
|
||||
return err
|
||||
}
|
||||
return h.theme.validate()
|
||||
}
|
||||
|
||||
// validate 检查应用主题及解析后的配色是否一致
|
||||
// 返回: error 偏好错误
|
||||
func (a applicationTheme) validate() error {
|
||||
if a.Mode != "system" && a.Mode != "light" && a.Mode != "dark" {
|
||||
return fmt.Errorf("主题无效")
|
||||
}
|
||||
if a.Mode == "light" && a.Dark || a.Mode == "dark" && !a.Dark {
|
||||
return fmt.Errorf("主题配色无效")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// themeIndex 在页面首次绘制前提供当前主题偏好
|
||||
// 返回: []byte 页面内容, error 资源读取错误
|
||||
func (h *Host) themeIndex() ([]byte, error) {
|
||||
data, err := webuiassets.NativeFS.ReadFile("index.html")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h.themeMu.Lock()
|
||||
mode := h.theme.Mode
|
||||
h.themeMu.Unlock()
|
||||
if mode == "" {
|
||||
mode = "system"
|
||||
}
|
||||
return bytes.Replace(data, []byte(`<html lang="zh-CN">`), []byte(`<html lang="zh-CN" data-theme-mode="`+mode+`">`), 1), nil
|
||||
}
|
||||
|
||||
// setTheme 串行保存应用偏好并同步当前窗口配色
|
||||
// 入参: value 已解析的主题偏好
|
||||
// 返回: error 校验、持久化或窗口更新错误
|
||||
func (h *Host) setTheme(value applicationTheme) error {
|
||||
if err := value.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
h.themeMu.Lock()
|
||||
defer h.themeMu.Unlock()
|
||||
h.mu.Lock()
|
||||
window := h.window
|
||||
h.mu.Unlock()
|
||||
if window == nil {
|
||||
return fmt.Errorf("窗口尚未就绪")
|
||||
}
|
||||
if h.theme != value {
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.CreateTemp(filepath.Dir(h.themePath), "appearance-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
_, writeErr := file.Write(data)
|
||||
closeErr := file.Close()
|
||||
if writeErr != nil {
|
||||
return writeErr
|
||||
}
|
||||
if closeErr != nil {
|
||||
return closeErr
|
||||
}
|
||||
if err := os.Rename(file.Name(), h.themePath); err != nil {
|
||||
return err
|
||||
}
|
||||
h.theme = value
|
||||
}
|
||||
return window.SetTitleBarColors(themeColors(value.Dark))
|
||||
}
|
||||
Reference in new issue
Block a user