Skip to content

Commit

Permalink
refactor(dialog): Move dialog popup to reusable components.
Browse files Browse the repository at this point in the history
  • Loading branch information
imoize committed Sep 5, 2024
1 parent 4b74441 commit 424a275
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 39 deletions.
87 changes: 48 additions & 39 deletions package/contents/ui/ListPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,46 @@ import "Utils.js" as Utils

ColumnLayout {
id: listPage

property alias view: modelListView
property alias model: modelListView.model
property alias modelsCombobox: modelsCombobox
property string modelName: ""
property var actionsDialog: null

function createActionsDialog(modelName, action) {
if (actionsDialog === null) {
var component = Qt.createComponent("./components/ActionsDialog.qml");
actionsDialog = component.createObject(parent);
actionsDialog.modelName = modelName;
actionsDialog.action = action;
if (action === "copy") {
actionsDialog.standardButtons = QQC2.Dialog.Ok | QQC2.Dialog.Cancel;
actionsDialog.standardButton(QQC2.Dialog.Ok).enabled = false;
} else if (action === "delete") {
actionsDialog.standardButtons = QQC2.Dialog.Yes | QQC2.Dialog.No;
}
if (actionsDialog !== null) {
actionsDialog.closeActionsDialog.connect(destroyActionsDialog);
actionsDialog.doActions.connect(doActionsHandler);
}
}
}

function destroyActionsDialog() {
if (actionsDialog !== null) {
actionsDialog.destroy();
actionsDialog = null;
}
}

function doActionsHandler(modelName, destination, action) {
if (action === "copy") {
const source = modelName;

} else if (action === "delete") {

}
}

property var header: PlasmaExtras.PlasmoidHeading {
contentItem: RowLayout {
Expand All @@ -34,7 +70,9 @@ ColumnLayout {
Utils.getModels();
}
display: QQC2.AbstractButton.IconOnly
PlasmaComponents.ToolTip{ text: parent.text }
PlasmaComponents.ToolTip {
text: parent.text
}
}
}
}
Expand All @@ -45,7 +83,6 @@ ColumnLayout {

PlasmaComponents.ComboBox {
id: modelsCombobox

Layout.fillWidth: true
model: runningModels
textRole: "text"
Expand All @@ -59,10 +96,12 @@ ColumnLayout {
text: i18n("Eject")
icon.name: Qt.resolvedUrl("icons/eject.svg")
onClicked: {
var model = modelsCombobox.currentText
var model = modelsCombobox.currentText;
Utils.unloadModel(model);
}
PlasmaComponents.ToolTip{ text: i18n("Eject Model") }
PlasmaComponents.ToolTip {
text: i18n("Eject Model")
}
}
}
}
Expand All @@ -75,8 +114,8 @@ ColumnLayout {
if (ollamaRunning) {
Utils.getModels();
}
} else {
deleteDialog.close();
} else if (!main.expanded) {
destroyActionsDialog();
}
}
}
Expand All @@ -89,7 +128,7 @@ ColumnLayout {
contentItem: ListView {
id: modelListView
model: models
highlight: PlasmaExtras.Highlight { }
highlight: PlasmaExtras.Highlight {}
highlightFollowsCurrentItem: true
highlightMoveDuration: 0
currentIndex: -1
Expand All @@ -103,36 +142,6 @@ ColumnLayout {
deleteDialog.open();
listPage.modelName = modelName;
}

QQC2.Dialog {
id: deleteDialog
anchors.centerIn: parent
contentWidth: Kirigami.Units.gridUnit * 13
modal: true
standardButtons: QQC2.Dialog.Yes | QQC2.Dialog.Cancel
closePolicy: QQC2.Popup.NoAutoClose | QQC2.Popup.CloseOnPressOutside

PlasmaComponents.Label {
id: dialogMessage
anchors.centerIn: parent
width: deleteDialog.contentWidth
// width: Kirigami.Units.gridUnit * 13
horizontalAlignment: Text.AlignHCenter
font.bold: true
wrapMode: Text.WordWrap
font.pixelSize: Kirigami.Theme.defaultFont.pixelSize
text: i18n("Delete '" + modelName + "' model ?")
}

footer: QQC2.DialogButtonBox {
alignment: Qt.AlignHCenter
}

onAccepted: Utils.deleteModel(modelName);
onRejected: {
deleteDialog.close();
}
}
}
}

Expand All @@ -147,4 +156,4 @@ ColumnLayout {
recursiveFilteringEnabled: true
sortOrder: Qt.AscendingOrder
}
}
}
96 changes: 96 additions & 0 deletions package/contents/ui/components/ActionsDialog.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls as QQC2
import org.kde.kirigami as Kirigami
import org.kde.plasma.components as PlasmaComponents

PlasmaComponents.Dialog {
id: actionsDialog

property string modelName: ""
property string action: ""
signal doActions(string modelName, string destination, string action)
signal closeActionsDialog

anchors.centerIn: parent
contentWidth: Kirigami.Units.gridUnit * 18
height: actionsDialogItem.height + footer.height + Kirigami.Units.gridUnit
bottomInset: -10

dim: true
modal: true
visible: true
closePolicy: QQC2.Popup.CloseOnPressOutside

ColumnLayout {
id: actionsDialogItem
anchors.centerIn: parent
Layout.preferredWidth: actionsDialog.width
Layout.fillWidth: true
Layout.fillHeight: true

PlasmaComponents.Label {
id: copyMessage
Layout.alignment: Qt.AlignHCenter
Layout.topMargin: Kirigami.Units.smallSpacing * 2
Layout.bottomMargin: Kirigami.Units.smallSpacing * 2
Layout.preferredWidth: actionsDialog.contentWidth
horizontalAlignment: Text.AlignHCenter
font.bold: true
wrapMode: Text.WordWrap
text: {
if (actionsDialog.action === "copy") {
return i18n("Copy \"%1\" to:", actionsDialog.modelName);
} else if (actionsDialog.action === "delete") {
return i18n("Delete \"%1\" model ?", actionsDialog.modelName);
} else {
return "";
}
}
}

PlasmaComponents.TextField {
id: copyDestination
Layout.alignment: Qt.AlignHCenter
Layout.preferredWidth: parent.width
Layout.fillWidth: true
visible: actionsDialog.action === "copy" ? true : false
focus: true
placeholderText: i18n("e.g. llama3.1-instruct, llama3.1:instruct")
onTextChanged: {
if (copyDestination.text !== "") {
actionsDialog.standardButton(QQC2.Dialog.Ok).enabled = true;
} else {
actionsDialog.standardButton(QQC2.Dialog.Ok).enabled = false;
}
}
}
}

footer: QQC2.DialogButtonBox {
id: dialogButtonBox
alignment: Qt.AlignHCenter
}

QQC2.Overlay.modal: Rectangle {
color: "#50000000"
bottomLeftRadius: 5
bottomRightRadius: 5
}

onAccepted: {
if (actionsDialog.action === "copy") {
const destination = copyDestination.text;
if (copyDestination.text !== "") {

}
actionsDialog.closeActionsDialog();
} else if (actionsDialog.action === "delete") {

}
actionsDialog.closeActionsDialog();
}
onRejected: {
actionsDialog.closeActionsDialog();
}
}

0 comments on commit 424a275

Please sign in to comment.