From 141feacb33930c45422a42df12bb04cc25689fe0 Mon Sep 17 00:00:00 2001 From: imoize <51510865+imoize@users.noreply.github.com> Date: Thu, 5 Sep 2024 13:24:15 +0700 Subject: [PATCH] refactor(context menu): Move model context menu to reusable components. --- package/contents/ui/ModelsDelegate.qml | 72 ++++++++++------- .../contents/ui/components/ContextMenu.qml | 80 +++++++++++++++++++ 2 files changed, 124 insertions(+), 28 deletions(-) create mode 100644 package/contents/ui/components/ContextMenu.qml diff --git a/package/contents/ui/ModelsDelegate.qml b/package/contents/ui/ModelsDelegate.qml index 011d2af..5b30c18 100644 --- a/package/contents/ui/ModelsDelegate.qml +++ b/package/contents/ui/ModelsDelegate.qml @@ -8,7 +8,7 @@ import org.kde.plasma.components as PlasmaComponents import "Utils.js" as Utils PlasmaComponents.ItemDelegate { - id: modeltem + id: modelItem height: Math.max(label.height, Math.round(Kirigami.Units.gridUnit * 1.6)) + 2 * Kirigami.Units.smallSpacing enabled: true @@ -18,7 +18,7 @@ PlasmaComponents.ItemDelegate { anchors.fill: parent hoverEnabled: true onEntered: { - modelListView.currentIndex = index + modelListView.currentIndex = index; } onExited: { if (modelListView.currentIndex === index) @@ -27,7 +27,6 @@ PlasmaComponents.ItemDelegate { Item { id: label - height: labelLayout.height anchors { left: parent.left right: parent.right @@ -35,6 +34,7 @@ PlasmaComponents.ItemDelegate { // rightMargin: Kirigami.Units.gridUnit - 3.2 verticalCenter: parent.verticalCenter } + height: labelLayout.height RowLayout { id: labelLayout @@ -114,40 +114,56 @@ PlasmaComponents.ItemDelegate { text: i18n("Load Model") icon.name: Qt.resolvedUrl("icons/up-square.svg") onClicked: { - var model = modelName - Utils.loadModel(model); + var model = modelName; + } + display: QQC2.AbstractButton.IconOnly + PlasmaComponents.ToolTip { + text: parent.text } - display:QQC2.AbstractButton.IconOnly - PlasmaComponents.ToolTip { text: parent.text } } PlasmaComponents.ToolButton { id: contextMenuButton + property var contextMenu: null // anchors.centerIn: parent checkable: true - checked: contextMenu.opened text: i18n("More") icon.name: Qt.resolvedUrl("icons/options.svg") - onClicked: { - contextMenu.open(); + onClicked: { + createContextMenu(modelName); } - display:QQC2.AbstractButton.IconOnly - PlasmaComponents.ToolTip { text: parent.text } - - QQC2.Menu { - id: contextMenu - modal: true - y: contextMenuButton.height + Kirigami.Units.smallSpacing - margins: Kirigami.Units.smallSpacing * 5 - // width: Kirigami.Units.gridUnit * 7 - closePolicy: QQC2.Popup.CloseOnPressOutside | QQC2.Popup.CloseOnReleaseOutside - - QQC2.MenuItem { - text: i18n("Delete (WIP)") - icon.name: Qt.resolvedUrl("icons/delete.svg") - // onTriggered: { - // listPage.view.openDialog(modelName); - // } + display: QQC2.AbstractButton.IconOnly + PlasmaComponents.ToolTip { + text: parent.text + } + + function createContextMenu(modelName) { + if (contextMenu === null) { + var component = Qt.createComponent("./components/ContextMenu.qml"); + contextMenu = component.createObject(contextMenuButton); + contextMenuButton.checked = true; + contextMenu.modelName = modelName; + contextMenu.open(); + if (contextMenu !== null) { + contextMenu.closeContextMenu.connect(destroyContextMenu); + } + } + } + + function destroyContextMenu() { + if (contextMenu !== null) { + contextMenu.destroy(); + contextMenuButton.checked = false; + contextMenu = null; + } + } + } + + Connections { + target: main + function onExpandedChanged() { + if (!main.expanded) { + contextMenuButton.destroyContextMenu(); } } } @@ -166,4 +182,4 @@ PlasmaComponents.ItemDelegate { width: parent.width - Kirigami.Units.gridUnit visible: showSeparator } -} \ No newline at end of file +} diff --git a/package/contents/ui/components/ContextMenu.qml b/package/contents/ui/components/ContextMenu.qml new file mode 100644 index 0000000..4da3ff4 --- /dev/null +++ b/package/contents/ui/components/ContextMenu.qml @@ -0,0 +1,80 @@ +import QtQuick +import QtQuick.Controls as QQC2 +import org.kde.kirigami as Kirigami +import org.kde.plasma.components as PlasmaComponents + +PlasmaComponents.Menu { + id: contextMenu + + property string modelName: "" + signal closeContextMenu + + width: Kirigami.Units.gridUnit * 8 + margins: Kirigami.Units.smallSpacing * 3 + y: contextMenuButton.height + modal: true + closePolicy: QQC2.Popup.CloseOnPressOutside + + PlasmaComponents.MenuItem { + id: copyMenuItem + text: i18n("Copy") + + onTriggered: { + + } + onHoveredChanged: { + if (!hovered) { + highlighted = false; + } + } + PlasmaComponents.ToolTip { + text: i18n("Creates a model with another name from an existing model.") + } + } + + // PlasmaComponents.MenuSeparator {} + + // PlasmaComponents.MenuItem { + // id: pushMenuItem + // text: i18n("Push") + // icon.name: Qt.resolvedUrl("../icons/up-tray.svg") + // onTriggered: {} + // onHoveredChanged: { + // if (!hovered) { + // highlighted = false; + // } + // } + // } + + // PlasmaComponents.MenuItem { + // id: updateMenuItem + // text: i18n("Update") + // icon.name: Qt.resolvedUrl("../icons/down-tray.svg") + // onTriggered: {} + // onHoveredChanged: { + // if (!hovered) { + // highlighted = false; + // } + // } + // } + + PlasmaComponents.MenuSeparator {} + + PlasmaComponents.MenuItem { + id: deleteMenuItem + text: i18n("Delete") + icon.name: Qt.resolvedUrl("../icons/delete.svg") + onTriggered: { + + } + onHoveredChanged: { + if (!hovered) { + highlighted = false; + } + } + } + + onClosed: { + closeContextMenu(); + } +}