From 7dede4017c400c9f165df9ebacfd17e4cbf8b2a7 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 23 May 2021 19:22:43 +0100 Subject: [PATCH 01/11] Refactor name check and title update so works with custom prompt * Run update on new tab, switch tab, close tab and `Return` entered * Do not use terminal.window_title --- src/MainWindow.vala | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 37adf6f66c..c2faeb1e3d 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -585,6 +585,8 @@ namespace Terminal { current_terminal.remember_command_end_position (); get_simple_action (ACTION_COPY_LAST_OUTPUT).set_enabled (false); } + + schedule_name_check (); // CWD may have changed break; case Gdk.Key.@1: //alt+[1-8] @@ -939,7 +941,6 @@ namespace Terminal { Granite.Widgets.Tab new_tab) { current_terminal = get_term_widget (new_tab); - title = current_terminal.tab_label ?? TerminalWidget.DEFAULT_LABEL; /* The font-scales of all terminals are currently the synchronized through saved-state binding */ new_tab.icon = null; Idle.add (() => { @@ -956,6 +957,8 @@ namespace Terminal { return false; }); + + update_title (); } private void open_tabs () { @@ -1077,17 +1080,6 @@ namespace Terminal { } }); - /* This signal is not emitted when the .bashrc is missing or does not force terminal to - * set its title. So we cannot detect path changes in the shell. Tab name remains "Terminal". - */ - t.window_title_changed.connect (() => { - if (t == current_terminal) { - title = t.window_title; - } - - schedule_name_check (); - }); - t.set_font (term_font); if (current_terminal != null) { @@ -1125,6 +1117,7 @@ namespace Terminal { t.run_program (program, location); } + schedule_name_check (); return t; } @@ -1301,6 +1294,7 @@ namespace Terminal { private void action_close_tab () { current_terminal.tab.close (); current_terminal.grab_focus (); + schedule_name_check (); } private void action_new_window () { @@ -1447,7 +1441,7 @@ namespace Terminal { foreach (TerminalWidget terminal in terms) { string term_path = terminal.get_shell_location (); - string term_label = terminal.window_title; + string term_label = Path.get_basename (term_path); if (term_label == "") { /* No point in continuing - tabs not finished updating */ return false; /* Try again later */ @@ -1462,24 +1456,27 @@ namespace Terminal { foreach (TerminalWidget terminal2 in terms2) { string term2_path = terminal2.get_shell_location (); - string term2_name = terminal2.window_title; + string term2_name = Path.get_basename (term2_path); if (terminal2 != terminal && term2_name == term_label) { if (term2_path != term_path) { terminal2.tab_label = disambiguate_label (term2_path, term_path); terminal.tab_label = disambiguate_label (term_path, term2_path); - - if (terminal == current_terminal) { - title = terminal.tab_label; - } } } } + + terminal.tab.tooltip = term_path; } + update_title (); return true; } + private void update_title () { + title = current_terminal.get_shell_location (); + } + private void save_opened_terminals () { string[] opened_tabs = {}; string[] zooms = {}; From ff8d6f860938b040790751d15c590653b90f9ea1 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 24 May 2021 08:48:06 +0100 Subject: [PATCH 02/11] Update tabs on contents changed * In case a script changes the CWD --- src/MainWindow.vala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index c2faeb1e3d..4a8de41c57 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -586,7 +586,6 @@ namespace Terminal { get_simple_action (ACTION_COPY_LAST_OUTPUT).set_enabled (false); } - schedule_name_check (); // CWD may have changed break; case Gdk.Key.@1: //alt+[1-8] @@ -1080,6 +1079,10 @@ namespace Terminal { } }); + t.contents_changed.connect (() => { + schedule_name_check (); + }); + t.set_font (term_font); if (current_terminal != null) { From df642e9e5129c9497025cf5174876ec63d7120ac Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 24 May 2021 11:25:01 +0100 Subject: [PATCH 03/11] Ellipsize middle --- src/MainWindow.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 4a8de41c57..524d21c30a 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -1129,7 +1129,7 @@ namespace Terminal { sw.add (term); var tab = new Granite.Widgets.Tab (label, icon, sw); term.tab = tab; - tab.ellipsize_mode = Pango.EllipsizeMode.START; + tab.ellipsize_mode = Pango.EllipsizeMode.MIDDLE; var reload_menu_item = new Gtk.MenuItem.with_label (_("Reload")); tab.menu.append (reload_menu_item); From eab39141312d7f3f81905adaafcd8d5e0318f4c1 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 24 May 2021 17:35:13 +0100 Subject: [PATCH 04/11] Improve efficiency * Re-use timeout if running instead of recreating on every call * Avoid possibility of timeout running continuously * Do not save terminals when updating labels * Call update labels max 2 times per sec --- src/MainWindow.vala | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 524d21c30a..e739d405e7 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -1419,25 +1419,28 @@ namespace Terminal { } private uint name_check_timeout_id = 0; + private bool next_time = false; private void schedule_name_check () { if (name_check_timeout_id > 0) { - Source.remove (name_check_timeout_id); + next_time = false; // Keep existing timeout running + return; } - name_check_timeout_id = Timeout.add (50, () => { - save_opened_terminals (); - - if (!check_for_tabs_with_same_name ()) { - return true; - } else { - name_check_timeout_id = 0; - return false; + name_check_timeout_id = Timeout.add (250, () => { + if (!next_time) { + next_time = true; + return Source.CONTINUE; } + + check_for_tabs_with_same_name (); + name_check_timeout_id = 0; + next_time = false; + return Source.REMOVE; }); } /** Compare every tab label with every other and resolve ambiguities **/ - private bool check_for_tabs_with_same_name () { + private void check_for_tabs_with_same_name () { /* Take list copies so foreach clauses can be nested safely*/ var terms = terminals.copy (); var terms2 = terminals.copy (); @@ -1446,12 +1449,10 @@ namespace Terminal { string term_path = terminal.get_shell_location (); string term_label = Path.get_basename (term_path); - if (term_label == "") { /* No point in continuing - tabs not finished updating */ - return false; /* Try again later */ - } + if (term_label == "" || + terminal.tab_label == TerminalWidget.DEFAULT_LABEL) { - if (terminal.tab_label == TerminalWidget.DEFAULT_LABEL) { /* Absent or incorrect .bashrc */ - return true; + continue; } /* Reset tab_name to basename so long name only used when required */ @@ -1473,7 +1474,7 @@ namespace Terminal { } update_title (); - return true; + return; } private void update_title () { From ed671552d8df970a0637a2e09e16d50dcc41e314 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 24 May 2021 18:35:31 +0100 Subject: [PATCH 05/11] Terminal checks whether cwd changed before notifying MainWindow * No need to throttle checking for same name --- src/MainWindow.vala | 43 +++++---------------------------- src/Widgets/TerminalWidget.vala | 17 +++++++++++++ 2 files changed, 23 insertions(+), 37 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index e739d405e7..c14d9a7580 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -739,7 +739,6 @@ namespace Terminal { var t = get_term_widget (tab); terminals.append (t); t.window = this; - schedule_name_check (); } private void on_tab_removed (Granite.Widgets.Tab tab) { @@ -748,8 +747,6 @@ namespace Terminal { if (notebook.n_tabs == 0) { destroy (); - } else { - schedule_name_check (); } } @@ -793,7 +790,6 @@ namespace Terminal { notebook.insert_tab (tab, -1); notebook.current = tab; term.grab_focus (); - schedule_name_check (); } private void on_tab_moved (Granite.Widgets.Tab tab, int x, int y) { @@ -957,7 +953,7 @@ namespace Terminal { return false; }); - update_title (); + check_for_tabs_with_same_name (); } private void open_tabs () { @@ -1074,13 +1070,11 @@ namespace Terminal { t.tab.close (); return; } - - schedule_name_check (); } }); - t.contents_changed.connect (() => { - schedule_name_check (); + t.cwd_changed.connect (() => { + check_for_tabs_with_same_name (); }); t.set_font (term_font); @@ -1120,7 +1114,6 @@ namespace Terminal { t.run_program (program, location); } - schedule_name_check (); return t; } @@ -1297,7 +1290,8 @@ namespace Terminal { private void action_close_tab () { current_terminal.tab.close (); current_terminal.grab_focus (); - schedule_name_check (); + check_for_tabs_with_same_name (); + // Closing a tab will switch to another, which will trigger check for same names } private void action_new_window () { @@ -1418,27 +1412,6 @@ namespace Terminal { return (TerminalWidget)((Gtk.Bin)tab.page).get_child (); } - private uint name_check_timeout_id = 0; - private bool next_time = false; - private void schedule_name_check () { - if (name_check_timeout_id > 0) { - next_time = false; // Keep existing timeout running - return; - } - - name_check_timeout_id = Timeout.add (250, () => { - if (!next_time) { - next_time = true; - return Source.CONTINUE; - } - - check_for_tabs_with_same_name (); - name_check_timeout_id = 0; - next_time = false; - return Source.REMOVE; - }); - } - /** Compare every tab label with every other and resolve ambiguities **/ private void check_for_tabs_with_same_name () { /* Take list copies so foreach clauses can be nested safely*/ @@ -1473,12 +1446,8 @@ namespace Terminal { terminal.tab.tooltip = term_path; } - update_title (); - return; - } - - private void update_title () { title = current_terminal.get_shell_location (); + return; } private void save_opened_terminals () { diff --git a/src/Widgets/TerminalWidget.vala b/src/Widgets/TerminalWidget.vala index e372457ecf..488088d9ac 100644 --- a/src/Widgets/TerminalWidget.vala +++ b/src/Widgets/TerminalWidget.vala @@ -28,6 +28,7 @@ namespace Terminal { internal const string DEFAULT_LABEL = _("Terminal"); public Terminal.Application app; public string terminal_id; + public string current_working_directory { get; private set; default = "";} static int terminal_id_counter = 0; private bool init_complete; public bool resized {get; set;} @@ -123,6 +124,8 @@ namespace Terminal { private double total_delta_y = 0.0; + public signal void cwd_changed (); + public TerminalWidget (MainWindow parent_window) { pointer_autohide = true; @@ -223,6 +226,8 @@ namespace Terminal { resized = true; }); + contents_changed.connect (check_cwd_changed); + child_exited.connect (on_child_exited); /* target entries specify what kind of data the terminal widget accepts */ @@ -331,6 +336,7 @@ namespace Terminal { void on_child_exited () { child_has_exited = true; last_key_was_return = true; + check_cwd_changed (); } public void kill_fg () { @@ -377,6 +383,8 @@ namespace Terminal { } catch (Error e) { warning (e.message); } + + check_cwd_changed (); } public void run_program (string program_string, string? working_directory) { @@ -608,6 +616,15 @@ namespace Terminal { Posix.kill (child_pid, Posix.Signal.TERM); reset (true, true); active_shell (old_loc); + check_cwd_changed (); + } + + private void check_cwd_changed () { + var cwd = get_shell_location (); + if (cwd != current_working_directory) { + current_working_directory = cwd; + cwd_changed (); + } } } } From 446f71f690db14931ba35c01c47434f916300ed9 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 24 May 2021 18:39:26 +0100 Subject: [PATCH 06/11] Remove unnecessary check --- src/MainWindow.vala | 1 - 1 file changed, 1 deletion(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index c14d9a7580..c73f4efbf4 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -1290,7 +1290,6 @@ namespace Terminal { private void action_close_tab () { current_terminal.tab.close (); current_terminal.grab_focus (); - check_for_tabs_with_same_name (); // Closing a tab will switch to another, which will trigger check for same names } From c851552988357b1c3990d2a53fcd9a363321174e Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 24 May 2021 18:42:29 +0100 Subject: [PATCH 07/11] Use current_working_directory instead of get_shell_location () --- src/MainWindow.vala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index c73f4efbf4..d79bcf440e 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -1418,7 +1418,7 @@ namespace Terminal { var terms2 = terminals.copy (); foreach (TerminalWidget terminal in terms) { - string term_path = terminal.get_shell_location (); + string term_path = terminal.current_working_directory; string term_label = Path.get_basename (term_path); if (term_label == "" || @@ -1431,7 +1431,7 @@ namespace Terminal { terminal.tab_label = term_label; foreach (TerminalWidget terminal2 in terms2) { - string term2_path = terminal2.get_shell_location (); + string term2_path = terminal2.current_working_directory; string term2_name = Path.get_basename (term2_path); if (terminal2 != terminal && term2_name == term_label) { @@ -1445,7 +1445,7 @@ namespace Terminal { terminal.tab.tooltip = term_path; } - title = current_terminal.get_shell_location (); + title = current_terminal.current_working_directory; return; } From 3a9ebab9b166cf6e251f8bbbc7d6a403fd25a72d Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 24 May 2021 18:50:20 +0100 Subject: [PATCH 08/11] Update labels after drag out of window --- src/MainWindow.vala | 1 + 1 file changed, 1 insertion(+) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index d79bcf440e..02e06d2eaa 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -808,6 +808,7 @@ namespace Terminal { notebook.remove_tab (tab); new_notebook.insert_tab (tab, -1); new_window.current_terminal = t; + check_for_tabs_with_same_name (); // Update remaining tabs return false; }); } From eaa406abe1c246e29811a25522b9d6e975a709b4 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 24 May 2021 18:58:19 +0100 Subject: [PATCH 09/11] src/MainWindow.vala Simplify signal connection Co-authored-by: Felix Andreas --- src/MainWindow.vala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 02e06d2eaa..8f81d33247 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -1074,9 +1074,7 @@ namespace Terminal { } }); - t.cwd_changed.connect (() => { - check_for_tabs_with_same_name (); - }); + t.cwd_changed.connect (check_for_tabs_with_same_name); t.set_font (term_font); From 56d62630677f7b6e42d0d433d792162225e36eed Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 25 May 2021 12:27:24 +0100 Subject: [PATCH 10/11] Address review issues; improve efficiency --- src/MainWindow.vala | 10 ++++++++-- src/Widgets/TerminalWidget.vala | 2 -- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 8f81d33247..9477ea5adc 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -752,6 +752,7 @@ namespace Terminal { private bool on_close_tab_requested (Granite.Widgets.Tab tab) { var t = get_term_widget (tab); + bool closing_current_terminal = t == current_terminal; if (t.has_foreground_process ()) { var d = new ForegroundProcessDialog (this); @@ -774,6 +775,10 @@ namespace Terminal { } current_terminal.grab_focus (); + if (!closing_current_terminal) { // Otherwise current_terminal will change triggering a name check + check_for_tabs_with_same_name (); + } + return true; } @@ -1437,11 +1442,12 @@ namespace Terminal { if (term2_path != term_path) { terminal2.tab_label = disambiguate_label (term2_path, term_path); terminal.tab_label = disambiguate_label (term_path, term2_path); + // Overwrite tooltip_text set by changing tab_label + terminal2.tooltip_text = term2_path; + terminal.tooltip_text = term_path; } } } - - terminal.tab.tooltip = term_path; } title = current_terminal.current_working_directory; diff --git a/src/Widgets/TerminalWidget.vala b/src/Widgets/TerminalWidget.vala index 488088d9ac..03da3067fa 100644 --- a/src/Widgets/TerminalWidget.vala +++ b/src/Widgets/TerminalWidget.vala @@ -336,7 +336,6 @@ namespace Terminal { void on_child_exited () { child_has_exited = true; last_key_was_return = true; - check_cwd_changed (); } public void kill_fg () { @@ -616,7 +615,6 @@ namespace Terminal { Posix.kill (child_pid, Posix.Signal.TERM); reset (true, true); active_shell (old_loc); - check_cwd_changed (); } private void check_cwd_changed () { From f0fb321d6638ce00f0d8efc73d20ab4f3cc9d311 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 25 May 2021 15:57:11 +0100 Subject: [PATCH 11/11] Implement some review suggestions * Lose unnecessary boolean * Check names directly on tab restored etc rather than on switch page --- src/MainWindow.vala | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 9477ea5adc..d4da1e8ac5 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -752,7 +752,6 @@ namespace Terminal { private bool on_close_tab_requested (Granite.Widgets.Tab tab) { var t = get_term_widget (tab); - bool closing_current_terminal = t == current_terminal; if (t.has_foreground_process ()) { var d = new ForegroundProcessDialog (this); @@ -775,7 +774,7 @@ namespace Terminal { } current_terminal.grab_focus (); - if (!closing_current_terminal) { // Otherwise current_terminal will change triggering a name check + if (t != current_terminal) { // Otherwise current_terminal will change triggering a name check check_for_tabs_with_same_name (); } @@ -795,6 +794,7 @@ namespace Terminal { notebook.insert_tab (tab, -1); notebook.current = tab; term.grab_focus (); + check_for_tabs_with_same_name (); } private void on_tab_moved (Granite.Widgets.Tab tab, int x, int y) { @@ -958,8 +958,6 @@ namespace Terminal { return false; }); - - check_for_tabs_with_same_name (); } private void open_tabs () { @@ -1438,14 +1436,13 @@ namespace Terminal { string term2_path = terminal2.current_working_directory; string term2_name = Path.get_basename (term2_path); - if (terminal2 != terminal && term2_name == term_label) { - if (term2_path != term_path) { - terminal2.tab_label = disambiguate_label (term2_path, term_path); - terminal.tab_label = disambiguate_label (term_path, term2_path); - // Overwrite tooltip_text set by changing tab_label - terminal2.tooltip_text = term2_path; - terminal.tooltip_text = term_path; - } + if (terminal2 != terminal && term2_name == term_label && term2_path != term_path) { + terminal2.tab_label = disambiguate_label (term2_path, term_path); + terminal.tab_label = disambiguate_label (term_path, term2_path); + // Overwrite tooltip_text set by changing tab_label + terminal2.tooltip_text = term2_path; + terminal.tooltip_text = term_path; + } } }