-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
173 lines (144 loc) · 3.89 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
// Debido a un bug, las funciones exportadas en app.go no se exportan correctamente al guardar.
// Hay que apagar wails.dev y volver a arrancar
import (
"context"
"encoding/json"
"fmt"
// "io/ioutil"
"os"
"path/filepath"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
/*
Notas de este archivo por nxito
LAs funciones con (a *App) serán exportadas como un export en js
notense las estructuras de datos de go : strings , mapas ( []map[string]interface{} ), ctx contexto...
Una cosa muy importante es el runtime de WAils para tareas de escritorio que js tiene vetado por defecto, como OpenDirectoryDialog. js no permite seleccionar carpetas pero wails en go si
Hay funciones exportadas al main js que se pueden llamar directamente, otras con windows.runtime y otras window.go.main.App
*/
// App struct
type App struct {
ctx context.Context
}
func NewApp() *App {
return &App{}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// func (a *App) shutdown(ctx context.Context) {
// }
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s!", name)
}
// Greet returns a greeting for the given name
func (a *App) ReadDirectory(dir string) (string, error) {
var fileInfos []map[string]interface{}
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
fileInfo := map[string]interface{}{
"Name": info.Name(),
"Size": info.Size(),
"Mode": info.Mode(),
"ModTime": info.ModTime().String(),
"IsDir": info.IsDir(),
"Path": filepath.Join(path),
}
fileInfos = append(fileInfos, fileInfo)
return nil
})
if err != nil {
return "nil", err
}
// return fileInfos, nil
jsonData, err := json.MarshalIndent(fileInfos, "", " ")
if err != nil {
return "nil", err
}
return fmt.Sprintln(string(jsonData)), nil
}
func (a *App) Confirm() {
selection, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.QuestionDialog,
Title: "CONFIRM ?",
Message: "Ejemplo de confirm con Wails, Funciona?",
})
if err != nil {
fmt.Println(err)
}
fmt.Println(selection)
}
// CRUD struct
type CRUD struct {
// ctx context.Context
}
func NewCRUD() *CRUD {
return &CRUD{}
}
func (a *CRUD) Create_folder(dir string) string {
// // Crear una carpeta
// err := os.Mkdir(dir, 0755)
// if err != nil {
// fmt.Println("Error al crear la carpeta:", err)
// return ""
// }
// fmt.Println("Carpeta creada correctamente.")
// Crear una carpeta y sus subcarpetas recursivamente
rutaCarpeta := dir
err := os.MkdirAll(rutaCarpeta, 0755)
if err != nil {
fmt.Println("Error al crear la carpeta:", err)
return " "
}
fmt.Println("Carpeta y subcarpetas creadas correctamente.")
return dir
}
func (a *CRUD) Read_folder(n int, m int) int {
return n + m
}
func (a *CRUD) OverWrite_folder(n int, m int) int {
return n + m
}
func (a *CRUD) Delete_folder(n int, m int) int {
return n + m
}
func (a *CRUD) Create_file(n int, m int) int {
return n + m
}
func (a *CRUD) Read_file(n int, m int) int {
return n + m
}
func (a *CRUD) OverWrite_file(n int, m int) int {
return n + m
}
func (a *CRUD) Delete_file(n int, m int) int {
return n + m
}
func (a *App) OpenDirectory() string {
selection, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
DefaultDirectory: ".\\",
Title: "Selecciona carpeta",
//LosFiltros solo valdrian si se seleccionan archivos
// Filters: []runtime.FileFilter{
// {
// DisplayName: "Images (*.png;*.jpg)",
// Pattern: "*.png;*.jpg",
// }, {
// DisplayName: "Videos (*.mov;*.mp4)",
// Pattern: "*.mov;*.mp4",
// },
// },
// Filters: "Are you sure you want to delete these records?",
CanCreateDirectories: true,
// ShowHiddenFiles: true,
})
if err != nil {
fmt.Println(err)
}
fmt.Println(selection)
runtime.EventsEmit(a.ctx, "directorySelected", selection)
return selection
}