Skip to content

Commit

Permalink
HoverTime: fix redraw problem across unknown duration
Browse files Browse the repository at this point in the history
When duration went to 0 and then came back with the same value it had
previously (e.g. playing the same video twice in a row), the time
would change to ???? and not change back to a timestamp until the
mouse was moved. This happened because the computed hover timestamp
did not get updated during the portion of unknown duration, so the
value was cached too aggressively.

By decoupling the positioning logic from the timestamp logic, the
intent becomes more obvious and hopefully less bug-prone.
  • Loading branch information
torque committed Nov 6, 2022
1 parent 7459d8a commit 67683dc
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/HoverTime.moon
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,40 @@ class HoverTime extends BarAccent
@line[2] = ("%g,%g")\format clamp( Mouse.x, leftMargin, Window.w - rightMargin ), @position
@needsUpdate = true

_setXPosition: (x) =>
@line[2] = ("%g,%g")\format clamp( x, leftMargin, Window.w - rightMargin ), @position
@needsUpdate = true

_setUnknownDuration: =>
@line[4] = "????"
@needsUpdate = true

_setTime: ( hoverTime ) =>
@line[4] = ([[%d:%02d:%02d]])\format(
math.floor( hoverTime / 3600 ),
math.floor( (hoverTime / 60) % 60 ),
math.floor( hoverTime % 60 )
)
@needsUpdate = true

redraw: =>
if @active
super!

duration = mp.get_property_number( 'duration', 0 )
hoverTime = duration * Mouse.x / Window.w

if Mouse.x != @lastX or duration != @lastDuration
@lastDuration = duration

@line[2] = ("%g,%g")\format clamp( Mouse.x, leftMargin, Window.w - rightMargin ), @position
if Mouse.x != @lastX
@lastX = Mouse.x
@_setXPosition( Mouse.x )

if duration != @lastDuration or hoverTime != @lastTime
@lastDuration = duration
@lastTime = hoverTime

if duration == 0
@line[4] = "????"
@_setUnknownDuration!
else
hoverTime = duration * Mouse.x / Window.w
if hoverTime != @lastTime
@line[4] = ([[%d:%02d:%02d]])\format math.floor( hoverTime / 3600 ), math.floor( (hoverTime / 60) % 60 ), math.floor( hoverTime % 60 )
@lastTime = hoverTime

@needsUpdate = true
@_setTime( hoverTime )

return @needsUpdate

0 comments on commit 67683dc

Please sign in to comment.