/*******************************************************************************
This file is part of the Shellfish UI toolkit.
Copyright (c) 2020 - 2024 Martin Grimme <martin.grimme@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/
require "shellfish/ui";
/**
* Element representing a drop-down selection box.
*
* @example
* SelectionBox {
* model: ListModel { data: ["One", "Two", "Three"] }
* }
*
* @memberof ui
* @name SelectionBox
* @class
* @extends ui.Button
*
* @property {string} emptyText - (default: `""`) The text to show if the model is empty.
* @property {function} formatText - A function `(modelData, index) => <string>` for creating the display text.
* @property {html.ListModel} model - (default: `ListModel { }`) The list model holding the selection items. If the items are not strings, specify a `formatText` function.
* @property {number} selection - (default: `0`) The index of the currently selected item.
*/
Button {
id: box
property model: ListModel { }
property formatText: (item, idx) => "" + item
property selection: 0
property emptyText: "?"
minWidth: theme.itemWidthLarge
secondaryIcon: "core-menu"
text: model.size > selection ? formatText(model.at(selection), selection) : emptyText
onPointerDown: ev =>
{
ev.accepted = true;
const menu = _menuT();
menu.popup(self);
}
onWheel: (ev) =>
{
ev.accepted = true;
if (ev.deltaY < 0 && selection > 0)
{
--selection;
}
else if (ev.deltaY > 0 && selection < model.size - 1)
{
++selection;
}
}
onKeyDown: (ev) =>
{
if (ev.key === "ArrowUp")
{
if (selection > 0)
{
--selection;
}
ev.accepted = true;
}
else if (ev.key === "ArrowDown")
{
if (selection < model.size - 1)
{
++selection;
}
ev.accepted = true;
}
}
property _menuT: template Menu {
id: selMenu
property selection: box.selection
onCascadingClose: () =>
{
// give focus back to the button when the menu closes
box.focus = true;
}
// reserve space for all items
Box {
width: box.bboxWidth - 2 /* menu borders */
height: theme.itemHeightMedium * box.model.size
}
Repeater {
id: itemRepeater
model: box.model
dynamicItems: model.data.map((v, idx) => idx).filter(
idx => (idx + 1) * theme.itemHeightMedium > thisMenu.menu.contentY &&
idx * theme.itemHeightMedium < thisMenu.menu.contentY + thisMenu.menu.bboxHeight ||
idx === thisMenu.selection
)
delegate: template MenuItem {
position: "free"
y: modelData.index * bboxHeight
text: box.formatText(modelData.value, modelData.index)
onInitialization: () =>
{
if (modelData.index === thisMenu.selection)
{
wait(10).then(() =>
{
selectItem(false);
});
}
}
thisMenu.onSelectionChanged: () =>
{
if (modelData.index === thisMenu.selection)
{
selectItem(false);
}
}
// custom navigation on dynamically created items
onKeyDown: ev =>
{
if (ev.key === "ArrowUp")
{
thisMenu.selection = (box.model.size + thisMenu.selection - 1) % box.model.size;
ev.accepted = true;
}
else if (ev.key === "ArrowDown")
{
thisMenu.selection = (thisMenu.selection + 1) % box.model.size;
ev.accepted = true;
}
}
onClick: ev =>
{
box.selection = modelData.index;
}
}
}
}
}