-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
100 lines (84 loc) · 2.42 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
osruntime "runtime"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
httpServer *http.Server
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// Greet returns a greeting for the given name
// func (a *App) Greet(name string) string {
// return fmt.Sprintf("Hello %s, It's show time! ", name)
// }
// SelectedDirectory 选择目录
func (a *App) SelectedDirectory() (string, error) {
dialog, _ := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{})
if dialog == "" {
return "", nil // 用户取消了对话框
}
return dialog, nil
}
// 设置草稿根目录
func (a *App) SetDraftRootPath(draftRootPath string) (string, error) {
config.DraftRootPath = draftRootPath
err := configStore.SaveConfig(config)
if err != nil {
return "", err
}
return draftRootPath, nil
}
// 自动检测草稿目录
func (a *App) AutoDetectDraftRootPath() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("无法获取用户主目录: %w", err)
}
var draftPath string
switch osruntime.GOOS {
case "darwin":
draftPath = filepath.Join(homeDir, "Movies", "JianyingPro", "User Data", "Projects", "com.lveditor.draft")
case "windows":
draftPath = filepath.Join(homeDir, "AppData", "Local", "JianyingPro", "User Data", "Projects", "com.lveditor.draft")
default:
return "", fmt.Errorf("不支持的操作系统: %s", osruntime.GOOS)
}
// 检查目录是否存在,并且包含 root_meta_info.json 文件
if _, err := os.Stat(draftPath); os.IsNotExist(err) {
return "", fmt.Errorf("草稿目录不存在: %s", draftPath)
}
rootMetaInfoPath := filepath.Join(draftPath, "root_meta_info.json")
if _, err := os.Stat(rootMetaInfoPath); os.IsNotExist(err) {
return "", fmt.Errorf("草稿目录无效,缺少 root_meta_info.json 文件: %s", draftPath)
}
return draftPath, nil
}
func (a *App) SendLogsToPage(message string) {
// json 格式的日志
log := Log{
Type: "log",
Message: message,
}
jsonData, err := json.Marshal(log)
if err != nil {
fmt.Println("Error:", err)
return
}
runtime.EventsEmit(a.ctx, "logs", string(jsonData))
}