Skip to content

Commit df4349c

Browse files
committed
application: remove RxJS
#389
1 parent 16bbe12 commit df4349c

File tree

2 files changed

+137
-127
lines changed

2 files changed

+137
-127
lines changed

ddterm/app/application.js

+132-124
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const System = imports.system;
2424
const Me = imports.misc.extensionUtils.getCurrentExtension();
2525

2626
const { backport } = imports.ddterm;
27-
const { translations, simpleaction } = imports.ddterm.util;
27+
const { translations } = imports.ddterm.util;
2828
const { timers } = imports.ddterm.rx;
2929

3030
translations.init(Me.dir);
@@ -41,20 +41,27 @@ imports.ddterm.app.dependencies.gi_require({
4141

4242
const { Gdk, Gtk } = imports.gi;
4343

44-
const { rxjs } = imports.ddterm.thirdparty.rxjs;
45-
const { rxutil, settings } = imports.ddterm.rx;
44+
const { AppWindow } = imports.ddterm.app.appwindow;
45+
const { PrefsDialog } = imports.ddterm.pref.dialog;
4646

4747
const APP_DIR = Me.dir.get_child('ddterm').get_child('app');
4848

4949
const Application = backport.GObject.registerClass(
5050
{
5151
Properties: {
52-
'preferences-visible': GObject.ParamSpec.boolean(
53-
'preferences-visible',
52+
'window': GObject.ParamSpec.object(
53+
'window',
5454
'',
5555
'',
56-
GObject.ParamFlags.READABLE | GObject.ParamFlags.EXPLICIT_NOTIFY,
57-
false
56+
GObject.ParamFlags.READWRITE | GObject.ParamFlags.EXPLICIT_NOTIFY,
57+
AppWindow
58+
),
59+
'prefs-dialog': GObject.ParamSpec.object(
60+
'prefs-dialog',
61+
'',
62+
'',
63+
GObject.ParamFlags.READWRITE | GObject.ParamFlags.EXPLICIT_NOTIFY,
64+
PrefsDialog
5865
),
5966
},
6067
},
@@ -97,14 +104,11 @@ const Application = backport.GObject.registerClass(
97104
null
98105
);
99106

100-
this.env_gdk_backend = null;
101-
this.unset_gdk_backend = false;
102-
103107
this.connect('startup', this.startup.bind(this));
104108
this.connect('handle-local-options', this.handle_local_options.bind(this));
105109

106-
this.window = null;
107-
this.prefs_dialog = null;
110+
this.env_gdk_backend = null;
111+
this.unset_gdk_backend = false;
108112
}
109113

110114
startup() {
@@ -114,41 +118,28 @@ const Application = backport.GObject.registerClass(
114118
if (this.env_gdk_backend !== null)
115119
GLib.setenv('GDK_BACKEND', this.env_gdk_backend, true);
116120

117-
this.rx = rxutil.scope(this, rxutil.signal(this, 'shutdown'));
118-
119-
const actions = {
120-
'quit': () => this.quit(),
121-
'preferences': () => this.preferences(),
122-
'begin-subscription-leak-check': () => rxutil.begin_subscription_leak_check(),
123-
'end-subscription-leak-check': () => rxutil.end_subscription_leak_check(),
124-
'gc': () => System.gc(),
125-
};
126-
127-
for (const [name, activate] of Object.entries(actions))
128-
this.add_action(new simpleaction.Action({ name, activate }));
129-
130-
this.add_action(new simpleaction.Action({
131-
name: 'dump-heap',
132-
activate: arg => this.dump_heap(arg.deepUnpack()),
133-
parameter_type: new GLib.VariantType('s'),
134-
}));
135-
136-
const close_preferences_action = new simpleaction.Action({
137-
name: 'close-preferences',
138-
activate: () => this.close_preferences(),
139-
});
121+
this.simple_action('quit', () => this.quit());
122+
this.simple_action('preferences', () => this.preferences());
123+
this.simple_action('gc', () => System.gc());
140124

141-
this.rx.subscribe(
142-
rxutil.property(this, 'preferences-visible'),
143-
rxutil.property(close_preferences_action, 'enabled')
125+
this.simple_action(
126+
'dump-heap',
127+
(_, param) => this.dump_heap(param.deepUnpack()),
128+
{ parameter_type: new GLib.VariantType('s') }
144129
);
145130

146-
this.add_action(close_preferences_action);
131+
const close_preferences_action = this.simple_action(
132+
'close-preferences',
133+
() => this.close_preferences(),
134+
{ enabled: false }
135+
);
147136

148-
this.settings = new settings.Settings({
149-
gsettings: imports.ddterm.util.settings.get_settings(),
137+
this.connect('notify::prefs-dialog', () => {
138+
close_preferences_action.enabled = this.prefs_dialog !== null;
150139
});
151140

141+
this.settings = imports.ddterm.util.settings.get_settings();
142+
152143
[
153144
'window-above',
154145
'window-stick',
@@ -161,24 +152,11 @@ const Application = backport.GObject.registerClass(
161152
'preserve-working-directory',
162153
'transparent-background',
163154
].forEach(key => {
164-
this.add_action(this.settings.gsettings.create_action(key));
155+
this.add_action(this.settings.create_action(key));
165156
});
166157

167-
const gtk_settings = Gtk.Settings.get_default();
168-
169-
this.rx.subscribe(
170-
this.settings.resolved['theme-variant'],
171-
theme => {
172-
if (theme === 'default')
173-
gtk_settings.reset_property('gtk-application-prefer-dark-theme');
174-
else if (theme === 'dark')
175-
gtk_settings.gtk_application_prefer_dark_theme = true;
176-
else if (theme === 'light')
177-
gtk_settings.gtk_application_prefer_dark_theme = false;
178-
else
179-
printerr(`Unknown theme-variant: ${theme}`);
180-
}
181-
);
158+
this.settings.connect('changed::theme-variant', this.update_theme.bind(this));
159+
this.update_theme();
182160

183161
const menus = Gtk.Builder.new_from_file(APP_DIR.get_child('menus.ui').get_path());
184162

@@ -190,70 +168,51 @@ const Application = backport.GObject.registerClass(
190168
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
191169
);
192170

193-
this.window = new imports.ddterm.app.appwindow.AppWindow({
171+
this.window = new AppWindow({
194172
application: this,
195173
decorated: this.decorated,
196-
settings: this.settings,
174+
gsettings: this.settings,
197175
menus,
198176
});
199177

200178
this.add_action(this.window.lookup_action('toggle'));
201179
this.add_action(this.window.lookup_action('show'));
202180
this.add_action(this.window.lookup_action('hide'));
203181

204-
const shortcut_actions = {
205-
'shortcut-window-hide': 'win.hide',
206-
'shortcut-window-size-inc': 'win.window-size-inc',
207-
'shortcut-window-size-dec': 'win.window-size-dec',
208-
'shortcut-background-opacity-inc': 'win.background-opacity-inc',
209-
'shortcut-background-opacity-dec': 'win.background-opacity-dec',
210-
'shortcut-toggle-maximize': 'app.window-maximize',
211-
'shortcut-toggle-transparent-background': 'app.transparent-background',
212-
'shortcut-terminal-copy': 'terminal.copy',
213-
'shortcut-terminal-copy-html': 'terminal.copy-html',
214-
'shortcut-terminal-paste': 'terminal.paste',
215-
'shortcut-terminal-select-all': 'terminal.select-all',
216-
'shortcut-terminal-reset': 'terminal.reset',
217-
'shortcut-terminal-reset-and-clear': 'terminal.reset-and-clear',
218-
'shortcut-win-new-tab': 'win.new-tab',
219-
'shortcut-win-new-tab-front': 'win.new-tab-front',
220-
'shortcut-win-new-tab-before-current': 'win.new-tab-before-current',
221-
'shortcut-win-new-tab-after-current': 'win.new-tab-after-current',
222-
'shortcut-page-close': 'page.close',
223-
'shortcut-prev-tab': 'win.prev-tab',
224-
'shortcut-next-tab': 'win.next-tab',
225-
'shortcut-move-tab-prev': 'win.move-tab-prev',
226-
'shortcut-move-tab-next': 'win.move-tab-next',
227-
'shortcut-set-custom-tab-title': 'page.use-custom-title(true)',
228-
'shortcut-reset-tab-title': 'page.use-custom-title(false)',
229-
'shortcut-find': 'terminal.find',
230-
'shortcut-find-next': 'terminal.find-next',
231-
'shortcut-find-prev': 'terminal.find-prev',
232-
};
182+
this.bind_shortcuts({
183+
'win.hide': 'shortcut-window-hide',
184+
'win.window-size-inc': 'shortcut-window-size-inc',
185+
'win.window-size-dec': 'shortcut-window-size-dec',
186+
'win.background-opacity-inc': 'shortcut-background-opacity-inc',
187+
'win.background-opacity-dec': 'shortcut-background-opacity-dec',
188+
'app.window-maximize': 'shortcut-toggle-maximize',
189+
'app.transparent-background': 'shortcut-toggle-transparent-background',
190+
'terminal.copy': 'shortcut-terminal-copy',
191+
'terminal.copy-html': 'shortcut-terminal-copy-html',
192+
'terminal.paste': 'shortcut-terminal-paste',
193+
'terminal.select-all': 'shortcut-terminal-select-all',
194+
'terminal.reset': 'shortcut-terminal-reset',
195+
'terminal.reset-and-clear': 'shortcut-terminal-reset-and-clear',
196+
'win.new-tab': 'shortcut-win-new-tab',
197+
'win.new-tab-front': 'shortcut-win-new-tab-front',
198+
'win.new-tab-before-current': 'shortcut-win-new-tab-before-current',
199+
'win.new-tab-after-current': 'shortcut-win-new-tab-after-current',
200+
'page.close': 'shortcut-page-close',
201+
'win.prev-tab': 'shortcut-prev-tab',
202+
'win.next-tab': 'shortcut-next-tab',
203+
'win.move-tab-prev': 'shortcut-move-tab-prev',
204+
'win.move-tab-next': 'shortcut-move-tab-next',
205+
'page.use-custom-title(true)': 'shortcut-set-custom-tab-title',
206+
'page.use-custom-title(false)': 'shortcut-reset-tab-title',
207+
'terminal.find': 'shortcut-find',
208+
'terminal.find-next': 'shortcut-find-next',
209+
'terminal.find-prev': 'shortcut-find-prev',
210+
});
233211

234212
for (let i = 0; i < 10; i += 1)
235-
shortcut_actions[`shortcut-switch-to-tab-${i + 1}`] = `win.switch-to-tab(${i})`;
213+
this.bind_shortcut(`win.switch-to-tab(${i})`, `shortcut-switch-to-tab-${i + 1}`);
236214

237-
const shortcuts_enabled = this.settings['shortcuts-enabled'];
238-
239-
const append_escape = rxjs.pipe(
240-
rxjs.combineLatestWith(this.settings['hide-window-on-esc']),
241-
rxjs.map(([shortcuts, append]) => append ? shortcuts.concat(['Escape']) : shortcuts)
242-
);
243-
244-
for (const [key, action] of Object.entries(shortcut_actions)) {
245-
this.rx.subscribe(
246-
rxutil.switch_on(shortcuts_enabled, {
247-
true: this.settings[key],
248-
false: rxjs.of([]),
249-
}).pipe(action === 'win.hide' ? append_escape : rxjs.identity),
250-
value => {
251-
this.set_accels_for_action(action, value);
252-
}
253-
);
254-
}
255-
256-
this.rx.connect(this, 'activate', this.activate.bind(this));
215+
this.connect('activate', this.activate.bind(this));
257216
}
258217

259218
activate() {
@@ -297,20 +256,14 @@ const Application = backport.GObject.registerClass(
297256

298257
preferences() {
299258
if (this.prefs_dialog === null) {
300-
this.prefs_dialog = new imports.ddterm.pref.dialog.PrefsDialog({
259+
this.prefs_dialog = new PrefsDialog({
301260
transient_for: this.window,
302-
settings: this.settings.gsettings,
261+
settings: this.settings,
303262
});
304263

305-
this.rx.subscribe(
306-
rxutil.signal(this.prefs_dialog, 'delete-event').pipe(rxjs.take(1)),
307-
() => {
308-
this.prefs_dialog = null;
309-
this.notify('preferences-visible');
310-
}
311-
);
312-
313-
this.notify('preferences-visible');
264+
this.prefs_dialog.connect('destroy', () => {
265+
this.prefs_dialog = null;
266+
});
314267
}
315268

316269
this.prefs_dialog.show();
@@ -321,10 +274,6 @@ const Application = backport.GObject.registerClass(
321274
this.prefs_dialog.close();
322275
}
323276

324-
get preferences_visible() {
325-
return this.prefs_dialog !== null;
326-
}
327-
328277
dump_heap(path = null) {
329278
if (!path) {
330279
path = GLib.build_filenamev([
@@ -345,6 +294,65 @@ const Application = backport.GObject.registerClass(
345294
System.dumpHeap(path);
346295
printerr(`Dumped heap to ${path}`);
347296
}
297+
298+
simple_action(name, activate, params = {}) {
299+
const action = new Gio.SimpleAction({
300+
name,
301+
...params,
302+
});
303+
action.connect('activate', activate);
304+
this.add_action(action);
305+
return action;
306+
}
307+
308+
update_theme() {
309+
const gtk_settings = Gtk.Settings.get_default();
310+
const theme = this.settings.get_string('theme-variant');
311+
312+
switch (theme) {
313+
case 'system':
314+
gtk_settings.reset_property('gtk-application-prefer-dark-theme');
315+
break;
316+
317+
case 'dark':
318+
gtk_settings.gtk_application_prefer_dark_theme = true;
319+
break;
320+
321+
case 'light':
322+
gtk_settings.gtk_application_prefer_dark_theme = false;
323+
break;
324+
325+
default:
326+
printerr(`Unknown theme-variant: ${theme}`);
327+
}
328+
}
329+
330+
bind_shortcut(action, settings_key) {
331+
const handler = this.update_shortcut.bind(this, action, settings_key);
332+
333+
this.settings.connect(`changed::${settings_key}`, handler);
334+
this.settings.connect('changed::shortcuts-enabled', handler);
335+
336+
if (action === 'win.hide')
337+
this.settings.connect('changed::hide-window-on-esc', handler);
338+
339+
handler();
340+
}
341+
342+
update_shortcut(action, settings_key) {
343+
const enable = this.settings.get_boolean('shortcuts-enabled');
344+
const keys = enable ? this.settings.get_strv(settings_key) : [];
345+
346+
if (action === 'win.hide' && this.settings.get_boolean('hide-window-on-esc'))
347+
keys.push('Escape');
348+
349+
this.set_accels_for_action(action, keys);
350+
}
351+
352+
bind_shortcuts(action_to_settings_key) {
353+
for (const [action, settings_key] of Object.entries(action_to_settings_key))
354+
this.bind_shortcut(action, settings_key);
355+
}
348356
}
349357
);
350358

ddterm/app/appwindow.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,21 @@ var AppWindow = backport.GObject.registerClass(
5858
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
5959
Gtk.Builder
6060
),
61-
'settings': GObject.ParamSpec.object(
62-
'settings',
61+
'gsettings': GObject.ParamSpec.object(
62+
'gsettings',
6363
'',
6464
'',
6565
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
66-
settings.Settings
66+
Gio.Settings
6767
),
6868
},
6969
},
7070
class AppWindow extends Gtk.ApplicationWindow {
7171
_init(params) {
7272
super._init(params);
7373

74+
this.settings = new settings.Settings({ gsettings: this.gsettings });
75+
7476
this.rx = rxutil.scope(this);
7577

7678
this.extension_dbus = extensiondbus.get();

0 commit comments

Comments
 (0)