moment/src/gui/Base/HMenu.qml

71 lines
2.0 KiB
QML
Raw Normal View History

2019-12-19 22:46:16 +11:00
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12
import QtQuick.Controls 2.12
import CppUtils 0.1
Menu {
2019-08-22 06:23:22 +10:00
id: menu
modal: true
dim: false
2019-08-22 06:23:22 +10:00
padding: theme.controls.menu.borderWidth
implicitWidth: {
2019-09-10 01:21:31 +10:00
let result = 0
2019-08-22 06:23:22 +10:00
for (let i = 0; i < count; ++i) {
2020-03-08 19:46:20 +11:00
const item = itemAt(i)
if (! item.visible) continue
2019-09-10 01:21:31 +10:00
result = Math.max(item.implicitWidth, result)
2019-08-22 06:23:22 +10:00
}
2019-09-10 01:21:31 +10:00
return Math.min(result + menu.padding * 2, window.width)
2019-08-22 06:23:22 +10:00
}
2019-08-28 12:46:31 +10:00
background: Rectangle {
color: theme.controls.menu.background
2019-08-22 06:23:22 +10:00
border.color: theme.controls.menu.border
border.width: theme.controls.menu.borderWidth
Item {
// Workaround for this: when opening the menu at cursor position,
// cursor will be in the menu's border instead of first menu item,
// forcing the user to move the mouse for the click to do anything.
width: parent.width
height: parent.border.width
TapHandler {
gesturePolicy: TapHandler.ReleaseWithinBounds
onTapped: if (menu.itemAt(0) && menu.itemAt(0).clicked)
menu.itemAt(0).clicked()
}
}
2019-08-22 06:23:22 +10:00
}
onAboutToShow: {
previouslyFocused = window.activeFocusItem
focusOnClosed = Qt.binding(() => previouslyFocused)
}
onOpened: {
window.visibleMenus[uuid] = this
window.visibleMenusChanged()
menu.currentIndex = 0
}
onClosed: {
if (focusOnClosed) focusOnClosed.forceActiveFocus()
delete window.visibleMenus[uuid]
window.visibleMenusChanged()
}
Component.onDestruction: closed()
property var previouslyFocused: null
// MenuItems that open popups (or other elements taking focus when opened)
// should set this to null. It will be reset to previouslyFocus when
// the Menu is closed and opened again.
property Item focusOnClosed: previouslyFocused
readonly property string uuid: CppUtils.uuid()
}