Skip to content

Commit

Permalink
feat(api)!: MenuBase.actions renamed to MenuBase.item_actions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tomasklaen committed Sep 1, 2024
1 parent c183c37 commit 57fd8c3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/uosc/elements/Menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/uosc/lib/menus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 57fd8c3

Please sign in to comment.