forked from Amm1rr/gnome-shell-extension-ddterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
256 lines (199 loc) · 10.1 KB
/
application.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
Copyright © 2020, 2021 Aleksandr Mezin
This file is part of ddterm GNOME Shell extension.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
'use strict';
const System = imports.system;
/* eslint-disable-next-line consistent-return */
function checked_import(libname, version) {
try {
imports.gi.versions[libname] = version;
return imports.gi[libname];
} catch (ex) {
const message = `Can't start ddterm - library ${libname}, version ${version} not available:\n${ex}\n\n` +
`You likely need to install the package that contains the file '${libname}-${version}.typelib'`;
printerr(message);
if (typeof GLib !== 'undefined')
GLib.spawn_sync(null, ['zenity', '--error', '--width=300', '--text', message], null, GLib.SpawnFlags.SEARCH_PATH, null);
System.exit(1);
}
}
const GLib = checked_import('GLib', '2.0');
const GObject = checked_import('GObject', '2.0');
const Gio = checked_import('Gio', '2.0');
const Gtk = checked_import('Gtk', '3.0');
const Gdk = checked_import('Gdk', '3.0');
/* These are used in other modules - check that they are available, and set required versions */
checked_import('Pango', '1.0');
checked_import('Vte', '2.91');
const APP_DATA_DIR = Gio.File.new_for_commandline_arg(System.programInvocationName).get_parent();
imports.searchPath.unshift(APP_DATA_DIR.get_path());
const { util } = imports;
util.APP_DATA_DIR = APP_DATA_DIR;
const Application = GObject.registerClass(
class Application extends Gtk.Application {
_init(params) {
super._init(params);
this.decorated = true;
this.add_main_option(
'undecorated', 0, GLib.OptionFlags.NONE, GLib.OptionArg.NONE, 'Hide window decorations', null
);
this.add_main_option(
'unset-gdk-backend', 0, GLib.OptionFlags.NONE, GLib.OptionArg.NONE, 'Unset GDK_BACKEND variable for subprocesses', null
);
this.add_main_option(
'reset-gdk-backend', 0, GLib.OptionFlags.NONE, GLib.OptionArg.STRING, 'Set GDK_BACKEND variable for subprocesses', null
);
this.env_gdk_backend = null;
this.unset_gdk_backend = false;
this.connect('startup', this.startup.bind(this));
this.connect('activate', this.activate.bind(this));
this.connect('handle-local-options', this.handle_local_options.bind(this));
this.window = null;
this.prefs_dialog = null;
}
startup() {
this.simple_action('quit', this.quit.bind(this));
const settings_source = Gio.SettingsSchemaSource.new_from_directory(
APP_DATA_DIR.get_child('schemas').get_path(),
Gio.SettingsSchemaSource.get_default(),
false
);
this.settings = new Gio.Settings({
settings_schema: settings_source.lookup('com.github.amezin.ddterm', true),
});
if (this.unset_gdk_backend)
GLib.unsetenv('GDK_BACKEND');
if (this.env_gdk_backend !== null)
GLib.setenv('GDK_BACKEND', this.env_gdk_backend, true);
const desktop_settings = new Gio.Settings({
schema_id: 'org.gnome.desktop.interface',
});
const menus = Gtk.Builder.new_from_file(APP_DATA_DIR.get_child('menus.ui').get_path());
const style = Gtk.CssProvider.new();
style.load_from_path(APP_DATA_DIR.get_child('style.css').get_path());
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), style, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
this.window = new imports.appwindow.AppWindow({
application: this,
decorated: this.decorated,
settings: this.settings,
desktop_settings,
menus,
});
this.add_action(this.window.toggle_action);
this.add_action(this.window.hide_action);
this.simple_action('preferences', this.preferences.bind(this));
this.add_action(this.settings.create_action('window-above'));
this.add_action(this.settings.create_action('window-stick'));
this.add_action(this.settings.create_action('window-maximize'));
this.add_action(this.settings.create_action('hide-when-focus-lost'));
this.add_action(this.settings.create_action('hide-window-on-esc'));
this.add_action(this.settings.create_action('shortcuts-enabled'));
this.add_action(this.settings.create_action('scroll-on-output'));
this.add_action(this.settings.create_action('scroll-on-keystroke'));
this.add_action(this.settings.create_action('preserve-working-directory'));
this.add_action(this.settings.create_action('transparent-background'));
this.gtk_settings = Gtk.Settings.get_default();
this.settings.connect('changed::theme-variant', this.update_theme.bind(this));
this.update_theme();
this.setup_shortcut('shortcut-window-hide', 'win.hide');
this.setup_shortcut('shortcut-toggle-maximize', 'app.window-maximize');
this.setup_shortcut('shortcut-toggle-transparent-background', 'app.transparent-background');
this.setup_shortcut('shortcut-terminal-copy', 'terminal.copy');
this.setup_shortcut('shortcut-terminal-copy-html', 'terminal.copy-html');
this.setup_shortcut('shortcut-terminal-paste', 'terminal.paste');
this.setup_shortcut('shortcut-terminal-select-all', 'terminal.select-all');
this.setup_shortcut('shortcut-terminal-reset', 'terminal.reset');
this.setup_shortcut('shortcut-terminal-reset-and-clear', 'terminal.reset-and-clear');
this.setup_shortcut('shortcut-win-new-tab', 'win.new-tab');
this.setup_shortcut('shortcut-win-new-tab-front', 'win.new-tab-front');
this.setup_shortcut('shortcut-win-new-tab-before-current', 'win.new-tab-before-current');
this.setup_shortcut('shortcut-win-new-tab-after-current', 'win.new-tab-after-current');
this.setup_shortcut('shortcut-page-close', 'page.close');
this.setup_shortcut('shortcut-prev-tab', 'win.prev-tab');
this.setup_shortcut('shortcut-next-tab', 'win.next-tab');
this.setup_shortcut('shortcut-set-custom-tab-title', 'page.use-custom-title(true)');
this.setup_shortcut('shortcut-reset-tab-title', 'page.use-custom-title(false)');
for (let i = 0; i < 10; i += 1)
this.setup_shortcut(`shortcut-switch-to-tab-${i + 1}`, `win.switch-to-tab(${i})`);
}
simple_action(name, func) {
const action = new Gio.SimpleAction({
name,
});
action.connect('activate', func);
this.add_action(action);
return action;
}
activate() {
if (!this.window) // There was an exception in startup()
System.exit(1);
this.window.show();
}
handle_local_options(_, options) {
if (options.contains('undecorated'))
this.decorated = false;
if (options.contains('unset-gdk-backend'))
this.unset_gdk_backend = true;
this.env_gdk_backend = options.lookup_value('reset-gdk-backend', GLib.VariantType.new('s'));
if (this.env_gdk_backend !== null)
this.env_gdk_backend = this.env_gdk_backend.unpack();
return -1;
}
preferences() {
if (this.prefs_dialog === null) {
this.prefs_dialog = new imports.prefsdialog.PrefsDialog({
transient_for: this.window,
settings: this.settings,
});
this.prefs_dialog.signal_connect(this.prefs_dialog, 'delete-event', () => {
this.prefs_dialog = null;
});
}
this.prefs_dialog.show();
}
quit() {
super.quit();
}
update_shortcut(key, action) {
const accels = this.settings.get_boolean('shortcuts-enabled') ? this.settings.get_strv(key) : [];
if (action === 'win.hide' && this.settings.get_boolean('hide-window-on-esc'))
accels.push('Escape');
this.set_accels_for_action(action, accels);
}
setup_shortcut(key, action) {
const update_fn = this.update_shortcut.bind(this, key, action);
this.settings.connect(`changed::${key}`, update_fn);
this.settings.connect('changed::shortcuts-enabled', update_fn);
if (action === 'win.hide')
this.settings.connect('changed::hide-window-on-esc', update_fn);
update_fn();
}
update_theme() {
const theme = this.settings.get_string('theme-variant');
if (theme === 'system')
this.gtk_settings.reset_property('gtk-application-prefer-dark-theme');
else if (theme === 'dark')
this.gtk_settings.gtk_application_prefer_dark_theme = true;
else if (theme === 'light')
this.gtk_settings.gtk_application_prefer_dark_theme = false;
}
}
);
GLib.set_prgname('com.github.amezin.ddterm');
GLib.set_application_name('Drop Down Terminal');
const app = new Application({
application_id: 'com.github.amezin.ddterm',
flags: Gio.ApplicationFlags.ALLOW_REPLACEMENT,
});
app.run([System.programInvocationName].concat(ARGV));