linphone-desktop/tests/ui/modules/Common/Popup/DropDownMenu.qml
2016-10-25 14:44:20 +02:00

113 lines
2.3 KiB
QML

import QtQuick 2.7
import Common 1.0
import Common.Styles 1.0
import Utils 1.0
// ===================================================================
// Low component to display a list/menu in a popup.
// ===================================================================
Rectangle {
// Attributes used only with a ListView child.
property int entryHeight
property int maxMenuHeight
// Optionnal parameter, if defined and if a click is detected
// on it, menu is not closed.
property var launcher
// Optionnal parameters, set the position of Menu relative
// to this item.
property var relativeTo
property int relativeX: 0
property int relativeY: 0
default property alias _content: content.data
signal menuClosed
signal menuOpened
function isOpen () {
return visible
}
function showMenu () {
if (visible) {
return
}
if (relativeTo != null) {
this.x = relativeTo.mapToItem(null, relativeX, relativeY).x
this.y = relativeTo.mapToItem(null, relativeX, relativeY).y
}
visible = true
menuOpened()
}
function hideMenu () {
if (!visible) {
return
}
visible = false
menuClosed()
}
function _computeHeight () {
var model = _content[0].model
if (model == null || !Utils.qmlTypeof(model, 'QQmlListModel')) {
return content.height
}
console.assert(
entryHeight != null,
'`entryHeight` must be defined when used with `ListView`.'
)
var height = model.count * entryHeight
return (maxMenuHeight !== undefined && height > maxMenuHeight)
? maxMenuHeight
: height
}
implicitHeight: _computeHeight()
visible: false
z: Constants.zPopup
Keys.onEscapePressed: hideMenu()
// Set parent menu to root.
Component.onCompleted: {
if (relativeTo != null) {
parent = Utils.getTopParent(this)
}
}
// Menu content.
Rectangle {
id: content
anchors.fill: parent
color: PopupStyle.backgroundColor
layer {
enabled: true
effect: PopupShadow {}
}
}
// Inverted mouse area to detect click outside menu.
InvertedMouseArea {
anchors.fill: parent
enabled: parent.visible
onPressed: {
if (launcher != null && pointIsInItem(launcher)) {
return
}
hideMenu()
}
}
}