Add some more music block hacks, add absolute aligment

This commit is contained in:
Camille Scholtz 2017-07-13 23:06:10 +02:00
parent 21173205a3
commit a9daf23fbe
4 changed files with 30 additions and 16 deletions

@ -32,7 +32,8 @@ explanation of each parameter from left to right:
* The initial string the block should display. (`string`) * The initial string the block should display. (`string`)
* The width of the block. (`int`) * The width of the block. (`int`)
* The aligment of the text, this can be `'l'` for left aligment, `'c'` * The aligment of the text, this can be `'l'` for left aligment, `'c'`
for center aligment and `'r'` for right aligment. (`rune`) for center aligment `'r'` for right aligment and `'a'` for absolute
center aligment. (`rune`)
* Additional x offset to further tweak the location of the text. * Additional x offset to further tweak the location of the text.
(`int`) (`int`)
* The foreground color of the block in hexadecimal. (`string`) * The foreground color of the block in hexadecimal. (`string`)

37
bar.go

@ -124,15 +124,26 @@ func initBar(x, y, w, h int, font string, fsize float64) (*Bar,
bar.block = new(sync.Map) bar.block = new(sync.Map)
bar.redraw = make(chan *Block) bar.redraw = make(chan *Block)
// Listen to mouse events and execute the requires function. // Listen to mouse events and execute the required function.
xevent.ButtonPressFun(func(_ *xgbutil.XUtil, xevent.ButtonPressFun(func(_ *xgbutil.XUtil,
ev xevent.ButtonPressEvent) { ev xevent.ButtonPressEvent) {
// Determine what block the cursor is in. // Determine what block the cursor is in.
// TODO: This feels a bit slow at the moment, can I improve // TODO: This feels a bit slow at the moment, can I improve
// it? // it?
var block *Block var block *Block
bar.block.Range(func(val, i interface{}) bool { bar.block.Range(func(name, i interface{}) bool {
block = i.(*Block) block = i.(*Block)
// XXX: Hack for music block.
if name == "music" {
tw, _ := xgraphics.Extents(bar.font, bar.fsize,
block.txt)
if ev.EventX > int16(block.x+(block.w-tw+(block.xoff*
2))) && ev.EventX < int16(block.x+block.w) {
return false
}
return true
}
if ev.EventX > int16(block.x) && ev.EventX < int16( if ev.EventX > int16(block.x) && ev.EventX < int16(
block.x+block.w) { block.x+block.w) {
return false return false
@ -150,24 +161,26 @@ func initBar(x, y, w, h int, font string, fsize float64) (*Bar,
func (bar *Bar) draw(block *Block) error { func (bar *Bar) draw(block *Block) error {
// Calculate the required x coordinate for the different // Calculate the required x coordinate for the different
// aligments. // aligments.
tw, th := xgraphics.Extents(bar.font, bar.fsize, block.txt) tw, _ := xgraphics.Extents(bar.font, bar.fsize, block.txt)
var x int var x int
switch block.align { switch block.align {
case 'l': case 'l':
x = block.x + block.xoff x = block.x
case 'c': case 'c':
x = block.x + ((block.w / 2) - (tw / 2)) + block.xoff x = block.x + ((block.w / 2) - (tw / 2))
case 'r': case 'r':
x = (block.x + block.w) - tw + block.xoff x = (block.x + block.w) - tw
case 'a':
x = (bar.w / 2) - (tw / 2)
default: default:
return fmt.Errorf("draw %#U: Not a valid aligment rune", return fmt.Errorf("draw %#U: Not a valid aligment rune",
block.align) block.align)
} }
x += block.xoff
// Color the backround. // Color the background.
block.img.For(func(cx, cy int) xgraphics.BGRA { block.img.For(func(cx, cy int) xgraphics.BGRA {
// Hack for music block background. // XXX: Hack for music block.
// TODO: I should handle this in `initBlock()`.
if block.w == 660 { if block.w == 660 {
if cx < x+block.xoff { if cx < x+block.xoff {
return hexToBGRA("#445967") return hexToBGRA("#445967")
@ -179,9 +192,9 @@ func (bar *Bar) draw(block *Block) error {
}) })
// Draw the text. // Draw the text.
// TODO: Center text vertically a bit more precisely. // TODO: Center text vertically automatically.
if _, _, err := block.img.Text(x, (bar.h/2)-(th/2)-2, hexToBGRA( if _, _, err := block.img.Text(x, 6, hexToBGRA(block.fg),
block.fg), bar.fsize, bar.font, block.txt); err != nil { bar.fsize, bar.font, block.txt); err != nil {
return err return err
} }

@ -15,8 +15,8 @@ type Block struct {
x, w int x, w int
/// The aligment of the text, this can be `l` for left aligment, /// The aligment of the text, this can be `l` for left aligment,
// `c` for center aligment (the default) and `r` for right // `c` for center aligment, `r` for right aligment and `a` for
// aligment. // absolute center aligment.
align rune align rune
// Additional x offset to further tweak the location of the text. // Additional x offset to further tweak the location of the text.

@ -17,7 +17,7 @@ import (
) )
func (bar *Bar) clockFun() { func (bar *Bar) clockFun() {
block := bar.initBlock("clock", "?", 800, 'c', 140, "#445967", block := bar.initBlock("clock", "?", 800, 'a', 0, "#445967",
"#CCCCCC") "#CCCCCC")
init := true init := true