refactor: title template updating

closes #312
This commit is contained in:
tomasklaen
2022-10-11 10:23:23 +02:00
parent 840181825c
commit 66a29d67f9

View File

@@ -4298,21 +4298,17 @@ mp.register_event('end-file', function(event)
handle_file_end()
end
end)
function observe_title()
-- Idle is needed as some template variables might not be present until everything else initialized
mp.unregister_idle(observe_title)
local hot_keywords = {'time'}
do
local hot_keywords = {'time', 'percent'}
local timer = mp.add_periodic_timer(0.9, function() update_title(mp.get_property_native('title')) end)
timer:kill()
mp.observe_property('title', 'string', function(_, title)
-- Don't change title if there is currently none
if state.title then update_title(title) end
update_title(title)
-- Enable periodic updates for templates with hot variables
local is_hot = itable_find(hot_keywords, function(var) return string.find(title or '', var) ~= nil end)
if is_hot then timer:resume() else timer:kill() end
end)
end
mp.register_idle(observe_title)
mp.observe_property('playback-time', 'number', create_state_setter('time', function()
-- Create a file-end event that triggers right before file ends
file_end_timer:kill()