hbspbar

[WIP] bspwm status bar
git clone https://hhvn.uk/hbspbar
git clone git://hhvn.uk/hbspbar
Log | Files | Refs

x.go (1593B)


      1 package bar // import "hhvn.uk/hbspbar/bar"
      2 
      3 import (
      4 	"os"
      5 	"image"
      6 
      7 	"hhvn.uk/hbspbar/common"
      8 
      9 	"github.com/jezek/xgbutil/xgraphics"
     10 	"github.com/BurntSushi/freetype-go/freetype/truetype"
     11 )
     12 
     13 func getFont(path string) (*truetype.Font, error) {
     14 	read, err := os.Open(path)
     15 	if err != nil {
     16 		return nil, common.Perror("os.Open", err)
     17 	}
     18 
     19 	font, err := xgraphics.ParseFont(read)
     20 	if err != nil {
     21 		return nil, common.Perror("xgraphics.ParseFont", err)
     22 	}
     23 
     24 	return font, nil
     25 }
     26 
     27 func int2BGRA(argb uint32) (xgraphics.BGRA) {
     28 	return xgraphics.BGRA{
     29 		B: uint8( argb & 0x000000ff),
     30 		G: uint8((argb & 0x0000ff00) >> 8),
     31 		R: uint8((argb & 0x00ff0000) >> 16),
     32 		A: uint8((argb & 0xff000000) >> 24) }
     33 }
     34 
     35 func (b *bar) rect() image.Rectangle {
     36 	return image.Rect( 0, 0,
     37 		int(b.Mon.Rectangle.Width), int(conf.H))
     38 }
     39 
     40 func (b *bar) drawReInit() {
     41 	if b.i != nil {
     42 		b.i.Destroy()
     43 	}
     44 
     45 	b.i = xgraphics.New(b.Xu, b.rect())
     46 }
     47 
     48 func (b *bar) drawText(x int, col uint32, text string) (int, error) {
     49 	nx, _, err := b.i.Text(x, conf.FontYPad,
     50 		int2BGRA(col), conf.FontSize, font, text)
     51 	return nx - x, err
     52 }
     53 
     54 func (b *bar) textWidth(str string) int {
     55 	return len(str) * b.tw
     56 }
     57 
     58 func pointInRect(px, py, x, y, w, h int) bool {
     59 	if px >= x && px <= x + w && py >= x && py <= y + h {
     60 		return true
     61 	} else {
     62 		return false
     63 	}
     64 }
     65 
     66 func (b *bar) drawRect(x, y, w, h int, c uint32, fill bool) {
     67 	col := int2BGRA(c)
     68 
     69 	var ix, iy int
     70 
     71 	for ix = x; ix < x + w; ix++ {
     72 		for iy = y; iy < y + h; iy++ {
     73 			if fill || ix == x || ix == x + w - 1 ||
     74 					iy == y || iy == y + h - 1 {
     75 				b.i.Set(ix, iy, col)
     76 			}
     77 		}
     78 	}
     79 }