Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continous saving of current tabs and tab-zooms #742

Merged
merged 17 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/io.elementary.terminal.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</key>
<key name="remember-tabs" type="b">
<default>true</default>
<summary>Whether open tabs should be remembered on closing (subject to privacy setting).</summary>
<summary>Whether open tabs should be remembered (subject to privacy setting).</summary>
<description>
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
Expand Down
2 changes: 2 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
100 changes: 64 additions & 36 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ namespace Terminal {
private HashTable<string, TerminalWidget> 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;
Expand Down Expand Up @@ -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;
});
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) */
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -978,6 +976,8 @@ namespace Terminal {
terminal_widget.run_program (program, location);
}

save_opened_terminals (true, true);

return terminal_widget;
}

Expand Down Expand Up @@ -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 <TerminalWidget> ();

foreach (var terminal_widget in terminals) {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 ();
jeremypw marked this conversation as resolved.
Show resolved Hide resolved
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")) {
Expand All @@ -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 **/
Expand Down
8 changes: 4 additions & 4 deletions src/Widgets/TerminalWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/tests/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()));
Expand Down