Skip to content

Commit

Permalink
feat: subtitle menu now support selecting secondary subtitles
Browse files Browse the repository at this point in the history
closes #505
  • Loading branch information
tomasklaen committed Aug 30, 2024
1 parent 47cccb4 commit 7150ceb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/uosc/elements/Menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ local Element = require('elements/Element')
---@alias Fling {y: number, distance: number, time: number, easing: fun(x: number), duration: number, update_cursor?: boolean}
---@alias Search {query: string; timeout: unknown; min_top: number; max_width: number; source: {width: number; top: number; scroll_y: number; selected_index?: integer; items?: MenuDataItem[]}}

---@alias MenuEventActivate {type: 'activate'; index: number; value: any; action?: string; modifiers: string; alt: boolean; ctrl: boolean; shift: boolean; keep_open?: boolean; menu_id: string;}
---@alias MenuEventActivate {type: 'activate'; index: number; value: any; action?: string; modifiers?: string; alt: boolean; ctrl: boolean; shift: boolean; keep_open?: boolean; menu_id: string;}
---@alias MenuEventMove {type: 'move'; from_index: number; to_index: number; menu_id: string;}
---@alias MenuEventSearch {type: 'search'; query: string; menu_id: string;}
---@alias MenuEventKey {type: 'key'; id: string; key: string; modifiers: string; alt: boolean; ctrl: boolean; shift: boolean; menu_id: string; selected_item?: {index: number; value: any; action?: string;}}
---@alias MenuEventKey {type: 'key'; id: string; key: string; modifiers?: string; alt: boolean; ctrl: boolean; shift: boolean; menu_id: string; selected_item?: {index: number; value: any; action?: string;}}
---@alias MenuEventPaste {type: 'paste'; value: string; menu_id: string; selected_item?: {index: number; value: any; action?: string;}}
---@alias MenuEventBack {type: 'back';}
---@alias MenuEventClose {type: 'close';}
Expand Down Expand Up @@ -637,7 +637,7 @@ function Menu:activate_selected_item(shortcut)
value = item.value,
action = action and action.name,
keep_open = item.keep_open or menu.keep_open,
modifiers = shortcut and shortcut.modifiers or '',
modifiers = shortcut and shortcut.modifiers or nil,
alt = shortcut and shortcut.alt or false,
ctrl = shortcut and shortcut.ctrl or false,
shift = shortcut and shortcut.shift or false,
Expand Down Expand Up @@ -1107,10 +1107,10 @@ function Menu:handle_shortcut(shortcut, info)
if menu.search then
if modifiers == 'shift' then
self:search_clear_query()
elseif modifiers == '' or modifiers == 'ctrl' then
elseif not modifiers or modifiers == 'ctrl' then
self:search_backspace(info.event, modifiers == 'ctrl')
end
elseif modifiers == '' and info.event ~= 'repeat' then
elseif not modifiers and info.event ~= 'repeat' then
self:back()
end
elseif key == 'mbtn_back' then
Expand Down
44 changes: 31 additions & 13 deletions src/uosc/lib/menus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ function create_self_updating_menu_opener(opts)
end
elseif event.action == 'remove' and (opts.on_remove or opts.on_delete) then
remove_or_delete(event.index, event.value, event.menu_id, event.modifiers)
elseif itable_has({'', 'alt'}, event.modifiers) then
else
opts.on_activate(event --[[@as MenuEventActivate]])
if event.modifiers ~= 'alt' then menu:close() end
if not event.modifiers then menu:close() end
end
elseif event.type == 'key' then
if event.id == 'enter' then
Expand Down Expand Up @@ -164,9 +164,12 @@ function create_self_updating_menu_opener(opts)
end
end

---@param opts {title: string, type: string, prop: string, load_command: string, download_command?: string}
---@param opts {title: string; type: string; prop: string; enable_prop?: string; secondary?: {prop: string; icon: string; enable_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 snd = opts.secondary
local function get_props()
return tonumber(mp.get_property(opts.prop)), snd and tonumber(mp.get_property(snd.prop)) or nil
end

local function serialize_tracklist(tracklist)
local items = {}
Expand All @@ -187,15 +190,19 @@ function create_select_tracklist_type_menu_opener(opts)
items[#items].separator = true
end

local track_prop_index = get_prop()
local track_prop_index, snd_prop_index = get_props()
local first_item_index = #items + 1
local active_index = nil
local disabled_item = nil
local track_actions = snd and {
{name = 'as_secondary', icon = snd.icon, label = t('Activate as secondary') .. ' (shift)'},
} or nil

for _, track in ipairs(tracklist) do
if track.type == opts.type then
local hint_values = {}
local track_selected = track.selected and track.id == track_prop_index
local snd_selected = snd and track.id == snd_prop_index
local function h(value) hint_values[#hint_values + 1] = value end

if track.lang then h(track.lang) end
Expand All @@ -218,7 +225,10 @@ function create_select_tracklist_type_menu_opener(opts)
title = (track.title and track.title or t('Track %s', track.id)),
hint = table.concat(hint_values, ', '),
value = track.id,
active = track_selected,
active = track_selected or snd_selected,
italic = snd_selected,
icon = snd and snd_selected and snd.icon or nil,
actions = track_actions,
}

if track_selected then
Expand All @@ -236,12 +246,19 @@ function create_select_tracklist_type_menu_opener(opts)
if event.value == '{load}' then
mp.command(event.action == 'download' and opts.download_command or opts.load_command)
else
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 opts.type == 'sub' then
mp.commandv('set', 'sub-visibility', 'yes')
if snd and (event.action == 'as_secondary' or event.modifiers == 'shift') then
local _, snd_track_index = get_props()
mp.commandv('set', snd.prop, event.value == snd_track_index and 'no' or event.value)
if snd.enable_prop then
mp.commandv('set', snd.enable_prop, 'yes')
end
elseif not event.modifiers or event.modifiers == 'alt' then
mp.commandv('set', opts.prop, event.value == get_props() and 'no' or event.value)
if opts.enable_prop then
mp.commandv('set', opts.enable_prop, 'yes')
end
end

end
end

Expand Down Expand Up @@ -426,7 +443,7 @@ function open_file_navigation_menu(directory_path, handle_activate, opts)
return
end

if info.is_dir and event.modifiers == '' and event.action == nil then
if info.is_dir and not event.modifiers and not event.action then
open_directory(path)
else
handle_activate(event)
Expand Down Expand Up @@ -963,7 +980,8 @@ function open_subtitle_downloader()
hint = table.concat(hints, ', '),
value = {kind = 'file', id = sub.attributes.files[1].file_id, url = url},
keep_open = true,
actions = url and {{name = 'open_in_browser', icon = 'open_in_new', label = t('Open in browser') .. ' (shift)'}},
actions = url and
{{name = 'open_in_browser', icon = 'open_in_new', label = t('Open in browser') .. ' (shift)'}},
}
end)

Expand Down
2 changes: 2 additions & 0 deletions src/uosc/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,8 @@ bind_command('subtitles', create_select_tracklist_type_menu_opener({
title = t('Subtitles'),
type = 'sub',
prop = 'sid',
enable_prop = 'sub-visibility',
secondary = {prop = 'secondary-sid', icon = 'vertical_align_top', enable_prop = 'secondary-sub-visibility'},
load_command = 'script-binding uosc/load-subtitles',
download_command = 'script-binding uosc/download-subtitles',
}))
Expand Down

0 comments on commit 7150ceb

Please # to comment.