-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.go
604 lines (520 loc) · 17.9 KB
/
gui.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strconv"
l "github.com/HugeFrog24/kopfschmerzkalender-generator/localization"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
"golang.org/x/text/language"
)
// Helper function to create a range input container
func createRangeField(minEntry, maxEntry *widget.Entry) *fyne.Container {
return container.NewHBox(
minEntry,
widget.NewLabel(" - "),
maxEntry,
)
}
// Add this function to create a language selector
func createLanguageSelector(updateUI func()) *widget.Select {
languages := []string{"English", "Deutsch"}
langSelect := widget.NewSelect(languages, func(selected string) {
switch selected {
case "English":
l.SetLanguage(language.English)
log.Println("Language changed to English") // Add logging for language change
case "Deutsch":
l.SetLanguage(language.German)
log.Println("Language changed to German") // Add logging for language change
}
// Call the updateUI function to refresh all widgets
updateUI()
})
langSelect.SetSelected("Deutsch") // Set default language
return langSelect
}
func runGUI() {
a := app.NewWithID("com.tibik.kopfschmerzkalender")
w := a.NewWindow(l.T(l.MsgAppTitle))
// Create input fields
minIntensityEntry := widget.NewEntry()
maxIntensityEntry := widget.NewEntry()
minDaysBetweenMedicationEntry := widget.NewEntry()
maxDaysBetweenMedicationEntry := widget.NewEntry()
// New entries for duration hours
minDurationHoursEntry := widget.NewEntry()
maxDurationHoursEntry := widget.NewEntry()
nameEntry := widget.NewEntry()
nameEntry.SetPlaceHolder(l.T(l.MsgNamePlaceholder))
medicationAEntry := widget.NewEntry()
medicationAEntry.SetPlaceHolder(l.T(l.MsgMedicationAPlaceholder))
medicationBEntry := widget.NewEntry()
medicationBEntry.SetPlaceHolder(l.T(l.MsgMedicationBPlaceholder))
medicationCEntry := widget.NewEntry()
medicationCEntry.SetPlaceHolder(l.T(l.MsgMedicationCPlaceholder))
outputFilePathEntry := widget.NewEntry()
outputFilePathEntry.SetPlaceHolder(l.T(l.MsgOutputFilePathPlaceholder))
// Create Browse button
browseButton := widget.NewButton(l.T(l.MsgBrowse), func() {
fd := dialog.NewFileSave(func(writer fyne.URIWriteCloser, err error) {
if err != nil {
dialog.ShowError(err, w)
return
}
if writer == nil {
return
}
defer writer.Close()
outputFilePathEntry.SetText(writer.URI().Path())
}, w)
fd.SetFileName("Kopfschmerzkalender.xlsx")
fd.SetFilter(storage.NewExtensionFileFilter([]string{".xlsx"}))
fd.Show()
})
// Create output file path container
outputFilePathContainer := container.NewBorder(nil, nil, nil, browseButton, outputFilePathEntry)
sampleDataCheck := widget.NewCheck(l.T(l.MsgSampleData), func(checked bool) {
// Log the state change
if checked {
log.Println("Sample data activated")
} else {
log.Println("Sample data deactivated")
}
// Toggle entry fields based on the checkbox state
setEntryFieldsEnabled(checked,
minIntensityEntry, maxIntensityEntry,
minDaysBetweenMedicationEntry, maxDaysBetweenMedicationEntry,
minDurationHoursEntry, maxDurationHoursEntry, // Include new fields
nameEntry, medicationAEntry, medicationBEntry, medicationCEntry)
})
// Set default values
sampleDataCheck.SetChecked(true)
// Enable entry fields initially since sample data is checked by default
setEntryFieldsEnabled(true,
minIntensityEntry, maxIntensityEntry,
minDaysBetweenMedicationEntry, maxDaysBetweenMedicationEntry,
minDurationHoursEntry, maxDurationHoursEntry, // Include new fields
nameEntry, medicationAEntry, medicationBEntry, medicationCEntry)
minIntensityEntry.SetText("5") // Pre-populate with 5
maxIntensityEntry.SetText("10") // Pre-populate with 10
minDaysBetweenMedicationEntry.SetText("5")
maxDaysBetweenMedicationEntry.SetText("9")
minDurationHoursEntry.SetText("1") // Example default min hours
maxDurationHoursEntry.SetText("24") // Example default max hours
// Create a slice to hold the selected months
selectedMonths := make([]string, 0)
// Create the months selection grid
monthsGrid := createMonthsSelection(&selectedMonths)
// Group range fields into horizontal containers
intensityRange := createRangeField(minIntensityEntry, maxIntensityEntry)
daysBetweenMedRange := createRangeField(minDaysBetweenMedicationEntry, maxDaysBetweenMedicationEntry)
durationHoursRange := createRangeField(minDurationHoursEntry, maxDurationHoursEntry) // New range field
// Create form
form := &widget.Form{
Items: []*widget.FormItem{
{Text: l.T(l.MsgSampleData), Widget: sampleDataCheck},
{Text: l.T(l.MsgIntensity), Widget: intensityRange},
{Text: l.T(l.MsgDaysBetweenMed), Widget: daysBetweenMedRange},
{Text: l.T(l.MsgDurationHours), Widget: durationHoursRange}, // New form item
{Text: l.T(l.MsgMonths), Widget: monthsGrid},
{Text: l.T(l.MsgName), Widget: nameEntry},
{Text: l.T(l.MsgMedicationA), Widget: medicationAEntry},
{Text: l.T(l.MsgMedicationB), Widget: medicationBEntry},
{Text: l.T(l.MsgMedicationC), Widget: medicationCEntry},
{Text: l.T(l.MsgOutputFilePath), Widget: outputFilePathContainer},
},
}
// Create start button
startButton := widget.NewButton(l.T(l.MsgStart), func() {
// Validate intensity values
minIntensity, minErr := validateIntensity(minIntensityEntry.Text)
maxIntensity, maxErr := validateIntensity(maxIntensityEntry.Text)
if minErr != nil || maxErr != nil {
dialog.ShowError(errors.New(l.T(l.MsgIntensityError)), w)
return
}
if minIntensity > maxIntensity {
dialog.ShowError(errors.New(l.T(l.MsgMinIntensityError)), w)
return
}
// Validate medication delta time
minDaysBetweenMedication, minDaysErr := parseInt(minDaysBetweenMedicationEntry.Text)
maxDaysBetweenMedication, maxDaysErr := parseInt(maxDaysBetweenMedicationEntry.Text)
if minDaysErr != nil || maxDaysErr != nil {
dialog.ShowError(errors.New(l.T(l.MsgDaysBetweenMedError)), w)
return
}
if minDaysBetweenMedication > maxDaysBetweenMedication {
dialog.ShowError(errors.New(l.T(l.MsgMinDaysBetweenMedError)), w)
return
}
// Validate duration hours
minDurationHours, minDurationErr := parseInt(minDurationHoursEntry.Text)
maxDurationHours, maxDurationErr := parseInt(maxDurationHoursEntry.Text)
if minDurationErr != nil || maxDurationErr != nil {
dialog.ShowError(errors.New(l.T(l.MsgDurationHoursError)), w)
return
}
if minDurationHours < 0 {
dialog.ShowError(errors.New(l.T(l.MsgMinDurationHoursNegativeError)), w)
return
}
if maxDurationHours > 24 {
dialog.ShowError(errors.New(l.T(l.MsgMaxDurationHoursExceededError)), w)
return
}
if minDurationHours > maxDurationHours {
dialog.ShowError(errors.New(l.T(l.MsgMinDurationHoursGreaterError)), w)
return
}
log.Printf("Selected months before creating config: %v\n", selectedMonths)
config := Config{
SampleData: sampleDataCheck.Checked,
MinDaysBetweenMedication: minDaysBetweenMedication,
MaxDaysBetweenMedication: maxDaysBetweenMedication,
MinDurationHours: minDurationHours, // New field
MaxDurationHours: maxDurationHours, // New field
Months: make([]string, len(selectedMonths)),
Name: nameEntry.Text,
MedicationA: medicationAEntry.Text,
MedicationB: medicationBEntry.Text,
MedicationC: medicationCEntry.Text,
OutputFilePath: outputFilePathEntry.Text,
MinIntensity: minIntensity,
MaxIntensity: maxIntensity,
}
copy(config.Months, selectedMonths)
log.Printf("Config months after creation: %v\n", config.Months)
// Save config to file
saveConfig(config)
// Run the main program
filePath, err := GenerateKopfschmerzkalender(config)
if err != nil {
dialog.ShowError(err, w)
} else {
showSuccessDialog(w, filePath)
}
})
// Create exit button
exitButton := widget.NewButton(l.T(l.MsgExit), func() {
a.Quit()
})
// Create about button
aboutButton := widget.NewButton(l.T(l.MsgAbout), func() {
showAboutDialog(w)
})
// Declare content variable before using it
var content *fyne.Container
// Create a function to update all UI elements
updateUI := func() {
w.SetTitle(l.T(l.MsgAppTitle))
sampleDataCheck.Text = l.T(l.MsgSampleData)
sampleDataCheck.Refresh() // Refresh the sampleDataCheck widget
// Update all other widget texts
form.Items[0].Text = l.T(l.MsgSampleData)
form.Items[1].Text = l.T(l.MsgIntensity)
form.Items[2].Text = l.T(l.MsgDaysBetweenMed)
form.Items[3].Text = l.T(l.MsgDurationHours) // Update duration hours label
form.Items[4].Text = l.T(l.MsgMonths)
form.Items[5].Text = l.T(l.MsgName)
form.Items[6].Text = l.T(l.MsgMedicationA)
form.Items[7].Text = l.T(l.MsgMedicationB)
form.Items[8].Text = l.T(l.MsgMedicationC)
form.Items[9].Text = l.T(l.MsgOutputFilePath)
nameEntry.SetPlaceHolder(l.T(l.MsgNamePlaceholder))
medicationAEntry.SetPlaceHolder(l.T(l.MsgMedicationAPlaceholder))
medicationBEntry.SetPlaceHolder(l.T(l.MsgMedicationBPlaceholder))
medicationCEntry.SetPlaceHolder(l.T(l.MsgMedicationCPlaceholder))
outputFilePathEntry.SetPlaceHolder(l.T(l.MsgOutputFilePathPlaceholder))
browseButton.SetText(l.T(l.MsgBrowse))
startButton.SetText(l.T(l.MsgStart))
exitButton.SetText(l.T(l.MsgExit))
aboutButton.SetText(l.T(l.MsgAbout))
// Update months in the checkgroup
updateMonthsSelection(monthsGrid)
if content != nil {
content.Refresh()
}
}
// Create language selector
langSelector := createLanguageSelector(updateUI)
// Create button container
buttons := container.NewHBox(startButton, exitButton, aboutButton, langSelector)
// Create main container
content = container.NewVBox(
form,
buttons,
)
w.SetContent(content)
w.Resize(fyne.NewSize(400, 400))
w.ShowAndRun()
}
func parseInt(s string) (int, error) {
i, err := strconv.Atoi(s)
if err != nil {
return 0, err
}
return i, nil
}
func saveConfig(config Config) {
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return
}
err = os.WriteFile("config.json", data, 0600)
if err != nil {
fmt.Println("Error writing config file:", err)
return
}
fmt.Printf("Config saved successfully. Selected months: %v\n", config.Months)
}
func showSuccessDialog(w fyne.Window, filePath string) {
openButton := widget.NewButton(l.T(l.MsgOpenFile), func() {
openFile(filePath)
})
content := container.NewVBox(
widget.NewLabel(l.T(l.MsgSuccessGenerated)),
widget.NewLabel(l.T(l.MsgFileSavedAt, filePath)),
container.NewCenter(openButton),
)
dialog.ShowCustom(l.T(l.MsgSuccess), l.T(l.MsgClose), content, w)
}
func openFile(path string) {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("cmd", "/c", "start", "", path)
case "darwin":
cmd = exec.Command("open", path)
default: // Linux and other Unix-like systems
cmd = exec.Command("xdg-open", path)
}
err := cmd.Start()
if err != nil {
fmt.Printf(l.T(l.MsgErrorOpeningFile), err)
}
}
func setEntryFieldsEnabled(enabled bool, entries ...*widget.Entry) {
for _, entry := range entries {
if enabled {
entry.Enable()
} else {
entry.Disable()
}
entry.Refresh()
}
}
func validateIntensity(s string) (int, error) {
i, err := strconv.Atoi(s)
if err != nil || i < 0 || i > 10 {
return 0, errors.New(l.T(l.MsgIntensityError))
}
return i, nil
}
// Helper function to create the months selection widget
func createMonthsSelection(selectedMonths *[]string) *fyne.Container {
months := []string{
l.T(l.MsgJanuary), l.T(l.MsgFebruary), l.T(l.MsgMarch), l.T(l.MsgApril), l.T(l.MsgMay), l.T(l.MsgJune),
l.T(l.MsgJuly), l.T(l.MsgAugust), l.T(l.MsgSeptember), l.T(l.MsgOctober), l.T(l.MsgNovember), l.T(l.MsgDecember),
}
checkGroup := widget.NewCheckGroup(months, func(selected []string) {
*selectedMonths = selected
log.Printf("Updated selected months: %v\n", *selectedMonths)
})
grid := container.New(layout.NewGridLayout(3))
for _, month := range months {
check := widget.NewCheck(month, func(checked bool) {
if checked {
*selectedMonths = append(*selectedMonths, month)
} else {
for i, m := range *selectedMonths {
if m == month {
*selectedMonths = append((*selectedMonths)[:i], (*selectedMonths)[i+1:]...)
break
}
}
}
checkGroup.SetSelected(*selectedMonths)
})
grid.Add(check)
}
// Set initial state based on config
config, err := readConfig("config.json")
if err == nil {
checkGroup.SetSelected(config.Months)
*selectedMonths = config.Months
}
return grid
}
// Helper function to update the months selection widget
func updateMonthsSelection(grid *fyne.Container) {
months := []string{
l.T(l.MsgJanuary), l.T(l.MsgFebruary), l.T(l.MsgMarch), l.T(l.MsgApril), l.T(l.MsgMay), l.T(l.MsgJune),
l.T(l.MsgJuly), l.T(l.MsgAugust), l.T(l.MsgSeptember), l.T(l.MsgOctober), l.T(l.MsgNovember), l.T(l.MsgDecember),
}
for i, child := range grid.Objects {
if check, ok := child.(*widget.Check); ok {
check.Text = months[i]
check.Refresh()
}
}
}
func showAboutDialog(w fyne.Window) {
currentVersion := GetCurrentVersion()
checkUpdatesButton := widget.NewButton(l.T(l.MsgCheckUpdates), func() {
checkForUpdates(w)
})
repoURL := GithubRepoURL
repoLink := widget.NewRichTextFromMarkdown(fmt.Sprintf("%s: [%s](%s)",
l.T(l.MsgGitHubRepository), repoURL, repoURL))
// Combine multiple lines into a single label
infoText := fmt.Sprintf("%s\n%s\n%s: %s\n%s",
l.T(l.MsgAboutTitle),
l.T(l.MsgAboutDescription),
l.T(l.MsgAuthor), "HugeFrog24",
l.T(l.MsgVersion, currentVersion.String()),
)
infoLabel := widget.NewLabel(infoText)
infoLabel.Wrapping = fyne.TextWrapWord
content := container.NewVBox(
infoLabel,
repoLink,
checkUpdatesButton,
)
dialog.ShowCustom(l.T(l.MsgAbout), l.T(l.MsgClose), content, w)
}
func checkForUpdates(w fyne.Window) {
log.Println("Checking for updates...")
// Create and show a progress dialog with a cancel button
progressBar := widget.NewProgressBarInfinite()
cancelButton := widget.NewButton(l.T(l.MsgCancel), nil)
content := container.NewVBox(
widget.NewLabel(l.T(l.MsgPleaseWait)),
progressBar,
cancelButton,
)
// Create the progress dialog without any default buttons
progressDialog := dialog.NewCustomWithoutButtons(l.T(l.MsgCheckingForUpdates), content, w)
progressDialog.SetOnClosed(func() {
// Add cancellation logic here if needed
})
progressDialog.Show()
// Create a channel to signal cancellation
cancelChan := make(chan struct{})
cancelButton.OnTapped = func() {
close(cancelChan)
progressDialog.Hide()
}
// Use a goroutine to perform the update check
go func() {
latestVersion, downloadURL, err := CheckForUpdates(cancelChan)
// Hide the progress dialog when done
progressDialog.Hide()
if err != nil {
if err == ErrUpdateCancelled {
log.Println("Update check cancelled by user")
return
}
log.Printf("Error checking for updates: %v", err)
dialog.ShowError(err, w)
return
}
currentVersion := GetCurrentVersion()
log.Printf("Current version: %s, latest version: %s", currentVersion, latestVersion)
if latestVersion.GT(currentVersion) {
log.Println("Update available")
content := widget.NewLabel(l.T(l.MsgUpdateAvailable, latestVersion.String()))
downloadButton := widget.NewButton(l.T(l.MsgDownload), func() {
log.Println("Starting update download")
progressBar := widget.NewProgressBar()
cancelButton := widget.NewButton(l.T(l.MsgCancel), nil)
content := container.NewVBox(
widget.NewLabel(l.T(l.MsgPleaseWait)), // Change to "Please wait"
progressBar,
cancelButton,
)
progressDialog := dialog.NewCustomWithoutButtons(l.T(l.MsgDownloadingUpdate), content, w)
progressDialog.Show()
// Create a new channel for download cancellation
downloadCancelChan := make(chan struct{})
cancelButton.OnTapped = func() {
close(downloadCancelChan)
progressDialog.Hide()
}
go func() {
err := DownloadAndInstallUpdate(downloadURL, func(progress float64) {
log.Printf("Download progress: %.2f%%", progress*100)
progressBar.SetValue(progress)
}, downloadCancelChan)
progressDialog.Hide()
if err != nil {
if err == ErrUpdateCancelled {
log.Println("Update download cancelled by user")
return
}
log.Printf("Error during update: %v", err)
dialog.ShowError(err, w)
} else {
log.Println("Update completed successfully")
restartDialog := dialog.NewConfirm(
l.T(l.MsgUpdateSuccess),
l.T(l.MsgRestartRequired),
func(restart bool) {
if restart {
log.Println("Restarting application...")
executable, _ := os.Executable()
cmd := exec.Command(executable)
if err := cmd.Start(); err != nil {
log.Printf("Error restarting application: %v", err)
return
}
os.Exit(0)
} else {
log.Println("Restart postponed")
}
},
w,
)
restartDialog.SetDismissText(l.T(l.MsgLater))
restartDialog.SetConfirmText(l.T(l.MsgRestartNow))
restartDialog.Show()
}
}()
})
// Add a cancel button
cancelButton := widget.NewButton(l.T(l.MsgCancel), nil)
buttonsContainer := container.NewHBox(downloadButton, cancelButton)
updateDialog := dialog.NewCustomWithoutButtons(
l.T(l.MsgUpdateAvailable, latestVersion.String()),
container.NewVBox(content, buttonsContainer),
w,
)
// Set the cancel button's OnTapped function to close the dialog
cancelButton.OnTapped = func() {
updateDialog.Hide()
}
updateDialog.Show()
} else {
log.Println("No updates available")
dialog.ShowInformation(l.T(l.MsgNoUpdates), l.T(l.MsgLatestVersion), w)
}
}()
}
func init() {
// Set the initial language (e.g., German)
l.SetLanguage(language.German)
// Set log flags to include filename and line number
log.SetFlags(log.LstdFlags | log.Lshortfile)
}