-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
150 lines (118 loc) · 3.15 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"context"
"github.com/jsawo/renfield/config"
"github.com/jsawo/renfield/editor"
"github.com/jsawo/renfield/project"
"github.com/samber/lo"
"github.com/wailsapp/wails/v2/pkg/runtime"
"golang.design/x/clipboard"
)
type App struct {
ctx context.Context
config config.Config
}
func NewApp() *App {
err := clipboard.Init()
if err != nil {
panic(err)
}
return &App{
config: config.AppConfig,
}
}
func (a *App) OpenDirectoryDialog() string {
result, _ := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{})
return result
}
func (a *App) CopyToClipboard(data string) {
clipboard.Write(clipboard.FmtText, []byte(data))
}
func (a *App) GetConfig() config.Config {
return a.config
}
func (a *App) SetCurrentProject(projectId string) {
a.config.Currentproject = projectId
a.saveConfig()
}
func (a *App) RemoveProject(projectId string) {
projectCount := len(a.config.Projects)
if projectCount == 1 {
return
}
delete(a.config.Projects, projectId)
for k := range a.config.Projects {
a.config.Currentproject = k
break
}
a.saveConfig()
}
func (a *App) CreateProject() string {
id := project.GetNewId()
newProject := config.ProjectConfig{
Id: id,
Name: "Unnamed",
Tag: "local",
Command: config.DefaultCommand,
}
a.config.Projects[id] = newProject
a.config.Currentproject = id
a.saveConfig()
return id
}
func (a *App) SetActiveJSONTool(toolName string) {
currentProject := config.GetCurrentProject()
currentProject.JSONTools.ActiveTool = toolName
a.config.Projects[currentProject.Id] = currentProject
a.saveConfig()
}
func (a *App) NewTab(module string) {
newTab := editor.Tab{
Id: project.GetNewId(),
Name: "Untitled",
}
currentProject := config.GetCurrentProject()
currentProject.Tinker.Tabs = append(currentProject.Tinker.Tabs, newTab)
currentProject.Tinker.ActiveTab = newTab.Id
a.config.Projects[currentProject.Id] = currentProject
a.saveConfig()
}
func (a *App) CloseTab(module, tabId string) {
currentProject := config.GetCurrentProject()
if len(currentProject.Tinker.Tabs) == 1 {
return
}
currentProject.Tinker.Tabs = lo.Filter(currentProject.Tinker.Tabs, func(tab editor.Tab, index int) bool {
return tab.Id != tabId
})
if currentProject.Tinker.ActiveTab == tabId {
currentProject.Tinker.ActiveTab = currentProject.Tinker.Tabs[0].Id
}
config.UpdateProject(currentProject)
a.saveConfig()
}
func (a *App) SetActiveTab(module, tabId string) {
currentProject := config.GetCurrentProject()
currentProject.Tinker.ActiveTab = tabId
config.UpdateProject(currentProject)
a.saveConfig()
}
func (a *App) RenameTab(module, tabId, newName string) {
currentProject := config.GetCurrentProject()
currentProject.Tinker.Tabs = lo.Map(currentProject.Tinker.Tabs, func(tab editor.Tab, index int) editor.Tab {
if tab.Id == tabId {
tab.Name = newName
}
return tab
})
config.UpdateProject(currentProject)
a.saveConfig()
}
func (a *App) UpdateProjectSettings(projectId string, settings config.ProjectConfig) {
a.config.Projects[projectId] = settings
a.saveConfig()
}
func (a *App) saveConfig() {
runtime.EventsEmit(a.ctx, "configUpdated", a.config)
config.Save(a.config)
}