fix: properly update title (#318)
Get the properties relevant to the title, observe and update based on their changes.
This commit is contained in:

committed by
GitHub

parent
5995bfb7ae
commit
2c4cfd70ee
@@ -654,6 +654,24 @@ function wrap_text(text, target_line_length)
|
|||||||
return table.concat(lines, '\n'), max_length, #lines
|
return table.concat(lines, '\n'), max_length, #lines
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Extracts the properties used by property expansion of that string.
|
||||||
|
---@param str string
|
||||||
|
---@param res { [string] : boolean } | nil
|
||||||
|
---@return { [string] : boolean }
|
||||||
|
local function get_expansion_props(str, res)
|
||||||
|
res = res or {}
|
||||||
|
for str in str:gmatch('%$(%b{})') do
|
||||||
|
local name, str = str:match('^{[?!]?=?([^:]+):?(.*)}$')
|
||||||
|
if name then
|
||||||
|
local s = name:find('==') or nil
|
||||||
|
if s then name = name:sub(0, s - 1) end
|
||||||
|
res[name] = true
|
||||||
|
if str and str ~= '' then get_expansion_props(str, res) end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return res
|
||||||
|
end
|
||||||
|
|
||||||
-- Escape a string for verbatim display on the OSD
|
-- Escape a string for verbatim display on the OSD
|
||||||
---@param str string
|
---@param str string
|
||||||
function ass_escape(str)
|
function ass_escape(str)
|
||||||
@@ -4290,30 +4308,31 @@ mp.observe_property('mouse-pos', 'native', function(_, mouse)
|
|||||||
else handle_mouse_leave() end
|
else handle_mouse_leave() end
|
||||||
end)
|
end)
|
||||||
mp.observe_property('osc', 'bool', function(name, value) if value == true then mp.set_property('osc', 'no') end end)
|
mp.observe_property('osc', 'bool', function(name, value) if value == true then mp.set_property('osc', 'no') end end)
|
||||||
function update_title(title_template)
|
|
||||||
if title_template:sub(-6) == ' - mpv' then title_template = title_template:sub(1, -7) end
|
|
||||||
set_state('title', ass_escape(mp.command_native({'expand-text', title_template})))
|
|
||||||
end
|
|
||||||
mp.register_event('file-loaded', function()
|
mp.register_event('file-loaded', function()
|
||||||
set_state('path', normalize_path(mp.get_property_native('path')))
|
set_state('path', normalize_path(mp.get_property_native('path')))
|
||||||
update_title(mp.get_property_native('title'))
|
|
||||||
end)
|
end)
|
||||||
mp.register_event('end-file', function(event)
|
mp.register_event('end-file', function(event)
|
||||||
set_state('title', nil)
|
|
||||||
if event.reason == 'eof' then
|
if event.reason == 'eof' then
|
||||||
file_end_timer:kill()
|
file_end_timer:kill()
|
||||||
handle_file_end()
|
handle_file_end()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
do
|
do
|
||||||
local hot_keywords = {'time', 'percent'}
|
local template = nil
|
||||||
local timer = mp.add_periodic_timer(0.9, function() update_title(mp.get_property_native('title')) end)
|
function update_title()
|
||||||
timer:kill()
|
if template:sub(-6) == ' - mpv' then template = template:sub(1, -7) end
|
||||||
|
-- escape ASS, and strip newlines and trailing slashes and trim whitespace
|
||||||
|
local t = mp.command_native({'expand-text', template}):gsub('\\n', ' '):gsub('[\\%s]+$', ''):gsub('^%s+', '')
|
||||||
|
set_state('title', ass_escape(t))
|
||||||
|
end
|
||||||
mp.observe_property('title', 'string', function(_, title)
|
mp.observe_property('title', 'string', function(_, title)
|
||||||
update_title(title)
|
mp.unobserve_property(update_title)
|
||||||
-- Enable periodic updates for templates with hot variables
|
template = title
|
||||||
local is_hot = itable_find(hot_keywords, function(var) return string.find(title or '', var) ~= nil end)
|
local props = get_expansion_props(title)
|
||||||
if is_hot then timer:resume() else timer:kill() end
|
for prop, _ in pairs(props) do
|
||||||
|
mp.observe_property(prop, 'native', update_title)
|
||||||
|
end
|
||||||
|
if not next(props) then update_title() end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
mp.observe_property('playback-time', 'number', create_state_setter('time', function()
|
mp.observe_property('playback-time', 'number', create_state_setter('time', function()
|
||||||
|
Reference in New Issue
Block a user