-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutils.js
100 lines (85 loc) · 2.3 KB
/
utils.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
'use strict';
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
const pointer_wrapper = {
get_position: () => {
let [px, py] = global.get_pointer();
return [{}, px, py];
},
warp: (screen, x, y) => {
screen.simulated_pointer = [x, y];
},
};
// Use UUID to avoid conflicting with other instances of this extensions (multi user setup)
const uuid = GLib.uuid_string_random();
/**
* Return a path in /tmp folder with a unique name
* @param {*} path
* @returns {string}
*/
export const tempPath = (path) => {
return `/tmp/${uuid}-${path}`
};
export const getPointer = () => {
return pointer_wrapper;
};
export const warpPointer = (pointer, x, y, extension) => {
pointer.warp(extension, x, y);
};
export const setTimeout = (func, delay, ...args) => {
const wrappedFunc = () => {
func.apply(this, args);
};
return GLib.timeout_add(GLib.PRIORITY_DEFAULT, delay, wrappedFunc);
};
export const setInterval = (func, delay, ...args) => {
const wrappedFunc = () => {
return func.apply(this, args) || true;
};
return GLib.timeout_add(GLib.PRIORITY_DEFAULT, delay, wrappedFunc);
};
export const clearTimeout = (id) => {
GLib.source_remove(id);
};
export const clearInterval = (id) => {
GLib.source_remove(id);
};
export const get_distance_sqr = (pos1, pos2) => {
let a = pos1[0] - pos2[0];
let b = pos1[1] - pos2[1];
return a * a + b * b;
};
export const get_distance = (pos1, pos2) => {
return Math.sqrt(get_distance_sqr(pos1, pos2));
};
export const isOverlapRect = (r1, r2) => {
let [r1x, r1y, r1w, r1h] = r1;
let [r2x, r2y, r2w, r2h] = r2;
// are the sides of one rectangle touching the other?
if (
r1x + r1w >= r2x && // r1 right edge past r2 left
r1x <= r2x + r2w && // r1 left edge past r2 right
r1y + r1h >= r2y && // r1 top edge past r2 bottom
r1y <= r2y + r2h
) {
// r1 bottom edge past r2 top
return true;
}
return false;
};
export const isInRect = (r, p, pad) => {
let [x1, y1, w, h] = r;
let x2 = x1 + w;
let y2 = y1 + h;
let [px, py] = p;
return px + pad >= x1 && px - pad < x2 && py + pad >= y1 && py - pad < y2;
};
export const trySpawnCommandLine = function (cmd) {
return new Promise((resolve, reject) => {
try {
GLib.spawn_command_line_async(cmd);
} catch (err) {
console.log(err);
}
});
};