Clean up comments a bit.
This commit is contained in:
parent
36cbfd3d3f
commit
21173205a3
52
bar.go
52
bar.go
@ -18,14 +18,14 @@ import (
|
|||||||
|
|
||||||
// Bar is a struct with information about the bar.
|
// Bar is a struct with information about the bar.
|
||||||
type Bar struct {
|
type Bar struct {
|
||||||
// Connection to the X server, the abe window, and the bar image.
|
// Connection to the X server, the bar window, and the bar image.
|
||||||
xu *xgbutil.XUtil
|
xu *xgbutil.XUtil
|
||||||
win *xwindow.Window
|
win *xwindow.Window
|
||||||
img *xgraphics.Image
|
img *xgraphics.Image
|
||||||
|
|
||||||
// The font and fontsize that should be used.
|
// The font and fontsize that should be used.
|
||||||
font *truetype.Font
|
font *truetype.Font
|
||||||
fontSize float64
|
fsize float64
|
||||||
|
|
||||||
// The width and height of the bar.
|
// The width and height of the bar.
|
||||||
w, h int
|
w, h int
|
||||||
@ -34,35 +34,38 @@ type Bar struct {
|
|||||||
// to the right of the last block.
|
// to the right of the last block.
|
||||||
xsum int
|
xsum int
|
||||||
|
|
||||||
// A map with information about the block, see `Block`.
|
// A map with information about the block, see the `Block` type.
|
||||||
block *sync.Map
|
block *sync.Map
|
||||||
|
|
||||||
// The channel where the block name should be send to to once it's
|
// A channel where the block should be send to to once its ready
|
||||||
// ready to be redrawn.
|
// to be redrawn.
|
||||||
redraw chan *Block
|
redraw chan *Block
|
||||||
}
|
}
|
||||||
|
|
||||||
func initBar(x, y, w, h int, font string, fontSize float64) (*Bar,
|
func initBar(x, y, w, h int, font string, fsize float64) (*Bar,
|
||||||
error) {
|
error) {
|
||||||
bar := new(Bar)
|
bar := new(Bar)
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Connect to X.
|
// Set up a connection to the X server.
|
||||||
bar.xu, err = xgbutil.NewConn()
|
bar.xu, err = xgbutil.NewConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run the main X event loop, this is uses to catch events.
|
||||||
go xevent.Main(bar.xu)
|
go xevent.Main(bar.xu)
|
||||||
|
|
||||||
// Listen to the root window for property change event, used to
|
// Listen to the root window for property change events, used to
|
||||||
// check if the user changed the focused window or active
|
// check if the user changed the focused window or active
|
||||||
// workspace.
|
// workspace for example.
|
||||||
if err := xwindow.New(bar.xu, bar.xu.RootWin()).Listen(
|
if err := xwindow.New(bar.xu, bar.xu.RootWin()).Listen(
|
||||||
xproto.EventMaskPropertyChange); err != nil {
|
xproto.EventMaskPropertyChange); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a window.
|
// Create a window for the bar. This window listens to button
|
||||||
|
// press events in order to respond to them.
|
||||||
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
|
||||||
@ -70,10 +73,10 @@ func initBar(x, y, w, h int, font string, fontSize float64) (*Bar,
|
|||||||
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, 0x000000, xproto.EventMaskButtonPress)
|
xproto.CwEventMask, 0x000000, xproto.EventMaskButtonPress)
|
||||||
|
|
||||||
|
// EWMH stuff to make the window behave like an actuale bar.
|
||||||
// TODO: `WmStateSet` and `WmDesktopSet` are basically here to
|
// TODO: `WmStateSet` and `WmDesktopSet` are basically here to
|
||||||
// keep OpenBox happy, can I somehow remove them and just use
|
// keep OpenBox happy, can I somehow remove them and just use
|
||||||
// `_NET_WM_WINDOW_TYPE_DOCK`?
|
// `_NET_WM_WINDOW_TYPE_DOCK` like I can with WindowChef?
|
||||||
// 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
|
||||||
@ -91,19 +94,20 @@ func initBar(x, y, w, h int, font string, fontSize float64) (*Bar,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Moving the window is again a hack to keep OpenBox happy.
|
|
||||||
// Map window.
|
// Map window.
|
||||||
bar.win.Map()
|
bar.win.Map()
|
||||||
|
|
||||||
|
// TODO: Moving the window is again a hack to keep OpenBox happy.
|
||||||
bar.win.Move(x, y)
|
bar.win.Move(x, y)
|
||||||
|
|
||||||
// Create bar image.
|
// Create the 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()
|
bar.img.XDraw()
|
||||||
|
|
||||||
|
// Load font.
|
||||||
// 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.
|
||||||
// Load font.
|
|
||||||
f, err := os.Open(font)
|
f, err := os.Open(font)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -112,7 +116,7 @@ func initBar(x, y, w, h int, font string, fontSize float64) (*Bar,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
bar.fontSize = fontSize
|
bar.fsize = fsize
|
||||||
|
|
||||||
bar.w = w
|
bar.w = w
|
||||||
bar.h = h
|
bar.h = h
|
||||||
@ -120,10 +124,12 @@ func initBar(x, y, w, h int, font string, fontSize 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 requires function.
|
// Listen to mouse events and execute the requires 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
|
||||||
|
// it?
|
||||||
var block *Block
|
var block *Block
|
||||||
bar.block.Range(func(val, i interface{}) bool {
|
bar.block.Range(func(val, i interface{}) bool {
|
||||||
block = i.(*Block)
|
block = i.(*Block)
|
||||||
@ -144,7 +150,7 @@ func initBar(x, y, w, h int, font string, fontSize 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, _ := xgraphics.Extents(bar.font, bar.fontSize, block.txt)
|
tw, th := xgraphics.Extents(bar.font, bar.fsize, block.txt)
|
||||||
var x int
|
var x int
|
||||||
switch block.align {
|
switch block.align {
|
||||||
case 'l':
|
case 'l':
|
||||||
@ -160,8 +166,8 @@ func (bar *Bar) draw(block *Block) error {
|
|||||||
|
|
||||||
// Color the backround.
|
// Color the backround.
|
||||||
block.img.For(func(cx, cy int) xgraphics.BGRA {
|
block.img.For(func(cx, cy int) xgraphics.BGRA {
|
||||||
// TODO: Should I handle this in `initBlock()`?
|
|
||||||
// Hack for music block background.
|
// Hack for music block background.
|
||||||
|
// 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")
|
||||||
@ -172,10 +178,10 @@ func (bar *Bar) draw(block *Block) error {
|
|||||||
return hexToBGRA(block.bg)
|
return hexToBGRA(block.bg)
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: Center vertically automatically.
|
|
||||||
// Draw the text.
|
// Draw the text.
|
||||||
if _, _, err := block.img.Text(x, 6, hexToBGRA(block.fg),
|
// TODO: Center text vertically a bit more precisely.
|
||||||
bar.fontSize, bar.font, block.txt); err != nil {
|
if _, _, err := block.img.Text(x, (bar.h/2)-(th/2)-2, hexToBGRA(
|
||||||
|
block.fg), bar.fsize, bar.font, block.txt); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
7
block.go
7
block.go
@ -11,7 +11,7 @@ type Block struct {
|
|||||||
// The text the block should display.
|
// The text the block should display.
|
||||||
txt string
|
txt string
|
||||||
|
|
||||||
// The x coordinate and width of the bar.
|
// The x coordinate and width of the block.
|
||||||
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,
|
||||||
@ -22,10 +22,11 @@ type Block struct {
|
|||||||
// Additional x offset to further tweak the location of the text.
|
// Additional x offset to further tweak the location of the text.
|
||||||
xoff int
|
xoff int
|
||||||
|
|
||||||
// The foreground and background colors.
|
// The foreground and background colors in hex.
|
||||||
bg, fg string
|
bg, fg string
|
||||||
|
|
||||||
// Commands to execute on button events.
|
// A map with functions to execute on button events. Accepted
|
||||||
|
// button strings are `button0` to `button5`
|
||||||
actions map[string]func()
|
actions map[string]func()
|
||||||
|
|
||||||
// The sub-image that represents the block.
|
// The sub-image that represents the block.
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (bar *Bar) clockFun() {
|
func (bar *Bar) clockFun() {
|
||||||
block := bar.initBlock("clock", "?", 800, 'c', 300, "#445967",
|
block := bar.initBlock("clock", "?", 800, 'c', 140, "#445967",
|
||||||
"#CCCCCC")
|
"#CCCCCC")
|
||||||
|
|
||||||
init := true
|
init := true
|
||||||
|
Loading…
Reference in New Issue
Block a user