2019-08-22 10:09:55 -04:00
|
|
|
// +build darwin linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image/color"
|
2019-08-27 09:24:30 -04:00
|
|
|
"log"
|
2019-08-22 10:09:55 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
gio "git.wow.st/gmp/giowrap"
|
|
|
|
|
|
|
|
"gioui.org/ui"
|
|
|
|
"gioui.org/ui/app"
|
|
|
|
"gioui.org/ui/layout"
|
|
|
|
"gioui.org/ui/text"
|
|
|
|
|
|
|
|
"golang.org/x/image/font/gofont/goregular"
|
2019-08-27 09:24:30 -04:00
|
|
|
"golang.org/x/image/font/sfnt"
|
2019-08-22 10:09:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-27 09:24:30 -04:00
|
|
|
sel int32
|
2019-08-22 10:09:55 -04:00
|
|
|
face text.Face
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.Print("Staring event loop")
|
|
|
|
go eventloop()
|
|
|
|
app.Main()
|
|
|
|
log.Print("App closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewButton(t string, lops ...gio.LabelOption) gio.Clickable {
|
2019-08-22 14:40:30 -04:00
|
|
|
lops = append([]gio.LabelOption{gio.Face(face), gio.Align(text.Middle)}, lops...)
|
2019-08-22 10:09:55 -04:00
|
|
|
lbl := gio.NewLabel(t, lops...)
|
|
|
|
bg := gio.NewBackground(
|
|
|
|
gio.Color(color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xe0}),
|
|
|
|
gio.Radius(ui.Dp(4)))
|
|
|
|
return gio.AsClickable(bg(lbl))
|
|
|
|
}
|
|
|
|
|
|
|
|
type SLabel struct {
|
2019-08-27 09:24:30 -04:00
|
|
|
w gio.Clickable
|
|
|
|
bg *gio.Background
|
2019-08-22 10:09:55 -04:00
|
|
|
active *bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSLabel(t string, active *bool, lops ...gio.LabelOption) *SLabel {
|
|
|
|
ret := &SLabel{}
|
2019-08-27 09:24:30 -04:00
|
|
|
lops = append([]gio.LabelOption{gio.Face(face)}, lops...)
|
2019-08-22 10:09:55 -04:00
|
|
|
lbl := gio.NewLabel(t, lops...)
|
|
|
|
bg := &gio.Background{
|
2019-08-27 09:24:30 -04:00
|
|
|
Color: color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xe0},
|
2019-08-22 10:09:55 -04:00
|
|
|
Radius: ui.Dp(4),
|
2019-08-27 09:24:30 -04:00
|
|
|
Inset: layout.UniformInset(ui.Dp(4)),
|
2019-08-22 10:09:55 -04:00
|
|
|
}
|
|
|
|
if *active {
|
|
|
|
bg.Color = color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xe0}
|
|
|
|
}
|
2019-08-27 09:24:30 -04:00
|
|
|
l := gio.AsClickable(gio.Enclose(bg, lbl))
|
2019-08-22 10:09:55 -04:00
|
|
|
ret.w = l
|
|
|
|
ret.bg = bg
|
|
|
|
ret.active = active
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *SLabel) Layout(ctx *gio.Context) {
|
|
|
|
if *w.active {
|
|
|
|
w.bg.Color = color.RGBA{A: 0xff, R: 0xe0, G: 0xe0, B: 0xe0}
|
|
|
|
} else {
|
|
|
|
w.bg.Color = color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xe0}
|
|
|
|
}
|
|
|
|
w.w.Layout(ctx)
|
|
|
|
if w.w.Clicked(ctx) {
|
|
|
|
if *w.active {
|
|
|
|
*w.active = false
|
|
|
|
} else {
|
|
|
|
*w.active = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCal(sm *SelectableMonth) gio.Widget {
|
|
|
|
pad := gio.NewLabel("", gio.Face(face), gio.Align(text.End))
|
2019-08-27 09:24:30 -04:00
|
|
|
ds := make([]gio.Widget, 42)
|
2019-08-22 10:09:55 -04:00
|
|
|
for i := 0; i < len(ds); i++ {
|
2019-08-27 09:24:30 -04:00
|
|
|
if i < sm.Pad || i >= sm.DaysIn+sm.Pad {
|
2019-08-22 10:09:55 -04:00
|
|
|
ds[i] = pad
|
|
|
|
} else {
|
2019-08-27 09:24:30 -04:00
|
|
|
ds[i] = NewSLabel(fmt.Sprintf("%d", i+1-sm.Pad), &sm.S[i+1-sm.Pad], gio.Align(text.End))
|
2019-08-22 10:09:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return gio.NewGrid(7)(ds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func eventloop() {
|
2019-08-22 14:40:30 -04:00
|
|
|
w := app.NewWindow(
|
|
|
|
app.WithWidth(ui.Dp(600)),
|
|
|
|
app.WithHeight(ui.Dp(600)),
|
|
|
|
app.WithTitle("Calendar"))
|
2019-08-22 10:09:55 -04:00
|
|
|
ctx := gio.NewContext(w)
|
|
|
|
sm := NewMonth(time.Now())
|
|
|
|
|
|
|
|
regular, err := sfnt.Parse(goregular.TTF)
|
|
|
|
face = ctx.Faces.For(regular, ui.Sp(20))
|
2019-08-27 09:24:30 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Cannot parse font.")
|
|
|
|
}
|
2019-08-22 10:09:55 -04:00
|
|
|
|
2019-08-22 14:40:30 -04:00
|
|
|
bg := gio.NewBackground(
|
|
|
|
gio.Color(color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xe0}))
|
|
|
|
|
2019-08-22 10:09:55 -04:00
|
|
|
margin := gio.NewInset(gio.Size(ui.Dp(10)))
|
2019-08-22 14:40:30 -04:00
|
|
|
f1 := gio.NewFlex(gio.Axis(layout.Vertical))
|
|
|
|
f2 := gio.NewFlex(gio.Axis(layout.Horizontal))
|
|
|
|
sidegrid := gio.NewGrid(1)
|
2019-08-27 09:24:30 -04:00
|
|
|
yrs := make([]gio.Widget, 6)
|
2019-08-22 14:40:30 -04:00
|
|
|
curyr := sm.Year
|
|
|
|
|
2019-08-22 10:09:55 -04:00
|
|
|
topgrid := gio.NewGrid(3)
|
|
|
|
lbtn := NewButton(" < ")
|
2019-08-22 14:40:30 -04:00
|
|
|
mth := gio.NewLabel("", gio.Face(face), gio.Align(text.Middle))
|
2019-08-22 10:09:55 -04:00
|
|
|
rbtn := NewButton(" > ")
|
|
|
|
|
|
|
|
var cal gio.Widget
|
|
|
|
|
2019-08-22 14:40:30 -04:00
|
|
|
f3 := gio.NewFlex(gio.Axis(layout.Vertical))
|
2019-08-22 10:09:55 -04:00
|
|
|
daygrid := gio.NewGrid(7)
|
2019-08-27 09:24:30 -04:00
|
|
|
sun := NewSLabel("Sun", &sm.Ws[time.Sunday], gio.Align(text.End))
|
|
|
|
mon := NewSLabel("Mon", &sm.Ws[time.Monday], gio.Align(text.End))
|
|
|
|
tue := NewSLabel("Tue", &sm.Ws[time.Tuesday], gio.Align(text.End))
|
|
|
|
wed := NewSLabel("Wed", &sm.Ws[time.Wednesday], gio.Align(text.End))
|
|
|
|
thu := NewSLabel("Thu", &sm.Ws[time.Thursday], gio.Align(text.End))
|
|
|
|
fri := NewSLabel("Fri", &sm.Ws[time.Friday], gio.Align(text.End))
|
|
|
|
sat := NewSLabel("Sat", &sm.Ws[time.Saturday], gio.Align(text.End))
|
2019-08-22 10:09:55 -04:00
|
|
|
|
|
|
|
resetCal := func() {
|
|
|
|
cal = NewCal(sm)
|
2019-08-27 09:24:30 -04:00
|
|
|
for i, _ := range yrs {
|
|
|
|
yrs[i] = NewButton(fmt.Sprintf("%d", curyr-i),
|
2019-08-22 14:40:30 -04:00
|
|
|
gio.Align(text.Start))
|
|
|
|
}
|
2019-08-22 10:09:55 -04:00
|
|
|
w.Invalidate()
|
|
|
|
}
|
|
|
|
resetCal()
|
|
|
|
|
2019-08-27 09:24:30 -04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-w.Events():
|
|
|
|
switch e := e.(type) {
|
|
|
|
case app.DestroyEvent:
|
|
|
|
return
|
|
|
|
case app.UpdateEvent:
|
2019-08-28 11:48:22 -04:00
|
|
|
ctx.Reset(&e)
|
2019-08-27 09:24:30 -04:00
|
|
|
|
|
|
|
mth.SetText(fmt.Sprintf("%s %d", sm.Month.String(), sm.Year))
|
|
|
|
|
|
|
|
ows := sm.Ws
|
|
|
|
bg(margin(f1(
|
|
|
|
topgrid(lbtn, mth, rbtn),
|
|
|
|
f2(
|
|
|
|
sidegrid(yrs...),
|
|
|
|
f3(
|
|
|
|
daygrid(sun, mon, tue, wed, thu, fri, sat),
|
|
|
|
cal,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
))).Layout(ctx)
|
|
|
|
ctx.Update()
|
|
|
|
|
|
|
|
for i := time.Sunday; i < 7; i++ {
|
|
|
|
switch {
|
|
|
|
case sm.Ws[i] && !ows[i]:
|
|
|
|
sm.SelectWeekday(i)
|
|
|
|
resetCal()
|
|
|
|
case !sm.Ws[i] && ows[i]:
|
|
|
|
sm.DeselectWeekday(i)
|
|
|
|
resetCal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lbtn.Clicked(ctx) {
|
|
|
|
sm.Previous()
|
2019-08-22 10:09:55 -04:00
|
|
|
resetCal()
|
|
|
|
}
|
2019-08-27 09:24:30 -04:00
|
|
|
if rbtn.Clicked(ctx) {
|
|
|
|
sm.Next()
|
2019-08-22 14:40:30 -04:00
|
|
|
resetCal()
|
|
|
|
}
|
2019-08-27 09:24:30 -04:00
|
|
|
for i, y := range yrs {
|
|
|
|
if y.(gio.Clickable).Clicked(ctx) {
|
|
|
|
sm.Year = curyr - i
|
|
|
|
sm.Refresh()
|
|
|
|
resetCal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sm.CheckSel()
|
2019-08-22 14:40:30 -04:00
|
|
|
}
|
2019-08-22 10:09:55 -04:00
|
|
|
}
|
2019-08-27 09:24:30 -04:00
|
|
|
}
|
2019-08-22 10:09:55 -04:00
|
|
|
}
|