Skip to content

Commit

Permalink
osc: seekbar hover/drag: display target chapter at the title
Browse files Browse the repository at this point in the history
Fixes #8925
  • Loading branch information
avih committed Jun 22, 2021
1 parent a17d799 commit 76b1ac5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
3 changes: 2 additions & 1 deletion DOCS/man/osc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pl next
============= ================================================

title
| Displays current media-title, filename, or custom title
| Displays current media-title, filename, custom title, or target chapter
name while hovering the seekbar.
============= ================================================
left-click show playlist position and length and full title
Expand Down
34 changes: 33 additions & 1 deletion player/lua/osc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,38 @@ end
-- Element Rendering
--

-- returns nil or a chapter element from the native property chapter-list
function get_chapter(possec)
local cl = mp.get_property_native("chapter-list", {})
local ch = nil

-- chapters might not be sorted by time. find nearest-before/at possec
for n=1, #cl do
if possec >= cl[n].time and (not ch or cl[n].time > ch.time) then
ch = cl[n]
end
end
return ch
end

function render_elements(master_ass)

-- when the slider is dragged or hovered and we have a target chapter name
-- then we use it instead of the normal title. we calculate it before the
-- render iterations because the title may be rendered before the slider.
state.forced_title = nil
local se, ae = state.slider_element, elements[state.active_element]
if se and (ae == se or (not ae and mouse_hit(se))) then
local dur = mp.get_property_number("duration", 0)
if dur > 0 then
local possec = get_slider_value(se) * dur / 100 -- of mouse pos
local ch = get_chapter(possec)
if ch and ch.title and ch.title ~= "" then
state.forced_title = "Chapter: " .. ch.title
end
end
end

for n=1, #elements do
local element = elements[n]

Expand Down Expand Up @@ -1741,7 +1771,8 @@ function osc_init()
ne = new_element("title", "button")

ne.content = function ()
local title = mp.command_native({"expand-text", user_opts.title})
local title = state.forced_title or
mp.command_native({"expand-text", user_opts.title})
-- escape ASS, and strip newlines and trailing slashes
title = title:gsub("\\n", " "):gsub("\\$", ""):gsub("{","\\{")
return not (title == "") and title or "mpv"
Expand Down Expand Up @@ -1918,6 +1949,7 @@ function osc_init()
ne = new_element("seekbar", "slider")

ne.enabled = not (mp.get_property("percent-pos") == nil)
state.slider_element = ne.enabled and ne or nil -- used for forced_title
ne.slider.markerF = function ()
local duration = mp.get_property_number("duration", nil)
if not (duration == nil) then
Expand Down

0 comments on commit 76b1ac5

Please sign in to comment.