Skip to content

Commit

Permalink
feat: support for using uosc as toggle/cycle control prop owner to …
Browse files Browse the repository at this point in the history
…tap into uosc options

This allows stuff like:

```
toggle:hdr_auto:autoload@uosc
```

To toggle uosc's `autoload` flag.

ref #1004
  • Loading branch information
tomasklaen committed Oct 8, 2024
1 parent fdc16ad commit 8220bad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/uosc.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ progress_line_width=20
# `loop-playlist`, `loop-file`, `shuffle`
# `speed[:{scale}]` - display speed slider, [{scale}] - factor of controls_size, default: 1.3
# `command:{icon}:{command}` - button that executes a {command} when pressed
# `toggle:{icon}:{prop}[@{owner}]` - button that toggles mpv property
# `toggle:{icon}:{prop}[@{owner}]` - button that toggles mpv property. shorthand for yes/no cycle below
# `cycle:{default_icon}:{prop}[@{owner}]:{value1}[={icon1}][!]/{valueN}[={iconN}][!]`
# - button that cycles mpv property between values, each optionally having different icon and active flag
# - presence of `!` at the end will style the button as active
# - `{owner}` is the name of a script that manages this property if any
# - `{owner}` is the name of a script that manages this property if any. Set to `uosc` to tap into uosc options.
# `gap[:{scale}]` - display an empty gap
# {scale} - factor of controls_size, default: 0.3
# `space` - fills all available space between previous and next item, useful to align items to the right
Expand Down
31 changes: 26 additions & 5 deletions src/uosc/elements/CycleButton.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ local Button = require('elements/Button')
---@alias CycleState {value: any; icon: string; active?: boolean}
---@alias CycleButtonProps {prop: string; states: CycleState[]; anchor_id?: string; tooltip?: string}

local function yes_no_to_boolean(value)
if type(value) ~= 'string' then return value end
local lowercase = trim(value):lower()
if lowercase == 'yes' or lowercase == 'no' then
return lowercase == 'yes'
else
return value
end
end

---@class CycleButton : Button
local CycleButton = class(Button)

Expand All @@ -24,11 +34,17 @@ function CycleButton:init(id, props)
self.on_click = function()
local new_state = self.states[self.current_state_index + 1] or self.states[1]
local new_value = new_state.value
if self.owner then
if self.owner == 'uosc' then
if type(options[self.prop]) == 'number' then
options[self.prop] = tonumber(new_value) or 0
else
options[self.prop] = yes_no_to_boolean(new_value)
end
handle_options({[self.prop] = options[self.prop]})
elseif self.owner then
mp.commandv('script-message-to', self.owner, 'set', self.prop, new_value)
elseif is_state_prop then
if itable_index_of({'yes', 'no'}, new_value) then new_value = new_value == 'yes' end
set_state(self.prop, new_value)
set_state(self.prop, yes_no_to_boolean(new_value))
else
mp.set_property(self.prop, new_value)
end
Expand All @@ -52,8 +68,13 @@ function CycleButton:init(id, props)
local prop_parts = split(self.prop, '@')
if #prop_parts == 2 then -- External prop with a script owner
self.prop, self.owner = prop_parts[1], prop_parts[2]
self['on_external_prop_' .. self.prop] = function(_, value) handle_change(self.prop, value) end
handle_change(self.prop, external[self.prop])
if self.owner == 'uosc' then
self['on_options'] = function() handle_change(self.prop, options[self.prop]) end
handle_change(self.prop, options[self.prop])
else
self['on_external_prop_' .. self.prop] = function(_, value) handle_change(self.prop, value) end
handle_change(self.prop, external[self.prop])
end
elseif is_state_prop then -- uosc's state props
self['on_prop_' .. self.prop] = function(self, value) handle_change(self.prop, value) end
handle_change(self.prop, state[self.prop])
Expand Down
5 changes: 3 additions & 2 deletions src/uosc/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ defaults = {
disable_elements = '',
}
options = table_copy(defaults)
opt.read_options(options, 'uosc', function(changed_options)
function handle_options(changed_options)
if changed_options.time_precision then
timestamp_zero_rep_clear_cache()
end
Expand All @@ -113,7 +113,8 @@ opt.read_options(options, 'uosc', function(changed_options)
Elements:trigger('options')
Elements:update_proximities()
request_render()
end)
end
opt.read_options(options, 'uosc', handle_options)
-- 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
Expand Down

0 comments on commit 8220bad

Please sign in to comment.