diff --git a/UM/Qt/qml/UM/Menu.qml b/UM/Qt/qml/UM/Menu.qml index e5ea2a6d24..337007d69b 100644 --- a/UM/Qt/qml/UM/Menu.qml +++ b/UM/Qt/qml/UM/Menu.qml @@ -21,15 +21,18 @@ Menu } // Automatically set the width to fit the widest MenuItem - // https://martin.rpdev.net/2018/03/13/qt-quick-controls-2-automatically-set-the-width-of-menus.html + // Based on https://martin.rpdev.net/2018/03/13/qt-quick-controls-2-automatically-set-the-width-of-menus.html width: { var result = 0; var padding = 0; for (var i = 0; i < count; ++i) { var item = itemAt(i); - result = Math.max(item.contentItem.implicitWidth, result); - padding = Math.max(item.padding, padding); + if (item.hasOwnProperty("contentWidth")) + { + result = Math.max(item.contentWidth, result); + padding = Math.max(item.padding, padding); + } } return result + padding * 2; } diff --git a/UM/Qt/qml/UM/MenuItem.qml b/UM/Qt/qml/UM/MenuItem.qml index 215be8240d..6c8035dc6a 100644 --- a/UM/Qt/qml/UM/MenuItem.qml +++ b/UM/Qt/qml/UM/MenuItem.qml @@ -10,6 +10,12 @@ MenuItem property alias shortcut: _shortcut.sequence property bool indicatorVisible: root.icon.source.length > 0 || root.checkable height: visible ? UM.Theme.getSize("context_menu").height : 0 + property int contentWidth: + { + // This is the width of all the items in the contentItem except the filler + return leftSpacer.width + label.width + middleSpacer.width + shortcutLabel.width + rightSpacer.width + } + Shortcut { id: _shortcut @@ -55,14 +61,15 @@ MenuItem Item { - // Spacer + // Left side margin + id: leftSpacer width: root.indicatorVisible ? root.indicator.width + UM.Theme.getSize("default_margin").width : UM.Theme.getSize("default_margin").width } UM.Label { + id: label text: replaceText(root.text) - Layout.fillWidth: true Layout.fillHeight:true elide: Label.ElideRight wrapMode: Text.NoWrap @@ -75,13 +82,15 @@ MenuItem Item { - // Right side margin - width: UM.Theme.getSize("default_margin").width + // Middle margin + id: middleSpacer + width: visible ? UM.Theme.getSize("default_margin").width : 0 visible: _shortcut.nativeText != "" || root.subMenu } UM.Label { + id: shortcutLabel Layout.fillHeight: true text: _shortcut.nativeText color: UM.Theme.getColor("text_lighter") @@ -90,6 +99,7 @@ MenuItem Item { // Right side margin + id: rightSpacer width: UM.Theme.getSize("default_margin").width } }