summaryrefslogtreecommitdiff
path: root/launcher/components/Spell.qml
blob: 8feec57c18304d6b14bdd0b44ace8931d583fce9 (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
import Quickshell
import Quickshell.Io
import QtQuick
import QtQuick.Layouts

import qs.components
import qs.services
import qs.config

IComponent {
    id: root

    property list<string> aspell
    property int selectedIndex: -1

    name: "Spell"
    prefix: name.toLowerCase() + ' '

    onValidChanged: {
        if (valid)
            return;

        aspell = [];
        proc.running = false;
        selectedIndex = -1;
        return;
    }

    onAspellChanged: {
        selectedIndex = aspell.length > 0 ? 0 : -1;
        // selectedIndexChanged dont get called somehow
        syncSelectionState();
        if (loader.item && loader.item.flickable) {
            loader.item.flickable.contentY = 0;
        }
    }

    onSelectedIndexChanged: {
        syncSelectionState();
        if (!loader.repeater || !loader.item || !loader.item.flickable)
            return;
        if (selectedIndex < 0 || selectedIndex >= loader.repeater.count)
            return;
        Qt.callLater(() => {
            const flickable = loader.item.flickable;
            const i = loader.repeater.itemAt(selectedIndex);
            if (!i)
                return;
            const t = i.y;
            const b = i.y + i.height;
            const v = flickable.contentY;
            const vb = v + flickable.height;
            const maxY = Math.max(0, flickable.contentHeight - flickable.height);
            const y = t < v ? t : b > vb ? b - flickable.height : v;
            flickable.contentY = Math.max(0, Math.min(maxY, y));
        });
    }

    process: function () {
        Qt.callLater(() => {
            proc.running = true;
        });

        const isValid = selectedIndex >= 0 && aspell.length > 0;
        const selected = isValid ? aspell[selectedIndex] : "";
        const answer = selected === "*" ? input : selected;

        return {
            valid: isValid,
            priority: isValid,
            answer: isValid ? answer : "",
            predictiveCompletion: isValid ? answer.slice(input.length) : ""
        };
    }

    home: function () {
        if (loader.repeater.count === 0)
            return true;
        selectedIndex = -1;
        selectedIndex = 0;
    }
    end: function () {
        if (loader.repeater.count === 0)
            return true;
        selectedIndex = loader.repeater.count;
        selectedIndex = loader.repeater.count - 1;
    }

    prev: function () {
        if (selectedIndex <= 0)
            return true;
        selectedIndex--;
    }

    next: function () {
        if (selectedIndex + 1 >= aspell.length)
            return true;
        selectedIndex++;
    }

    up: function () {
        const cols = Math.max(1, Math.floor(innerLoader.width / Math.max(1, Math.round(Launcher.widgetFontSize * 6))));
        if (selectedIndex - cols < 0)
            return true;
        selectedIndex -= cols;
    }

    down: function () {
        const cols = Math.max(1, Math.floor(innerLoader.width / Math.max(1, Math.round(Launcher.widgetFontSize * 6))));
        if (selectedIndex + cols >= aspell.length)
            return true;
        selectedIndex += cols;
    }

    exec: function () {
        if (selectedIndex >= 0)
            if (aspell[selectedIndex] !== "*")
                Clip.copy(aspell[selectedIndex]);
            else
                Clip.copy(input);
    }

    syncSelectionState: function () {
        if (!loader.repeater)
            return;

        Qt.callLater(() => {
            SelectionState.selected = loader.repeater.itemAt(selectedIndex);
            SelectionState.exec = root.exec;
        });
    }

    IInnerComponent {
        id: innerLoader
        Loader {
            id: loader
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.preferredHeight: parent.height
            sourceComponent: aspell[0] === "*" ? correct : show
            property var repeater: null

            readonly property Component show: Component {
                Item {
                    property alias flickable: flickable

                    Flickable {
                        id: flickable
                        anchors.fill: parent
                        clip: true
                        contentWidth: grid.width
                        contentHeight: grid.height

                        GridLayout {
                            id: grid
                            width: flickable.width
                            columns: Math.max(1, Math.floor(width / Math.max(1, Math.round(Launcher.widgetFontSize * 6))))
                            rowSpacing: Launcher.innerMargin
                            columnSpacing: Launcher.innerMargin

                            Repeater {
                                id: repeater
                                model: aspell

                                IRect {
                                    Layout.fillWidth: true
                                    Layout.preferredWidth: Math.max(chipContent.implicitWidth + Launcher.innerMargin * 2, Math.floor(grid.width / grid.columns) - grid.columnSpacing)
                                    Layout.preferredHeight: chipContent.implicitHeight + Launcher.innerMargin * 2
                                    color: Qt.rgba(Colors.background2.r, Colors.background2.g, Colors.background2.b, Launcher.widgetBgTransparency)
                                    radius: Launcher.widgetRadius

                                    IText {
                                        id: chipContent
                                        anchors.centerIn: parent
                                        text: modelData
                                        font.pixelSize: Launcher.widgetFontSize
                                        elide: Text.ElideRight
                                    }

                                    MouseArea {
                                        anchors.fill: parent
                                        hoverEnabled: true
                                        onPositionChanged: {
                                            root.selectedIndex = index;
                                        }
                                        onPressed: {
                                            root._exec();
                                        }
                                    }
                                }

                                Component.onCompleted: {
                                    loader.repeater = repeater;
                                }
                            }
                        }
                    }
                }
            }

            readonly property Component correct: Component {
                IRect {
                    Layout.fillWidth: true
                    color: Qt.rgba(Colors.background1.r, Colors.background1.g, Colors.background1.b, Launcher.widgetBgTransparency / 4)
                    radius: Launcher.widgetRadius
                    IText {
                        anchors.centerIn: parent
                        text: "Correct"
                        font.pixelSize: Launcher.widgetFontSize
                        anchors.horizontalCenter: parent.horizontalCenter
                    }
                }
            }
        }
    }

    Process {
        id: proc
        command: ["sh", "-c", "echo " + input + " | aspell -a"]
        property bool found: false
        stdout: SplitParser {
            onRead: data => {
                if (data.startsWith("&")) {
                    proc.found = true;
                    aspell = data.split(/\W+/).filter(input => /^[A-Za-z]+$/.test(input)).slice(1);
                } else if (data.startsWith("*")) {
                    proc.found = true;
                    aspell = ["*"];
                }
            }
        }
        onStarted: {
            found = false;
        }
        onExited: {
            if (!found)
                aspell = [];
        }
    }
}