feat: implement external properties API

closes #278
This commit is contained in:
tomasklaen
2022-09-30 11:31:47 +02:00
parent a9da4cfa2b
commit 08e0aed8b2
2 changed files with 23 additions and 2 deletions

View File

@@ -509,6 +509,22 @@ mp.register_script_message('submit', function(prop, value)
end)
```
### `set <prop> <value>`
Tell **uosc** to set an external property to this value. Currently, this is only used to display control button badges:
In your script, set the value of `foo` to `1`.
```lua
mp.commandv('script-message-to', 'uosc', 'set', 'foo', 1)
```
This property can now be used as a control button badge by prefixing it with `@`.
```
controls=command:icon_name:command_name#@foo?My foo button
```
## Why _uosc_?
It stood for micro osc as it used to render just a couple rectangles before it grew to what it is today. And now it means a minimalist UI design direction where everything is out of your way until needed.

View File

@@ -3367,7 +3367,7 @@ end
function Controls:register_badge_updater(badge, element)
local prop_and_limit = split(badge, ' *> *')
local prop, limit = prop_and_limit[1], tonumber(prop_and_limit[2] or -1)
local observable_name, serializer = prop, nil
local observable_name, serializer, is_external_prop = prop, nil, false
if itable_index_of({'sub', 'audio', 'video'}, prop) then
observable_name = 'track-list'
@@ -3377,6 +3377,7 @@ function Controls:register_badge_updater(badge, element)
return count
end
else
if prop:sub(1, 1) == '@' then prop, is_external_prop = prop:sub(2), true end
serializer = function(value) return value and (type(value) == 'table' and #value or tostring(value)) or nil end
end
@@ -3388,7 +3389,8 @@ function Controls:register_badge_updater(badge, element)
request_render()
end
mp.observe_property(observable_name, 'native', handler)
if is_external_prop then element['on_external_prop_' .. prop] = function(_, value) handler(prop, value) end
else mp.observe_property(observable_name, 'native', handler) end
end
function Controls:get_visibility()
@@ -4734,3 +4736,6 @@ mp.register_script_message('thumbfast-info', function(json)
request_render()
end
end)
mp.register_script_message('set', function(name, value)
Elements:trigger('external_prop_' .. name, utils.parse_json(value))
end)