summaryrefslogtreecommitdiff
path: root/popups/launcher/components/Emoji.qml
blob: 67d33b4b7b1b297e40fc7dc24d670e1fb8f4bb64 (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
import Quickshell
import Quickshell.Io
import QtQuick
import QtQuick.Layouts

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

IComponent {
	id: root

	property var emojis: active ? Emoji.query(input) : []
	property int selectedIndex: -1

	property int cols: Math.max(1, Math.floor(innerLoader.width / Math.max(1, Math.round(Launcher.widgetFontSize * 4))))
	property int cellWidth: Math.floor(innerLoader.width / cols)
	property int cellHeight: Math.ceil(Launcher.widgetFontSize * 2) + Launcher.innerMargin * 2

	name: "Emoji"
	prefix: name.toLowerCase() + " "

	onEmojisChanged: {
		Qt.callLater(() => {
			if (selectedIndex !== -1) {
				syncSelectionState();
				return;
			}

			selectedIndex = 0;
			if (loader.item && loader.item.flickable)
				loader.item.flickable.contentY = 0;
		});
	}

	onSelectedIndexChanged: {
		Qt.callLater(() => {
			if (!loader?.view || selectedIndex < 0 || selectedIndex >= loader.view.count)
				return;

			if (!isSelectedPriority() && state.selected === loader.view.itemAtIndex(selectedIndex)) {
				state.selected = null;
				return;
			}

			loader.view.currentIndex = selectedIndex;
			loader.view.scrollToIndex(selectedIndex, GridView.Contain);
			syncSelectionState();
		});
	}

	process: function () {
		const isValid = selectedIndex >= 0 && emojis.length > 0;
		const select = isValid ? emojis[selectedIndex] : null;
		return {
			valid: isValid,
			priority: isValid,
			answer: isValid ? select.emoji : ""
		};
	}

	home: function () {
		if (!loader.view.count)
			return true;
		selectedIndex = 0;
	}
	end: function () {
		if (!loader.view.count)
			return true;
		selectedIndex = loader.view.count - 1;
	}
	prev: function () {
		if (selectedIndex <= 0)
			return true;
		selectedIndex--;
	}
	next: function () {
		if (selectedIndex + 1 >= emojis.length)
			return true;
		selectedIndex++;
	}
	up: function () {
		if (selectedIndex - cols < 0)
			return true;
		selectedIndex -= cols;
	}
	down: function () {
		if (selectedIndex + cols >= emojis.length)
			return true;
		selectedIndex += cols;
	}

	exec: function () {
		if (selectedIndex >= 0 && emojis.length > 0)
			Emoji.copy(emojis[selectedIndex].emoji);
	}

	syncSelectionState: function () {
		Qt.callLater(() => {
			if (!loader.view || selectedIndex < 0 || selectedIndex >= loader.view.count || !root.visible)
				return;

			state.selected = loader.view.itemAtIndex(selectedIndex);
		});
	}

	IInnerComponent {
		id: innerLoader

		Loader {
			id: loader
			Layout.fillWidth: true
			Layout.fillHeight: true
			Layout.preferredHeight: parent.height
			sourceComponent: show
			property var view: null

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

					GridView {
						id: grid
						anchors.fill: parent
						clip: true
						boundsBehavior: Flickable.StopAtBounds
						cacheBuffer: cellHeight * 2
						model: emojis
						interactive: true
						flow: GridView.FlowLeftToRight
						snapMode: GridView.NoSnap

						cellWidth: root.cellWidth
						cellHeight: root.cellHeight

						delegate: Item {
							required property var modelData
							required property int index
							width: grid.cellWidth
							height: chipContent.implicitHeight + Launcher.innerMargin * 2
							scale: isSelectedPriority() && index === root.selectedIndex ? 1.03 : 0.97

							IRect {
								anchors.fill: parent
								color: Qt.rgba(Colors.background2.r, Colors.background2.g, Colors.background2.b, Launcher.widgetBgTransparency)
								radius: Launcher.widgetRadius

								Column {
									id: chipContent
									anchors.centerIn: parent
									spacing: 2

									Icon {
										anchors.horizontalCenter: parent.horizontalCenter
										text: modelData.emoji
										font.pixelSize: Launcher.widgetFontSize * 1.2
									}
									IText {
										anchors.horizontalCenter: parent.horizontalCenter
										text: modelData.name
										font.pixelSize: Launcher.widgetFontSize * 0.7
										font.bold: isSelectedPriority() && index === selectedIndex
										color: index === selectedIndex ? Colors.foreground1 : Colors.foreground2
										elide: Text.ElideRight
										width: parent.parent.width - Launcher.innerMargin * 2
									}
								}

								MouseArea {
									anchors.fill: parent
									hoverEnabled: true
									onEntered: root.selectedIndex = index
									onClicked: root._exec()
								}
							}

							Behavior on scale {
								ISpringAnimation {}
							}
						}

						Component.onCompleted: loader.view = grid
					}
				}
			}
		}
	}
}