99 lines
1.9 KiB
Go
99 lines
1.9 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
// Package main implements the 7GUIs Timer task using prevara/gel.
|
|
// See: https://eugenkiss.github.io/7guis/tasks#timer
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
l "gioui.org/layout"
|
|
|
|
gel "git.mleku.dev/mleku/prevara"
|
|
"git.mleku.dev/mleku/prevara/pkg/qu"
|
|
)
|
|
|
|
type State struct {
|
|
*gel.Window
|
|
timer *Timer
|
|
duration *gel.Float
|
|
reset *gel.Clickable
|
|
}
|
|
|
|
func NewState(quit qu.C) *State {
|
|
w := gel.NewWindowP9(quit)
|
|
|
|
return &State{
|
|
Window: w,
|
|
timer: NewTimer(5 * time.Second),
|
|
duration: w.Float(),
|
|
reset: w.WidgetPool.GetClickable(),
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
quit := qu.T()
|
|
state := NewState(quit)
|
|
|
|
// Start timer and set initial duration value
|
|
state.duration.SetValue(0.5)
|
|
stopTimer := state.timer.Start()
|
|
defer stopTimer()
|
|
|
|
// Update window when timer changes
|
|
go func() {
|
|
for {
|
|
select {
|
|
case <-state.timer.Updated:
|
|
state.Invalidate()
|
|
case <-quit.Wait():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
if e := state.Window.
|
|
Size(24, 24).
|
|
Title("Timer").
|
|
Open().
|
|
Run(state.layout, quit.Q, quit); e != nil {
|
|
panic(e)
|
|
}
|
|
}
|
|
|
|
func (s *State) layout(gtx l.Context) l.Dimensions {
|
|
// Handle reset button
|
|
if s.reset.Clicked() {
|
|
s.timer.Reset()
|
|
}
|
|
|
|
// Handle duration slider
|
|
if s.duration.Changed() {
|
|
s.timer.SetDuration(time.Duration(s.duration.Value()*15) * time.Second)
|
|
}
|
|
|
|
// Get timer info
|
|
info := s.timer.Info()
|
|
progress := float32(0)
|
|
if info.Duration > 0 {
|
|
progress = float32(info.Progress.Seconds() / info.Duration.Seconds())
|
|
}
|
|
if progress > 1 {
|
|
progress = 1
|
|
}
|
|
|
|
return s.Inset(0.75,
|
|
s.VFlex().
|
|
Rigid(s.Body1("Elapsed Time").Fn).
|
|
Rigid(s.ProgressBar().SetProgress(int(progress*100)).Fn).
|
|
Rigid(s.Body1(fmt.Sprintf("%.1fs", info.Progress.Seconds())).Fn).
|
|
Rigid(gel.EmptySpace(0, 8)).
|
|
Rigid(s.Body1("Duration").Fn).
|
|
Rigid(s.Slider().Float(s.duration).Fn).
|
|
Rigid(gel.EmptySpace(0, 8)).
|
|
Rigid(s.Button(s.reset).Text("Reset").Fn).
|
|
Fn,
|
|
).Fn(gtx)
|
|
}
|