-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
145 lines (118 loc) · 3.29 KB
/
handlers.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
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"net/url"
)
type Handler struct{}
var internal = &Internal{}
var service = &Service{}
func (h *Handler) InputBoxChanged(t string) {
if t == "" {
outputBox.SetText("")
return
}
h.translateHandler()
}
func (h *Handler) translateHandler() {
s := internal.GetSettingsFile()
if s.ApiKey != "" {
if inputBox.Text != "" {
service.Translate(s.InputLanguage, s.OutputLanguage, s.ApiKey, inputBox.Text)
}
} else {
outputBox.SetText("Please enter your API Key in the settings.")
}
}
func (h *Handler) NewLanguageSelect(languages []string, defaultLang string, onChange func(string)) *widget.Select {
selectWidget := widget.NewSelect(languages, onChange)
selectWidget.SetSelected(defaultLang)
return selectWidget
}
func (h *Handler) InputLangChange(value string) {
internal.SyncSettingsFile(&Settings{
InputLanguage: value,
})
h.translateHandler()
}
func (h *Handler) OutputLangChange(value string) {
internal.SyncSettingsFile(&Settings{
OutputLanguage: value,
})
h.translateHandler()
}
func (h *Handler) SwitchButtonClick(inputLangSelect, outputLangSelect *widget.Select) {
inputLang := inputLangSelect.Selected
outputLang := outputLangSelect.Selected
inputLangSelect.SetSelected(outputLang)
outputLangSelect.SetSelected(inputLang)
internal.SyncSettingsFile(&Settings{
InputLanguage: outputLang,
OutputLanguage: inputLang,
})
h.translateHandler()
}
func (h *Handler) ClearInputBox() {
inputBox.SetText("")
}
func (h *Handler) CopyOutputToClipboard() {
w.Clipboard().SetContent(outputBox.Text)
}
func (h *Handler) SettingsButtonClick() {
settingsWindow := a.NewWindow(fmt.Sprintf("%s - %s", appName, "Settings"))
settingsWindow.Resize(fyne.NewSize(600, 200))
settingsWindow.CenterOnScreen()
apiKeyEntry := widget.NewEntry()
apiKeyEntry.SetPlaceHolder("AIzaSyBMqGQu_lWIj6dG__yzxzgN3S9yB1Zhgmo")
apiKeyEntry.Text = internal.GetSettingsFile().ApiKey
inputDelayEntry := widget.NewEntry()
inputDelayEntry.SetPlaceHolder("300")
inputDelayEntry.Text = internal.GetSettingsFile().InputDelay
settingsFileEntry := widget.NewEntry()
settingsFileEntry.Text = internal.GetDataPath() + "/settings.json"
getApiKeyLink, _ := url.Parse("https://aistudio.google.com/app/apikey")
contributeLink, _ := url.Parse("https://github.com/sercanarga/franslate")
form := &widget.Form{
Items: []*widget.FormItem{
{
Text: "API Key:",
Widget: apiKeyEntry,
HintText: "Enter your Gemini API Key",
},
{
Text: "Input Delay(ms):",
Widget: inputDelayEntry,
HintText: "Enter the input delay in milliseconds",
},
{
Text: "Settings:",
Widget: settingsFileEntry,
HintText: "Just for information (not editable)",
},
{
Widget: container.NewHBox(
widget.NewHyperlink("Get API Key", getApiKeyLink),
widget.NewHyperlink("Contribute", contributeLink),
),
},
},
OnSubmit: func() {
if apiKeyEntry.Text != "" {
internal.SyncSettingsFile(&Settings{
ApiKey: apiKeyEntry.Text,
})
}
if inputDelayEntry.Text != "" {
internal.SyncSettingsFile(&Settings{
InputDelay: inputDelayEntry.Text,
})
}
settingsWindow.Close()
},
SubmitText: "Save",
}
settingsWindow.SetContent(form)
settingsWindow.Show()
}