stonecal/event.go

98 lines
1.7 KiB
Go
Raw Normal View History

2022-11-28 22:26:10 +00:00
package main
2022-11-28 22:33:31 +00:00
import "sync"
2022-11-28 22:26:10 +00:00
import "time"
import "git.tebibyte.media/sashakoshka/stone"
2022-11-28 22:33:31 +00:00
var lock sync.Mutex
2022-11-28 22:26:10 +00:00
func onScroll (x, y int) {
2022-11-28 22:33:31 +00:00
lock.Lock()
defer lock.Unlock()
2022-11-28 22:26:10 +00:00
2022-11-28 22:33:31 +00:00
state.viewingMonth += y * state.columns
2022-11-28 22:26:10 +00:00
application.Clear()
redraw()
application.Draw()
}
func onPress (button stone.Button, modifiers stone.Modifiers) {
2022-11-28 22:33:31 +00:00
lock.Lock()
defer lock.Unlock()
2022-11-28 22:26:10 +00:00
switch button {
case stone.KeyUp:
2022-11-28 22:33:31 +00:00
state.viewingMonth -= state.columns
2022-11-28 22:26:10 +00:00
application.Clear()
redraw()
application.Draw()
case stone.KeyDown:
2022-11-28 22:33:31 +00:00
state.viewingMonth += state.columns
2022-11-28 22:26:10 +00:00
application.Clear()
redraw()
application.Draw()
case stone.KeyPageUp:
2022-11-28 22:33:31 +00:00
state.viewingMonth -= state.pageSize
2022-11-28 22:26:10 +00:00
application.Clear()
redraw()
application.Draw()
case stone.KeyPageDown:
2022-11-28 22:33:31 +00:00
state.viewingMonth += state.pageSize
2022-11-28 22:26:10 +00:00
application.Clear()
redraw()
application.Draw()
}
}
func onStart () {
2022-11-28 22:33:31 +00:00
lock.Lock()
defer lock.Unlock()
2022-11-28 22:26:10 +00:00
2022-11-28 22:33:31 +00:00
state.currentMonth = canonMonth(time.Now())
state.viewingMonth = state.currentMonth
2022-11-28 22:26:10 +00:00
redraw()
go func () {
for {
time.Sleep(time.Second)
onTick()
}
} ()
}
func onResize () {
2022-11-28 22:33:31 +00:00
lock.Lock()
defer lock.Unlock()
2022-11-28 22:26:10 +00:00
redraw()
}
func onTick () {
2022-11-28 22:33:31 +00:00
lock.Lock()
defer lock.Unlock()
2022-11-28 22:26:10 +00:00
within :=
2022-11-28 22:33:31 +00:00
state.currentMonth >= state.viewingMonth &&
state.currentMonth < state.viewingMonth + state.pageSize
2022-11-28 22:26:10 +00:00
newMonth := canonMonth(time.Now())
2022-11-28 22:33:31 +00:00
if state.currentMonth != newMonth {
state.currentMonth = newMonth
2022-11-28 22:26:10 +00:00
if within {
2022-11-28 22:33:31 +00:00
if state.currentMonth < state.viewingMonth {
state.viewingMonth -= state.pageSize
} else if state.currentMonth >= state.pageSize {
state.viewingMonth += state.pageSize
2022-11-28 22:26:10 +00:00
}
}
application.Clear()
redraw()
application.Draw()
}
}