Files
matocock/main.go
2025-08-30 12:15:55 +01:00

121 lines
3.1 KiB
Go

// See the LICENSE file to see your liberties and restrictions.
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"time"
)
func main() {
var timer time.Duration
var ticking bool = false
a := app.New()
w := a.NewWindow("matocock")
w.SetFixedSize(true)
w.Resize(fyne.NewSize(400, 50))
workEntry := widget.NewEntry()
workEntry.SetText("3s")
workEntry.TextStyle.Monospace = true
workEntry.Refresh()
breakEntry := widget.NewEntry()
breakEntry.SetText("5m")
breakEntry.TextStyle.Monospace = true
breakEntry.Refresh()
var beginButton *widget.Button
beginButton = widget.NewButtonWithIcon("Start", theme.Icon(theme.IconNameMediaPlay), func() {
go func() {
if !ticking {
ticking = true
duration, err := time.ParseDuration(workEntry.Text)
if err != nil {
neww := fyne.CurrentApp().NewWindow("Error")
neww.Resize(fyne.NewSize(500, 500))
fyne.Do(func() {
neww.Show()
})
err_dialog := dialog.NewError(err, neww)
err_dialog.SetOnClosed(func() {
neww.Close()
})
err_dialog.Show()
ticking = false
return
}
beginButton.Icon = theme.Icon(theme.IconNameMediaPause)
fyne.Do(func() {
beginButton.SetText("Stop")
})
beginButton.Importance = widget.LowImportance
fyne.Do(func() {
beginButton.Refresh()
})
timer = duration
timeStarted := time.Now()
workEntry.Disable()
for ticking {
cur := time.Since(timeStarted)
workEntry.SetText((cur.Round(time.Second) - timer.Round(time.Second)).Abs().String())
if cur >= timer {
ticking = false
a.SendNotification(fyne.NewNotification("matocock", "timer is done"))
workEntry.Enable()
beginButton.Icon = theme.Icon(theme.IconNameMediaPlay)
fyne.Do(func() {
beginButton.SetText("Start")
})
beginButton.Importance = widget.HighImportance
fyne.Do(func() {
beginButton.Refresh()
})
} else {
time.Sleep(time.Second * 1)
}
}
} else {
ticking = false
workEntry.Enable()
beginButton.Icon = theme.Icon(theme.IconNameMediaPlay)
fyne.Do(func() {
beginButton.SetText("Start")
})
beginButton.Importance = widget.HighImportance
fyne.Do(func() {
beginButton.Refresh()
})
}
}()
})
beginButton.Importance = widget.HighImportance
w.SetContent(container.NewGridWithColumns(3, workEntry, breakEntry, beginButton))
menu := fyne.NewMainMenu(fyne.NewMenu("root"), fyne.NewMenu("help", fyne.NewMenuItem("valid time arguments", func() {
neww := fyne.CurrentApp().NewWindow("Help")
neww.Resize(fyne.NewSize(500, 500))
fyne.Do(func() {
neww.Show()
})
help_dialog := dialog.NewInformation("Help", "Here are some valid time arguments:\n3ns (3 nanoseconds)\n3s (3 seconds)\n2m (2 minutes)\n2m3s (2 minutes and 3 seconds)\n2h1m1s (2 hours 1 minute and 1 second)", neww)
help_dialog.SetOnClosed(func() {
neww.Close()
})
help_dialog.Show()
})))
w.SetMainMenu(menu)
w.ShowAndRun()
}