/************************************************************ */ /* */ /* Copyright (C) Microsoft Corporation. All rights reserved. */ /* */ /**************************************************************/ import Colors 1.0 import Fonts 1.0 import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Controls.Styles 1.4 // This file contains formatted MenuItem objects. Since the menu styling will be uniform across menus in AC for consistency, colors are finalized. // Text is anchored relative to CustomMenuItem. User actions return, space, and clicking all use developer-defined callback function. // Keyboard up/down navigation between MenuItems are inherent to MenuItem object in QML. // However, developer can still change the following properties for additional customization: // - primarycolor // - hovercolor // - pressedcolor // - focuscolor // // Developer must change the following for each CustomMenuItem: // - textcontrol.txt (str) // - callback (function) MenuItem { id: menuItem property int menuItemHeight: 38 property int menuItemMargin: 10 property int menuItemWidth: txt.paintedWidth + iconImage.paintedWidth + 2*menuItemMargin Accessible.onPressAction: doCallback(false) Accessible.name: text Accessible.disabled: !enabled property bool hasSeparator: false width: (parent != null) ? parent.width : 0 function chooseBGColor(isHovering, isPressed, isActiveFocus) { var color = primarycolor; if (isHovering) { color = hovercolor; } if (isPressed) { color = pressedcolor; } if (isActiveFocus) { color = focuscolor; } return color; } LayoutMirroring.enabled: headerModel.isRTL LayoutMirroring.childrenInherit: headerModel.isRTL height: menuItemHeight activeFocusOnTab: true // set MenuItem hoverEnabled to be false to fix the bug when mouse hover over the menu item, // it will automatically get focus hoverEnabled: false background: Rectangle { id: backgroundRect anchors.left: parent.left anchors.leftMargin: 4 anchors.right: parent.right anchors.rightMargin: 4 anchors.top: parent.top anchors.topMargin: 4 anchors.bottom: parent.bottom anchors.bottomMargin:4 radius: 4 color: chooseBGColor(ma.containsMouse, ma.containsPress, menuItem.activeFocus) border.color: menuItem.activeFocus ? itemBorderColor : "transparent"; } MouseArea { id: ma anchors.fill: parent hoverEnabled: true onClicked: doCallback(false) } property color primarycolor: Colors.activity_center.context_menu.background property color hovercolor: Colors.activity_center.context_menu.item_hover property color pressedcolor: Colors.activity_center.context_menu.item_pressed property color focuscolor: Colors.activity_center.context_menu.item_focus property color itemBorderColor: Colors.activity_center.context_menu.item_border property alias textcontrol: txt property alias shortcut: keyshortcut property alias image: iconImage property int imageSize: 0 text: txt.text contentItem: Item { id: itemContainer anchors.fill: parent // Icon image is not visible by default since size is originally 0 Image { id: iconImage width: imageSize height: imageSize sourceSize.width: imageSize sourceSize.height: imageSize anchors.left: parent.left anchors.leftMargin: menuItem.menuItemMargin anchors.verticalCenter: parent.verticalCenter opacity: menuItem.enabled ? 1 : 0.3 } Text { id: txt text: "sample" horizontalAlignment: Text.AlignLeft anchors.verticalCenter: parent.verticalCenter font.family: Fonts.standard font.pixelSize: 14 color: menuItem.enabled ? (menuItem.activeFocus || menuItem.hovered || ma.containsMouse || ma.containsPress ? Colors.common.text_hover : Colors.common.text) : Colors.common.text_disabled anchors.left: iconImage.right anchors.leftMargin: menuItem.menuItemMargin anchors.right: parent.right anchors.rightMargin: menuItem.menuItemMargin } } Shortcut { // make sure all items in a menu have a unique shortcut // otherwise, menu items with conflicting shortcut sequence will be ignored // Currently the shortcut is disabled by default. Users will have to enable the shortcut by switching enabled: true id: keyshortcut sequence: "sample" context: Qt.WindowShortcut onActivated: doCallback(true) enabled: false } // consuming code should set callback property to a function to run for clicks property var callback: genericCallback function genericCallback(isShortcut) { print("CustomMenuItem: no click handler set"); } function doCallback(isShortcut) { if (typeof(callback) === "function") { callback(isShortcut); } else { print("CustomMenuItem: callback is not a function"); } } onTriggered: doCallback(false) Keys.onReturnPressed: doCallback(false) // Note: Keys.onSpacePressed is not set here as it will cause two space events to be produced. // Look in QML's BasicButton::Keys.onReleased to see the event duplication that causes this (BasicButton is the parent of Button) // So we omit defining the action of space here, since the onReleased event of space will call the onClicked action. Keys.onReleased: { if (event.key === Qt.Key_Space) { doCallback(false); // set event to accepted so that we don't get a duplicated event event.accepted = true; } } }