From 57fd8c33dddc711b236fdd96eaf81f510b4abc3b Mon Sep 17 00:00:00 2001 From: tomasklaen Date: Sun, 1 Sep 2024 14:43:30 +0200 Subject: [PATCH] feat(api)!: `MenuBase.actions` renamed to `MenuBase.item_actions` In future, we might add menu scoped actions that will be displayed inside/outside menu title, and I want `MenuBase.actions` to be reserved for that. --- src/uosc/elements/Menu.lua | 16 ++++++++-------- src/uosc/lib/menus.lua | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/uosc/elements/Menu.lua b/src/uosc/elements/Menu.lua index 18b0d195..2a7a4c0e 100644 --- a/src/uosc/elements/Menu.lua +++ b/src/uosc/elements/Menu.lua @@ -3,13 +3,13 @@ local Element = require('elements/Element') ---@alias MenuAction {name: string; icon: string; label?: string;} -- Menu data structure accepted by `Menu:open(menu)`. ----@alias MenuData {id?: string; type?: string; title?: string; hint?: string; footnote: string; search_style?: 'on_demand' | 'palette' | 'disabled'; actions?: MenuAction[]; callback?: string[]; keep_open?: boolean; bold?: boolean; italic?: boolean; muted?: boolean; separator?: boolean; align?: 'left'|'center'|'right'; items?: MenuDataItem[]; selected_index?: integer; on_search?: string|string[]; on_paste?: string|string[]; on_move?: string|string[]; on_close?: string|string[]; search_debounce?: number|string; search_submenus?: boolean; search_suggestion?: string} +---@alias MenuData {id?: string; type?: string; title?: string; hint?: string; footnote: string; search_style?: 'on_demand' | 'palette' | 'disabled'; item_actions?: MenuAction[]; callback?: string[]; keep_open?: boolean; bold?: boolean; italic?: boolean; muted?: boolean; separator?: boolean; align?: 'left'|'center'|'right'; items?: MenuDataItem[]; selected_index?: integer; on_search?: string|string[]; on_paste?: string|string[]; on_move?: string|string[]; on_close?: string|string[]; search_debounce?: number|string; search_submenus?: boolean; search_suggestion?: string} ---@alias MenuDataItem MenuDataValue|MenuData ---@alias MenuDataValue {title?: string; hint?: string; icon?: string; value: any; actions?: MenuAction[]; active?: boolean; keep_open?: boolean; selectable?: boolean; bold?: boolean; italic?: boolean; muted?: boolean; separator?: boolean; align?: 'left'|'center'|'right'} ---@alias MenuOptions {mouse_nav?: boolean;} -- Internal data structure created from `MenuData`. ----@alias MenuStack {id?: string; type?: string; title?: string; hint?: string; footnote: string; search_style?: 'on_demand' | 'palette' | 'disabled'; actions?: MenuAction[]; callback?: string[]; selected_index?: number; action_index?: number; keep_open?: boolean; bold?: boolean; italic?: boolean; muted?: boolean; separator?: boolean; align?: 'left'|'center'|'right'; items: MenuStackItem[]; on_search?: string|string[]; on_paste?: string|string[]; on_move?: string|string[]; on_close?: string|string[]; search_debounce?: number|string; search_submenus?: boolean; search_suggestion?: string; parent_menu?: MenuStack; submenu_path: integer[]; active?: boolean; width: number; height: number; top: number; scroll_y: number; scroll_height: number; title_width: number; hint_width: number; max_width: number; is_root?: boolean; fling?: Fling, search?: Search, ass_safe_title?: string} +---@alias MenuStack {id?: string; type?: string; title?: string; hint?: string; footnote: string; search_style?: 'on_demand' | 'palette' | 'disabled'; item_actions?: MenuAction[]; callback?: string[]; selected_index?: number; action_index?: number; keep_open?: boolean; bold?: boolean; italic?: boolean; muted?: boolean; separator?: boolean; align?: 'left'|'center'|'right'; items: MenuStackItem[]; on_search?: string|string[]; on_paste?: string|string[]; on_move?: string|string[]; on_close?: string|string[]; search_debounce?: number|string; search_submenus?: boolean; search_suggestion?: string; parent_menu?: MenuStack; submenu_path: integer[]; active?: boolean; width: number; height: number; top: number; scroll_y: number; scroll_height: number; title_width: number; hint_width: number; max_width: number; is_root?: boolean; fling?: Fling, search?: Search, ass_safe_title?: string} ---@alias MenuStackItem MenuStackValue|MenuStack ---@alias MenuStackValue {title?: string; hint?: string; icon?: string; value: any; actions?: MenuAction[]; active?: boolean; keep_open?: boolean; selectable?: boolean; bold?: boolean; italic?: boolean; muted?: boolean; separator?: boolean; align?: 'left'|'center'|'right'; title_width: number; hint_width: number; ass_safe_hint?: string} ---@alias Fling {y: number, distance: number, time: number, easing: fun(x: number), duration: number, update_cursor?: boolean} @@ -473,7 +473,7 @@ end function Menu:select_action(index, menu_id) local menu = self:get_menu(menu_id) if not menu then return end - local actions = menu.items[menu.selected_index] and menu.items[menu.selected_index].actions or menu.actions + local actions = menu.items[menu.selected_index] and menu.items[menu.selected_index].actions or menu.item_actions if not index or not actions or type(actions) ~= 'table' or index < 1 or index > #actions then menu.action_index = nil return @@ -487,7 +487,7 @@ end function Menu:navigate_action(delta, menu_id) local menu = self:get_menu(menu_id) if not menu then return end - local actions = menu.items[menu.selected_index] and menu.items[menu.selected_index].actions or menu.actions + local actions = menu.items[menu.selected_index] and menu.items[menu.selected_index].actions or menu.item_actions if actions and delta ~= 0 then -- Circular navigation where zero gets converted to nil local index = (menu.action_index or (delta > 0 and 0 or #actions + 1)) + delta @@ -629,7 +629,7 @@ function Menu:activate_selected_item(shortcut) self:tween(self.offset_x + menu.width / 2, 0, function(offset) self:set_offset_x(offset) end) self.opacity = 1 -- in case tween above canceled fade in animation else - local actions = item.actions or menu.actions + local actions = item.actions or menu.item_actions local action = actions and actions[menu.action_index] self.callback({ type = 'activate', @@ -766,7 +766,7 @@ function Menu:paste() if not payload then return end if menu.on_paste then local selected_item = menu.items and menu.selected_index and menu.items[menu.selected_index] - local actions = selected_item and selected_item.actions or menu.actions + local actions = selected_item and selected_item.actions or menu.item_actions local selected_action = actions and menu.action_index and actions[menu.action_index] self:command_or_event(menu.on_paste, {payload, menu.id}, { type = 'paste', @@ -1065,7 +1065,7 @@ function Menu:handle_shortcut(shortcut, info) local menu, id, key, modifiers = self.current, shortcut.id, shortcut.key, shortcut.modifiers local selected_index = menu.selected_index local selected_item = menu and selected_index and menu.items[selected_index] - local actions = selected_item and selected_item.actions or menu.actions + local actions = selected_item and selected_item.actions or menu.item_actions local selected_action = actions and menu.action_index and actions[menu.action_index] if info.event == 'down' then return end @@ -1291,7 +1291,7 @@ function Menu:render() local next_is_active = next_item and next_item.active local next_has_background = menu.selected_index == index + 1 or next_is_active local font_color = item.active and fgt or bgt - local actions = is_selected and (item.actions or menu.actions) -- not nil = actions are visible + local actions = is_selected and (item.actions or menu.item_actions) -- not nil = actions are visible local action = actions and actions[menu.action_index] -- not nil = action is selected -- Separator diff --git a/src/uosc/lib/menus.lua b/src/uosc/lib/menus.lua index fe9af6ec..fa71d4f0 100644 --- a/src/uosc/lib/menus.lua +++ b/src/uosc/lib/menus.lua @@ -116,7 +116,7 @@ function create_self_updating_menu_opener(opts) title = opts.title, footnote = opts.footnote, items = initial_items, - actions = actions, + item_actions = actions, selected_index = selected_index, on_move = opts.on_move and 'callback' or nil, on_paste = opts.on_paste and 'callback' or nil,