diff --git a/README.md b/README.md index 61f02ac..ab86056 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ explanation of each parameter from left to right: * The initial string the block should display. (`string`) * The width of the block. (`int`) * 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. (`int`) * The foreground color of the block in hexadecimal. (`string`) diff --git a/bar.go b/bar.go index 09187fa..d6f1def 100644 --- a/bar.go +++ b/bar.go @@ -124,15 +124,26 @@ func initBar(x, y, w, h int, font string, fsize float64) (*Bar, bar.block = new(sync.Map) 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, ev xevent.ButtonPressEvent) { // Determine what block the cursor is in. // TODO: This feels a bit slow at the moment, can I improve // it? var block *Block - bar.block.Range(func(val, i interface{}) bool { + bar.block.Range(func(name, i interface{}) bool { 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( block.x+block.w) { 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 { // Calculate the required x coordinate for the different // aligments. - tw, th := xgraphics.Extents(bar.font, bar.fsize, block.txt) + tw, _ := xgraphics.Extents(bar.font, bar.fsize, block.txt) var x int switch block.align { case 'l': - x = block.x + block.xoff + x = block.x case 'c': - x = block.x + ((block.w / 2) - (tw / 2)) + block.xoff + x = block.x + ((block.w / 2) - (tw / 2)) 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: return fmt.Errorf("draw %#U: Not a valid aligment rune", block.align) } + x += block.xoff - // Color the backround. + // Color the background. block.img.For(func(cx, cy int) xgraphics.BGRA { - // Hack for music block background. - // TODO: I should handle this in `initBlock()`. + // XXX: Hack for music block. if block.w == 660 { if cx < x+block.xoff { return hexToBGRA("#445967") @@ -179,9 +192,9 @@ func (bar *Bar) draw(block *Block) error { }) // Draw the text. - // TODO: Center text vertically a bit more precisely. - if _, _, err := block.img.Text(x, (bar.h/2)-(th/2)-2, hexToBGRA( - block.fg), bar.fsize, bar.font, block.txt); err != nil { + // TODO: Center text vertically automatically. + if _, _, err := block.img.Text(x, 6, hexToBGRA(block.fg), + bar.fsize, bar.font, block.txt); err != nil { return err } diff --git a/block.go b/block.go index 069d63c..9404e38 100644 --- a/block.go +++ b/block.go @@ -15,8 +15,8 @@ type Block struct { x, w int /// The aligment of the text, this can be `l` for left aligment, - // `c` for center aligment (the default) and `r` for right - // aligment. + // `c` for center aligment, `r` for right aligment and `a` for + // absolute center aligment. align rune // Additional x offset to further tweak the location of the text. diff --git a/blocks.go b/blocks.go index 79e4537..265e67e 100644 --- a/blocks.go +++ b/blocks.go @@ -17,7 +17,7 @@ import ( ) func (bar *Bar) clockFun() { - block := bar.initBlock("clock", "?", 800, 'c', 140, "#445967", + block := bar.initBlock("clock", "?", 800, 'a', 0, "#445967", "#CCCCCC") init := true