Skip to content

Commit

Permalink
feat: adjusted menu modifier behavior and added buttons where appropr…
Browse files Browse the repository at this point in the history
…iate

Activating item without closing the menu is now on `alt`. This is just a non-enforced guideline for all menus to implement when appropriate.

Adding files/folders to playlist is now on `shift`, and doesn't close the menu by default.

Actions that change something that is not immediately obvious (like adding files to playlist) now trigger an OSD message with what happened, so that there's some indication that it did in fact happen. I might move this into a custom notifications UI in future.

closes #909
  • Loading branch information
tomasklaen committed Aug 29, 2024
1 parent cd08350 commit c139963
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,11 @@ These bindings are active when any **uosc** menu is open (main menu, playlist, l
- `wheel_up`, `wheel_down` - Scroll menu.
- `pgup`, `pgdwn`, `home`, `end` - Self explanatory.
- `ctrl+f` or `\` - In case `menu_type_to_search` config option is disabled, these two trigger the menu search instead.
- `ctrl+enter` - Submits a search in menus without instant search.
- `ctrl+backspace` - Delete search query by word.
- `shift+backspace` - Clear search query.
- `ctrl+up/down/pgup/pgdwn/home/end` - Move selected item in menus that support it (playlist).
- `del` - Delete selected item in menus that support it (playlist).
- `shift+enter`, `shift+click` - Activate item without closing the menu. Might not be supported by all menus.
- `alt+enter`, `alt+click` - In file navigating menus, opens a directory in mpv instead of navigating to its contents.
- Holding `alt` while activating an item should prevent closing the menu (this is just a guideline, not all menus behave this way).
Each menu can also add its own shortcuts and bindings for special actions on items/menu, such as `del` to delete a playlist item, `ctrl+up/down/pgup/pgdwn/home/end` to move it around, etc. These are usually also exposed as item action buttons for you to find out about them that way.
Click on a faded parent menu to go back to it.
Expand Down
57 changes: 40 additions & 17 deletions src/uosc/lib/menus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ function create_self_updating_menu_opener(opts)
---@type MenuAction[]
local actions = opts.actions or {}
if opts.on_move then
actions[#actions + 1] = {name = 'move_up', icon = 'arrow_upward', label = t('Move up')}
actions[#actions + 1] = {name = 'move_down', icon = 'arrow_downward', label = t('Move down')}
actions[#actions + 1] = {name = 'move_up', icon = 'arrow_upward', label = t('Move up') .. ' (ctrl+up)'}
actions[#actions + 1] = {name = 'move_down', icon = 'arrow_downward', label = t('Move down' .. ' (ctrl+down)')}
end
if opts.on_remove or opts.on_delete then
local label = opts.on_remove and t('Remove') or t('Delete')
if opts.on_remove and opts.on_delete then label = t('Remove (ctrl to delete)') end
local label = (opts.on_remove and t('Remove') or t('Delete')) .. ' (del)'
if opts.on_remove and opts.on_delete then
label = t('Remove') .. ' (' .. t('%s to delete', 'del, ctrl+del') .. ')'
end
actions[#actions + 1] = {name = 'remove', icon = 'delete', label = label}
end

Expand Down Expand Up @@ -129,9 +131,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({'', 'shift'}, event.modifiers) then
elseif itable_has({'', 'alt'}, event.modifiers) then
opts.on_activate(event --[[@as MenuEventActivate]])
if event.modifiers == 'shift' then menu:close() end
if event.modifiers ~= 'alt' then menu:close() end
end
elseif event.type == 'key' then
if event.id == 'enter' then
Expand Down Expand Up @@ -258,7 +260,7 @@ function create_select_tracklist_type_menu_opener(menu_title, track_type, track_
})
end

---@alias NavigationMenuOptions {type: string, title?: string, allowed_types?: string[], keep_open?: boolean, active_path?: string, selected_path?: string; on_close?: fun()}
---@alias NavigationMenuOptions {type: string, title?: string, allowed_types?: string[], file_actions?: MenuAction[], directory_actions?: MenuAction[], active_path?: string, selected_path?: string; on_close?: fun()}

-- Opens a file navigation menu with items inside `directory_path`.
---@param directory_path string
Expand Down Expand Up @@ -346,11 +348,17 @@ function open_file_navigation_menu(directory_path, handle_activate, opts)
local selected_index = #items + 1

for _, dir in ipairs(directories) do
items[#items + 1] = {title = dir, value = join_path(path, dir), hint = separator}
items[#items + 1] = {
title = dir .. ' ' .. separator,
value = join_path(path, dir),
bold = true,
actions = opts
.directory_actions,
}
end

for _, file in ipairs(files) do
items[#items + 1] = {title = file, value = join_path(path, file)}
items[#items + 1] = {title = file, value = join_path(path, file), actions = opts.file_actions}
end

for index, item in ipairs(items) do
Expand Down Expand Up @@ -423,11 +431,10 @@ function open_file_navigation_menu(directory_path, handle_activate, opts)
return
end

if info.is_dir and event.modifiers == '' then
if info.is_dir and event.modifiers == '' and event.action == nil then
open_directory(path)
else
handle_activate(event)
if not opts.keep_open then close() end
end
end
menu = Menu:open(menu_data, function(event)
Expand Down Expand Up @@ -657,7 +664,7 @@ function open_stream_quality_menu()
mp.register_event('file-loaded', seeker)
end

if event.modifiers ~= 'shift' then menu:close() end
if not event.alt then menu:close() end
end
end)
end
Expand Down Expand Up @@ -701,17 +708,27 @@ function open_open_file_menu()
function(event)
if not menu then return end
local command = has_any_extension(event.value, config.types.playlist) and 'loadlist' or 'loadfile'
if itable_has({'ctrl', 'ctrl+shift'}, event.modifiers) then
if event.modifiers == 'shift' or event.action == 'add_to_playlist' then
mp.commandv(command, event.value, 'append')
elseif event.modifiers == '' then
local serialized = serialize_path(event.value)
local filename = serialized and serialized.basename or event.value
mp.commandv('show-text', t('Added to playlist') .. ': ' .. filename, 3000)
elseif itable_has({'', 'ctrl', 'alt', 'alt+ctrl'}, event.modifiers) and itable_has({nil, 'force_open'}, event.action) then
mp.commandv(command, event.value)
menu:close()
if not event.alt then menu:close() end
end
end,
{
type = 'open-file',
allowed_types = config.types.media,
active_path = active_file,
directory_actions = {
{name = 'add_to_playlist', icon = 'playlist_add', label = 'Add to playlist (shift)'},
{name = 'force_open', icon = 'folder_open', label = 'Open in mpv (ctrl)'},
},
file_actions = {
{name = 'add_to_playlist', icon = 'playlist_add', label = 'Add to playlist (shift)'},
},
keep_open = true,
on_close = function() mp.unregister_event(handle_file_loaded) end,
}
Expand All @@ -734,6 +751,8 @@ function create_track_loader_menu_opener(opts)
return
end

---@type Menu
local menu
local path = state.path
if path then
if is_protocol(path) then
Expand All @@ -748,10 +767,14 @@ function create_track_loader_menu_opener(opts)
end

local function handle_activate(event)
if event.modifiers == '' then load_track(opts.prop, event.value) end
load_track(opts.prop, event.value)
local serialized = serialize_path(event.value)
local filename = serialized and serialized.basename or event.value
mp.commandv('show-text', t('Loaded subtitles') .. ': ' .. filename, 3000)
if not event.alt then menu:close() end
end

open_file_navigation_menu(path, handle_activate, {
menu = open_file_navigation_menu(path, handle_activate, {
type = menu_type, title = title, allowed_types = opts.allowed_types,
})
end
Expand Down
4 changes: 2 additions & 2 deletions src/uosc/lib/std.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ end
---@param value any
---@return integer|nil
function itable_index_of(itable, value)
for index, item in ipairs(itable) do
if item == value then return index end
for index = 1, #itable do
if itable[index] == value then return index end
end
end

Expand Down

0 comments on commit c139963

Please sign in to comment.