aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/island/ControlsPanel.qml
blob: a7872d74729c1f692351a8fba6ad2017ac9de6e6 (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
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

import Quickshell
import Quickshell.Widgets
import QtQuick
import ".."
import "Controls.js" as Controls

// The grid of controls and its edit mode. In edit mode a tile is dragged to
// a new slot, a × hides it, and hidden tiles wait in a strip below with a +.
// Every change is written to Prefs at once.
Column {
    id: panel

    required property QtObject sound
    property bool editing: false

    signal openDrawer(string page)

    spacing: 12

    Item {
        width: panel.width
        height: 20

        Text {
            anchors.verticalCenter: parent.verticalCenter
            text: panel.editing ? "Edit controls" : "Controls"
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2; bold: true }
            color: Theme.text
        }

        IconImage {
            anchors { right: parent.right; verticalCenter: parent.verticalCenter }
            implicitSize: 16
            opacity: panel.editing ? 1 : 0.6
            source: Quickshell.iconPath("document-edit", true)

            MouseArea {
                anchors.fill: parent
                anchors.margins: -4
                cursorShape: Qt.PointingHandCursor
                onClicked: panel.editing = !panel.editing
            }
        }
    }

    Item {
        id: grid

        readonly property int cols: 4
        readonly property real gap: 8
        readonly property real cell: (width - gap * (cols - 1)) / cols
        readonly property real pitch: cell + gap
        readonly property int rows: Math.ceil(Prefs.controls.length / cols)

        width: panel.width
        height: Math.max(0, rows * pitch - gap)

        Repeater {
            model: Prefs.controls

            delegate: Item {
                id: slot
                required property string modelData
                required property int index

                width: grid.cell
                height: grid.cell
                x: (index % grid.cols) * grid.pitch
                y: Math.floor(index / grid.cols) * grid.pitch
                z: mover.pressed ? 1 : 0

                ControlTile {
                    width: parent.width
                    height: parent.height
                    x: mover.pressed ? mover.dx : 0
                    y: mover.pressed ? mover.dy : 0
                    tileId: slot.modelData
                    sound: panel.sound
                    onOpenPage: page => panel.openDrawer(page)
                }

                // Edit mode only, and above the tile so it takes the press
                // instead of the toggle. Offsets the tile while held; on
                // release the tile lands in the slot under its centre.
                MouseArea {
                    id: mover
                    anchors.fill: parent
                    enabled: panel.editing
                    preventStealing: true
                    // Both buttons and the wheel, so in edit mode nothing
                    // reaches the tile: no drawer page, no volume change.
                    acceptedButtons: Qt.LeftButton | Qt.RightButton
                    onWheel: wheel => wheel.accepted = true
                    cursorShape: pressed ? Qt.ClosedHandCursor : Qt.OpenHandCursor
                    property real sx: 0
                    property real sy: 0
                    property real dx: 0
                    property real dy: 0
                    onPressed: mouse => { sx = mouse.x; sy = mouse.y; dx = 0; dy = 0; }
                    onPositionChanged: mouse => { dx = mouse.x - sx; dy = mouse.y - sy; }
                    onReleased: {
                        const to = Controls.slotAt(slot.x + dx + width / 2, slot.y + dy + height / 2,
                                                   grid.pitch, grid.cols, Prefs.controls.length);
                        if (to !== slot.index)
                            Prefs.setControls(Controls.move(Prefs.controls, slot.index, to));
                    }
                }

                IconImage {
                    visible: panel.editing && !mover.pressed
                    anchors { top: parent.top; right: parent.right; margins: 3 }
                    implicitSize: 14
                    source: Quickshell.iconPath("window-close", true)

                    MouseArea {
                        anchors.fill: parent
                        anchors.margins: -4
                        cursorShape: Qt.PointingHandCursor
                        onClicked: Prefs.setControls(Controls.hide(Prefs.controls, slot.modelData))
                    }
                }
            }
        }
    }

    Flow {
        width: panel.width
        spacing: 6
        visible: panel.editing && Controls.hidden(Prefs.controls).length > 0

        Repeater {
            model: Controls.hidden(Prefs.controls)

            delegate: Rectangle {
                id: chip
                required property string modelData
                width: chipRow.implicitWidth + 16
                height: 28
                radius: 14
                color: Qt.alpha(Theme.surface, 0.5)

                Row {
                    id: chipRow
                    anchors.centerIn: parent
                    spacing: 4

                    IconImage {
                        anchors.verticalCenter: parent.verticalCenter
                        implicitSize: 14
                        source: Quickshell.iconPath("list-add", true)
                    }
                    Text {
                        anchors.verticalCenter: parent.verticalCenter
                        text: Controls.tile(chip.modelData).label
                        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
                        color: Theme.text
                    }
                }

                MouseArea {
                    anchors.fill: parent
                    cursorShape: Qt.PointingHandCursor
                    onClicked: Prefs.setControls(Controls.show(Prefs.controls, chip.modelData))
                }
            }
        }
    }
}