diff --git a/draw.go b/draw.go index 43bc7d4..17b8719 100644 --- a/draw.go +++ b/draw.go @@ -6,13 +6,14 @@ import "git.tebibyte.media/sashakoshka/stone" func redraw () { width, height := application.Size() - columns = (width - 20) / 23 + 1 - rows = height / 10 + 1 + state.columns = (width - 20) / 23 + 1 + state.rows = height / 10 + 1 + state.pageSize = state.columns * state.rows - monthIter := viewingMonth - xOffset := (width - columns * 23) / 2 + 1 - for y := 0; y < rows; y ++ { - for x := 0; x < columns; x ++ { + monthIter := state.viewingMonth + xOffset := (width - state.columns * 23) / 2 + 1 + for y := 0; y < state.rows; y ++ { + for x := 0; x < state.columns; x ++ { drawMonth ( x * 23 + xOffset, y * 10, monthIter / 12, @@ -23,7 +24,7 @@ func redraw () { } func drawMonth (xOffset, yOffset, year int, month time.Month) { - current := int(month) - 1 + year * 12 == currentMonth + current := int(month) - 1 + year * 12 == state.currentMonth bce := year < 0 if bce { year *= -1 } diff --git a/event.go b/event.go index a61068c..2d7a015 100644 --- a/event.go +++ b/event.go @@ -1,52 +1,46 @@ package main +import "sync" import "time" import "git.tebibyte.media/sashakoshka/stone" +var lock sync.Mutex + func onScroll (x, y int) { - drawMutex.Lock() - defer drawMutex.Unlock() + lock.Lock() + defer lock.Unlock() - viewingMonth += y * columns + 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: - drawMutex.Lock() - defer drawMutex.Unlock() - - viewingMonth -= columns + state.viewingMonth -= state.columns application.Clear() redraw() application.Draw() case stone.KeyDown: - drawMutex.Lock() - defer drawMutex.Unlock() - - viewingMonth += columns + state.viewingMonth += state.columns application.Clear() redraw() application.Draw() case stone.KeyPageUp: - drawMutex.Lock() - defer drawMutex.Unlock() - - viewingMonth -= columns * rows + state.viewingMonth -= state.pageSize application.Clear() redraw() application.Draw() case stone.KeyPageDown: - drawMutex.Lock() - defer drawMutex.Unlock() - - viewingMonth += columns * rows + state.viewingMonth += state.pageSize application.Clear() redraw() application.Draw() @@ -55,11 +49,11 @@ func onPress (button stone.Button, modifiers stone.Modifiers) { } func onStart () { - drawMutex.Lock() - defer drawMutex.Unlock() + lock.Lock() + defer lock.Unlock() - currentMonth = canonMonth(time.Now()) - viewingMonth = currentMonth + state.currentMonth = canonMonth(time.Now()) + state.viewingMonth = state.currentMonth redraw() go func () { for { @@ -70,30 +64,29 @@ func onStart () { } func onResize () { - drawMutex.Lock() - defer drawMutex.Unlock() + lock.Lock() + defer lock.Unlock() redraw() } func onTick () { - drawMutex.Lock() - defer drawMutex.Unlock() + lock.Lock() + defer lock.Unlock() - pageSize := columns * rows within := - currentMonth >= viewingMonth && - currentMonth < viewingMonth + pageSize + state.currentMonth >= state.viewingMonth && + state.currentMonth < state.viewingMonth + state.pageSize newMonth := canonMonth(time.Now()) - if currentMonth != newMonth { - currentMonth = newMonth + if state.currentMonth != newMonth { + state.currentMonth = newMonth if within { - if currentMonth < viewingMonth { - viewingMonth -= pageSize - } else if currentMonth >= viewingMonth + pageSize { - viewingMonth += pageSize + if state.currentMonth < state.viewingMonth { + state.viewingMonth -= state.pageSize + } else if state.currentMonth >= state.pageSize { + state.viewingMonth += state.pageSize } } diff --git a/main.go b/main.go index 7417719..2a943b2 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,5 @@ package main -import "sync" import "bytes" import "image" import _ "embed" @@ -13,10 +12,10 @@ var iconBytes []byte var application = &stone.Application { } -var viewingMonth int -var currentMonth int -var columns, rows int -var drawMutex sync.Mutex +var state struct { + viewingMonth, currentMonth int + columns, rows, pageSize int +} var shortMonthNames = []string { "",