-
Notifications
You must be signed in to change notification settings - Fork 3
/
widget.js
170 lines (139 loc) · 4.98 KB
/
widget.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
import GObject from 'gi://GObject';
import St from 'gi://St';
import Gio from 'gi://Gio';
import Clutter from 'gi://Clutter';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import {_addContextMenu} from './entryMenu.js';
const Widget = new GObject.registerClass(
class Widget extends PanelMenu.Button {
_init(settings, dir) {
super._init(0, 'AppWidget', false);
this._initialize(settings, dir);
}
_initialize(s, dir) {
if (this.initialized)
return;
this.initialized = true;
this._settings = s;
this._dir = dir;
this._e = Extension.lookupByURL(import.meta.url);
this._initEntry();
this._initPopup();
this._initContainer();
this._initEvents();
}
_initEntry() {
this.panelText = new St.Label({
text: this._settings.get_string('thing-value'),
track_hover: true,
can_focus: true,
y_align: Clutter.ActorAlign.CENTER,
style_class: 'one-thing-panel-text',
});
_addContextMenu(this.panelText);
this._settings.bind(
'thing-value',
this.panelText,
'text',
Gio.SettingsBindFlags.DEFAULT
);
}
_openPreferences() {
this._e.openPreferences();
this.menu.close();
}
_initPopup() {
this.inputText = new St.Entry({
hint_text: 'Write One Thing',
text: this._settings.get_string('thing-value'),
track_hover: true,
can_focus: true,
style_class: 'one-thing-input',
secondary_icon: new St.Icon({
icon_name: 'preferences-system-symbolic',
icon_size: 16,
}),
});
this.inputText.connect('secondary-icon-clicked', () => {
this._openPreferences();
});
this._settings.bind(
'show-settings-button-on-popup',
this.inputText.secondary_icon,
'visible',
Gio.SettingsBindFlags.DEFAULT
);
_addContextMenu(this.inputText);
const menuItem = new PopupMenu.PopupBaseMenuItem({
reactive: false,
});
if (typeof menuItem.add_child === 'function') {
menuItem.add_child(this.inputText);
} else {
menuItem.add_actor(this.inputText);
}
this.menu.addMenuItem(menuItem);
}
_initContainer() {
const oneThingContainer = new St.BoxLayout();
const path = this._dir
.get_child('assets')
.get_child('one-thing-gnome.svg')
.get_path();
this.icon = new St.Icon({
icon_name: 'one-thing-gnome',
icon_size: 24,
gicon: Gio.icon_new_for_string(path),
});
if (typeof oneThingContainer.add_child === 'function') {
oneThingContainer.add_child(this.icon);
oneThingContainer.add_child(this.panelText);
} else {
oneThingContainer.add(this.icon);
oneThingContainer.add(this.panelText);
}
const textValue = this.panelText.get_text();
this._showIconIfTextEmpty(textValue);
if (typeof this.add_child === 'function') {
this.add_child(oneThingContainer);
} else {
this.add_actor(oneThingContainer);
}
}
_initEvents() {
this.connect(
'button-press-event',
() => this._oneThing()
);
this.inputText.clutter_text.connect(
'activate',
this._onActivateEntry.bind(this)
);
}
_oneThing() {
let text = this._settings.get_string('thing-value');
if (this.menu.isOpen) {
this.inputText.grab_key_focus();
this.inputText.set_text(text);
if (text)
this.inputText.clutter_text.set_selection(-1, 0);
}
}
_onActivateEntry(actor) {
const textValue = actor.get_text();
this._showIconIfTextEmpty(textValue);
this.panelText.set_text(textValue);
this._settings.set_string('thing-value', textValue);
this.menu.close();
}
_showIconIfTextEmpty(text) {
if (text === '')
this.icon.show();
else
this.icon.hide();
}
}
);
export default Widget;