-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutil.js
More file actions
169 lines (146 loc) · 5.88 KB
/
Copy pathutil.js
File metadata and controls
169 lines (146 loc) · 5.88 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
/**
* WTMB (Window Thumbnails)
* util.js
*
* @author GdH <G-dH@github.com>
* @copyright 2024
* @license GPL-3.0
*/
'use strict';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import Clutter from 'gi://Clutter';
import Meta from 'gi://Meta';
import Shell from 'gi://Shell';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
let Me;
let _installedExtensions;
export function init(me) {
Me = me;
}
export function cleanGlobals() {
Me = null;
_installedExtensions = null;
}
export function openPreferences(metadata) {
if (!metadata)
metadata = Me.metadata;
const windows = global.display.get_tab_list(Meta.TabList.NORMAL_ALL, null);
let tracker = Shell.WindowTracker.get_default();
let metaWin, isMe = null;
for (let win of windows) {
const app = tracker.get_window_app(win);
if (win.get_title()?.includes(metadata.name) && app.get_name() === 'Extensions') {
// this is our existing window
metaWin = win;
isMe = true;
break;
} else if (win.wm_class?.includes('org.gnome.Shell.Extensions')) {
// this is prefs window of another extension
metaWin = win;
isMe = false;
}
}
if (metaWin && !isMe) {
// other prefs window blocks opening another prefs window, so close it
metaWin.delete(global.get_current_time());
} else if (metaWin && isMe) {
// if prefs window already exist, move it to the current WS and activate it
metaWin.change_workspace(global.workspace_manager.get_active_workspace());
metaWin.activate(global.get_current_time());
}
if (!metaWin || (metaWin && !isMe)) {
// delay to avoid errors if previous prefs window has been colsed
GLib.idle_add(GLib.PRIORITY_LOW, () => {
try {
Main.extensionManager.openExtensionPrefs(metadata.uuid, '', {});
} catch (e) {
console.error(e);
}
});
}
}
export function isShiftPressed(state = null) {
if (state === null)
[,, state] = global.get_pointer();
return (state & Clutter.ModifierType.SHIFT_MASK) !== 0;
}
export function isCtrlPressed(state = null) {
if (state === null)
[,, state] = global.get_pointer();
return (state & Clutter.ModifierType.CONTROL_MASK) !== 0;
}
export function isAltPressed(state = null) {
if (state === null)
[,, state] = global.get_pointer();
return (state & Clutter.ModifierType.MOD1_MASK) !== 0;
}
export function getScrollDirection(event) {
// scroll wheel provides two types of direction information:
// 1. Clutter.ScrollDirection.DOWN / Clutter.ScrollDirection.UP
// 2. Clutter.ScrollDirection.SMOOTH + event.get_scroll_delta()
// first SMOOTH event returns 0 delta,
// so we need to always read event.direction
// since mouse without smooth scrolling provides exactly one SMOOTH event on one wheel rotation click
// on the other hand, under X11, one wheel rotation click sometimes doesn't send direction event, only several SMOOTH events
// so we also need to convert the delta to direction
let direction = event.get_scroll_direction();
if (direction !== Clutter.ScrollDirection.SMOOTH)
return direction;
let [, delta] = event.get_scroll_delta();
if (!delta)
return null;
direction = delta > 0 ? Clutter.ScrollDirection.DOWN : Clutter.ScrollDirection.UP;
return direction;
}
export function getEnabledExtensions(pattern = '') {
let result = [];
// extensionManager is unreliable at startup because it is uncertain whether all extensions have been loaded
// also gsettings key can contain already removed extensions (user deleted them without disabling them first)
// therefore we have to check what's really installed in the filesystem
if (!_installedExtensions) {
const extensionFiles = [...collectFromDatadirs('extensions', true)];
_installedExtensions = extensionFiles.map(({ info }) => {
let fileType = info.get_file_type();
if (fileType !== Gio.FileType.DIRECTORY)
return null;
const uuid = info.get_name();
return uuid;
});
}
// _enabledExtensions contains content of the enabled-extensions key from gsettings, not actual state
const enabled = Main.extensionManager._enabledExtensions;
result = _installedExtensions.filter(ext => enabled.includes(ext));
// _extensions contains already loaded extensions, so we can try to filter out broken or incompatible extensions
const active = Main.extensionManager._extensions;
result = result.filter(ext => {
const extension = active.get(ext);
if (extension)
return ![3, 4].includes(extension.state); // 3 - ERROR, 4 - OUT_OF_TIME (not supported by shell-version in metadata)
// extension can be enabled but not yet loaded, we just cannot see its state at this moment, so let it pass as enabled
return true;
});
// return only extensions matching the search pattern
return result.filter(uuid => uuid !== null && uuid.includes(pattern));
}
function* collectFromDatadirs(subdir, includeUserDir) {
let dataDirs = GLib.get_system_data_dirs();
if (includeUserDir)
dataDirs.unshift(GLib.get_user_data_dir());
for (let i = 0; i < dataDirs.length; i++) {
let path = GLib.build_filenamev([dataDirs[i], 'gnome-shell', subdir]);
let dir = Gio.File.new_for_path(path);
let fileEnum;
try {
fileEnum = dir.enumerate_children('standard::name,standard::type',
Gio.FileQueryInfoFlags.NONE, null);
} catch (e) {
fileEnum = null;
}
if (fileEnum !== null) {
let info;
while ((info = fileEnum.next_file(null)))
yield { dir: fileEnum.get_child(info), info };
}
}
}