From 9595fa5d9de045294175136d202b057116a1a3b7 Mon Sep 17 00:00:00 2001 From: fieldOfView Date: Wed, 23 Mar 2022 10:06:16 +0100 Subject: [PATCH 1/5] Set the width of a Menu to fit the widest MenuItem --- UM/Qt/qml/UM/Menu.qml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/UM/Qt/qml/UM/Menu.qml b/UM/Qt/qml/UM/Menu.qml index 10b3db6bbd..54bdc04f99 100644 --- a/UM/Qt/qml/UM/Menu.qml +++ b/UM/Qt/qml/UM/Menu.qml @@ -19,6 +19,20 @@ Menu root.parent.height = shouldBeVisible ? UM.Theme.getSize("menu").height : 0 } } + + // 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 + 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); + } + return result + padding * 2; + } + Component.onCompleted: handleVisibility() onShouldBeVisibleChanged: handleVisibility() } \ No newline at end of file From adb79f11287ed2503c1d1d8c32a204d878a461bf Mon Sep 17 00:00:00 2001 From: fieldOfView Date: Thu, 24 Mar 2022 12:22:47 +0100 Subject: [PATCH 2/5] Fix codestyle --- UM/Qt/qml/UM/Menu.qml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/UM/Qt/qml/UM/Menu.qml b/UM/Qt/qml/UM/Menu.qml index 745c90de55..e5ea2a6d24 100644 --- a/UM/Qt/qml/UM/Menu.qml +++ b/UM/Qt/qml/UM/Menu.qml @@ -22,7 +22,8 @@ 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 - width: { + width: + { var result = 0; var padding = 0; for (var i = 0; i < count; ++i) { From 1fb00e1256ce8b61e999b3bb95018b89f15eaa0a Mon Sep 17 00:00:00 2001 From: fieldOfView Date: Thu, 24 Mar 2022 14:45:38 +0100 Subject: [PATCH 3/5] Fix menuitem spacing when there is a shortcut or submenu chevron --- UM/Qt/qml/UM/MenuItem.qml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/UM/Qt/qml/UM/MenuItem.qml b/UM/Qt/qml/UM/MenuItem.qml index 6106fd6b8a..215be8240d 100644 --- a/UM/Qt/qml/UM/MenuItem.qml +++ b/UM/Qt/qml/UM/MenuItem.qml @@ -73,6 +73,13 @@ MenuItem Layout.fillWidth: true } + Item + { + // Right side margin + width: UM.Theme.getSize("default_margin").width + visible: _shortcut.nativeText != "" || root.subMenu + } + UM.Label { Layout.fillHeight: true From 6a2aab7a72783eb69c74581947549d20bf9cef27 Mon Sep 17 00:00:00 2001 From: fieldOfView Date: Thu, 24 Mar 2022 18:16:45 +0100 Subject: [PATCH 4/5] Fix binding loop --- UM/Qt/qml/UM/Menu.qml | 9 ++++++--- UM/Qt/qml/UM/MenuItem.qml | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) 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 } } From 5e9c916890b084d2a8f9b4e7bbd0d23b7d476cb6 Mon Sep 17 00:00:00 2001 From: fieldOfView Date: Thu, 28 Apr 2022 15:17:53 +0200 Subject: [PATCH 5/5] Fix binding loops and spacing between label and shortcut --- UM/Qt/qml/UM/Menu.qml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/UM/Qt/qml/UM/Menu.qml b/UM/Qt/qml/UM/Menu.qml index 337007d69b..89a82fd314 100644 --- a/UM/Qt/qml/UM/Menu.qml +++ b/UM/Qt/qml/UM/Menu.qml @@ -17,26 +17,32 @@ Menu { root.parent.visible = shouldBeVisible root.parent.height = shouldBeVisible ? UM.Theme.getSize("menu").height : 0 + root.width = shouldBeVisible ? Qt.binding(setWidth) : 0 } } + Component.onCompleted: handleVisibility() + onShouldBeVisibleChanged: handleVisibility() + // Automatically set the width to fit the widest MenuItem // Based on https://martin.rpdev.net/2018/03/13/qt-quick-controls-2-automatically-set-the-width-of-menus.html - width: + function setWidth() { var result = 0; var padding = 0; + var result = 0; for (var i = 0; i < count; ++i) { var item = itemAt(i); if (item.hasOwnProperty("contentWidth")) { - result = Math.max(item.contentWidth, result); + var itemWidth = item.contentWidth; + if (item.hasOwnProperty("shortcut") && item.shortcut != null) { + itemWidth += UM.Theme.getSize("default_margin").width; + } + result = Math.max(itemWidth, result); padding = Math.max(item.padding, padding); } } - return result + padding * 2; + root.width = result + padding * 2; } - - Component.onCompleted: handleVisibility() - onShouldBeVisibleChanged: handleVisibility() } \ No newline at end of file