stonecal/event.go

98 lines
1.7 KiB
Go

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