/*******************************************************************************
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 regular menu item.
*
* @memberof ui
* @name MenuItem
* @class
* @extends ui.MenuItemBase
*
* @property {string} icon - (default: `""`) The ID of an optional icon.
* @property {string} text - (default: `""`) The menu item label text.
* @property {string} shortcut - (default: `""`) A keyboard shortcut. Separate the modifer and key with `+`, e.g. `"Ctrl+N"`.
* Valid modifiers are `Ctrl`, `Alt`, and `Shift`.
*/
MenuItemBase {
id: item
property icon: ""
property text: ""
property shortcut: ""
function format(t)
{
const pos = t.indexOf("&");
if (pos === -1)
{
return t;
}
else
{
return t.substr(0, pos) + "_" + t[pos + 1] + "_" + t.substr(pos + 2);
}
}
shortcutReceiver.onKeyDown: (ev) =>
{
if (ev.altKey)
{
const idx = text.indexOf("&");
if (idx !== -1)
{
const key = text[idx + 1].toLowerCase();
if (ev.key === key)
{
pointerDown(ev);
ev.accepted = true;
}
}
}
else if (shortcut !== "")
{
const parts = shortcut.split("+").filter((key) =>
{
if (key === "Ctrl")
{
return ! ev.ctrlKey;
}
else if (key === "Alt")
{
return ! ev.altKey;
}
else if (key === "Shift")
{
return ! ev.shiftKey;
}
else
{
return ev.key !== key.toLowerCase();
}
});
if (parts.length === 0)
{
pointerDown(ev);
ev.accepted = true;
}
}
}
Box {
fillWidth: true
fillHeight: true
layout: "center-row"
Label {
id: iconLabel
visible: item.icon !== ""
marginLeft: theme.paddingSmall
width: label.bboxHeight
height: width
color: label.color
text: visible ? "[icon:" + item.icon + "]" : ""
}
Box { width: theme.paddingMedium }
Label {
id: label
fillWidth: true
overflowBehavior: "ellipsis"
color: ancestorsEnabled ? (item.highlighted ? theme.highlightColor : theme.primaryColor)
: theme.disabledColor
text: item.format(item.text)
}
Box { width: theme.paddingMedium }
Box {
id: shortcutLabel
visible: item.shortcut !== ""
marginLeft: theme.paddingLarge
marginRight: theme.paddingSmall
color: theme.secondaryColor
borderRadius: theme.borderRadius
Label {
marginTop: 2
marginLeft: 2
marginRight: 2
marginBottom: 2
fontSize: theme.fontSizeSmall
color: theme.primaryBackgroundColor
text: item.shortcut
}
}
Label {
id: arrowLabel
visible: !! item.menu && !! thisMenu
marginLeft: 2
marginRight: 2
width: theme.paddingMedium
fontSize: theme.fontSizeSmall
color: label.color
text: "[icon:core-arrow_right]"
}
}
}