From 894a02cafb61e9fe504663d6a7321644b7cf0345 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 28 Jan 2016 00:09:57 +0100 Subject: nicer way for meter style diff --git a/src/rhlibrary/vumeter_widget.go b/src/rhlibrary/vumeter_widget.go index 323e826..35586fa 100644 --- a/src/rhlibrary/vumeter_widget.go +++ b/src/rhlibrary/vumeter_widget.go @@ -41,13 +41,17 @@ const ( ) var ( - meterBarCSS *gtk.CssProvider + meterBarCSS *gtk.CssProvider = nil ) -func init() { +func getMeterBarCSS() (*gtk.CssProvider, error) { + if meterBarCSS != nil { + return meterBarCSS, nil + } + var err error if meterBarCSS, err = gtk.CssProviderNew(); err != nil { - panic(err.Error()) + return nil, err } err = meterBarCSS.LoadFromData(` .level-bar { @@ -73,8 +77,9 @@ func init() { background-image: linear-gradient(to bottom, #FF3636, #AE0000); }`) if err != nil { - panic(err.Error()) + return nil, err } + return meterBarCSS, nil } type vuMeterLevel struct { @@ -121,7 +126,12 @@ func createMeterBar(segments float64) (bar *gtk.LevelBar, err error) { if sc, err = bar.GetStyleContext(); err != nil { return } - sc.AddProvider(meterBarCSS, 600) // TOOD: hardcoded value + + var cp *gtk.CssProvider + if cp, err = getMeterBarCSS(); err != nil { + return + } + sc.AddProvider(cp, 600) // TOOD: hardcoded value return } @@ -150,6 +160,21 @@ func (m *vuMeter) SetValue(lvl *vuMeterLevel) { m.red.SetValue(lvl.red) } +func updateVUMeterLevels(left, right *vuMeter, data player.Meter) { + ch := len(data) + lval := &vuMeterLevel{0, 0, 0} + rval := &vuMeterLevel{0, 0, 0} + if ch >= 2 { + lval.setLevel(data[0].Peak) + rval.setLevel(data[1].Peak) + } else if ch == 1 { + lval.setLevel(data[0].Peak) + rval = lval + } + left.SetValue(lval) + right.SetValue(rval) +} + func addVUMeterGrid(frame *gtk.Frame, p *player.PlayerChan) (err error) { var grid *gtk.Grid if grid, err = gtk.GridNew(); err != nil { @@ -166,20 +191,7 @@ func addVUMeterGrid(frame *gtk.Frame, p *player.PlayerChan) (err error) { } p.AddUpdateHandler(func(length time.Duration, pos time.Duration, meter player.Meter, userdata interface{}) bool { - glib.IdleAdd(func() { - ch := len(meter) - lval := &vuMeterLevel{0, 0, 0} - rval := &vuMeterLevel{0, 0, 0} - if ch >= 2 { - lval.setLevel(meter[0].Peak) - rval.setLevel(meter[1].Peak) - } else if ch == 1 { - lval.setLevel(meter[0].Peak) - rval = lval - } - left.SetValue(lval) - right.SetValue(rval) - }) + glib.IdleAdd(func() { updateVUMeterLevels(left, right, meter) }) return true }, nil) -- cgit v0.10.2