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

Save current tabs on append/remove/reorder #1

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
d8ed974
Save current tabs on append/remove/reorder
ldrahnik Oct 29, 2023
6e3a2df
Save current tab-zooms on zoom in/out
ldrahnik Oct 29, 2023
20d9cee
Save emptytabs, tab-zooms when is disabled history or remember-tabs i…
ldrahnik Oct 29, 2023
c1a44ec
Save current tabs, tab-zooms on insert/duplicate
ldrahnik Oct 29, 2023
d4787aa
Save current tabs, tab-zooms on cwd change
ldrahnik Oct 30, 2023
8b126dd
Save current tabs, tab-zooms on zoom change by control+scroll
ldrahnik Oct 30, 2023
24ce4cc
Split saving tabs and tab-zooms when possible
ldrahnik Oct 30, 2023
21c27f5
Fixed reorder action needs save tab-zooms aswell
ldrahnik Oct 30, 2023
0a0d06c
Refactored saving zoom on change via control+scroll to Glib.SimpleAction
ldrahnik Oct 30, 2023
0cbd59d
Save current tab-zooms on smooth zoom change
ldrahnik Oct 30, 2023
2574910
Refactored usage of split saving tabs and tab-zooms func to avoid dup…
ldrahnik Oct 30, 2023
2cebd34
Refactored back to saving not-terminal dependend zoom always
ldrahnik Oct 30, 2023
db5843a
Save current tabs, tab-zooms when is focus in (necessary when was clo…
ldrahnik Oct 30, 2023
4a6bc7a
Refactored usage of split function to one function with unnecessary u…
ldrahnik Oct 31, 2023
2e059a2
Added missing check whether is current_terminal not null
ldrahnik Nov 1, 2023
77215d1
Inhibit saving state when testing
Nov 1, 2023
e580218
Merge pull request #2 from elementary/jpw/continuous-saving
ldrahnik Nov 1, 2023
21f9633
Before each test change to appropriate remember-tabs gsettings
ldrahnik Nov 4, 2023
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
95 changes: 59 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,19 @@ 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);
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 +1368,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
51 changes: 46 additions & 5 deletions src/tests/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,24 @@ namespace Terminal.Test.Application {
delegate void CommandLineCallback (ApplicationCommandLine command_line);
delegate void ActivateCallback ();

private void setup () {
private GLib.Settings getSettingsWithNoContinuousSaving() {
GLib.Settings settings = new GLib.Settings ("io.elementary.terminal.settings");
settings.set_boolean ("remember-tabs", false);
return settings;
}

private GLib.Settings getSettingsWithContinousSaving() {
GLib.Settings settings = new GLib.Settings ("io.elementary.terminal.settings");
settings.set_boolean ("remember-tabs", true);
return settings;
}

private void setup (GLib.Settings ?settings = null) {

if (settings == null) {
Application.getSettingsWithNoContinuousSaving ();
}

application = new Terminal.Application () {
application_id = "io.elementary.terminal.tests.application"
};
Expand Down Expand Up @@ -47,9 +64,10 @@ namespace Terminal.Test.Application {
}
}

private void option (string options, string platform_data, CommandLineCallback callback) {
private void option (string options, string platform_data, CommandLineCallback callback, GLib.Settings ?settings = null) {
ulong oneshot = 0;
setup ();

setup (settings);

oneshot = application.command_line.connect ((nil) => {
application.disconnect (oneshot);
Expand All @@ -73,9 +91,10 @@ namespace Terminal.Test.Application {
}
}

private void action (string name, Variant? @value, ActivateCallback callback) {
private void action (string name, Variant? @value, ActivateCallback callback, GLib.Settings ?settings = null) {
ulong oneshot = 0;
setup ();

setup (settings);

oneshot = application.command_line.connect ((nill) => {
application.disconnect (oneshot);
Expand Down Expand Up @@ -154,6 +173,28 @@ namespace Terminal.Test.Application {
var n_tabs = (int) window.terminals.length ();
assert_cmpint (n_tabs, CompareOperator.EQ, 1);
});

// continous saving below
option ("{'new-tab':<true>}", "@a{sv} {}", () => {
unowned var window = (MainWindow) application.active_window;
assert_nonnull (window);
var n_tabs = (int) window.terminals.length ();
assert_cmpint (n_tabs, CompareOperator.EQ, 2);
}, getSettingsWithContinousSaving());

option ("{'new-tab':<false>}", "@a{sv} {}", () => {
unowned var window = (MainWindow) application.active_window;
assert_nonnull (window);
var n_tabs = (int) window.terminals.length ();
assert_cmpint (n_tabs, CompareOperator.EQ, 2);
}, getSettingsWithContinousSaving());

option ("{'new-tab':<true>}", "@a{sv} {}", () => {
unowned var window = (MainWindow) application.active_window;
assert_nonnull (window);
var n_tabs = (int) window.terminals.length ();
assert_cmpint (n_tabs, CompareOperator.EQ, 3);
}, getSettingsWithContinousSaving());
});

GLib.Test.add_func ("/application/command-line/new-window", () => {
Expand Down