Skip to content

Commit 4e1aadb

Browse files
authored
Jeremypw/fix new root tab (#1019)
* Lose recreate_tabs property of MainWindow; refactor App.commandline * Restore behaviour for new-tab option (ignore if commands) * Cleanup comments and whitespace * Lose unused variable * Lose nesting and return early in open_saved_tabs
1 parent c336daf commit 4e1aadb

3 files changed

Lines changed: 62 additions & 55 deletions

File tree

‎src/Application.vala‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public class Terminal.Application : Gtk.Application {
338338
dir = ((MainWindow)active_window).current_terminal.current_working_directory;
339339
}
340340

341-
var new_window = new MainWindow (this, active_window == null);
341+
var new_window = new MainWindow (this);
342342
new_window.present ();
343343
new_window.set_size_request (
344344
active_window.width_request,
@@ -366,7 +366,7 @@ public class Terminal.Application : Gtk.Application {
366366

367367
// Always restore tabs if creating first window, but no extra tab at this stage
368368
if (is_first_window || options.lookup ("new-window", "b", out new_window) && new_window) {
369-
window = new MainWindow (this, is_first_window);
369+
window = new MainWindow (this);
370370
}
371371

372372
// If a specified working directory is not requested, use the current working directory from the commandline
@@ -376,21 +376,36 @@ public class Terminal.Application : Gtk.Application {
376376
bool new_tab, minimized;
377377

378378
options.lookup ("new-tab", "b", out new_tab);
379+
Adw.TabPage? added_page = null;
379380

380-
// If "execute" option or "commandline" option used ignore any "new-tab option
381-
// because these add new tab(s) already
382381
if (options.lookup ("execute", "^a&ay", out commands)) {
383382
for (var i = 0; commands[i] != null; i++) {
384383
if (commands[i] != "\0") {
385-
window.add_tab_with_working_directory (working_directory, commands[i], new_tab);
384+
added_page = window.add_tab_with_working_directory (working_directory, commands[i], new_tab);
386385
}
387386
}
388387
} else if (options.lookup ("commandline", "^&ay", out command) && command != "\0") {
389-
window.add_tab_with_working_directory (working_directory, command, new_tab);
390-
} else if (new_tab || window.notebook.n_pages == 0) {
391-
window.add_tab_with_working_directory (working_directory, "", new_tab);
388+
added_page = window.add_tab_with_working_directory (working_directory, command, new_tab);
389+
}
390+
391+
// Only try to restore saved tabs if first window and a command was not executed
392+
// If "execute" option or "commandline" option used also ignore any "new-tab" option
393+
// because these add new tab(s) already
394+
if (window.notebook.n_pages == 0) {
395+
if (!new_tab && is_first_window) {
396+
window.open_saved_tabs ();
397+
}
398+
399+
// Ensure at least one tab
400+
if (new_tab || window.notebook.n_pages == 0) {
401+
added_page = window.add_tab_with_working_directory (working_directory, "", new_tab);
402+
}
392403
}
393404

405+
if (added_page != null) {
406+
window.notebook.selected_page = added_page;
407+
} // Else one of the restored tabs will be selected
408+
394409
if (options.lookup ("minimized", "b", out minimized) && minimized) {
395410
window.minimize ();
396411
} else {

‎src/MainWindow.vala‎

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace Terminal {
1414
private Dialogs.ColorPreferences? color_preferences_dialog;
1515
private uint focus_timeout = 0;
1616

17-
public bool recreate_tabs { get; construct; }
1817
public Terminal.Application app { get; construct; }
1918
public SimpleActionGroup actions { get; construct; }
2019
public TerminalView notebook { get; private set; }
@@ -66,10 +65,9 @@ namespace Terminal {
6665
private Adw.TabPage? tab_to_close = null;
6766
private TerminalWidget? term_to_close = null;
6867

69-
public MainWindow (Terminal.Application app, bool recreate_tabs = true) {
68+
public MainWindow (Terminal.Application app) {
7069
Object (
71-
app: app,
72-
recreate_tabs: recreate_tabs
70+
app: app
7371
);
7472
}
7573

@@ -271,50 +269,41 @@ namespace Terminal {
271269

272270
set_size_request (Application.MINIMUM_WIDTH, Application.MINIMUM_HEIGHT);
273271

274-
if (recreate_tabs) {
275-
open_tabs ();
276-
}
277272

278273
close_request.connect (on_delete_event);
279274
}
280275

281-
public void add_tab_with_working_directory (
276+
// Add requested tab or find duplicate only. Do not overload by adding default tab or
277+
// selecting a tab. This must be done by the caller if required.
278+
public Adw.TabPage? add_tab_with_working_directory (
282279
string directory = "",
283280
string command = "",
284281
bool create_new_tab = false
285282
) {
286-
287283
/* This requires all restored tabs to be initialized first so that
288284
* the shell location is available.
289285
* Do not add a new tab if location is already open in existing tab */
290-
string location = "";
291286
if (directory.length == 0) {
292-
if (notebook.n_pages == 0 || command != null || create_new_tab) { //Ensure at least one tab
293-
notebook.add_new_tab ("", command);
294-
}
295-
296-
return;
297-
} else {
298-
location = directory;
287+
return null;
299288
}
300289

290+
string location = directory;
291+
301292
/* We can match existing tabs only if there is no command and create_new_tab == false */
302293
if (command.length == 0 && !create_new_tab) {
303294
var file = File.new_for_commandline_arg (location);
304295
for (int pos = 0; pos < notebook.n_pages; pos++) {
305296
var tab = notebook.tab_view.get_nth_page (pos);
306297
var terminal_widget = get_term_widget (tab);
307298
var tab_path = terminal_widget.get_shell_location ();
308-
/* Detect equialent paths */
299+
/* Detect equivalent paths */
309300
if (file.equal (File.new_for_path (tab_path))) {
310-
/* Just focus the duplicate tab instead */
311-
notebook.selected_page = tab;
312-
return; /* Duplicate found, abandon adding tab */
301+
return tab; /* Duplicate found, return this in case it needs selecting etc */
313302
}
314303
}
315304
}
316305

317-
notebook.add_new_tab (location, command);
306+
return notebook.add_new_tab (location, command);
318307
}
319308

320309
private bool key_pressed (uint keyval, uint keycode, Gdk.ModifierType modifiers) {
@@ -365,39 +354,41 @@ namespace Terminal {
365354
return present_new_empty_window ().notebook.tab_view;
366355
}
367356

368-
private void open_tabs () {
357+
// Only adds restorable tabs - does not add default tab - that is left to the caller as required
358+
public void open_saved_tabs () {
369359
string[] tabs = {};
370360
double[] zooms = {};
371361
int focus = 0;
372362
var default_zoom = Application.saved_state.get_double ("zoom"); // Range set in settings 0.25 - 4.0
373363

374-
if (Granite.Services.System.history_is_enabled () && Application.settings.get_boolean ("remember-tabs")) {
375-
tabs = Terminal.Application.saved_state.get_strv ("tabs");
376-
var n_tabs = tabs.length;
364+
if (!Granite.Services.System.history_is_enabled () ||
365+
!Application.settings.get_boolean ("remember-tabs")) {
377366

378-
if (n_tabs == 0) {
379-
tabs += Environment.get_home_dir ();
380-
zooms += default_zoom;
381-
} else {
382-
foreach (unowned string zoom_s in Terminal.Application.saved_state.get_strv ("tab-zooms")) {
383-
if (zooms.length < n_tabs) {
384-
zooms += double.parse (zoom_s); // Locale independent
385-
} else {
386-
break;
387-
}
388-
}
367+
return;
368+
}
369+
370+
tabs = Terminal.Application.saved_state.get_strv ("tabs");
371+
var n_tabs = tabs.length;
389372

390-
while (zooms.length < n_tabs) {
391-
zooms += default_zoom;
373+
if (n_tabs == 0) {
374+
tabs += Environment.get_home_dir ();
375+
zooms += default_zoom;
376+
} else {
377+
foreach (unowned string zoom_s in Terminal.Application.saved_state.get_strv ("tab-zooms")) {
378+
if (zooms.length < n_tabs) {
379+
zooms += double.parse (zoom_s); // Locale independent
380+
} else {
381+
break;
392382
}
393383
}
394384

395-
focus = Terminal.Application.saved_state.get_int ("focused-tab");
396-
} else {
397-
tabs += Environment.get_current_dir ();
398-
zooms += default_zoom;
385+
while (zooms.length < n_tabs) {
386+
zooms += default_zoom;
387+
}
399388
}
400389

390+
focus = Terminal.Application.saved_state.get_int ("focused-tab");
391+
401392
assert (zooms.length == tabs.length);
402393

403394
int null_dirs = 0;
@@ -423,7 +414,8 @@ namespace Terminal {
423414
if (loc == "") {
424415
focus--;
425416
} else {
426-
var term = notebook.add_new_tab (loc, "");
417+
var page = notebook.add_new_tab (loc, "");
418+
var term = notebook.get_term_widget (page);
427419
term.font_scale = zooms[index].clamp (
428420
TerminalWidget.MIN_SCALE,
429421
TerminalWidget.MAX_SCALE
@@ -799,7 +791,7 @@ namespace Terminal {
799791
}
800792

801793
private MainWindow present_new_empty_window () {
802-
var new_window = new MainWindow (app, false);
794+
var new_window = new MainWindow (app);
803795
new_window.set_size_request (
804796
app.active_window.width_request,
805797
app.active_window.height_request

‎src/Widgets/TerminalView.vala‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public class Terminal.TerminalView : Granite.Bin {
148148
);
149149
}
150150

151-
public TerminalWidget add_new_tab (string? location, string program = "", int pos = -1) {
151+
public Adw.TabPage add_new_tab (string? location, string program = "", int pos = -1) {
152152
if (pos == -1) {
153153
pos = n_pages;
154154
}
@@ -210,7 +210,7 @@ public class Terminal.TerminalView : Granite.Bin {
210210

211211
main_window.save_opened_terminals (true, true);
212212

213-
return terminal_widget;
213+
return tab;
214214
}
215215

216216
public void close_tab () {

0 commit comments

Comments
 (0)