fix UI weirdness when waveform image is being updated

This commit is contained in:
Drew Weymouth
2025-08-01 12:50:01 -07:00
parent 5209c08444
commit b9df60bdcd
2 changed files with 21 additions and 16 deletions
+1 -1
View File
@@ -269,7 +269,7 @@ func (p *playbackEngine) PlaybackStatus() PlaybackStatus {
return PlaybackStatus{ return PlaybackStatus{
State: stat.State, State: stat.State,
TimePos: stat.TimePos, TimePos: stat.TimePos,
Duration: stat.Duration, Duration: p.curTrackDuration,
} }
} }
+20 -15
View File
@@ -41,16 +41,16 @@ func NewWaveformSeekbar() *WaveformSeekbar {
} }
func (w *WaveformSeekbar) UpdateImage(img *backend.WaveformImage) { func (w *WaveformSeekbar) UpdateImage(img *backend.WaveformImage) {
w.img.Image = img
prm, fg := w.getThemeColors() prm, fg := w.getThemeColors()
w.recolorImage(prm, fg, w.imgProgressPixel) recolorWaveformImage(img, prm, fg, 0, w.imgProgressPixel, true)
w.img.Image = img
w.img.Refresh() w.img.Refresh()
} }
func (w *WaveformSeekbar) Refresh() { func (w *WaveformSeekbar) Refresh() {
w.cursor.Resize(fyne.NewSize(1, w.Size().Height-4)) w.cursor.Resize(fyne.NewSize(1, w.Size().Height-4))
prm, fg := w.getThemeColors() prm, fg := w.getThemeColors()
w.recolorImage(prm, fg, w.imgProgressPixel) w.updateImageProgress(prm, fg, w.imgProgressPixel)
w.recolorCursor(prm, fg, w.cursor.Position().X) w.recolorCursor(prm, fg, w.cursor.Position().X)
w.BaseWidget.Refresh() w.BaseWidget.Refresh()
} }
@@ -97,7 +97,7 @@ func (w *WaveformSeekbar) CreateRenderer() fyne.WidgetRenderer {
func (w *WaveformSeekbar) SetProgress(v float64) { func (w *WaveformSeekbar) SetProgress(v float64) {
prm, fg := w.getThemeColors() prm, fg := w.getThemeColors()
thresholdPixel := int(math.Round(1024.0 /*pixel width of waveform*/ * v)) thresholdPixel := int(math.Round(1024.0 /*pixel width of waveform*/ * v))
if w.recolorImage(prm, fg, thresholdPixel) { if w.updateImageProgress(prm, fg, thresholdPixel) {
w.img.Refresh() w.img.Refresh()
} }
} }
@@ -119,7 +119,7 @@ func (w *WaveformSeekbar) recolorCursor(prm, fg color.Color, posX float32) {
} }
} }
func (w *WaveformSeekbar) recolorImage(cL, cR color.Color, progress int) (updated bool) { func (w *WaveformSeekbar) updateImageProgress(cL, cR color.Color, progress int) (updated bool) {
if w.img.Image == nil { if w.img.Image == nil {
return false return false
} }
@@ -127,29 +127,34 @@ func (w *WaveformSeekbar) recolorImage(cL, cR color.Color, progress int) (update
return false return false
} }
img := w.img.Image.(*image.NRGBA)
recolorWaveformImage(img, cL, cR, w.imgProgressPixel, progress, false)
w.imgColorL, w.imgColorR = cL, cR
w.imgProgressPixel = progress
return true
}
func recolorWaveformImage(img *image.NRGBA, cL, cR color.Color, oldProgress, newProgress int, fullRecolor bool) {
_r, _g, _b, _ := cL.RGBA() _r, _g, _b, _ := cL.RGBA()
rL, gL, bL := byte(_r>>8), byte(_g>>8), byte(_b>>8) rL, gL, bL := byte(_r>>8), byte(_g>>8), byte(_b>>8)
_r, _g, _b, _ = cR.RGBA() _r, _g, _b, _ = cR.RGBA()
rR, gR, bR := byte(_r>>8), byte(_g>>8), byte(_b>>8) rR, gR, bR := byte(_r>>8), byte(_g>>8), byte(_b>>8)
// TODO- smartly figure out which pixels we need
// to update for different scenarios (e.g. progress update only)
// and not iterate the whole thing every time
img := w.img.Image.(*image.NRGBA)
bnds := img.Rect.Bounds() bnds := img.Rect.Bounds()
for x := 0; x < bnds.Dx(); x++ { xMin, xMax := 0, bnds.Dx()
if !fullRecolor {
xMin = min(oldProgress, newProgress)
xMax = max(oldProgress, newProgress)
}
for x := xMin; x < xMax; x++ {
for y := 0; y < bnds.Dy(); y++ { for y := 0; y < bnds.Dy(); y++ {
if x < progress { if x < newProgress {
setPixelRGB(img, x, y, rL, gL, bL) setPixelRGB(img, x, y, rL, gL, bL)
} else { } else {
setPixelRGB(img, x, y, rR, gR, bR) setPixelRGB(img, x, y, rR, gR, bR)
} }
} }
} }
w.imgColorL, w.imgColorR = cL, cR
w.imgProgressPixel = progress
return true
} }
func setPixelRGB(img *image.NRGBA, x, y int, r, g, b byte) { func setPixelRGB(img *image.NRGBA, x, y int, r, g, b byte) {