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`. 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 On top of each block function you should run `bar.initBlock()`, this
is where most of the configuration happens. Here is a short 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. // Things you only want to do after the first loop.
} }
init = false 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 this function takes two parameters, the second one being the new
string the block should display. 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 ## AUTHORS

41
bar.go

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

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