forked from mechtifs/wiggle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
122 lines (112 loc) · 4.37 KB
/
Copy pathextension.js
File metadata and controls
122 lines (112 loc) · 4.37 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
'use strict';
import GLib from 'gi://GLib';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import { getPointerWatcher } from 'resource:///org/gnome/shell/ui/pointerWatcher.js';
import { Field } from './const.js';
import Effect from './effect.js';
import History from './history.js';
const initSettings = (settings, entries) => {
const getValue = (name, type) => ({
'b': () => settings.get_boolean(name),
'd': () => settings.get_double(name),
'i': () => settings.get_int(name),
's': () => settings.get_string(name),
}[type]());
entries.forEach(([name, type, func]) => {
func(getValue(name, type));
settings.connect(`changed::${name}`, () => func(getValue(name, type)));
});
};
export default class WiggleExtension extends Extension {
_onCheckIntervalChange(interval) {
if (this._checkTimeoutId) {
GLib.Source.remove(this._checkTimeoutId);
}
this._checkTimeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, interval, () => {
if (this._checkCursorHiddenByProgram()) {
return GLib.SOURCE_CONTINUE;
}
if (this._history.check()) {
if (!this._effect.isWiggling) {
this._effect.move(this._history.lastCoords.x, this._history.lastCoords.y);
this._effect.magnify();
}
} else if (this._effect.isWiggling) {
this._effect.unmagnify();
}
return GLib.SOURCE_CONTINUE;
});
}
_checkCursorHiddenByProgram() {
// different program might take other methods to hide the cursor, so this check should contain more conditions
if (!this._effect.cursor.sprite) {
if (!this._isHiddenByProgram) {
this._togglePointerWatcher(false);
this._isHiddenByProgram = true;
}
return true;
} else if (this._isHiddenByProgram) {
this._togglePointerWatcher(true);
this._isHiddenByProgram = false;
}
return false;
}
_onDrawIntervalChange(interval) {
this._drawInterval = interval;
if (this._drawIntervalWatch) {
this._pointerWatcher._removeWatch(this._drawIntervalWatch);
}
this._drawIntervalWatch = this._pointerWatcher.addWatch(interval, (x, y) => {
this._history.push(x, y);
if (this._effect.isWiggling) {
this._effect.move(x, y);
}
});
}
_togglePointerWatcher(state) {
if (state) {
if (!this._drawIntervalWatch) {
this._onDrawIntervalChange(this._drawInterval);
}
} else {
if (this._effect.isWiggling) {
this._effect.unmagnify();
}
if (this._drawIntervalWatch) {
this._pointerWatcher._removeWatch(this._drawIntervalWatch);
this._drawIntervalWatch = null;
this._history.clear();
}
}
}
enable() {
this._pointerWatcher = getPointerWatcher();
this._history = new History();
this._effect = new Effect();
this._settings = this.getSettings();
initSettings(this._settings, [
[Field.HIDE, 'b', (r) => {this._effect.isHidden = r}],
[Field.SIZE, 'i', (r) => {this._effect.cursorSize = r}],
[Field.PATH, 's', (r) => {this._effect.cursorPath = r}],
[Field.MAGN, 'i', (r) => {this._effect.magnifyDuration = r}],
[Field.UMGN, 'i', (r) => {this._effect.unmagnifyDuration = r}],
[Field.DLAY, 'i', (r) => {this._effect.unmagnifyDelay = r}],
[Field.SAMP, 'i', (r) => {this._history.sampleSize = r}],
[Field.RADI, 'i', (r) => {this._history.radiansThreshold = r}],
[Field.DIST, 'i', (r) => {this._history.distanceThreshold = r}],
[Field.CHCK, 'i', (r) => this._onCheckIntervalChange(r)],
[Field.DRAW, 'i', (r) => this._onDrawIntervalChange(r)],
]);
}
disable() {
if (this._checkTimeoutId) {
GLib.Source.remove(this._checkTimeoutId);
}
this._togglePointerWatcher(false);
this._effect.destroy();
this._effect = null;
this._pointerWatcher = null;
this._history = null;
this._settings = null;
}
}