Repository navigation
Expand file tree
/
Copy pathhoverPopups.js
More file actions
276 lines (232 loc) · 7.95 KB
/
Copy pathhoverPopups.js
File metadata and controls
276 lines (232 loc) · 7.95 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// SPDX-License-Identifier: GPL-3.0-or-later
import Clutter from 'gi://Clutter';
import GLib from 'gi://GLib';
import St from 'gi://St';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import {POPUP_HIDE_TIME, POPUP_SHOW_TIME} from './motion.js';
import {PreviewPopup, popupPosition} from './previews.js';
// How long the pointer rests on an icon before its popup shows.
// DASH_ITEM_HOVER_TIMEOUT in dash.js.
const HOVER_DELAY = 300;
// Time for the pointer to move from a button into its preview.
const HOVER_GRACE = 300;
// After a popup goes away, the next icon shows its popup without waiting,
// so sliding along the bar reads each icon in turn, as in the dash.
const RESHOW_WINDOW = 300;
// The tooltip or window preview shown while the pointer rests on an icon.
// Buttons report hover changes; this decides what to show and when.
//
// A button passed in must provide getWindows() and getTooltipText().
export class HoverPopups {
// `side` is the screen edge the bar sits on, as St.Side.
constructor(settings, side) {
this._settings = settings;
this._side = side;
this._source = null;
this._tooltip = null;
this._tooltipShown = false;
this._preview = null;
this._recentlyShown = false;
this._showTimeoutId = 0;
this._hideTimeoutId = 0;
this._reshowTimeoutId = 0;
this._showPreviews = settings.get_boolean('show-previews');
settings.connectObject('changed::show-previews', () => {
this._showPreviews = settings.get_boolean('show-previews');
if (!this._showPreviews)
this._preview?.dismiss();
}, this);
}
show(button) {
this._source = button;
this._removeShowTimeout();
this._removeHideTimeout();
// With a popup up, or one just gone, move to the new button at once.
if (this._popupShown() || this._recentlyShown) {
this._present();
return;
}
this._showTimeoutId = GLib.timeout_add(
GLib.PRIORITY_DEFAULT, HOVER_DELAY, () => {
this._showTimeoutId = 0;
this._present();
return GLib.SOURCE_REMOVE;
});
}
// A null `button` hides whatever is up. `immediate` skips HOVER_GRACE,
// which otherwise lets the pointer travel into the preview, and the
// reshow window, as a click or a menu should.
hide(button, immediate = false) {
if (button && this._source !== button)
return;
this._removeShowTimeout();
this._source = null;
if (immediate)
this._endReshowWindow();
else if (this._popupShown())
this._startReshowWindow();
this._hideTooltip();
if (!this._preview?.isShown)
return;
if (immediate)
this._preview.dismiss();
else
this._scheduleHide();
}
// Call before destroying a button.
forget(button) {
if (this._source === button) {
this._removeShowTimeout();
this._source = null;
this._hideTooltip();
}
if (this._preview?.sourceButton === button)
this._preview.dismiss();
}
// Call after the bar rebuilds, since a previewed button's windows may
// have changed.
refresh() {
const source = this._preview?.isShown ? this._preview.sourceButton : null;
if (!source)
return;
const windows = source.getWindows();
if (windows.length > 0)
this._preview.present(source, windows);
else
this._preview.dismiss();
}
refreshTitles() {
if (this._tooltipShown && this._source)
this._presentTooltip(this._source);
this._preview?.refreshTitles();
}
_popupShown() {
return this._tooltipShown || (this._preview?.isShown ?? false);
}
_present() {
const button = this._source;
if (!button)
return;
const windows = button.getWindows();
if (windows.length > 0 && this._showPreviews) {
this._hideTooltip();
this._ensurePreview().present(button, windows);
} else {
this._preview?.dismiss();
this._presentTooltip(button);
}
}
_ensurePreview() {
if (this._preview)
return this._preview;
this._preview = new PreviewPopup(this._settings, this._side);
this._preview.connect('notify::hover', () => {
if (!this._preview.isShown)
return;
if (this._preview.hover)
this._removeHideTimeout();
else if (!this._source)
this._scheduleHide();
});
return this._preview;
}
// A tooltip still on screen glides to the new button; one that has
// gone fades in where it is needed.
_presentTooltip(button) {
if (!this._tooltip) {
// Not reactive, so it stays out of the input region.
this._tooltip = new St.Label({style_class: 'appbar-tooltip'});
this._tooltip.hide();
Main.layoutManager.addTopChrome(this._tooltip);
}
const tooltip = this._tooltip;
const glide = tooltip.visible;
this._tooltipShown = true;
tooltip.text = button.getTooltipText();
tooltip.show();
const [x, y] = popupPosition(tooltip, button, this._side);
if (glide) {
tooltip.ease({
x,
y,
opacity: 255,
duration: POPUP_SHOW_TIME,
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
});
return;
}
tooltip.set_position(x, y);
tooltip.opacity = 0;
tooltip.ease({
opacity: 255,
duration: POPUP_SHOW_TIME,
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
});
}
_hideTooltip() {
if (!this._tooltipShown)
return;
this._tooltipShown = false;
this._tooltip.ease({
opacity: 0,
duration: POPUP_HIDE_TIME,
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
onComplete: () => this._tooltip.hide(),
});
}
_startReshowWindow() {
this._removeReshowTimeout();
this._recentlyShown = true;
this._reshowTimeoutId = GLib.timeout_add(
GLib.PRIORITY_DEFAULT, RESHOW_WINDOW, () => {
this._reshowTimeoutId = 0;
this._recentlyShown = false;
return GLib.SOURCE_REMOVE;
});
}
_endReshowWindow() {
this._removeReshowTimeout();
this._recentlyShown = false;
}
_scheduleHide() {
this._removeHideTimeout();
this._hideTimeoutId = GLib.timeout_add(
GLib.PRIORITY_DEFAULT, HOVER_GRACE, () => {
this._hideTimeoutId = 0;
if (this._preview && !this._preview.hover)
this._preview.dismiss();
return GLib.SOURCE_REMOVE;
});
}
_removeHideTimeout() {
if (!this._hideTimeoutId)
return;
GLib.Source.remove(this._hideTimeoutId);
this._hideTimeoutId = 0;
}
_removeShowTimeout() {
if (!this._showTimeoutId)
return;
GLib.Source.remove(this._showTimeoutId);
this._showTimeoutId = 0;
}
_removeReshowTimeout() {
if (!this._reshowTimeoutId)
return;
GLib.Source.remove(this._reshowTimeoutId);
this._reshowTimeoutId = 0;
}
destroy() {
this._removeShowTimeout();
this._removeHideTimeout();
this._removeReshowTimeout();
this._settings.disconnectObject(this);
this._settings = null;
this._source = null;
// Destroying chrome also untracks it, and stops its animations.
this._tooltip?.destroy();
this._tooltip = null;
this._preview?.destroy();
this._preview = null;
}
}