Skip to content

Commit

Permalink
refactor: add on_key to create_self_updating_menu_opener() options
Browse files Browse the repository at this point in the history
ref #966
  • Loading branch information
tomasklaen committed Sep 1, 2024
1 parent 16fc7ae commit 5011139
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/uosc/lib/menus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function toggle_menu_with_items(opts)
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);}
---@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())}
function create_self_updating_menu_opener(opts)
return function()
if Menu:is_open(opts.type) then
Expand Down Expand Up @@ -73,6 +73,12 @@ function create_self_updating_menu_opener(opts)
end
end

local function cleanup_and_close()
mp.unobserve_property(handle_list_prop_change)
mp.unobserve_property(handle_active_prop_change)
menu:close()
end

local initial_items, selected_index = opts.serializer(list, active)

---@type MenuAction[]
Expand Down Expand Up @@ -125,18 +131,18 @@ function create_self_updating_menu_opener(opts)
elseif event.type == 'key' then
local item = event.selected_item
if event.id == 'enter' then
menu:close()
cleanup_and_close()
elseif event.key == 'del' 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]])
elseif event.type == 'close' then
mp.unobserve_property(handle_list_prop_change)
mp.unobserve_property(handle_active_prop_change)
menu:close()
cleanup_and_close()
elseif event.type == 'move' and opts.on_move then
opts.on_move(event --[[@as MenuEventMove]])
elseif event.type == 'remove' and opts.on_move then
Expand Down

0 comments on commit 5011139

Please # to comment.