Skip to content

Commit

Permalink
feat: disable tracks in track menus by toggling off active track
Browse files Browse the repository at this point in the history
This eliminates the need for "Disabled" item in track menus.

closes #954
  • Loading branch information
tomasklaen committed Aug 29, 2024
1 parent 17ddf24 commit fbd734d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
35 changes: 14 additions & 21 deletions src/uosc/lib/menus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,21 @@ function create_self_updating_menu_opener(opts)
end
end

function create_select_tracklist_type_menu_opener(menu_title, track_type, track_prop, load_command, download_command)
---@param opts {title: string, type: string, prop: string, load_command: string, download_command?: string}
function create_select_tracklist_type_menu_opener(opts)
local function get_prop() return tonumber(mp.get_property(opts.prop)) end

local function serialize_tracklist(tracklist)
local items = {}

if load_command then
if opts.load_command then
items[#items + 1] = {
title = t('Load'),
bold = true,
italic = true,
hint = t('open file'),
value = '{load}',
actions = download_command
actions = opts.download_command
and {{name = 'download', icon = 'language', label = t('Search online')}}
or nil,
}
Expand All @@ -182,23 +185,13 @@ function create_select_tracklist_type_menu_opener(menu_title, track_type, track_
items[#items].separator = true
end

local track_prop_index = tonumber(mp.get_property(track_prop))
local track_prop_index = get_prop()
local first_item_index = #items + 1
local active_index = nil
local disabled_item = nil

-- Add option to disable a subtitle track. This works for all tracks,
-- but why would anyone want to disable audio or video? Better to not
-- let people mistakenly select what is unwanted 99.999% of the time.
-- If I'm mistaken and there is an active need for this, feel free to
-- open an issue.
if track_type == 'sub' then
disabled_item = {title = t('Disabled'), italic = true, muted = true, hint = '', value = nil, active = true}
items[#items + 1] = disabled_item
end

for _, track in ipairs(tracklist) do
if track.type == track_type then
if track.type == opts.type then
local hint_values = {}
local track_selected = track.selected and track.id == track_prop_index
local function h(value) hint_values[#hint_values + 1] = value end
Expand Down Expand Up @@ -239,24 +232,24 @@ function create_select_tracklist_type_menu_opener(menu_title, track_type, track_
---@param event MenuEventActivate
local function handle_activate(event)
if event.value == '{load}' then
mp.command(event.action == 'download' and download_command or load_command)
mp.command(event.action == 'download' and opts.download_command or opts.load_command)
else
mp.commandv('set', track_prop, event.value and event.value or 'no')
mp.commandv('set', opts.prop, event.value == get_prop() and 'no' or event.value)

-- If subtitle track was selected, assume the user also wants to see it
if event.value and track_type == 'sub' then
if event.value and opts.type == 'sub' then
mp.commandv('set', 'sub-visibility', 'yes')
end
end
end

return create_self_updating_menu_opener({
title = menu_title,
type = track_type,
title = opts.title,
type = opts.type,
list_prop = 'track-list',
serializer = serialize_tracklist,
on_activate = handle_activate,
on_paste = function(event) load_track(track_type, event.value) end,
on_paste = function(event) load_track(opts.type, event.value) end,
})
end

Expand Down
22 changes: 13 additions & 9 deletions src/uosc/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -893,15 +893,19 @@ bind_command('load-audio', create_track_loader_menu_opener({
bind_command('load-video', create_track_loader_menu_opener({
name = 'video', prop = 'video', allowed_types = config.types.video,
}))
bind_command('subtitles', create_select_tracklist_type_menu_opener(
t('Subtitles'), 'sub', 'sid', 'script-binding uosc/load-subtitles', 'script-binding uosc/download-subtitles'
))
bind_command('audio', create_select_tracklist_type_menu_opener(
t('Audio'), 'audio', 'aid', 'script-binding uosc/load-audio'
))
bind_command('video', create_select_tracklist_type_menu_opener(
t('Video'), 'video', 'vid', 'script-binding uosc/load-video'
))
bind_command('subtitles', create_select_tracklist_type_menu_opener({
title = t('Subtitles'),
type = 'sub',
prop = 'sid',
load_command = 'script-binding uosc/load-subtitles',
download_command = 'script-binding uosc/download-subtitles',
}))
bind_command('audio', create_select_tracklist_type_menu_opener({
title = t('Audio'), type = 'audio', prop = 'aid', load_command = 'script-binding uosc/load-audio',
}))
bind_command('video', create_select_tracklist_type_menu_opener({
title = t('Video'), type = 'video', prop = 'vid', load_command = 'script-binding uosc/load-video',
}))
bind_command('playlist', create_self_updating_menu_opener({
title = t('Playlist'),
type = 'playlist',
Expand Down

0 comments on commit fbd734d

Please # to comment.