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 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`)

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.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
}

@ -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.

@ -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