2020-09-24 09:57:54 +10:00
|
|
|
// Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
|
2019-12-19 22:46:16 +11:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2019-08-22 04:58:57 +10:00
|
|
|
import QtQuick 2.12
|
|
|
|
import QtQuick.Controls 2.12
|
2020-04-01 23:01:13 +11:00
|
|
|
import CppUtils 0.1
|
2019-08-22 04:58:57 +10:00
|
|
|
|
|
|
|
Menu {
|
2019-08-22 06:23:22 +10:00
|
|
|
id: menu
|
2020-07-12 14:25:57 +10:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2020-03-27 12:55:30 +11:00
|
|
|
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)
|
2019-08-22 06:41:52 +10:00
|
|
|
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 {
|
2019-09-07 06:11:25 +10:00
|
|
|
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
|
2021-01-22 01:09:06 +11:00
|
|
|
radius: theme.radius
|
2020-07-10 12:38:08 +10:00
|
|
|
|
2020-07-10 17:36:16 +10:00
|
|
|
// Workaround for this: when opening menu at mouse position,
|
|
|
|
// cursor will be in menu's border which doesn't handle clicks
|
|
|
|
TapHandler {
|
|
|
|
gesturePolicy: TapHandler.ReleaseWithinBounds
|
|
|
|
onTapped: eventPoint => {
|
|
|
|
const pos = eventPoint.position
|
|
|
|
const border = parent.border.width
|
2020-07-10 12:38:08 +10:00
|
|
|
|
2020-07-10 17:36:16 +10:00
|
|
|
if (pos.x <= border || pos.x >= parent.width - border)
|
|
|
|
menu.close()
|
|
|
|
|
|
|
|
if (pos.y <= border || pos.y >= parent.height - border)
|
|
|
|
menu.close()
|
2020-07-10 12:38:08 +10:00
|
|
|
}
|
|
|
|
}
|
2019-08-22 06:23:22 +10:00
|
|
|
}
|
2019-12-12 23:36:51 +11:00
|
|
|
|
2019-12-21 01:29:45 +11:00
|
|
|
onAboutToShow: {
|
|
|
|
previouslyFocused = window.activeFocusItem
|
|
|
|
focusOnClosed = Qt.binding(() => previouslyFocused)
|
|
|
|
}
|
2020-07-12 14:25:57 +10:00
|
|
|
|
2020-04-01 23:01:13 +11:00
|
|
|
onOpened: {
|
|
|
|
window.visibleMenus[uuid] = this
|
|
|
|
window.visibleMenusChanged()
|
|
|
|
}
|
2020-07-12 14:25:57 +10:00
|
|
|
|
2020-04-01 23:01:13 +11:00
|
|
|
onClosed: {
|
|
|
|
if (focusOnClosed) focusOnClosed.forceActiveFocus()
|
|
|
|
delete window.visibleMenus[uuid]
|
|
|
|
window.visibleMenusChanged()
|
|
|
|
}
|
|
|
|
|
2020-07-12 14:25:57 +10:00
|
|
|
Component.onDestruction: closed()
|
2019-08-22 04:58:57 +10:00
|
|
|
}
|