itd/cmd/itgui/time.go

58 lines
1.3 KiB
Go
Raw Normal View History

2021-08-26 04:18:24 +00:00
package main
import (
2022-05-01 22:22:28 +00:00
"context"
2021-08-26 04:18:24 +00:00
"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
)
2022-05-05 21:00:49 +00:00
func timeTab(ctx context.Context, client *api.Client, w fyne.Window) fyne.CanvasObject {
c := container.NewVBox()
c.Add(layout.NewSpacer())
// Create entry for time string
2021-08-26 04:18:24 +00:00
timeEntry := widget.NewEntry()
timeEntry.SetText(time.Now().Format(time.RFC1123))
2022-05-05 21:00:49 +00:00
timeEntry.SetPlaceHolder("RFC1123")
2021-08-26 04:18:24 +00:00
2021-08-26 15:47:17 +00:00
// Create button to set current time
2022-05-05 21:00:49 +00:00
setCurrentBtn := widget.NewButton("Set current time", func() {
// Set current time
err := client.SetTime(ctx, time.Now())
if err != nil {
guiErr(err, "Error setting time", false, w)
return
}
// Set time entry to current time
2021-08-26 04:18:24 +00:00
timeEntry.SetText(time.Now().Format(time.RFC1123))
})
2022-05-05 21:00:49 +00:00
// Create button to set time from entry
setBtn := widget.NewButton("Set", func() {
// Parse RFC1123 time string in entry
newTime, err := time.Parse(time.RFC1123, timeEntry.Text)
2021-08-26 04:18:24 +00:00
if err != nil {
2022-05-05 21:00:49 +00:00
guiErr(err, "Error parsing time string", false, w)
return
}
// Set time from parsed string
err = client.SetTime(ctx, newTime)
if err != nil {
guiErr(err, "Error setting time", false, w)
2021-08-26 04:18:24 +00:00
return
}
})
2022-05-05 21:00:49 +00:00
c.Add(timeEntry)
c.Add(setBtn)
c.Add(setCurrentBtn)
2021-08-26 04:18:24 +00:00
2022-05-05 21:00:49 +00:00
c.Add(layout.NewSpacer())
return c
2021-08-26 04:18:24 +00:00
}