forked from elcste/hide-cursor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
119 lines (103 loc) · 3.38 KB
/
extension.js
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
/**
* Copyright 2025 Digitalone
* Copyright 2024-25 Alexander Browne
* Copyright 2020 Evan Welsh (https://gjs.guide/extensions/review-guidelines/review-guidelines.html#remove-main-loop-sources)
* Copyright 2020 Jeff Channell (https://github.com/jeffchannell/jiggle/blob/master/cursor.js)
*/
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
import GLib from "gi://GLib";
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
export default class HideCursor extends Extension {
enable() {
// Configuration.
const checkEvery = 1; // Seconds.
const disappearAfter = 3000000; // MicroSeconds.
// Retrieve Cursor Tracker.
try {
this._tracker = global.backend.get_cursor_tracker();
} catch (e) {
console.error("Failed to initialize cursor tracker:", e);
return;
}
// Internals.
this._tick = GLib.get_monotonic_time(); // MicroSecond timestamp to track cursor changes.
this._visibleCursor = true; // Visibility flag to perform less work.
// Set GLib timeout callback.
this._hide = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
checkEvery,
() => {
if (
this._visibleCursor === true &&
GLib.get_monotonic_time() - this._tick >= disappearAfter
) {
this._tracker.set_pointer_visible(false);
this._visibleCursor = false;
}
return GLib.SOURCE_CONTINUE;
}
);
// Callbacks.
const updateTick = () => {
let visible = false;
if (this._tracker?.get_pointer_visible() === true) {
this._tick = GLib.get_monotonic_time();
visible = true;
}
// Update visibility flag if it's changed.
if (this._visibleCursor !== visible) {
this._visibleCursor = visible;
}
};
// Connect callback to tracker signals.
this._resetOnMotion = this._tracker.connect(
"position-invalidated",
updateTick
);
this._resetOnVisibility = this._tracker.connect(
"visibility-changed",
updateTick
);
this._resetOnNewCursor = this._tracker.connect(
"cursor-changed",
updateTick
);
}
disable() {
// Cleanup.
if (this._tracker?.get_pointer_visible() === false) {
this._tracker.set_pointer_visible(true);
}
if (this._resetOnMotion) {
this._tracker.disconnect(this._resetOnMotion);
this._resetOnMotion = null;
}
if (this._resetOnVisibility) {
this._tracker.disconnect(this._resetOnVisibility);
this._resetOnVisibility = null;
}
if (this._resetOnNewCursor) {
this._tracker.disconnect(this._resetOnNewCursor);
this._resetOnNewCursor = null;
}
if (this._hide) {
GLib.Source.remove(this._hide);
this._hide = null;
}
}
}