mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 08:46:55 +08:00
This commit is contained in:
1 parent
eac5a659e6
commit
6b0332224f
10 files changed
+203
-22
No files matched your search
@@ -92,7 +92,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", "child", "remove", "write.begin", "write.data", "write.commit", "write.abort", "fonts", "ready", "title"} {
|
||||
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", "title"} {
|
||||
bindings["ofdgo.host."+name] = func(ctx context.Context, args any) (any, error) { return h.call(ctx, name, args) }
|
||||
}
|
||||
static := http.FileServerFS(webuiassets.NativeFS)
|
||||
@@ -248,17 +248,22 @@ func (h *Host) emit(name string, value any) {
|
||||
func (h *Host) openPaths(names []string) {
|
||||
files := make([]any, 0, len(names))
|
||||
for _, name := range names {
|
||||
handle, err := h.files.add(name, false)
|
||||
file, err := h.files.addDrop(name)
|
||||
if err != nil {
|
||||
h.emit("ofdgo.error", err.Error())
|
||||
continue
|
||||
}
|
||||
file, err := h.files.file(handle["nativeHandle"].(string))
|
||||
if err == nil {
|
||||
files = append(files, file)
|
||||
}
|
||||
files = append(files, file)
|
||||
}
|
||||
h.mu.Lock()
|
||||
w := h.window
|
||||
h.mu.Unlock()
|
||||
if w != nil && w.Emit("ofdgo.files", files) == nil {
|
||||
return
|
||||
}
|
||||
for _, file := range files {
|
||||
h.files.discardDrop(file.(map[string]any)["nativeFile"].(string))
|
||||
}
|
||||
h.emit("ofdgo.files", files)
|
||||
}
|
||||
|
||||
// call 执行宿主协议并保留业务结果结构
|
||||
@@ -455,6 +460,9 @@ func (h *Host) call(ctx context.Context, name string, value any) (any, error) {
|
||||
case "release":
|
||||
h.files.release(text("file"))
|
||||
return nil, nil
|
||||
case "drop.release":
|
||||
h.files.discardDrop(text("file"))
|
||||
return nil, nil
|
||||
case "child":
|
||||
value, err := h.files.child(text("handle"), text("name"), flag("directory"), flag("create"))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
@@ -819,7 +827,11 @@ func windowOptions(dataDir string) (guigo.Options, error) {
|
||||
if err != nil {
|
||||
return guigo.Options{}, err
|
||||
}
|
||||
dataDir = filepath.Join(cache, "OFDGo")
|
||||
name := "ofdgo"
|
||||
if runtime.GOOS == "windows" {
|
||||
name = "OFDGo"
|
||||
}
|
||||
dataDir = filepath.Join(cache, name)
|
||||
}
|
||||
dataDir, err := filepath.Abs(dataDir)
|
||||
if err != nil {
|
||||
|
||||
@@ -47,6 +47,7 @@ type fileEntry struct {
|
||||
saveTarget ofdgo.Output
|
||||
readers int
|
||||
retired bool
|
||||
dropHandle string
|
||||
}
|
||||
|
||||
// fileStore 管理窗口范围内的文件授权和临时数据
|
||||
@@ -137,6 +138,59 @@ func (s *fileStore) add(name string, directory bool) (map[string]any, error) {
|
||||
return s.descriptor(id), nil
|
||||
}
|
||||
|
||||
// addDrop 登记拖放文件及读取快照,失败时撤销本次授权
|
||||
// 入参: name 拖放文件路径
|
||||
// 返回: map[string]any 文件描述, error 登记错误
|
||||
func (s *fileStore) addDrop(name string) (map[string]any, error) {
|
||||
handle, err := s.add(name, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id := handle["nativeHandle"].(string)
|
||||
file, err := s.file(id)
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if err == nil && !s.closed {
|
||||
if entry := s.entries[file["nativeFile"].(string)]; entry != nil && s.entries[id] != nil {
|
||||
entry.dropHandle = id
|
||||
file["drop"] = true
|
||||
return file, nil
|
||||
}
|
||||
err = os.ErrNotExist
|
||||
}
|
||||
delete(s.entries, id)
|
||||
if file != nil {
|
||||
key := file["nativeFile"].(string)
|
||||
if entry := s.entries[key]; entry != nil {
|
||||
delete(s.entries, key)
|
||||
if entry.file != nil {
|
||||
entry.file.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
err = os.ErrClosed
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// discardDrop 撤销未接收的拖放文件及其授权,不删除源文件
|
||||
// 入参: id 拖放读取快照标识
|
||||
func (s *fileStore) discardDrop(id string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
entry := s.entries[id]
|
||||
if entry == nil || entry.dropHandle == "" {
|
||||
return
|
||||
}
|
||||
delete(s.entries, entry.dropHandle)
|
||||
delete(s.entries, id)
|
||||
entry.retired = true
|
||||
if entry.file != nil && entry.readers == 0 {
|
||||
entry.file.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// save 登记用户选择的输出路径
|
||||
// 入参: name 输出文件路径
|
||||
// 返回: map[string]any 句柄描述
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
// 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.
|
||||
|
||||
package webview
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// conversionMemory 未提供系统内存探测时仅按并行数调度
|
||||
var conversionMemory func() int64
|
||||
|
||||
// fontNameLanguage 获取Linux区域设置对应的OpenType名称表标识
|
||||
// 返回: uint16 名称表平台标识, uint16 显示语言标识
|
||||
func fontNameLanguage() (uint16, uint16) {
|
||||
locale := ""
|
||||
for _, name := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} {
|
||||
if locale = os.Getenv(name); locale != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
locale = strings.ToLower(strings.ReplaceAll(locale, "-", "_"))
|
||||
locale, _, _ = strings.Cut(locale, ".")
|
||||
locale, _, _ = strings.Cut(locale, "@")
|
||||
switch locale {
|
||||
case "zh", "zh_cn", "zh_hans":
|
||||
return 3, 0x0804
|
||||
case "zh_hk":
|
||||
return 3, 0x0c04
|
||||
case "zh_mo":
|
||||
return 3, 0x1404
|
||||
case "zh_tw", "zh_hant":
|
||||
return 3, 0x0404
|
||||
case "zh_sg":
|
||||
return 3, 0x1004
|
||||
case "ja", "ja_jp":
|
||||
return 3, 0x0411
|
||||
case "ko", "ko_kr":
|
||||
return 3, 0x0412
|
||||
case "c", "posix", "en", "en_us":
|
||||
return 3, 0x0409
|
||||
}
|
||||
return 0xffff, 0xffff
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build !windows
|
||||
//go:build !windows && !linux
|
||||
|
||||
package webview
|
||||
|
||||
|
||||
Reference in new issue
Block a user