Some lighter refactoring

This commit is contained in:
Sasha Koshka 2022-11-28 17:33:31 -05:00
parent f2cc5e8099
commit 924430617e
3 changed files with 41 additions and 48 deletions

15
draw.go
View File

@ -6,13 +6,14 @@ import "git.tebibyte.media/sashakoshka/stone"
func redraw () { func redraw () {
width, height := application.Size() width, height := application.Size()
columns = (width - 20) / 23 + 1 state.columns = (width - 20) / 23 + 1
rows = height / 10 + 1 state.rows = height / 10 + 1
state.pageSize = state.columns * state.rows
monthIter := viewingMonth monthIter := state.viewingMonth
xOffset := (width - columns * 23) / 2 + 1 xOffset := (width - state.columns * 23) / 2 + 1
for y := 0; y < rows; y ++ { for y := 0; y < state.rows; y ++ {
for x := 0; x < columns; x ++ { for x := 0; x < state.columns; x ++ {
drawMonth ( drawMonth (
x * 23 + xOffset, y * 10, x * 23 + xOffset, y * 10,
monthIter / 12, monthIter / 12,
@ -23,7 +24,7 @@ func redraw () {
} }
func drawMonth (xOffset, yOffset, year int, month time.Month) { 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 bce := year < 0
if bce { year *= -1 } if bce { year *= -1 }

View File

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

View File

@ -1,6 +1,5 @@
package main package main
import "sync"
import "bytes" import "bytes"
import "image" import "image"
import _ "embed" import _ "embed"
@ -13,10 +12,10 @@ var iconBytes []byte
var application = &stone.Application { } var application = &stone.Application { }
var viewingMonth int var state struct {
var currentMonth int viewingMonth, currentMonth int
var columns, rows int columns, rows, pageSize int
var drawMutex sync.Mutex }
var shortMonthNames = []string { var shortMonthNames = []string {
"", "",