From 76b1ac57a4f49ff2c7f18b7e46aefa7fb3362984 Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Sun, 20 Jun 2021 15:45:43 +0300 Subject: osc: seekbar hover/drag: display target chapter at the title Fixes #8925 --- player/lua/osc.lua | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'player/lua') diff --git a/player/lua/osc.lua b/player/lua/osc.lua index 8701f4acd0..e3cefd1642 100644 --- a/player/lua/osc.lua +++ b/player/lua/osc.lua @@ -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] @@ -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" @@ -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 -- cgit v1.2.3