@@ -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
0 commit comments