summaryrefslogtreecommitdiff
path: root/raveos-hyprland-theme/theme-data/DankMaterialShell/quickshell/Modals/DankLauncherV2/ActionPanel.qml
blob: 31ada32a4e0b5b97d5d8b652c6d72ebf0a305b36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
101
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
pragma ComponentBehavior: Bound

import QtQuick
import qs.Common
import qs.Services
import qs.Widgets

Rectangle {
    id: root

    property var selectedItem: null
    property var controller: null
    property bool expanded: false
    property int selectedActionIndex: 0

    function getPluginContextMenuActions() {
        if (selectedItem?.type !== "plugin" || !selectedItem?.pluginId)
            return [];
        var instance = PluginService.pluginInstances[selectedItem.pluginId];
        if (!instance)
            return [];
        if (typeof instance.getContextMenuActions !== "function")
            return [];
        var actions = instance.getContextMenuActions(selectedItem.data);
        if (!Array.isArray(actions))
            return [];
        return actions;
    }

    readonly property var actions: {
        var result = [];
        if (selectedItem?.primaryAction) {
            result.push(selectedItem.primaryAction);
        }

        switch (selectedItem?.type) {
        case "plugin":
            var pluginActions = getPluginContextMenuActions();
            for (var i = 0; i < pluginActions.length; i++) {
                var act = pluginActions[i];
                result.push({
                    name: act.text || act.name || "",
                    icon: act.icon || "play_arrow",
                    action: "plugin_action",
                    pluginAction: act.action
                });
            }
            break;
        case "plugin_browse":
            if (selectedItem?.actions) {
                for (var i = 0; i < selectedItem.actions.length; i++) {
                    result.push(selectedItem.actions[i]);
                }
            }
            break;
        case "app":
            if (selectedItem?.isCore)
                break;
            if (SessionService.nvidiaCommand) {
                result.push({
                    name: I18n.tr("Launch on dGPU"),
                    icon: "memory",
                    action: "launch_dgpu"
                });
            }
            if (selectedItem?.actions) {
                for (var i = 0; i < selectedItem.actions.length; i++) {
                    result.push(selectedItem.actions[i]);
                }
            }
            break;
        }
        return result;
    }

    readonly property bool hasActions: {
        switch (selectedItem?.type) {
        case "app":
            return !selectedItem?.isCore;
        case "plugin":
            return getPluginContextMenuActions().length > 0;
        case "plugin_browse":
            return selectedItem?.actions?.length > 0;
        default:
            return actions.length > 1;
        }
    }

    width: parent?.width ?? 200
    height: expanded && hasActions ? 52 : 0
    color: Theme.surfaceContainerHigh
    radius: Theme.cornerRadius

    clip: true

    Behavior on height {
        NumberAnimation {
            duration: Theme.shortDuration
            easing.type: Theme.standardEasing
        }
    }

    Rectangle {
        anchors.top: parent.top
        width: parent.width
        height: 1
        color: Theme.outlineMedium
    }

    Item {
        anchors.fill: parent
        anchors.margins: Theme.spacingS

        Flickable {
            id: actionsFlickable
            anchors.left: parent.left
            anchors.right: tabHint.left
            anchors.rightMargin: Theme.spacingS
            anchors.verticalCenter: parent.verticalCenter
            height: parent.height
            contentWidth: actionsRow.width
            contentHeight: height
            clip: true
            boundsBehavior: Flickable.StopAtBounds
            flickableDirection: Flickable.HorizontalFlick

            Row {
                id: actionsRow
                height: parent.height
                spacing: Theme.spacingS

                Repeater {
                    model: root.actions

                    Rectangle {
                        id: actionButton

                        required property var modelData
                        required property int index

                        width: actionContent.implicitWidth + Theme.spacingM * 2
                        height: actionsRow.height
                        radius: Theme.cornerRadius
                        color: index === root.selectedActionIndex ? Theme.primaryHover : actionArea.containsMouse ? Theme.surfaceHover : "transparent"

                        Row {
                            id: actionContent
                            anchors.centerIn: parent
                            spacing: Theme.spacingXS

                            DankIcon {
                                anchors.verticalCenter: parent.verticalCenter
                                name: actionButton.modelData?.icon ?? "play_arrow"
                                size: 16
                                color: actionButton.index === root.selectedActionIndex ? Theme.primary : Theme.surfaceText
                            }

                            StyledText {
                                anchors.verticalCenter: parent.verticalCenter
                                text: actionButton.modelData?.name ?? ""
                                font.pixelSize: Theme.fontSizeSmall
                                font.weight: Font.Medium
                                color: actionButton.index === root.selectedActionIndex ? Theme.primary : Theme.surfaceText
                            }
                        }

                        MouseArea {
                            id: actionArea
                            anchors.fill: parent
                            hoverEnabled: true
                            cursorShape: Qt.PointingHandCursor
                            onClicked: {
                                if (root.controller && root.selectedItem) {
                                    root.controller.executeAction(root.selectedItem, actionButton.modelData);
                                }
                            }
                            onEntered: root.selectedActionIndex = actionButton.index
                        }
                    }
                }
            }
        }

        StyledText {
            id: tabHint
            anchors.right: parent.right
            anchors.verticalCenter: parent.verticalCenter
            visible: root.hasActions
            text: "Tab"
            font.pixelSize: Theme.fontSizeSmall - 2
            color: Theme.outlineButton
        }
    }

    function toggle() {
        expanded = !expanded;
        selectedActionIndex = 0;
    }

    function show() {
        expanded = true;
        selectedActionIndex = actions.length > 1 ? 1 : 0;
    }

    function hide() {
        expanded = false;
        selectedActionIndex = 0;
    }

    function cycleAction(reverse = false) {
        if (actions.length > 0) {
            if (! reverse)
                selectedActionIndex = (selectedActionIndex + 1) % actions.length;
            else
                selectedActionIndex = (selectedActionIndex - 1) % actions.length;
            ensureSelectedVisible();
        }
    }

    function ensureSelectedVisible() {
        if (selectedActionIndex < 0 || !actionsRow.children || selectedActionIndex >= actionsRow.children.length)
            return;
        var buttonX = 0;
        for (var i = 0; i < selectedActionIndex; i++) {
            var child = actionsRow.children[i];
            if (child)
                buttonX += child.width + actionsRow.spacing;
        }

        var button = actionsRow.children[selectedActionIndex];
        if (!button)
            return;
        var buttonRight = buttonX + button.width;
        var viewLeft = actionsFlickable.contentX;
        var viewRight = viewLeft + actionsFlickable.width;

        if (buttonX < viewLeft) {
            actionsFlickable.contentX = Math.max(0, buttonX - Theme.spacingS);
        } else if (buttonRight > viewRight) {
            actionsFlickable.contentX = Math.min(actionsFlickable.contentWidth - actionsFlickable.width, buttonRight - actionsFlickable.width + Theme.spacingS);
        }
    }

    function executeSelectedAction() {
        if (!controller || !selectedItem || selectedActionIndex >= actions.length)
            return;
        var action = actions[selectedActionIndex];
        if (action.action === "plugin_action" && typeof action.pluginAction === "function") {
            action.pluginAction();
            controller.performSearch();
            controller.itemExecuted();
        } else {
            controller.executeAction(selectedItem, action);
        }
    }
}