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
+42
-20
@@ -48,6 +48,9 @@ const applicationDownloadURL = "https://ofdgo.aite.me/releases/"
|
||||
|
||||
// Host 连接桌面系统能力和共用文档业务
|
||||
type Host struct {
|
||||
themeMu sync.Mutex
|
||||
theme applicationTheme
|
||||
themePath string
|
||||
mu sync.Mutex
|
||||
window *guigo.Window
|
||||
files *fileStore
|
||||
@@ -104,7 +107,11 @@ func Run(ctx context.Context, paths []string, dataDir string) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
h := &Host{pendingPaths: paths}
|
||||
h := &Host{pendingPaths: paths, themePath: filepath.Join(options.UserDataDir, "appearance.json")}
|
||||
if err := h.loadTheme(); err != nil {
|
||||
return err
|
||||
}
|
||||
options.TitleBarColors = themeColors(h.theme.Dark)
|
||||
data, err := json.Marshal(paths)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -142,7 +149,7 @@ func Run(ctx context.Context, paths []string, dataDir string) error {
|
||||
h.conversionPool = h.resources.ConversionPool()
|
||||
defer h.close()
|
||||
bindings := make(map[string]guigo.HandlerFunc)
|
||||
for _, name := range []string{"engine.open", "engine.close", "engine.call", "engine.start", "engine.cancel", "engine.finish", "batch", "open", "save", "directory", "file", "release", "drop.release", "child", "remove", "write.begin", "write.data", "write.commit", "write.abort", "fonts", "ready", "launch.done", "title", "window.info", "window.drag", "window.close", "update.check", "update.open", "association.query", "association.register", "association.remove", "print.capabilities", "print.dialog", "print.printers", "print.query", "print.properties", "print"} {
|
||||
for _, name := range []string{"engine.open", "engine.close", "engine.call", "engine.start", "engine.cancel", "engine.finish", "batch", "open", "save", "directory", "file", "release", "drop.release", "child", "remove", "write.begin", "write.data", "write.commit", "write.abort", "fonts", "ready", "launch.done", "title", "window.info", "window.drag", "window.close", "appearance", "update.check", "update.open", "association.query", "association.register", "association.remove", "print.capabilities", "print.dialog", "print.printers", "print.query", "print.properties", "print"} {
|
||||
bindings["ofdgo.host."+name] = func(ctx context.Context, args any) (any, error) { return h.call(ctx, name, args) }
|
||||
}
|
||||
static := http.FileServerFS(webuiassets.NativeFS)
|
||||
@@ -155,6 +162,18 @@ func Run(ctx context.Context, paths []string, dataDir string) error {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/" || r.URL.Path == "/index.html" {
|
||||
data, err := h.themeIndex()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("X-OFDGo-Build-Version", build.Version)
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Write(data)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Header().Set("X-OFDGo-Build-Version", build.Version)
|
||||
static.ServeHTTP(w, r)
|
||||
@@ -237,7 +256,13 @@ func (h *Host) resource(r *http.Request) (*guigo.Resource, error) {
|
||||
if name == "" {
|
||||
name = "index.html"
|
||||
}
|
||||
data, err := webuiassets.NativeFS.ReadFile(name)
|
||||
var data []byte
|
||||
var err error
|
||||
if name == "index.html" {
|
||||
data, err = h.themeIndex()
|
||||
} else {
|
||||
data, err = webuiassets.NativeFS.ReadFile(name)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -411,6 +436,8 @@ func (h *Host) call(ctx context.Context, name string, value any) (any, error) {
|
||||
text := func(key string) string { value, _ := args[key].(string); return value }
|
||||
flag := func(key string) bool { value, _ := args[key].(bool); return value }
|
||||
switch name {
|
||||
case "appearance":
|
||||
return nil, h.setTheme(applicationTheme{Mode: text("mode"), Dark: flag("dark")})
|
||||
case "print.capabilities", "print.dialog", "print.printers", "print.query", "print.properties", "print":
|
||||
h.mu.Lock()
|
||||
window := h.window
|
||||
@@ -1186,22 +1213,17 @@ func windowOptions(dataDir string) (guigo.Options, error) {
|
||||
return guigo.Options{}, err
|
||||
}
|
||||
return guigo.Options{
|
||||
AppID: appID,
|
||||
Title: "OFDGo - OFD编辑器",
|
||||
TitleBar: guigo.TitleBarContent,
|
||||
TitleBarColors: guigo.TitleBarColors{
|
||||
Background: "#e8edf0",
|
||||
Foreground: "#1f2428",
|
||||
InactiveBackground: "#eef1f4",
|
||||
InactiveForeground: "#66737f",
|
||||
},
|
||||
Icon: icon,
|
||||
Width: 1440,
|
||||
Height: 900,
|
||||
MinWidth: 360,
|
||||
MinHeight: 240,
|
||||
Center: true,
|
||||
Maximized: true,
|
||||
UserDataDir: dataDir,
|
||||
AppID: appID,
|
||||
Title: "OFDGo - OFD编辑器",
|
||||
TitleBar: guigo.TitleBarContent,
|
||||
TitleBarColors: themeColors(false),
|
||||
Icon: icon,
|
||||
Width: 1440,
|
||||
Height: 900,
|
||||
MinWidth: 360,
|
||||
MinHeight: 240,
|
||||
Center: true,
|
||||
Maximized: true,
|
||||
UserDataDir: dataDir,
|
||||
}, nil
|
||||
}
|
||||
@@ -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