Skip to content

Commit

Permalink
fix(StatusMenu): StatusSuccessAction is not taken into account for Me…
Browse files Browse the repository at this point in the history
…nu width

- StatusSuccessAction, despite its name, is a visual item (`MenuItem` ->
`AbstractButton`) which is not part of the `contentModel` but just added
to the menu container
- therefore, calculate the maximum menu item width differently, using
`FontMetrics`

Fixes #14037
  • Loading branch information
caybro committed Sep 18, 2024
1 parent ea8827e commit f070fb7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
31 changes: 17 additions & 14 deletions ui/StatusQ/src/StatusQ/Popups/StatusMenu.qml
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@ Menu {
property var openHandler
property var closeHandler

function checkIfEmpty() {
for (let i = 0; i < root.contentItem.count; ++i) {
const menuItem = root.contentItem.itemAtIndex(i)
if (menuItem.text !== undefined && menuItem.enabled) { // skip menu separators
return false
}
}
return true
}

dim: false
closePolicy: Popup.CloseOnPressOutside | Popup.CloseOnEscape
topPadding: 8
Expand All @@ -94,21 +84,34 @@ Menu {
if (typeof closeHandler === "function") {
closeHandler()
}
d.maxDelegateImplWidth = 0
}

QtObject {
id: d
//helper property to get the max implicit width of the delegate
property real maxDelegateImplWidth: 0

function calcMaxWidth() {
for (let i = 0; i < root.count; i++) {
const menuItem = root.itemAt(i)
if (!!menuItem && menuItem.text && menuItem.enabled) {
metrics.font = menuItem.font
d.maxDelegateImplWidth = Math.max(d.maxDelegateImplWidth, metrics.boundingRect(menuItem.text).width +
menuItem.indicator.width + menuItem.horizontalPadding*4 + menuItem.spacing*4)
}
}
}

readonly property var metrics: FontMetrics {}
}

Component.onCompleted: Qt.callLater(d.calcMaxWidth)
onAboutToShow: d.calcMaxWidth()

delegate: StatusMenuItem {
visible: root.hideDisabledItems && !visibleOnDisabled ? enabled : true
height: visible ? implicitHeight : 0
onImplicitWidthChanged: {
if (visible)
d.maxDelegateImplWidth = Math.max(d.maxDelegateImplWidth, implicitWidth)
}
}

contentItem: StatusListView {
Expand Down
4 changes: 2 additions & 2 deletions ui/StatusQ/src/StatusQ/Popups/StatusMenuItem.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import QtQuick 2.13
import QtQuick.Controls 2.13
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

import StatusQ.Core 0.1
Expand Down
16 changes: 8 additions & 8 deletions ui/StatusQ/src/StatusQ/Popups/StatusSuccessAction.qml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import QtQuick 2.14
import QtQuick 2.15

import StatusQ.Popups 0.1

Expand All @@ -9,7 +9,7 @@ import StatusQ.Popups 0.1
\since StatusQ.Popups 0.1
\brief Menu action displaying success state.
The \c StatusSuccessAction visually indicate a success state after being triggered.
The \c StatusSuccessAction visually indicates a success state after being triggered.
Success state is showed by changing action type to \c{Success}.
\qml
Expand All @@ -27,31 +27,31 @@ StatusMenuItem {
id: root

/*!
\qmlproperty bool StatusSuccessAction.qml::success
\qmlproperty bool StatusSuccessAction::success
This property holds state of the action.
*/
property bool success: false
/*!
\qmlproperty string StatusSuccessAction.qml::successText
\qmlproperty string StatusSuccessAction::successText
This property holds success text displayed on success state.
Default value is binded to \c{text}.
Default value is bound to \c{text}.
*/
property string successText: text
/*!
\qmlproperty string StatusSuccessAction.qml::successIconName
\qmlproperty string StatusSuccessAction::successIconName
This property holds icon name displayed on success state.
Default value is \c{tiny/checkmark}.
*/
property string successIconName: "tiny/checkmark"
/*!
\qmlproperty bool StatusSuccessAction.qml::autoDismissMenu
\qmlproperty bool StatusSuccessAction::autoDismissMenu
This property enable menu closing on click.
*/
property bool autoDismissMenu: false
/*!
\qmlproperty int StatusSuccessAction.qml::timeout
\qmlproperty int StatusSuccessAction::timeout
This property controls how long success state is showed.
*/
property int timeout: 2000
Expand Down

0 comments on commit f070fb7

Please sign in to comment.