Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thumbnail: thumbfast integration #62

Merged
merged 12 commits into from
Nov 6, 2022
54 changes: 18 additions & 36 deletions src/Thumbnail.moon
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,40 @@ class Thumbnail extends BarAccent
leftMargin = settings['thumbnail-left-margin']
bottomMargin = settings['thumbnail-bottom-margin']

thumbfast = {
width: 0,
height: 0,
disabled: false
}

new: =>
new: ( thumbfastInfo ) =>
super!

@line = {}
@lastX = -1
@animation = Animation bottomMargin, bottomMargin, @animationDuration, @\animate, nil, 0.5

mp.register_script_message('thumbfast-info', (json) ->
data = utils.parse_json(json)
if type(data) ~= 'table' or not data.width or not data.height then
msg.error('thumbfast-info: received json didn\'t produce a table with thumbnail information')
else
thumbfast = data
)
@thumbfast = thumbfastInfo

reconfigure: =>
super!
rightMargin = settings['thumbnail-right-margin']
leftMargin = settings['thumbnail-left-margin']
bottomMargin = settings['thumbnail-bottom-margin']
@animation = Animation bottomMargin, bottomMargin, @animationDuration, @\animate, nil, 0.5

animate: ( value ) =>
@needsUpdate = true

if @active and Mouse.x != @lastX
@lastX = Mouse.x
if not thumbfast.disabled and thumbfast.width ~= 0 and thumbfast.height ~= 0
hoverTime = mp.get_property_number( 'duration', 0 )*Mouse.x/Window.w
mp.commandv( 'script-message-to', 'thumbfast', 'thumb',
hoverTime,
math.min(Window.w - thumbfast.width - 10, math.max(10, Mouse.x - thumbfast.width / 2)),
Window.h - bottomMargin - thumbfast.height
)
activate: ( activate ) =>
super activate
if not activate
mp.commandv( 'script-message-to', 'thumbfast', 'clear' )
@needsUpdate = true

redraw: =>
if @active
super!
if not thumbfast.disabled and thumbfast.width ~= 0 and thumbfast.height ~= 0
hoverTime = mp.get_property_number( 'duration', 0 )*Mouse.x/Window.w
mp.commandv( 'script-message-to', 'thumbfast', 'thumb',
if Mouse.x != @lastX and not @thumbfast.disabled and @thumbfast.width >= 0 and @thumbfast.height >= 0
torque marked this conversation as resolved.
Show resolved Hide resolved
@lastX = Mouse.x

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

mp.commandv(
'script-message-to', 'thumbfast', 'thumb',
hoverTime,
math.min(Window.w - thumbfast.width - rightMargin, math.max(leftMargin, Mouse.x - thumbfast.width / 2)),
Window.h - bottomMargin - thumbfast.height
clamp( Mouse._rawX - @thumbfast.width / 2, leftMargin, Window._rawW - @thumbfast.width - rightMargin ),
Window._rawH - bottomMargin*Window.osdScale - @thumbfast.height
)
elseif thumbfast.width ~= 0 and thumbfast.height ~= 0 then
mp.commandv( 'script-message-to', 'thumbfast', 'clear' )

@needsUpdate = true

return @needsUpdate
18 changes: 15 additions & 3 deletions src/main.moon
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ topZone = ActivityZone =>

-- This is kind of ugly but I have gone insane and don't care any more.
-- Watch the rapidly declining quality of this codebase in realtime.
local chapters, progressBar, barCache, barBackground, elapsedTime, remainingTime, hoverTime, thumbnail
local chapters, progressBar, barCache, barBackground, elapsedTime, remainingTime, hoverTime

if settings['enable-bar']
-- this order is recorded and (ab)used by BarBase and
Expand Down Expand Up @@ -59,8 +59,20 @@ if settings['enable-hover-time']
hoverTime = HoverTime!
hoverTimeZone\addUIElement hoverTime

thumbnail = Thumbnail!
bottomZone\addUIElement thumbnail
if settings['enable-thumbnail']
mp.register_script_message 'thumbfast-info', ( json ) ->
data = utils.parse_json json
if type(data) != 'table' or not data.width or not data.height then
log.warn 'thumbfast did not respond with proper thumbnail information. Thumbnails are disabled.'
else
thumbnail = Thumbnail data
bottomZone\addUIElement thumbnail

-- a bit of a weird hack, but since this resolves asynchronously, it
-- may be called after initDraw has been called. In that case, we
-- have to call generateUIFromZones to actually make the thumbnail
-- get redrawn by the event loop.
eventLoop\generateUIFromZones!

title = nil
if settings['enable-title']
Expand Down