Menu checkbox

This commit is contained in:
Luke committed 2022-10-29 22:20:43 +02:00
1 parent dfc45b1468
commit 146b1f9def
1 file changed
+52 -17
+52 -17
View File
@@ -36,11 +36,17 @@ lib.registerMenu(data, cb)
* values?: `table` (`Array<string | {label: string; description: string}>`) * values?: `table` (`Array<string | {label: string; description: string}>`)
* If provided creates a side scrollabel list. * If provided creates a side scrollabel list.
* When using object and setting description, the set description will be displayed in the menu tooltip. * When using object and setting description, the set description will be displayed in the menu tooltip.
* checked?: `boolean`
* Setting either true or false will make the button a checkbox, if `values` is also provided the button will be a
scrollable list.
* description?: `string` * description?: `string`
* Displays tooltip below menu on hovered item with provided description. * Displays tooltip below menu on hovered item with provided description.
* defaultIndex?: `number` * defaultIndex?: `number`
* Sets the current index for the list to specified number. * Sets the current index for the list to specified number.
* args?: `any` * args?: {[key: `string`]: `any`}
* Allows you to pass any arguments through the button.
* If the button has `values` then `isScroll` is automatically passed.
* If the button has `checked` to either true or false then `isCheck` is automatically passed.
* close?: `boolean` * close?: `boolean`
* If set to false, it won't close the menu upon interacting with this option. * If set to false, it won't close the menu upon interacting with this option.
* position?: `'top-left'` or `'top-right'` or `'bottom-left'` or `'bottom-right'` * position?: `'top-left'` or `'top-right'` or `'bottom-left'` or `'bottom-right'`
@@ -51,11 +57,12 @@ lib.registerMenu(data, cb)
* If set to false the user won't be able to exit the menu without pressing one of the buttons. * If set to false the user won't be able to exit the menu without pressing one of the buttons.
* onClose: `function`(keyPressed?: `'Escape' | 'Backspace'`) * onClose: `function`(keyPressed?: `'Escape' | 'Backspace'`)
* Function that runs when the menu is exited via ESC/Backspace. * Function that runs when the menu is exited via ESC/Backspace.
* onSelected: `function`(selected: `number`: scrollIndex: `number`: args: `any`) * onSelected: `function`(selected: `number`, secondary: `number` | `boolean`, args: {[key: `string`]: `any`})
* Function being ran when the selected button in the menu changes. * Function being ran when the selected button in the menu changes.
* onSideScroll: `function`(selected: `number`: scrollIndex: `number`: args: `any`) * onSideScroll: `function`(selected: `number`, scrollIndex: `number`, args: {[key: `string`]: `any`})
* Function ran whenever a scroll list item is changed. * Function ran whenever a scroll list item is changed.
* cb: `function`(selected: `number`: scrollIndex: `number`: args: `any`) * onCheck: `function`(selected: `number`, checked: `boolean`, args: {[key: `string`]: `any`})
* cb: `function`(selected: `number`, scrollIndex: `number`, args: {[key: `string`]: `any`})
* Callback function when the menu item is pressed. * Callback function when the menu item is pressed.
### lib.showMenu ### lib.showMenu
@@ -188,10 +195,24 @@ lib.registerMenu({
title = 'Menu title', title = 'Menu title',
position = 'top-right', position = 'top-right',
onSideScroll = function(selected, scrollIndex, args) onSideScroll = function(selected, scrollIndex, args)
print(selected, scrollIndex, args) print("Scroll: ", selected, scrollIndex, args)
end, end,
onSelected = function(selected, scrollIndex, args) onSelected = function(selected, secondary, args)
print(selected, scrollIndex, args) if not secondary then
print("Normal button")
else
if args.isCheck then
print("Check button")
end
if args.isScroll then
print("Scroll button")
end
end
print(selected, secondary, json.encode(args, {indent=true}))
end,
onCheck = function(selected, checked, args)
print("Check: ", selected, checked, args)
end, end,
onClose = function(keyPressed) onClose = function(keyPressed)
print('Menu closed') print('Menu closed')
@@ -201,9 +222,9 @@ lib.registerMenu({
end, end,
options = { options = {
{label = 'Simple button', description = 'It has a description!'}, {label = 'Simple button', description = 'It has a description!'},
{label = 'Simple button with icon', icon = 'arrows-up-down-left-right'}, {label = 'Checkbox button', checked = true},
{label = 'Simple button that won\'t close the menu', close = false}, {label = 'Scroll button with icon', icon = 'arrows-up-down-left-right', values={'hello', 'there'}},
{label = 'Button with args', args = 'nice_button'}, {label = 'Button with args', args = {someArg = 'nice_button'}},
{label = 'List button', values = {'You', 'can', 'side', 'scroll', 'this'}, description = 'It also has a description!'}, {label = 'List button', values = {'You', 'can', 'side', 'scroll', 'this'}, description = 'It also has a description!'},
{label = 'List button with default index', values = {'You', 'can', 'side', 'scroll', 'this'}, defaultIndex = 5}, {label = 'List button with default index', values = {'You', 'can', 'side', 'scroll', 'this'}, defaultIndex = 5},
{label = 'List button with args', values = {'You', 'can', 'side', 'scroll', 'this'}, args = {someValue = 3, otherValue = 'value'}}, {label = 'List button with args', values = {'You', 'can', 'side', 'scroll', 'this'}, args = {someValue = 3, otherValue = 'value'}},
@@ -227,10 +248,24 @@ lib.registerMenu({
title: 'Menu title', title: 'Menu title',
position: 'top-right', position: 'top-right',
onSideScroll: (selected, scrollIndex, args) => { onSideScroll: (selected, scrollIndex, args) => {
console.log(selected, scrollIndex, args) console.log("Scroll: ", selected, scrollIndex, args)
}, },
onSelected: (selected, scrollIndex, args) => { onSelected: (selected, secondary, args) => {
console.log(selected, scrollIndex, args) if (!secondary) {
console.log("Normal button")
} else {
if (args.isCheck) {
console.log("Check button")
}
if (args.isScroll) {
console.log("Scroll button")
}
}
console.log(selected, secondary, JSON.stringify(args, null, 2))
},
onCheck: (selected, checked, args) => {
console.log("Check: ", selected, checked, args)
}, },
onClose: (keyPressed) => { onClose: (keyPressed) => {
console.log('Menu closed') console.log('Menu closed')
@@ -240,9 +275,9 @@ lib.registerMenu({
}, },
options: [ options: [
{label: 'Simple button', description: 'It has a description!'}, {label: 'Simple button', description: 'It has a description!'},
{label: 'Simple button with icon', icon: 'arrows-up-down-left-right'}, {label: 'Checkbox button', checked: true},
{label: 'Simple button that won\'t close the menu', close: false}, {label: 'Scroll button with icon', icon: 'arrows-up-down-left-right', values: ['hello', 'there']},
{label: 'Button with args', args: 'nice_button'}, {label: 'Button with args', args: {someArg: 'nice_button'}},
{label: 'List button', values: ['You', 'can', 'side', 'scroll', 'this'], description: 'It also has a description!'}, {label: 'List button', values: ['You', 'can', 'side', 'scroll', 'this'], description: 'It also has a description!'},
{label: 'List button with default index', values: ['You', 'can', 'side', 'scroll', 'this'], defaultIndex: 5}, {label: 'List button with default index', values: ['You', 'can', 'side', 'scroll', 'this'], defaultIndex: 5},
{label: 'List button with args', values: ['You', 'can', 'side', 'scroll', 'this'], args: {someValue: 3, otherValue: 'value'}}, {label: 'List button with args', values: ['You', 'can', 'side', 'scroll', 'this'], args: {someValue: 3, otherValue: 'value'}},
@@ -258,4 +293,4 @@ RegisterCommand('testmenu', () => {
</TabItem> </TabItem>
</Tabs> </Tabs>
![Example](https://i.imgur.com/lmimH7e.png) ![Example](https://i.imgur.com/eIdWZ4c.png)