RulesEditor.qml 8.93 KB
Newer Older
Ismael Asensio's avatar
Ismael Asensio committed
1
/*
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
2 3 4 5
    SPDX-FileCopyrightText: 2020 Ismael Asensio <isma.af@gmail.com>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
Ismael Asensio's avatar
Ismael Asensio committed
6 7 8 9

import QtQuick 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.14 as QQC2
10
import org.kde.kirigami 2.12 as Kirigami
Ismael Asensio's avatar
Ismael Asensio committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
import org.kde.kcm 1.2
import org.kde.kitemmodels 1.0
import org.kde.kcms.kwinrules 1.0


ScrollViewKCM {
    id: rulesEditor

    property var rulesModel: kcm.rulesModel

    title: rulesModel.description

    view: ListView {
        id: rulesView
        clip: true

        model: enabledRulesModel
        delegate: RuleItemDelegate {}
        section {
            property: "section"
            delegate: Kirigami.ListSectionHeader { label: section }
        }

34
        Kirigami.PlaceholderMessage {
Ismael Asensio's avatar
Ismael Asensio committed
35 36
            id: hintArea
            visible: rulesView.count <= 4
37 38 39 40 41 42 43 44 45
            anchors {
                // We need to center on the free space below contentItem, not the full ListView.
                // Setting both top and bottom anchors (or using anchors.fill) stretches the component
                // and distorts the spacing between its internal items.
                // This is fine as long as we have a single item here.
                horizontalCenter: parent.horizontalCenter
                top: parent.contentItem.bottom
                bottom: parent.bottom
            }
46
            width: parent.width - (units.largeSpacing * 4)
47
            helpfulAction: QQC2.Action {
Ismael Asensio's avatar
Ismael Asensio committed
48 49
                text: i18n("Add Properties...")
                icon.name: "list-add-symbolic"
50
                onTriggered: {
Ismael Asensio's avatar
Ismael Asensio committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
                    propertySheet.open();
                }
            }
        }
    }

    header: Kirigami.InlineMessage {
        Layout.fillWidth: true
        Layout.fillHeight: true
        text: rulesModel.warningMessage
        visible: text != ""
    }

    footer:  RowLayout {
        QQC2.Button {
            text: checked ? i18n("Close") : i18n("Add Properties...")
            icon.name: checked ? "dialog-close" : "list-add-symbolic"
            checkable: true
            checked: propertySheet.sheetOpen
            visible: !hintArea.visible || checked
            onToggled: {
                propertySheet.sheetOpen = checked;
            }
        }
        Item {
            Layout.fillWidth: true
        }
        QQC2.Button {
            text: i18n("Detect Window Properties")
            icon.name: "edit-find"
            onClicked: {
                overlayModel.onlySuggestions = true;
                rulesModel.detectWindowProperties(delaySpin.value);
            }
        }
        QQC2.SpinBox {
            id: delaySpin
            Layout.preferredWidth: Kirigami.Units.gridUnit * 8
            from: 0
            to: 30
            textFromValue: (value, locale) => {
                return (value == 0) ? i18n("Instantly")
                                    : i18np("After %1 second", "After %1 seconds", value)
            }
        }

    }

    Connections {
        target: rulesModel
101
        function onSuggestionsChanged() {
Ismael Asensio's avatar
Ismael Asensio committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
            propertySheet.sheetOpen = true;
        }
    }

    Kirigami.OverlaySheet {
        id: propertySheet

        parent: view

        header: Kirigami.Heading {
            text: i18n("Select properties")
        }
        footer: Kirigami.SearchField {
            id: searchField
            horizontalAlignment: Text.AlignLeft
        }

        ListView {
            id: overlayView
            model: overlayModel
            Layout.preferredWidth: Kirigami.Units.gridUnit * 28

            section {
                property: "section"
                delegate: Kirigami.ListSectionHeader { label: section }
            }

            delegate: Kirigami.AbstractListItem {
                id: propertyDelegate
                highlighted: false
                width: ListView.view.width

                RowLayout {
                    Kirigami.Icon {
                        source: model.icon
                        Layout.preferredHeight: Kirigami.Units.iconSizes.smallMedium
                        Layout.preferredWidth: Kirigami.Units.iconSizes.smallMedium
                        Layout.alignment: Qt.AlignVCenter
                    }
                    QQC2.Label {
                        id: itemNameLabel
                        text: model.name
                        horizontalAlignment: Qt.AlignLeft
                        Layout.preferredWidth: implicitWidth
                        Layout.fillWidth: true
                        Layout.alignment: Qt.AlignVCenter
                        QQC2.ToolTip {
                            text: model.description
150
                            visible: hovered && (model.description.length > 0)
Ismael Asensio's avatar
Ismael Asensio committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                        }
                    }
                    QQC2.Label {
                        id: suggestedLabel
                        text: formatValue(model.suggested, model.type, model.options)
                        horizontalAlignment: Text.AlignRight
                        elide: Text.ElideRight
                        opacity: 0.7
                        Layout.maximumWidth: propertyDelegate.width - itemNameLabel.implicitWidth - Kirigami.Units.gridUnit * 6
                        Layout.alignment: Qt.AlignVCenter
                        QQC2.ToolTip {
                            text: suggestedLabel.text
                            visible: hovered && suggestedLabel.truncated
                        }
                    }
                    QQC2.ToolButton {
                        icon.name: (model.enabled) ? "dialog-ok-apply" : "list-add-symbolic"
                        opacity: propertyDelegate.hovered ? 1 : 0
                        onClicked: propertyDelegate.clicked()
                        Layout.preferredWidth: implicitWidth
171 172
                        Layout.leftMargin: -Kirigami.Units.smallSpacing
                        Layout.rightMargin: -Kirigami.Units.smallSpacing
Ismael Asensio's avatar
Ismael Asensio committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
                        Layout.alignment: Qt.AlignVCenter
                    }
                }

                onClicked: {
                    model.enabled = true;
                    if (model.suggested != null) {
                        model.value = model.suggested;
                        model.suggested = null;
                    }
                }
            }
        }

        onSheetOpenChanged: {
            searchField.text = "";
            if (sheetOpen) {
190
                overlayModel.ready = true;
Ismael Asensio's avatar
Ismael Asensio committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
                searchField.forceActiveFocus();
            } else {
                overlayModel.onlySuggestions = false;
            }
        }
    }

    function formatValue(value, type, options) {
        if (value == null) {
            return "";
        }
        switch (type) {
            case RuleItem.Boolean:
                return value ? i18n("Yes") : i18n("No");
            case RuleItem.Percentage:
                return i18n("%1 %", value);
207 208 209 210
            case RuleItem.Point:
                return i18nc("Coordinates (x, y)", "(%1, %2)", value.x, value.y);
            case RuleItem.Size:
                return i18nc("Size (width, height)", "(%1, %2)", value.width, value.height);
Ismael Asensio's avatar
Ismael Asensio committed
211 212
            case RuleItem.Option:
                return options.textOfValue(value);
213
            case RuleItem.NetTypes:
Ismael Asensio's avatar
Ismael Asensio committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
                var selectedValue = value.toString(2).length - 1;
                return options.textOfValue(selectedValue);
        }
        return value;
    }

    KSortFilterProxyModel {
        id: enabledRulesModel
        sourceModel: rulesModel
        filterRowCallback: (source_row, source_parent) => {
            var index = sourceModel.index(source_row, 0, source_parent);
            return sourceModel.data(index, RulesModel.EnabledRole);
        }
    }

    KSortFilterProxyModel {
        id: overlayModel
        sourceModel: rulesModel

        property bool onlySuggestions: false
        onOnlySuggestionsChanged: {
            invalidateFilter();
        }

238 239 240 241 242 243 244
        // Delay the model filtering until `ready` is set
        // FIXME: Workaround https://bugs.kde.org/show_bug.cgi?id=422289
        property bool ready: false
        onReadyChanged: {
            invalidateFilter();
        }

Ismael Asensio's avatar
Ismael Asensio committed
245 246
        filterString: searchField.text.trim().toLowerCase()
        filterRowCallback: (source_row, source_parent) => {
247 248 249 250
            if (!ready) {
                return false;
            }

Ismael Asensio's avatar
Ismael Asensio committed
251 252 253 254 255 256 257 258 259 260 261
            var index = sourceModel.index(source_row, 0, source_parent);

            var hasSuggestion = sourceModel.data(index, RulesModel.SuggestedValueRole) != null;
            var isOptional = sourceModel.data(index, RulesModel.SelectableRole);
            var isEnabled = sourceModel.data(index, RulesModel.EnabledRole);

            var showItem = hasSuggestion || (!onlySuggestions && isOptional && !isEnabled);

            if (!showItem) {
                return false;
            }
262
            if (filterString.length > 0) {
Ismael Asensio's avatar
Ismael Asensio committed
263 264 265 266 267 268 269
                return sourceModel.data(index, RulesModel.NameRole).toLowerCase().includes(filterString)
            }
            return true;
        }
    }

}