-
Notifications
You must be signed in to change notification settings - Fork 6
/
prefsWindowPickableEntry.js
216 lines (186 loc) · 8.13 KB
/
prefsWindowPickableEntry.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
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
'use strict';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
import * as PrefsWidgets from './prefsWidgets.js';
export const WindowPickableEntry = GObject.registerClass({
Signals: {
'entry-changed': {
param_types: [Gtk.Entry]
},
'entry-edit-complete': {
param_types: [Gtk.Entry]
},
}
}, class WindowPickableEntry extends Gtk.Box {
_init(entryParams, boxParams) {
super._init(PrefsWidgets.gtkBoxProperties);
Object.assign(this, boxParams);
const entry = new Gtk.Entry({
editable: false,
can_focus: false,
focus_on_click: false,
halign: Gtk.Align.START,
hexpand: true,
// Make sure that text align left
xalign: 0,
width_chars: 20,
max_width_chars: 20,
// ellipsize: Pango.EllipsizeMode.END,
});
this.entry = entry;
this.pickConditionFunc = entryParams.pickConditionFunc;
Object.assign(entry, entryParams);
this._initEntry(entry);
entry.set_tooltip_text(entry.get_text());
this.append(entry);
this.append(this.chooseButton);
}
setText(text) {
this.entry.set_text(text);
}
_initEntry(entry) {
entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, 'document-edit-symbolic');
entry.set_icon_tooltip_text(Gtk.EntryIconPosition.SECONDARY, 'Edit the entry');
entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY, true);
const iconPressId = entry.connect('icon-press', (source, icon_pos) => {
if (icon_pos !== Gtk.EntryIconPosition.SECONDARY)
return;
if (source._showSaveIconAWSM) {
delete source._showSaveIconAWSM;
this._completeEditEntry(entry);
if (this._prefsDialogCloseRequestId) {
const prefsDialogWindow = entry.get_root();
if (prefsDialogWindow) prefsDialogWindow.disconnect(this._prefsDialogCloseRequestId);
}
} else {
source.block_signal_handler(iconPressId);
source.set_can_focus(true);
source.set_editable(true);
source.grab_focus_without_selecting();
// -1 put the cursor to the end
source.set_position(-1);
source.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, 'emblem-ok-symbolic');
source.set_icon_tooltip_text(Gtk.EntryIconPosition.SECONDARY, 'Complete editing');
source._showSaveIconAWSM = true;
// Save the entry when we close the prefs dialog window
const prefsDialogWindow = entry.get_root();
if (prefsDialogWindow) {
this._prefsDialogCloseRequestId = prefsDialogWindow.connect('close-request', () => {
this.emit('entry-edit-complete', entry);
prefsDialogWindow.disconnect(this._prefsDialogCloseRequestId);
});
}
source.unblock_signal_handler(iconPressId);
}
});
// Accept Enter key to complete the editing
entry.connect('activate', () => {
this._completeEditEntry(entry);
});
let entryController = Gtk.EventControllerFocus.new();
entry.add_controller(entryController);
entryController.connect('leave', (source) => {
this._completeEditEntry(entry);
});
entry.connect('changed', (source) => {
this.emit('entry-changed', source);
});
// const image = new Gtk.Image({
// file: IconFinder.findPath('choose-window-symbolic.svg'),
// });
const chooseButton = new Gtk.Button({
icon_name: 'find-location-symbolic',
// label: 'Pick...',
tooltip_text: 'Choose a window to fill the entry based on the current setting',
});
this.chooseButton = chooseButton;
PrefsWidgets.updateStyle(entry,
`entry {
border-top-right-radius: 0px;
border-bottom-right-radius: 0px;
}`);
PrefsWidgets.updateStyle(chooseButton,
// Use .text-button if the button displays a label; Use .image-button if it displays an image
`.image-button {
padding-left: 0px;
padding-right: 6px;
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
}`);
// Pick a window to fetch application and window infos according to the current rule setting
chooseButton.connect('clicked', (source, pickedWidget) => {
if (this._dbusConnection) {
// Unsubscribe the existing PickWindow DBus service, just in case of modifying another entry.
Gio.DBus.session.signal_unsubscribe(this._dbusConnection);
this._dbusConnection = null;
}
Gio.DBus.session.call(
'org.gnome.Shell',
'/org/gnome/shell/extensions/awsm',
'org.gnome.Shell.Extensions.awsm.PickWindow', 'PickWindow',
null, null, Gio.DBusCallFlags.NO_AUTO_START, -1, null, null);
this._dbusConnection = this._subscribeSignal('WindowPicked', (conn, sender, obj_path, iface, signal, results) => {
// Unsubscribe the PickWindow DBus service, it's really no necessary to keep the subscription all the time
Gio.DBus.session.signal_unsubscribe(this._dbusConnection);
this._dbusConnection = null;
this._unfocus(entry);
const resultsArray = results.recursiveUnpack();
// Pick nothing, so we ignore this pick
if(!resultsArray.length) {
return;
}
const [appName, wmClass, wmClassInstance, title] = resultsArray;
let entryValue = '';
switch (this.pickConditionFunc()) {
case 'wm_class':
entryValue = wmClass;
break;
case 'wm_class_instance':
entryValue = wmClassInstance;
break;
case 'app_name':
entryValue = appName;
break;
case 'title':
entryValue = title;
break;
default:
break;
}
entry.set_text(entryValue);
entry.set_tooltip_text(entryValue);
this.emit('entry-edit-complete', entry);
});
});
this._subscribeSignal('WindowPickCancelled', () => {
// Unsubscribe the PickWindow DBus service, it's really no necessary to keep the subscription all the time
Gio.DBus.session.signal_unsubscribe(this._dbusConnection);
this._dbusConnection = null;
this._unfocus(entry);
});
}
_subscribeSignal(signalName, callback) {
const dbusConnection = Gio.DBus.session.signal_subscribe(
'org.gnome.Shell', 'org.gnome.Shell.Extensions.awsm.PickWindow',
signalName,
'/org/gnome/shell/extensions/awsm', null, Gio.DBusSignalFlags.NONE,
callback);
return dbusConnection;
}
_completeEditEntry(entry) {
entry.set_can_focus(false);
entry.set_editable(false);
this._unfocus(entry);
entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, 'document-edit-symbolic');
entry.set_icon_tooltip_text(Gtk.EntryIconPosition.SECONDARY, 'Edit the entry');
entry.set_tooltip_text(entry.get_text());
this.emit('entry-edit-complete', entry);
}
_unfocus(widget) {
const prefsDialogWindow = widget.get_root();
if (prefsDialogWindow)
// Pass `null` to unfocus the entry
prefsDialogWindow.set_focus(null);
}
});