Skip to content

Commit

Permalink
move title escaping code to lua
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Feb 7, 2024
1 parent 3d5fa54 commit a349305
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 643 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ configure_file(${PROJECT_SOURCE_DIR}/menu.rc.in ${PROJECT_BINARY_DIR}/menu.rc @O

set(CMAKE_SHARED_LIBRARY_PREFIX "")
add_library(menu SHARED
src/mpv/misc/bstr.c
src/mpv/misc/dispatch.c
src/mpv/ta/ta.c
src/mpv/ta/ta_talloc.c
Expand Down
52 changes: 26 additions & 26 deletions src/lua/dyn_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local msg = require('mp.msg')
-- user options
local o = {
uosc_syntax = false, -- toggle uosc menu syntax support
escape_title = true, -- escape & to && in menu title
max_title_length = 80, -- limit the title length, set to 0 to disable.
max_playlist_items = 20, -- limit the playlist items in submenu, set to 0 to disable.
}
Expand Down Expand Up @@ -232,8 +233,8 @@ local function build_track_items(list, type, prop, prefix)
local state = {}
-- there may be 2 tracks selected at the same time, for example: subtitle
if track.selected then
table.insert(state, 'checked')
if track.id ~= pos then table.insert(state, 'disabled') end
state[#state + 1] = 'checked'
if track.id ~= pos then state[#state + 1] = 'disabled' end
end

items[#items + 1] = {
Expand Down Expand Up @@ -281,11 +282,11 @@ local function update_tracks_menu(menu)
local items_s = build_track_items(track_list, 'sub', 'sid', true)

-- append video/audio/sub tracks into one submenu, separated by a separator
for _, item in ipairs(items_v) do table.insert(submenu, item) end
if #submenu > 0 and #items_a > 0 then table.insert(submenu, { type = 'separator' }) end
for _, item in ipairs(items_a) do table.insert(submenu, item) end
if #submenu > 0 and #items_s > 0 then table.insert(submenu, { type = 'separator' }) end
for _, item in ipairs(items_s) do table.insert(submenu, item) end
for _, item in ipairs(items_v) do submenu[#submenu + 1] = item end
if #submenu > 0 and #items_a > 0 then submenu[#submenu + 1] = { type = 'separator' } end
for _, item in ipairs(items_a) do submenu[#submenu + 1] = item end
if #submenu > 0 and #items_s > 0 then submenu[#submenu + 1] = { type = 'separator' } end
for _, item in ipairs(items_s) do submenu[#submenu + 1] = item end
end

-- handle #@tracks/<type> menu update for given type
Expand All @@ -295,7 +296,7 @@ local function update_track_menu(menu, type, prop)
if #track_list == 0 then return end

local items = build_track_items(track_list, type, prop, false)
for _, item in ipairs(items) do table.insert(submenu, item) end
for _, item in ipairs(items) do submenu[#submenu + 1] = item end
end

-- handle #@chapters menu update
Expand Down Expand Up @@ -418,7 +419,7 @@ local function update_menu_state(menu)

local state = {}
if type(res) == 'string' then
for s in res:gmatch('[^,%s]+') do table.insert(state, s) end
for s in res:gmatch('[^,%s]+') do state[#state + 1] = s end
end
menu.item.state = state
menu_items_dirty = true
Expand Down Expand Up @@ -456,7 +457,7 @@ local function dyn_menu_load(item, keyword)
state = nil,
dirty = false,
}
table.insert(dyn_menus, menu)
dyn_menus[#dyn_menus + 1] = menu
keyword_to_menu[keyword] = menu

local expr = keyword:match('^state=(.-)%s*$')
Expand Down Expand Up @@ -620,38 +621,37 @@ local function parse_input_conf(conf)
return list
end

local function append_menu(menu, type, title, cmd, submenu)
menu[#menu + 1] = {
type = type,
title = (title and o.escape_title) and title:gsub('&', '&&') or title,
cmd = cmd,
submenu = submenu,
}
end

for line in conf:gmatch('[^\r\n]+') do
if line:sub(1, 1) ~= '#' or o.uosc_syntax then
local key, cmd = line:match('%s*([%S]+)%s+(.-)%s*$')
local title = extract_title(cmd)
local list = split_title(title)

local submenu_id = ''
local target_menu = items
local key, cmd = line:match('%s*([%S]+)%s+(.-)%s*$')
local list = split_title(extract_title(cmd))

for id, name in ipairs(list) do
if id < #list then
submenu_id = submenu_id .. name
if not by_id[submenu_id] then
local submenu = {}
by_id[submenu_id] = submenu
target_menu[#target_menu + 1] = {
title = name,
type = 'submenu',
submenu = submenu,
}
append_menu(target_menu, 'submenu', name, nil, submenu)
end
target_menu = by_id[submenu_id]
else
if name == '-' or (o.uosc_syntax and name:sub(1, 3) == '---') then
target_menu[#target_menu + 1] = {
type = 'separator',
}
append_menu(target_menu, 'separator')
else
target_menu[#target_menu + 1] = {
title = (key ~= '' and key ~= '_') and (name .. "\t" .. key) or name,
cmd = cmd,
}
local title = (key ~= '' and key ~= '_') and (name .. "\t" .. key) or name
append_menu(target_menu, nil, title, cmd)
end
end
end
Expand Down
23 changes: 2 additions & 21 deletions src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,9 @@
// SPDX-License-Identifier: GPL-2.0-only

#include <windows.h>
#include "misc/bstr.h"
#include "mpv_talloc.h"
#include "menu.h"

// escape & to && for menu title
static wchar_t *escape_title(void *talloc_ctx, char *title) {
void *tmp = talloc_new(NULL);
bstr left, rest;
bstr escaped = bstr0(NULL);

left = bstr_split(bstr0(title), "&", &rest);
while (rest.len > 0) {
bstr_xappend(tmp, &escaped, left);
bstr_xappend(tmp, &escaped, bstr0("&&"));
left = bstr_split(rest, "&", &rest);
}
bstr_xappend(tmp, &escaped, left);

wchar_t *ret = mp_from_utf8(talloc_ctx, bstrdup0(tmp, escaped));
talloc_free(tmp);
return ret;
}

// append menu item to HMENU
static int append_menu(HMENU hmenu, UINT fMask, UINT fType, UINT fState,
wchar_t *title, HMENU submenu, void *data) {
Expand Down Expand Up @@ -135,7 +116,7 @@ static void build_menu(void *talloc_ctx, HMENU hmenu, mpv_node *node) {
strcmp(cmd, "ignore") == 0;
}
int id = append_menu(hmenu, fMask, 0, (UINT)fState,
escape_title(talloc_ctx, title), submenu,
mp_from_utf8(talloc_ctx, title), submenu,
talloc_strdup(talloc_ctx, cmd));
if (grayed) EnableMenuItem(hmenu, id, MF_BYCOMMAND | MF_GRAYED);
}
Expand Down
Loading

0 comments on commit a349305

Please sign in to comment.