/*SPDX-FileCopyrightText: 2018 Roman Gilg <subdiff@gmail.com>SPDX-FileCopyrightText: 2018 Furkan Tokac <furkantokac34@gmail.com>SPDX-FileCopyrightText: 2024 Jakob Petsovits <jpetso@petsovits.com>SPDX-FileCopyrightText: 2024 ivan tkachenko <me@ratijas.tk>SPDX-License-Identifier: GPL-2.0-or-later*/pragma ComponentBehavior: Boundimport QtQuickimport QtQuick.Controls as QQC2import QtQuick.Layoutsimport org.kde.kcmutils as KCMUtilsimport org.kde.kirigami as Kirigamiimport org.kde.kquickcontrols as KQuickControlsimport org.kde.plasma.private.kcm_mouse as MouseKCMUtils.SimpleKCM {id: rootreadonly property Mouse.KWinWaylandBackend backend: KCMUtils.ConfigModule.inputBackendtitle: i18ndc("kcmmouse", "@title", "Extra Mouse Buttons")header: Header {saveLoadMessage: root.KCMUtils.ConfigModule.saveLoadMessagehotplugMessage: root.KCMUtils.ConfigModule.hotplugMessage}Kirigami.ColumnView.onViewChanged: {const view = Kirigami.ColumnView.viewif (view) {view.interactive = Qt.binding(() => !newBinding.checked)}}MouseArea {// Deliberately using MouseArea on the page instead of a TapHandler on the button, so we can capture clicks anywhereid: buttonCaptureproperty var lastButton: undefinedparent: root.contentItemanchors.fill: parentenabled: newBinding.checkedpreventStealing: trueacceptedButtons: Qt.AllButtons & ~(Qt.LeftButton | Qt.RightButton | Qt.MiddleButton)onClicked: mouse => {lastButton = buttonMappings.extraButtons.find(entry => Qt[entry.buttonName] === mouse.button)newBinding.visible = falsenewKeySequenceItem.visible = truenewKeySequenceItem.startCapturing()}}ColumnLayout {spacing: Kirigami.Units.smallSpacingKirigami.FormLayout {id: buttonLayouttwinFormLayouts: otherLayoutRepeater {id: buttonMappingsmodel: extraButtons.filter(entry => entry.buttonName in root.backend.buttonMapping) ?? []readonly property var extraButtons: Array.from({length: 24}, (value, index) => ({buttonName: "ExtraButton" + (index + 1),button: Qt["ExtraButton" + (index + 1)],label: i18ndc("kcmmouse", "@label for assigning an action to a numbered button", "Extra Button %1:", index + 1)}))delegate: KQuickControls.KeySequenceItem {required property var modelDataKirigami.FormData.label: modelData.labelkeySequence: root.backend.buttonMapping[modelData.buttonName]patterns: KQuickControls.ShortcutPattern.Modifier | KQuickControls.ShortcutPattern.Key | KQuickControls.ShortcutPattern.ModifierAndKeymultiKeyShortcutsAllowed: falsecheckForConflictsAgainst: KQuickControls.ShortcutType.NoneonCaptureFinished: {const copy = root.backend.buttonMapping;copy[modelData.buttonName] = keySequenceroot.backend.buttonMapping = copy}}}}Kirigami.InlineMessage {id: explanationLabelLayout.fillWidth: truevisible: newBinding.checked || newKeySequenceItem.visibletext: newBinding.visible? i18ndc("kcmmouse","@action:button", "Press the mouse button for which you want to add a key binding"): i18ndc("kcmmouse","@action:button, %1 is the translation of 'Extra Button %1' from above","Enter the new key combination for %1", buttonCapture.lastButton?.label ?? "")actions: Kirigami.Action {icon.name: "dialog-cancel"text: i18ndc("kcmmouse", "@action:button", "Cancel")onTriggered: source => {newKeySequenceItem.visible = false;newBinding.visible = truenewBinding.checked = false}}}Kirigami.FormLayout {id: otherLayouttwinFormLayouts: buttonLayoutQQC2.Button {id: newBindingcheckable: truetext: checked? i18ndc("kcmmouse", "@action:button", "Press a mouse button"): i18ndc("kcmmouse", "@action:button, Bind a mousebutton to keyboard key(s)", "Add Bindingβ¦")icon.name: "list-add"}KQuickControls.KeySequenceItem {id: newKeySequenceItemvisible: falsepatterns: KQuickControls.ShortcutPattern.Modifier | KQuickControls.ShortcutPattern.Key | KQuickControls.ShortcutPattern.ModifierAndKeymultiKeyShortcutsAllowed: falsecheckForConflictsAgainst: KQuickControls.ShortcutType.NoneonCaptureFinished: {visible = falsenewBinding.visible = truenewBinding.checked = falseconst copy = root.backend.buttonMapping;copy[buttonCapture.lastButton.buttonName] = keySequenceroot.backend.buttonMapping = copy}}}}}