feat: initial support for updating options at runtime (#571)

Options can be changed during runtime by changing `script-opts`.
So far such option changes were simply ignored.
Now most options work, and the rest can be implemented when the needed.
This commit is contained in:
christoph-heinrich
2023-09-18 11:13:48 +02:00
committed by GitHub
parent f62a9d1bbb
commit 8fe748c4d2
10 changed files with 53 additions and 13 deletions

View File

@@ -19,6 +19,10 @@ function Controls:init()
---@type ControlItem[] Only controls that match current dispositions.
self.layout = {}
self:init_options()
end
function Controls:init_options()
-- Serialize control elements
local shorthands = {
menu = 'command:menu:script-binding uosc/menu-blurred?Menu',
@@ -326,4 +330,11 @@ function Controls:on_prop_border() self:update_dimensions() end
function Controls:on_prop_fullormaxed() self:update_dimensions() end
function Controls:on_timeline_enabled() self:update_dimensions() end
function Controls:on_options()
for _, control in ipairs(self.controls) do
if control.element then control.element:destroy() end
end
self:init_options()
end
return Controls

View File

@@ -146,6 +146,7 @@ function Element:flash()
self:tween_stop()
self.forced_visibility = 1
request_render()
self._flash_out_timer.timeout = options.flash_duration / 1000
self._flash_out_timer:kill()
self._flash_out_timer:resume()
end

View File

@@ -527,6 +527,7 @@ end
function Menu:on_display() self:update_dimensions() end
function Menu:on_prop_fullormaxed() self:update_content_dimensions() end
function Menu:on_options() self:update_content_dimensions() end
function Menu:handle_cursor_down()
if self.proximity_raw == 0 then

View File

@@ -7,22 +7,16 @@ function PauseIndicator:new() return Class.new(self) --[[@as PauseIndicator]] en
function PauseIndicator:init()
Element.init(self, 'pause_indicator')
self.ignores_menu = true
self.base_icon_opacity = options.pause_indicator == 'flash' and 1 or 0.8
self.paused = state.pause
self.type = options.pause_indicator
self.is_manual = options.pause_indicator == 'manual'
self.fadeout_requested = false
self.opacity = 0
self:init_options()
end
mp.observe_property('pause', 'bool', function(_, paused)
if Elements.timeline.pressed then return end
if options.pause_indicator == 'flash' then
if self.paused == paused then return end
self:flash()
elseif options.pause_indicator == 'static' then
self:decide()
end
end)
function PauseIndicator:init_options()
self.base_icon_opacity = options.pause_indicator == 'flash' and 1 or 0.8
self.type = options.pause_indicator
self.is_manual = options.pause_indicator == 'manual'
end
function PauseIndicator:flash()
@@ -49,6 +43,22 @@ function PauseIndicator:decide()
mp.add_timeout(.05, function() osd:update() end)
end
function PauseIndicator:on_prop_pause()
if Elements.timeline.pressed then return end
if options.pause_indicator == 'flash' then
if self.paused == state.pause then return end
self:flash()
elseif options.pause_indicator == 'static' then
self:decide()
end
end
function PauseIndicator:on_options()
self:init_options()
self:on_prop_pause()
if self.type == 'flash' then self.opacity = 0 end
end
function PauseIndicator:render()
if self.opacity == 0 then return end

View File

@@ -27,6 +27,7 @@ function Speed:on_coordinates()
self.notch_spacing = self.width / (self.notches + 1)
self.font_size = round(self.height * 0.48 * options.font_scale)
end
function Speed:on_options() self:on_coordinates() end
function Speed:speed_step(speed, up)
if options.speed_step_is_factor then

View File

@@ -108,6 +108,10 @@ function Timeline:on_prop_time() self:decide_enabled() end
function Timeline:on_prop_border() self:update_dimensions() end
function Timeline:on_prop_fullormaxed() self:update_dimensions() end
function Timeline:on_display() self:update_dimensions() end
function Timeline:on_options()
self.top_border = options.timeline_border
self:update_dimensions()
end
function Timeline:handle_cursor_up()
if self.pressed then
mp.set_property_native('pause', self.pressed.pause)

View File

@@ -161,6 +161,11 @@ end
function TopBar:on_display() self:update_dimensions() end
function TopBar:on_options()
self:decide_enabled()
self:update_dimensions()
end
function TopBar:render()
local visibility = self:get_visibility()
if visibility <= 0 then return end

View File

@@ -248,5 +248,6 @@ end
function Volume:on_display() self:update_dimensions() end
function Volume:on_prop_border() self:update_dimensions() end
function Volume:on_controls_reflow() self:update_dimensions() end
function Volume:on_options() self:update_dimensions() end
return Volume

View File

@@ -17,6 +17,7 @@ end
function WindowBorder:on_prop_border() self:decide_enabled() end
function WindowBorder:on_prop_fullormaxed() self:decide_enabled() end
function WindowBorder:on_options() self:decide_enabled() end
function WindowBorder:render()
if self.size > 0 then

View File

@@ -109,7 +109,12 @@ defaults = {
languages = 'slang,en',
}
options = table_shallow_copy(defaults)
opt.read_options(options, 'uosc')
opt.read_options(options, 'uosc', function(_)
update_human_times()
Elements:trigger('options')
Elements:update_proximities()
request_render()
end)
-- Normalize values
options.proximity_out = math.max(options.proximity_out, options.proximity_in + 1)
if options.chapter_ranges:sub(1, 4) == '^op|' then options.chapter_ranges = defaults.chapter_ranges end