diff --git a/data/io.elementary.terminal.gschema.xml b/data/io.elementary.terminal.gschema.xml index 370aac75e5..a983f51952 100644 --- a/data/io.elementary.terminal.gschema.xml +++ b/data/io.elementary.terminal.gschema.xml @@ -68,7 +68,7 @@ true - Whether open tabs should be remembered on closing (subject to privacy setting). + Whether open tabs should be remembered (subject to privacy setting). Defines whether the terminal should remember the last open tabs and restore them when the terminal is reopened. If the global privacy setting is on, the tabs diff --git a/src/Application.vala b/src/Application.vala index 311dc0a5b8..0f8ec0b900 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -14,6 +14,8 @@ public class Terminal.Application : Gtk.Application { public static GLib.Settings settings; public static GLib.Settings settings_sys; + public bool is_testing { get; set construct; } + private static Themes themes; public Application () { diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 34622dc090..1f8d51e48e 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -32,8 +32,6 @@ namespace Terminal { private HashTable restorable_terminals; private bool is_fullscreen = false; private bool on_drag = false; - private string[] saved_tabs; - private string[] saved_zooms; private const int NORMAL = 0; private const int MAXIMIZED = 1; @@ -250,6 +248,7 @@ namespace Terminal { if (focus_timeout == 0) { focus_timeout = Timeout.add (20, () => { focus_timeout = 0; + save_opened_terminals (true, true); return Source.REMOVE; }); } @@ -550,15 +549,6 @@ namespace Terminal { } private void restore_saved_state (bool restore_pos = true) { - if (Granite.Services.System.history_is_enabled () && - Application.settings.get_boolean ("remember-tabs")) { - - saved_tabs = Terminal.Application.saved_state.get_strv ("tabs"); - saved_zooms = Terminal.Application.saved_state.get_strv ("tab-zooms"); - } else { - saved_tabs = {}; - saved_zooms = {}; - } var rect = Gdk.Rectangle (); Terminal.Application.saved_state.get ("window-size", "(ii)", out rect.width, out rect.height); @@ -594,16 +584,18 @@ namespace Terminal { var terminal_widget = get_term_widget (tab); terminals.append (terminal_widget); terminal_widget.window = this; + save_opened_terminals (true, true); } private void on_tab_removed (Granite.Widgets.Tab tab) { var terminal_widget = get_term_widget (tab); if (!on_drag && notebook.n_tabs == 0) { - save_opened_terminals (); + save_opened_terminals (true, true); destroy (); } else { terminals.remove (terminal_widget); check_for_tabs_with_same_name (); + save_opened_terminals (true, true); } } @@ -639,8 +631,14 @@ namespace Terminal { } private void on_tab_reordered (Granite.Widgets.Tab tab, int new_pos) { + var terminal_widget = get_term_widget (tab); + current_terminal.grab_focus (); - save_opened_terminals (); + + terminals.remove (terminal_widget); + terminals.insert (terminal_widget, new_pos); + + save_opened_terminals (true, true); } private void on_tab_restored (string label, string restore_key, GLib.Icon? icon) { @@ -652,6 +650,8 @@ namespace Terminal { notebook.current = tab; term.grab_focus (); check_for_tabs_with_same_name (); + + save_opened_terminals (true, true); } private void on_tab_moved (Granite.Widgets.Tab tab, int x, int y) { @@ -831,14 +831,14 @@ namespace Terminal { if (Granite.Services.System.history_is_enabled () && Application.settings.get_boolean ("remember-tabs")) { - tabs = saved_tabs; + tabs = Terminal.Application.saved_state.get_strv ("tabs"); var n_tabs = tabs.length; if (n_tabs == 0) { tabs += Environment.get_home_dir (); zooms += default_zoom; } else { - foreach (unowned string zoom_s in saved_zooms) { + foreach (unowned string zoom_s in Terminal.Application.saved_state.get_strv ("tab-zooms")) { var zoom = double.parse (zoom_s); // Locale independent if (zooms.length < n_tabs) { @@ -875,8 +875,6 @@ namespace Terminal { } } - Terminal.Application.saved_state.set_strv ("tabs", {}); - focus = focus.clamp (0, tabs.length - 1); /* This must not be in an Idle loop to avoid duplicate tabs being opened (issue #245) */ @@ -939,7 +937,7 @@ namespace Terminal { }); terminal_widget.window_title_changed.connect (check_for_tabs_with_same_name); - terminal_widget.cwd_changed.connect (check_for_tabs_with_same_name); + terminal_widget.cwd_changed.connect (cwd_changed); terminal_widget.set_font (term_font); @@ -978,6 +976,8 @@ namespace Terminal { terminal_widget.run_program (program, location); } + save_opened_terminals (true, true); + return terminal_widget; } @@ -1044,7 +1044,7 @@ namespace Terminal { } protected override bool delete_event (Gdk.EventAny event) { - save_opened_terminals (); + save_opened_terminals (true, true); var tabs_to_terminate = new GLib.List (); foreach (var terminal_widget in terminals) { @@ -1205,14 +1205,17 @@ namespace Terminal { private void action_zoom_in_font () { current_terminal.increment_size (); + save_opened_terminals (false, true); } private void action_zoom_out_font () { current_terminal.decrement_size (); + save_opened_terminals (false, true); } private void action_zoom_default_font () { current_terminal.set_default_font_size (); + save_opened_terminals (false, true); } private void action_next_tab () { @@ -1344,11 +1347,24 @@ namespace Terminal { return; } - private void save_opened_terminals () { - string[] opened_tabs = {}; + private void cwd_changed () { + check_for_tabs_with_same_name (); + save_opened_terminals (true, false); + } + + private void save_opened_terminals (bool save_tabs, bool save_zooms) { string[] zooms = {}; + string[] opened_tabs = {}; + int focused_tab = 0; - Application.saved_state.set_double ("zoom", current_terminal.font_scale); + // Continuous saving of opened terminals interferes with current unit tests + if (app.is_testing) { + return; + } + + if (save_zooms && current_terminal != null) { + Application.saved_state.set_double ("zoom", current_terminal.font_scale); + } if (Granite.Services.System.history_is_enabled () && Application.settings.get_boolean ("remember-tabs")) { @@ -1357,27 +1373,39 @@ namespace Terminal { if (term != null) { var location = term.get_shell_location (); if (location != null && location != "") { - opened_tabs += location; - zooms += term.font_scale.to_string (); // Locale independent + if (save_tabs) { + opened_tabs += location; + } + if (save_zooms) { + zooms += term.font_scale.to_string (); // Locale independent + } } } }); + + if (save_tabs && notebook.current != null) { + focused_tab = notebook.get_tab_position (notebook.current); + } } - Terminal.Application.saved_state.set_strv ( - "tabs", - opened_tabs - ); + if (save_tabs) { + Terminal.Application.saved_state.set_strv ( + "tabs", + opened_tabs + ); - Terminal.Application.saved_state.set_strv ( - "tab-zooms", - zooms - ); + Terminal.Application.saved_state.set_int ( + "focused-tab", + focused_tab + ); + } - Terminal.Application.saved_state.set_int ( - "focused-tab", - notebook.current != null ? notebook.get_tab_position (notebook.current) : 0 - ); + if (save_zooms) { + Terminal.Application.saved_state.set_strv ( + "tab-zooms", + zooms + ); + } } /** Return enough of @path to distinguish it from @conflict_path **/ diff --git a/src/Widgets/TerminalWidget.vala b/src/Widgets/TerminalWidget.vala index 046c89984f..61adc24331 100644 --- a/src/Widgets/TerminalWidget.vala +++ b/src/Widgets/TerminalWidget.vala @@ -201,11 +201,11 @@ namespace Terminal { if ((event.state & Gdk.ModifierType.CONTROL_MASK) > 0) { switch (event.direction) { case Gdk.ScrollDirection.UP: - increment_size (); + window.get_simple_action (MainWindow.ACTION_ZOOM_IN_FONT).activate (null); return Gdk.EVENT_STOP; case Gdk.ScrollDirection.DOWN: - decrement_size (); + window.get_simple_action (MainWindow.ACTION_ZOOM_OUT_FONT).activate (null); return Gdk.EVENT_STOP; case Gdk.ScrollDirection.SMOOTH: @@ -215,10 +215,10 @@ namespace Terminal { if (total_delta_y >= 0.5) { total_delta_y = 0; - decrement_size (); + window.get_simple_action (MainWindow.ACTION_ZOOM_OUT_FONT).activate (null); } else if (total_delta_y <= -0.5) { total_delta_y = 0; - increment_size (); + window.get_simple_action (MainWindow.ACTION_ZOOM_IN_FONT).activate (null); } return Gdk.EVENT_STOP; diff --git a/src/tests/Application.vala b/src/tests/Application.vala index df88c9dd62..150f66b2d0 100644 --- a/src/tests/Application.vala +++ b/src/tests/Application.vala @@ -12,7 +12,8 @@ namespace Terminal.Test.Application { private void setup () { application = new Terminal.Application () { - application_id = "io.elementary.terminal.tests.application" + application_id = "io.elementary.terminal.tests.application", + is_testing = true }; application.shutdown.connect (() => application.get_windows ().foreach ((win) => win.destroy ()));