Skip to content

Commit 0a53407

Browse files
guidocellakasper93
authored andcommitted
context_menu.lua: prevent some rare crashes
Fix crashes that won't happen in regular usage: - If you open the menu with an empty menu-data and use keybindings - If you open the menu with menu-data only containing hidden items - If you invoke script-message-to context_menu select with the menu closed
1 parent 6e8283d commit 0a53407

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

player/lua/context_menu.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ local options = {
3333
seconds_to_close_submenus = 0.2,
3434
}
3535

36-
local open_menus
36+
local open_menus = {}
3737
local items
3838
local focused_level = 1
3939
local focused_index
@@ -181,17 +181,17 @@ local function calculate_height(menu_items)
181181
end
182182

183183
local function add_menu(menu_items, x, y)
184-
if not menu_items[1] then
185-
return
186-
end
187-
188184
local visible_items = {}
189185
for _, item in ipairs(menu_items) do
190186
if not has_state(item, "hidden") then
191187
visible_items[#visible_items + 1] = item
192188
end
193189
end
194190

191+
if not visible_items[1] then
192+
return
193+
end
194+
195195
local checkbox = has_checkbox(visible_items)
196196
local osd_w, osd_h = get_scaled_osd_dimensions()
197197
local width = calculate_width(visible_items, osd_w, osd_h, checkbox) +
@@ -628,6 +628,10 @@ mp.register_script_message("open", function ()
628628

629629
add_menu(mp.get_property_native("menu-data"), x, y)
630630

631+
if not open_menus[1] then
632+
return
633+
end
634+
631635
render()
632636

633637
for key, fn in pairs(bindings) do
@@ -638,6 +642,10 @@ mp.register_script_message("open", function ()
638642
end
639643
end)
640644

641-
mp.register_script_message("select", activate_focused_item)
645+
mp.register_script_message("select", function ()
646+
if open_menus[1] then
647+
activate_focused_item()
648+
end
649+
end)
642650

643651
require "mp.options".read_options(options)

0 commit comments

Comments
 (0)