itd/cmd/itgui/time.go

61 lines
1.4 KiB
Go
Raw Permalink Normal View History

2021-08-26 04:18:24 +00:00
package main
import (
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
2021-10-24 20:27:14 +00:00
"go.arsenm.dev/itd/api"
2021-08-26 04:18:24 +00:00
)
2021-10-24 20:27:14 +00:00
func timeTab(parent fyne.Window, client *api.Client) *fyne.Container {
2021-08-26 15:47:17 +00:00
// Create new entry for time string
2021-08-26 04:18:24 +00:00
timeEntry := widget.NewEntry()
2021-08-26 15:47:17 +00:00
// Set text to current time formatter properly
2021-08-26 04:18:24 +00:00
timeEntry.SetText(time.Now().Format(time.RFC1123))
2021-08-26 15:47:17 +00:00
// Create button to set current time
2021-08-26 04:18:24 +00:00
currentBtn := widget.NewButton("Set Current", func() {
timeEntry.SetText(time.Now().Format(time.RFC1123))
2021-10-24 20:27:14 +00:00
setTime(client, true)
2021-08-26 04:18:24 +00:00
})
2021-08-26 15:47:17 +00:00
// Create button to set time inside entry
2021-08-26 04:18:24 +00:00
timeBtn := widget.NewButton("Set", func() {
2021-08-26 15:47:17 +00:00
// Parse time as RFC1123 string
2021-08-26 04:18:24 +00:00
parsedTime, err := time.Parse(time.RFC1123, timeEntry.Text)
if err != nil {
2021-08-27 15:47:24 +00:00
guiErr(err, "Error parsing time string", false, parent)
2021-08-26 04:18:24 +00:00
return
}
2021-08-26 15:47:17 +00:00
// Set time to parsed time
2021-10-24 20:27:14 +00:00
setTime(client, false, parsedTime)
2021-08-26 04:18:24 +00:00
})
2021-08-26 15:47:17 +00:00
// Return new container with all elements centered
2021-08-26 04:18:24 +00:00
return container.NewVBox(
layout.NewSpacer(),
timeEntry,
currentBtn,
timeBtn,
layout.NewSpacer(),
)
}
// setTime sets the first element in the variadic parameter
// if current is false, otherwise, it sets the current time.
2021-10-24 20:27:14 +00:00
func setTime(client *api.Client, current bool, t ...time.Time) error {
var err error
2021-08-26 04:18:24 +00:00
if current {
2022-04-23 00:12:30 +00:00
err = client.SetTime(time.Now())
2021-08-26 04:18:24 +00:00
} else {
2021-10-24 20:27:14 +00:00
err = client.SetTime(t[0])
2021-08-26 04:18:24 +00:00
}
if err != nil {
return err
}
return nil
}