Skip to content

Commit

Permalink
refactor: track menu simplifications and some other tweaks
Browse files Browse the repository at this point in the history
ref #966
  • Loading branch information
tomasklaen committed Sep 2, 2024
1 parent bb83e40 commit f6159ad
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 45 deletions.
72 changes: 33 additions & 39 deletions src/uosc/lib/menus.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---@alias OpenCommandMenuOptions {submenu?: string; mouse_nav?: boolean; on_close?: string | string[]}
---@param data MenuData
---@param opts? {submenu?: string; mouse_nav?: boolean; on_close?: string | string[]}
---@param opts? OpenCommandMenuOptions
function open_command_menu(data, opts)
opts = opts or {}
local menu
Expand Down Expand Up @@ -30,7 +31,7 @@ function open_command_menu(data, opts)
return menu
end

---@param opts? {submenu?: string; mouse_nav?: boolean; on_close?: string | string[]}
---@param opts? OpenCommandMenuOptions
function toggle_menu_with_items(opts)
if Menu:is_open('menu') then
Menu:close()
Expand All @@ -39,8 +40,9 @@ function toggle_menu_with_items(opts)
end
end

---@alias EventRemove {type: 'remove' | 'delete', index: number; value: any; menu_id: string;}
---@param opts {type: string; title: string; list_prop: string; active_prop?: string; footnote?: string; serializer: fun(list: any, active: any): MenuDataItem[]; actions?: MenuAction[]; actions_place?: 'inside'|'outside'; on_paste: fun(event: MenuEventPaste); on_move?: fun(event: MenuEventMove); on_activate?: fun(event: MenuEventActivate); on_remove?: fun(event: EventRemove); on_delete?: fun(event: EventRemove); on_key?: fun(event: MenuEventKey, close: fun())}
---@alias TrackEventRemove {type: 'remove' | 'delete', index: number; value: any;}
---@alias TrackEventReload {type: 'reload', index: number; value: any;}
---@param opts {type: string; title: string; list_prop: string; active_prop?: string; footnote?: string; serializer: fun(list: any, active: any): MenuDataItem[]; actions?: MenuAction[]; actions_place?: 'inside'|'outside'; on_paste: fun(event: MenuEventPaste); on_move?: fun(event: MenuEventMove); on_activate?: fun(event: MenuEventActivate); on_remove?: fun(event: TrackEventRemove); on_delete?: fun(event: TrackEventRemove); on_reload?: fun(event: TrackEventReload); on_key?: fun(event: MenuEventKey, close: fun())}
function create_self_updating_menu_opener(opts)
return function()
if Menu:is_open(opts.type) then
Expand Down Expand Up @@ -83,6 +85,9 @@ function create_self_updating_menu_opener(opts)

---@type MenuAction[]
local actions = opts.actions or {}
if opts.on_reload then
actions[#actions + 1] = {name = 'reload', icon = 'refresh', label = t('Reload') .. ' (f5)'}
end
if opts.on_remove or opts.on_delete then
local label = (opts.on_remove and t('Remove') or t('Delete')) .. ' (del)'
if opts.on_remove and opts.on_delete then
Expand All @@ -96,13 +101,13 @@ function create_self_updating_menu_opener(opts)
local method = modifiers == 'ctrl' and 'delete' or 'remove'
local handler = method == 'delete' and opts.on_delete or opts.on_remove
if handler then
handler({type = method, value = value, index = index, menu_id = menu_id})
handler({type = method, value = value, index = index})
end
elseif opts.on_remove or opts.on_delete then
local method = opts.on_delete and 'delete' or 'remove'
local handler = opts.on_delete or opts.on_remove
if handler then
handler({type = method, value = value, index = index, menu_id = menu_id})
handler({type = method, value = value, index = index})
end
end
end
Expand All @@ -122,22 +127,27 @@ function create_self_updating_menu_opener(opts)
on_close = 'callback',
}, function(event)
if event.type == 'activate' then
if event.action == 'remove' and (opts.on_remove or opts.on_delete) then
if event.action == 'reload' and opts.on_reload then
opts.on_reload({type = 'reload', index = event.index, value = event.value})
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)
else
opts.on_activate(event --[[@as MenuEventActivate]])
if not event.modifiers then menu:close() end
if not event.modifiers and not event.action then cleanup_and_close() end
end
elseif event.type == 'key' then
local item = event.selected_item
if opts.on_key then
opts.on_key(event --[[@as MenuEventKey]], cleanup_and_close)
elseif event.id == 'enter' then
if event.id == 'enter' then
-- We get here when there's no selectable item in menu and user presses enter.
cleanup_and_close()
elseif event.key == 'del' and item then
elseif event.key == 'f5' and opts.on_reload and item then
opts.on_reload({type = 'reload', index = item.index, value = item.value})
elseif event.key == 'del' and (opts.on_remove or opts.on_delete) and item then
if itable_has({nil, 'ctrl'}, event.modifiers) then
remove_or_delete(item.index, item.value, event.menu_id, event.modifiers)
end
elseif opts.on_key then
opts.on_key(event --[[@as MenuEventKey]], cleanup_and_close)
end
elseif event.type == 'paste' and opts.on_paste then
opts.on_paste(event --[[@as MenuEventPaste]])
Expand Down Expand Up @@ -190,7 +200,9 @@ function create_select_tracklist_type_menu_opener(opts)
local track_external_actions = {}

if snd then
local action = {name = 'as_secondary', icon = snd.icon, label = t('Use as secondary') .. ' (shift+enter/click)'}
local action = {
name = 'as_secondary', icon = snd.icon, label = t('Use as secondary') .. ' (shift+enter/click)',
}
track_actions = {action}
table.insert(track_external_actions, action)
end
Expand Down Expand Up @@ -241,25 +253,10 @@ function create_select_tracklist_type_menu_opener(opts)
end

local function reload(id)
if not id then return end
if opts.type == "video" then
mp.commandv("video-reload", id)
elseif opts.type == "audio" then
mp.commandv("audio-reload", id)
elseif opts.type == "sub" then
mp.commandv("sub-reload", id)
end
if id then mp.commandv(opts.type .. '-reload', id) end
end

local function remove(id)
if not id then return end
if opts.type == "video" then
mp.commandv("video-remove", id)
elseif opts.type == "audio" then
mp.commandv("audio-remove", id)
elseif opts.type == "sub" then
mp.commandv("sub-remove", id)
end
if id then mp.commandv(opts.type .. '-remove', id) end
end

---@param event MenuEventActivate
Expand Down Expand Up @@ -288,14 +285,11 @@ function create_select_tracklist_type_menu_opener(opts)

---@param event MenuEventKey
local function handle_key(event)
local item = event.selected_item
if event.id == 'f5' then
if item then
reload(item.value)
end
elseif event.id == 'del' then
if item then
remove(item.value)
if event.selected_item then
if event.id == 'f5' then
reload(event.selected_item.value)
elseif event.id == 'del' then
remove(event.selected_item.value)
end
end
end
Expand Down Expand Up @@ -337,7 +331,7 @@ function open_file_navigation_menu(directory_path, handle_activate, opts)

---@param path string Can be path to a directory, or special string `'{drives}'` to get windows drives items.
---@param selected_path? string Marks item with this path as active.
---@return MenuStackValue[] menu_items
---@return MenuStackChild[] menu_items
---@return number selected_index
---@return string|nil error
local function serialize_items(path, selected_path)
Expand Down
10 changes: 5 additions & 5 deletions src/uosc/lib/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
---@alias Hitbox Rect|Circle
---@alias ComplexBindingInfo {event: 'down' | 'repeat' | 'up' | 'press'; is_mouse: boolean; canceled: boolean; key_name?: string; key_text?: string;}

--- In place sorting of filenames
---@param filenames string[]

-- String sorting
do
----- winapi start -----
Expand Down Expand Up @@ -474,7 +471,10 @@ function get_adjacent_files(file_path, opts)
local current_meta = serialize_path(file_path)
if not current_meta then return end
local files, _dirs, error = read_directory(current_meta.dirname, {hidden = opts.hidden})
if error then msg.error(error) return end
if error then
msg.error(error)
return
end
sort_strings(files)
local current_file_index
local paths = {}
Expand Down Expand Up @@ -637,7 +637,7 @@ function delete_file_navigate(delta)
if Menu:is_open('open-file') then
Elements:maybe('menu', 'delete_value', path)
end
delete_file(path)
if path then delete_file(path) end
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/uosc/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ mp.observe_property('demuxer-cache-state', 'native', function(prop, cache_state)
for _, range in ipairs(cached_ranges) do
ranges[#ranges + 1] = {
math.max(range['start'] or 0, 0),
math.min(range['end'] or state.duration, state.duration),
math.min(range['end'] or state.duration --[[@as number]], state.duration),
}
end
table.sort(ranges, function(a, b) return a[1] < b[1] end)
Expand Down

0 comments on commit f6159ad

Please sign in to comment.