feat: added control bar button badge limits

It is now possible to specify a threshold when to hide the badge by doing `#audio>1` to display the audio counter badge only when there's more than 1 audio track.

ref #212
This commit is contained in:
tomasklaen
2022-09-19 22:08:13 +02:00
parent 3fcafbd64a
commit 5d92c73481
2 changed files with 19 additions and 9 deletions

View File

@@ -27,7 +27,7 @@ timeline_chapters_opacity=0.8
# A comma delimited list of items to construct the controls bar above the timeline. Set to `never` to disable. # A comma delimited list of items to construct the controls bar above the timeline. Set to `never` to disable.
# Parameter spec: enclosed in `{}` means value, enclosed in `[]` means optional # Parameter spec: enclosed in `{}` means value, enclosed in `[]` means optional
# Full item syntax: `[<[!]{disposition1}[,[!]{dispositionN}]>]{element}[:{paramN}][#{badge}][?{tooltip}]` # Full item syntax: `[<[!]{disposition1}[,[!]{dispositionN}]>]{element}[:{paramN}][#{badge}[>{limit}]][?{tooltip}]`
# Common properties: # Common properties:
# `{icon}` - parameter used to specify an icon name (example: `face`) # `{icon}` - parameter used to specify an icon name (example: `face`)
# - you can pick one here: https://fonts.google.com/icons?selected=Material+Icons # - you can pick one here: https://fonts.google.com/icons?selected=Material+Icons
@@ -63,10 +63,12 @@ timeline_chapters_opacity=0.8
# - `<stream>stream-quality` - show stream quality button only for streams # - `<stream>stream-quality` - show stream quality button only for streams
# - `<has_audio,!audio>audio` - show audio tracks button for all files that have # - `<has_audio,!audio>audio` - show audio tracks button for all files that have
# an audio track, but are not exclusively audio only files # an audio track, but are not exclusively audio only files
# Place `#{badge}` after the element params to give it a badge. Available badges: # Place `#{badge}[>{limit}]` after the element params to give it a badge. Available badges:
# `sub`, `audio`, `video` - track type counters # `sub`, `audio`, `video` - track type counters
# `{mpv_prop}` - any mpv prop that makes sense to you: https://mpv.io/manual/master/#property-list # `{mpv_prop}` - any mpv prop that makes sense to you: https://mpv.io/manual/master/#property-list
# - if prop value is an array it'll display its size # - if prop value is an array it'll display its size
# `>{limit}` will display the badge only if it's numerical value is above this threshold.
# Example: `#audio>1`
# Place `?{tooltip}` after the element config to give it a tooltip. # Place `?{tooltip}` after the element config to give it a tooltip.
# Example: `<stream>stream-quality?Stream quality` # Example: `<stream>stream-quality?Stream quality`
# Example implementations of some of the available shorthands: # Example implementations of some of the available shorthands:

View File

@@ -3093,12 +3093,12 @@ end
function Controls:serialize() function Controls:serialize()
local shorthands = { local shorthands = {
menu = 'command:menu:script-binding uosc/menu?Menu', menu = 'command:menu:script-binding uosc/menu?Menu',
subtitles = 'command:subtitles:script-binding uosc/subtitles#sub?Subtitles', subtitles = 'command:subtitles:script-binding uosc/subtitles#sub>0?Subtitles',
audio = 'command:graphic_eq:script-binding uosc/audio#audio?Audio', audio = 'command:graphic_eq:script-binding uosc/audio#audio>1?Audio',
['audio-device'] = 'command:speaker:script-binding uosc/audio-device?Audio device', ['audio-device'] = 'command:speaker:script-binding uosc/audio-device?Audio device',
video = 'command:theaters:script-binding uosc/video#video?Video', video = 'command:theaters:script-binding uosc/video#video>1?Video',
playlist = 'command:list_alt:script-binding uosc/playlist?Playlist', playlist = 'command:list_alt:script-binding uosc/playlist?Playlist',
chapters = 'command:bookmarks:script-binding uosc/chapters#chapters?Chapters', chapters = 'command:bookmarks:script-binding uosc/chapters#chapters>0?Chapters',
['stream-quality'] = 'command:high_quality:script-binding uosc/stream-quality?Stream quality', ['stream-quality'] = 'command:high_quality:script-binding uosc/stream-quality?Stream quality',
['open-file'] = 'command:file_open:script-binding uosc/open-file?Open file', ['open-file'] = 'command:file_open:script-binding uosc/open-file?Open file',
['items'] = 'command:list_alt:script-binding uosc/items?Playlist/Files', ['items'] = 'command:list_alt:script-binding uosc/items?Playlist/Files',
@@ -3244,10 +3244,13 @@ function Controls:clean_controls()
request_render() request_render()
end end
---@param prop string ---@param badge string
---@param element Element An element that supports `badge` property. ---@param element Element An element that supports `badge` property.
function Controls:register_badge_updater(prop, element) 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 = prop, nil
if itable_index_of({'sub', 'audio', 'video'}, prop) then if itable_index_of({'sub', 'audio', 'video'}, prop) then
observable_name = 'track-list' observable_name = 'track-list'
serializer = function(value) serializer = function(value)
@@ -3258,10 +3261,15 @@ function Controls:register_badge_updater(prop, element)
else else
serializer = function(value) return value and (type(value) == 'table' and #value or tostring(value)) or nil end serializer = function(value) return value and (type(value) == 'table' and #value or tostring(value)) or nil end
end end
local function handler(_, value) local function handler(_, value)
element.badge = serializer(value) local new_value = serializer(value) --[[@as nil|string|integer]]
local value_number = tonumber(new_value)
if value_number then new_value = value_number > limit and value_number or nil end
element.badge = new_value
request_render() request_render()
end end
mp.observe_property(observable_name, 'native', handler) mp.observe_property(observable_name, 'native', handler)
self.disposers[#self.disposers + 1] = function() mp.unobserve_property(handler) end self.disposers[#self.disposers + 1] = function() mp.unobserve_property(handler) end
end end