-
Notifications
You must be signed in to change notification settings - Fork 2
/
clock.go
74 lines (56 loc) · 1.33 KB
/
clock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"image"
"time"
"code.google.com/p/jamslam-freetype-go/freetype/truetype"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/xgraphics"
"github.com/BurntSushi/xgbutil/xwindow"
"github.com/AmandaCameron/gobar/utils"
)
type Clock struct {
Background xgraphics.BGRA
Foreground xgraphics.BGRA
Width int
Height int
Position int
Font *truetype.Font
FontSize float64
Format string
X *xgbutil.XUtil
Parent *xwindow.Window
img *xgraphics.Image
window *xwindow.Window
}
func (c *Clock) Init() {
var err error
c.img = xgraphics.New(c.X, image.Rect(0, 0, c.Width, c.Height))
c.window, err = xwindow.Create(c.X, c.Parent.Id)
utils.FailMeMaybe(err)
c.window.Resize(c.Width, c.Height)
c.window.Move(c.Position, 0)
c.img.XSurfaceSet(c.window.Id)
c.window.Map()
c.Draw()
go c.tickTock()
}
func (c *Clock) Draw() {
c.img.For(func(x, y int) xgraphics.BGRA {
return c.Background
})
now := time.Now().Format(c.Format)
w, h := xgraphics.Extents(c.Font, c.FontSize, now)
//println("OH MYYYY: ", w, h)
c.img.Text((c.Width/2)-(w/2), (c.Height/2)-(h/2), c.Foreground, c.FontSize, c.Font, now)
c.img.XDraw()
c.img.XPaint(c.window.Id)
}
func (c *Clock) tickTock() {
ticker := time.Tick(1 * time.Second)
for {
select {
case <-ticker:
c.Draw()
}
}
}