Minor simplifications when redrawing the bar

This commit is contained in:
Camille Scholtz 2017-07-11 21:58:23 +02:00
parent e109215a0b
commit 0e0e4f0f55
3 changed files with 37 additions and 37 deletions

@ -19,7 +19,7 @@ Users can modify, configure and create new "blocks" in `blocks.go`.
and configure the position, width and font of the bar in `main.go`.
## CREATING A BLOCK FUNCTIONS
## CREATING BLOCK FUNCTIONS
On top of each block function you should run `bar.initBlock()`, this
is where most of the configuration happens. Here is a short
@ -54,6 +54,7 @@ for {
// Things you only want to do after the first loop.
}
init = false
...
```
@ -76,10 +77,6 @@ When you've gathered all needed information you can:
this function takes two parameters, the second one being the new
string the block should display.
Agter having ran the required `updateBlock*` functions you must run
`bar.redraw <- "blockname"`, where blockname is the name of the block
map key.
## AUTHORS

41
bar.go

@ -72,26 +72,32 @@ func initBar(x, y, w, h int, font string, fontSize float64) (*Bar,
if err != nil {
return nil, err
}
// TODO: Is this needed when I have a bar?
// Listen to the root window for events.
xwindow.New(bar.xu, bar.xu.RootWin()).Listen(
xproto.EventMaskPropertyChange)
// Crate and map window.
// Create a window.
bar.win, err = xwindow.Generate(bar.xu)
if err != nil {
return nil, err
}
bar.win.Create(bar.xu.RootWin(), x, y, w, h, xproto.CwBackPixel|
xproto.CwEventMask, 0xEEEEEE, xproto.EventMaskPropertyChange)
bar.win.Create(bar.xu.RootWin(), x, y, w, h, xproto.CwBackPixel,
0x000000)
// EWMH stuff.
if err := ewmh.WmWindowTypeSet(bar.xu, bar.win.Id, []string{
"_NET_WM_WINDOW_TYPE_DOCK"}); err != nil {
return nil, err
}
// Map window.
bar.win.Map()
// Create bar image.
bar.img = xgraphics.New(bar.xu, image.Rect(0, 0, w, h))
bar.img.XSurfaceSet(bar.win.Id)
bar.img.XDraw()
// TODO: I don't *really* want to use `ttf` fonts but there
// doesn't seem to be any `pcf` Go library at the moment.
@ -127,21 +133,41 @@ func (bar *Bar) updateBlockBg(name, bg string) {
i, _ := bar.block.Load(name)
block := i.(*Block)
block.bg = hexToBGRA(bg)
nbg := hexToBGRA(bg)
if block.bg == nbg {
return
}
block.bg = nbg
bar.redraw <- name
return
}
func (bar *Bar) updateBlockFg(name, fg string) {
i, _ := bar.block.Load(name)
block := i.(*Block)
block.fg = hexToBGRA(fg)
nfg := hexToBGRA(fg)
if block.fg == nfg {
return
}
block.fg = nfg
bar.redraw <- name
return
}
func (bar *Bar) updateBlockTxt(name, txt string) {
i, _ := bar.block.Load(name)
block := i.(*Block)
if block.txt == txt {
return
}
block.txt = txt
bar.redraw <- name
return
}
func (bar *Bar) draw(name string) error {
@ -153,6 +179,7 @@ func (bar *Bar) draw(name string) error {
block := i.(*Block)
// Color the backround.
tw, _ := xgraphics.Extents(bar.font, bar.fontSize, block.txt)
block.img.For(func(x, y int) xgraphics.BGRA {
return block.bg
})
@ -164,10 +191,8 @@ func (bar *Bar) draw(name string) error {
case 'l':
x = block.x + block.xoff
case 'c':
tw, _ := xgraphics.Extents(bar.font, bar.fontSize, block.txt)
x = block.x + ((block.w / 2) - (tw / 2)) + block.xoff
case 'r':
tw, _ := xgraphics.Extents(bar.font, bar.fontSize, block.txt)
x = (block.x + block.w) - tw + block.xoff
}

@ -28,8 +28,7 @@ func (bar *Bar) clockFun() {
t := time.Now()
bar.updateBlockTxt("clock", t.Format(
"Monday, January 2th 02:04 PM"))
bar.redraw <- "clock"
"Monday, January 2th 03:04 PM"))
}
}
@ -70,15 +69,12 @@ func (bar *Bar) musicFun() error {
continue
}
var state string
if s["state"] == "play" {
state = "[playing] "
} else {
if s["state"] == "pause" {
state = "[paused] "
}
bar.updateBlockTxt("music", state+cur["Artist"]+" - "+
cur["Title"])
bar.redraw <- "music"
}
}
@ -119,7 +115,6 @@ func (bar *Bar) todoFun() {
}
bar.updateBlockTxt("todo", strconv.Itoa(c))
bar.redraw <- "todo"
}
}
@ -169,7 +164,6 @@ func (bar *Bar) weatherFun() {
bar.updateBlockTxt("weather", strconv.FormatFloat(w.Main.Temp,
'f', 0, 64)+" °C")
bar.redraw <- "weather"
}
}
@ -177,7 +171,6 @@ func (bar *Bar) windowFun() {
bar.initBlock("window", "?", 220, 'c', 0, "#37BF8D", "#FFFFFF")
init := true
var Owin string
for {
if !init {
ev, xgbErr := bar.xu.Conn().WaitForEvent()
@ -202,19 +195,13 @@ func (bar *Bar) windowFun() {
log.Print(err)
continue
}
win, err := ewmh.WmNameGet(bar.xu, id)
if err != nil {
log.Print(err)
continue
}
if Owin == win {
continue
}
Owin = win
bar.updateBlockTxt("window", win)
bar.redraw <- "window"
}
}
@ -224,7 +211,6 @@ func (bar *Bar) workspaceFun() {
bar.initBlock("src", "src", 70, 'c', 0, "#5394C9", "#FFFFFF")
init := true
var Owsp uint
for {
if !init {
ev, xgbErr := bar.xu.Conn().WaitForEvent()
@ -250,11 +236,6 @@ func (bar *Bar) workspaceFun() {
continue
}
if Owsp == wsp {
continue
}
Owsp = wsp
switch wsp {
case 0:
bar.updateBlockBg("www", "#72A7D3")
@ -269,8 +250,5 @@ func (bar *Bar) workspaceFun() {
bar.updateBlockBg("irc", "#5394C9")
bar.updateBlockBg("src", "#72A7D3")
}
bar.redraw <- "www"
bar.redraw <- "irc"
bar.redraw <- "src"
}
}