Calendar now checks ever second for a date change

This commit is contained in:
Sasha Koshka 2022-11-28 17:18:23 -05:00
parent 816da6e019
commit a71fb3de4b

82
main.go
View File

@ -2,6 +2,7 @@ package main
import "fmt" import "fmt"
import "time" import "time"
import "sync"
import "bytes" import "bytes"
import "image" import "image"
import _ "embed" import _ "embed"
@ -14,9 +15,10 @@ var iconBytes []byte
var application = &stone.Application { } var application = &stone.Application { }
var now time.Time
var viewingMonth int var viewingMonth int
var currentMonth int
var columns, rows int var columns, rows int
var drawMutex sync.Mutex
var shortMonthNames = []string { var shortMonthNames = []string {
"", "",
@ -43,7 +45,7 @@ func main () {
application.SetIcon([]image.Image { icon }) application.SetIcon([]image.Image { icon })
application.OnStart(onStart) application.OnStart(onStart)
application.OnResize(redraw) application.OnResize(onResize)
application.OnPress(onPress) application.OnPress(onPress)
application.OnScroll(onScroll) application.OnScroll(onScroll)
@ -52,6 +54,9 @@ func main () {
} }
func onScroll (x, y int) { func onScroll (x, y int) {
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth += y * columns viewingMonth += y * columns
application.Clear() application.Clear()
redraw() redraw()
@ -61,50 +66,98 @@ func onScroll (x, y int) {
func onPress (button stone.Button, modifiers stone.Modifiers) { func onPress (button stone.Button, modifiers stone.Modifiers) {
switch button { switch button {
case stone.KeyUp: case stone.KeyUp:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth -= columns viewingMonth -= columns
application.Clear() application.Clear()
redraw() redraw()
application.Draw() application.Draw()
case stone.KeyDown: case stone.KeyDown:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth += columns viewingMonth += columns
application.Clear() application.Clear()
redraw() redraw()
application.Draw() application.Draw()
case stone.KeyPageUp: case stone.KeyPageUp:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth -= columns * rows viewingMonth -= columns * rows
application.Clear() application.Clear()
redraw() redraw()
application.Draw() application.Draw()
case stone.KeyPageDown: case stone.KeyPageDown:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth += columns * rows viewingMonth += columns * rows
application.Clear() application.Clear()
redraw() redraw()
application.Draw() application.Draw()
} }
} }
func onStart () { func onStart () {
now = time.Now() currentMonth = canonMonth(time.Now())
viewingMonth = int(now.Month() - 1) + now.Year() * 12 viewingMonth = currentMonth
redraw() redraw()
go tick()
}
func onResize () {
drawMutex.Lock()
defer drawMutex.Unlock()
redraw()
}
func tick () {
for {
time.Sleep(time.Second)
drawMutex.Lock()
newMonth := canonMonth(time.Now())
if currentMonth != newMonth {
currentMonth = newMonth
pageSize := columns * rows
if currentMonth < viewingMonth {
viewingMonth -= pageSize
} else if currentMonth >= viewingMonth + pageSize {
viewingMonth += pageSize
}
application.Clear()
redraw()
application.Draw()
}
drawMutex.Unlock()
}
} }
func redraw () { func redraw () {
width, height := application.Size() width, height := application.Size()
columns = (width - 20) / 23 + 1
rows = height / 10 + 1
monthIter := viewingMonth monthIter := viewingMonth
rows = 0
columns = (width - 20) / 23 + 1
xOffset := (width - columns * 23) / 2 + 1 xOffset := (width - columns * 23) / 2 + 1
for y := 0; y < height; y += 10 { for y := 0; y < rows; y ++ {
for x := 0; x < width - 19; x += 23 { for x := 0; x < columns; x ++ {
drawMonth ( drawMonth (
x + xOffset, y, x * 23 + xOffset, y * 10,
monthIter / 12, monthIter / 12,
time.Month(wrap(monthIter, 12) + 1)) time.Month(wrap(monthIter, 12) + 1))
monthIter ++ monthIter ++
} }
rows ++
} }
} }
@ -118,7 +171,7 @@ func wrap (value, around int) (wrapped int) {
} }
func drawMonth (xOffset, yOffset, year int, month time.Month) { func drawMonth (xOffset, yOffset, year int, month time.Month) {
current := month == now.Month() && year == now.Year() current := int(month) - 1 + year * 12 == currentMonth
bce := year < 0 bce := year < 0
if bce { year *= -1 } if bce { year *= -1 }
@ -159,7 +212,7 @@ func drawMonth (xOffset, yOffset, year int, month time.Month) {
fmt.Fprint(application, dayIter) fmt.Fprint(application, dayIter)
} }
if current && dayIter == int(now.Day()) { if current && dayIter == int(time.Now().Day()) {
application.SetColor(x, y, stone.ColorRed) application.SetColor(x, y, stone.ColorRed)
application.SetColor(x + 1, y, stone.ColorRed) application.SetColor(x + 1, y, stone.ColorRed)
} else if weekday == 0 || weekday == 6 { } else if weekday == 0 || weekday == 6 {
@ -205,3 +258,8 @@ func daysInMonth (year int, month time.Month) (days int) {
return return
} }
func canonMonth (when time.Time) (month int) {
month = int(when.Month() - 1) + when.Year() * 12
return
}