| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2021 George Florea Bănuș <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
pragma ComponentBehavior: Bound |
| 8 |
|
| 9 |
import QtQuick |
| 10 |
import QtQuick.Layouts |
| 11 |
import QtQuick.Controls |
| 12 |
|
| 13 |
import org.kde.kirigami as Kirigami |
| 14 |
import org.kde.kquickcontrols |
| 15 |
import org.kde.haruna |
| 16 |
|
| 17 |
SettingsBasePage { |
| 18 |
id: root |
| 19 |
|
| 20 |
required property CustomCommandsModel m_customCommandsModel |
| 21 |
|
| 22 |
property string settingsPath: "qrc:/qt/qml/org/kde/haruna/qml/Settings" |
| 23 |
|
| 24 |
ListView { |
| 25 |
id: customCommandsView |
| 26 |
|
| 27 |
reuseItems: true |
| 28 |
model: root.m_customCommandsModel |
| 29 |
delegate: customCommandDelegate |
| 30 |
|
| 31 |
displaced: Transition { |
| 32 |
NumberAnimation { |
| 33 |
properties: "y" |
| 34 |
duration: Kirigami.Units.shortDuration |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
Kirigami.PlaceholderMessage { |
| 39 |
anchors.centerIn: parent |
| 40 |
visible: customCommandsView.count === 0 |
| 41 |
text: i18nc("@label:textbox", "No custom commands yet") |
| 42 |
helpfulAction: Action { |
| 43 |
text: i18nc("@action:button", "&Add Command") |
| 44 |
onTriggered: applicationWindow().pageStack.replace( |
| 45 |
`${root.settingsPath}/EditCustomCommand.qml`, |
| 46 |
{m_customCommandsModel: root.m_customCommandsModel}) |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
Component { |
| 52 |
id: customCommandDelegate |
| 53 |
|
| 54 |
Loader { |
| 55 |
id: delegate |
| 56 |
|
| 57 |
required property int index |
| 58 |
required property string command |
| 59 |
required property string shortcut |
| 60 |
required property string commandId |
| 61 |
required property string osdMessage |
| 62 |
required property string type |
| 63 |
required property bool setOnStartup |
| 64 |
|
| 65 |
width: customCommandsView.width |
| 66 |
asynchronous: true |
| 67 |
sourceComponent: ItemDelegate { |
| 68 |
id: customCommandItem |
| 69 |
|
| 70 |
height: Kirigami.Units.gridUnit * 3 |
| 71 |
padding: 0 |
| 72 |
|
| 73 |
contentItem: RowLayout { |
| 74 |
anchors.fill: parent |
| 75 |
spacing: 0 |
| 76 |
|
| 77 |
Kirigami.ListItemDragHandle { |
| 78 |
listItem: customCommandItem |
| 79 |
listView: customCommandsView |
| 80 |
onMoveRequested: function (sourceRow, destinationRow) { |
| 81 |
const modelIndex = root.m_customCommandsModel.index(sourceRow, 0).parent |
| 82 |
root.m_customCommandsModel.moveRows(modelIndex, sourceRow, 1, modelIndex, destinationRow) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
CheckBox { |
| 87 |
visible: delegate.type === "startup" |
| 88 |
checked: delegate.setOnStartup |
| 89 |
onClicked: { |
| 90 |
root.m_customCommandsModel.toggleCustomCommand(delegate.commandId, delegate.index, checked) |
| 91 |
} |
| 92 |
|
| 93 |
ToolTip { |
| 94 |
text: i18nc("@info:tooltip", "Checked: property will be set at startup\n" + |
| 95 |
"Unchecked: property will not be set at startup") |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
Kirigami.Icon { |
| 100 |
source: delegate.type === "shortcut" ? "configure-shortcuts" : "code-context" |
| 101 |
enabled: delegate.setOnStartup |
| 102 |
Layout.preferredWidth: Kirigami.Units.iconSizes.small |
| 103 |
Layout.preferredHeight: Kirigami.Units.iconSizes.small |
| 104 |
} |
| 105 |
|
| 106 |
LabelWithTooltip { |
| 107 |
text: delegate.command |
| 108 |
elide: Text.ElideRight |
| 109 |
enabled: delegate.setOnStartup |
| 110 |
opacity: enabled ? 1.0 : 0.7 |
| 111 |
|
| 112 |
Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter |
| 113 |
Layout.fillWidth: true |
| 114 |
Layout.margins: Kirigami.Units.largeSpacing |
| 115 |
} |
| 116 |
|
| 117 |
Loader { |
| 118 |
active: delegate.type === "shortcut" |
| 119 |
asynchronous: true |
| 120 |
sourceComponent: KeySequenceItem { |
| 121 |
checkForConflictsAgainst: ShortcutType.None |
| 122 |
modifierlessAllowed: true |
| 123 |
keySequence: delegate.shortcut |
| 124 |
|
| 125 |
onKeySequenceModified: { |
| 126 |
if (keySequence.toString() === "" ) { |
| 127 |
return |
| 128 |
} |
| 129 |
if (keySequence !== delegate.shortcut) { |
| 130 |
// use action name (commandId) since this changes the actionsModel |
| 131 |
if (!actionsModel.saveShortcut(delegate.commandId, keySequence)) { |
| 132 |
keySequence = delegate.shortcut |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
Layout.alignment: Qt.AlignRight | Qt.AlignVCenter |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
Item { Layout.preferredWidth: Kirigami.Units.smallSpacing } |
| 143 |
|
| 144 |
Button { |
| 145 |
icon.name: "edit-entry" |
| 146 |
Layout.rightMargin: Kirigami.Units.largeSpacing |
| 147 |
onClicked: { |
| 148 |
const properties = { |
| 149 |
m_customCommandsModel: root.m_customCommandsModel, |
| 150 |
command: delegate.command, |
| 151 |
osdMessage: delegate.osdMessage, |
| 152 |
type: delegate.type, |
| 153 |
commandId: delegate.commandId, |
| 154 |
index: delegate.index, |
| 155 |
mode: EditCustomCommand.Mode.Edit |
| 156 |
} |
| 157 |
applicationWindow().pageStack.replace(`${root.settingsPath}/EditCustomCommand.qml`, properties) |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
footer: ToolBar { |
| 166 |
visible: customCommandsView.count > 0 |
| 167 |
|
| 168 |
RowLayout { |
| 169 |
anchors.fill: parent |
| 170 |
|
| 171 |
ToolButton { |
| 172 |
id: addButton |
| 173 |
|
| 174 |
text: i18nc("@action:intoolbar", "&Add") |
| 175 |
icon.name: "list-add" |
| 176 |
onClicked: applicationWindow().pageStack.replace(`${root.settingsPath}/EditCustomCommand.qml`, |
| 177 |
{m_customCommandsModel: root.m_customCommandsModel}) |
| 178 |
Layout.alignment: Qt.AlignRight |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
} |