Skip to content

Commit

Permalink
Restore tab names (#574)
Browse files Browse the repository at this point in the history
* Fix tab name disambiguation

* Give each view container a unique id and use instead of current_tab
* Store path in ViewContainer tab-name rather than basename (if not custom)
* Refactor check_for_same_name function.
* Window responsible for final tab name
* Window responsible for adding "as Administrator"
* Resequence adding tab
* Remove seemingly unnecessary Idle loop
* Simplify Window::update_labels ()

* Remove unnecessary direct call to Window::update_labels and make private

* Do not update labels if no current tab

* Fix double semi-colon
  • Loading branch information
jeremypw authored and danirabbit committed Aug 15, 2018
1 parent 38df487 commit bcec244
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 39 deletions.
21 changes: 16 additions & 5 deletions src/View/ViewContainer.vala
Expand Up @@ -26,7 +26,17 @@ using Marlin;

namespace Marlin.View {
public class ViewContainer : Gtk.Bin {
private static int container_id;

protected static int get_next_container_id () {
return ++container_id;
}

static construct {
container_id = -1;
}

public int id {get; construct;}
public Gtk.Widget? content_item;
public bool can_show_folder { get; private set; default = false; }
private Marlin.View.Window? _window = null;
Expand Down Expand Up @@ -106,6 +116,10 @@ namespace Marlin.View {
public signal void active ();
/* path-changed signal no longer used */

construct {
id = ViewContainer.get_next_container_id ();
}

/* Initial location now set by Window.make_tab after connecting signals */
public ViewContainer (Marlin.View.Window win) {
window = win;
Expand Down Expand Up @@ -345,8 +359,7 @@ namespace Marlin.View {

private void refresh_slot_info (GLib.File loc) {
update_tab_name ();
window.loading_uri (loc.get_uri ());
window.update_labels (loc.get_parse_name (), tab_name);
window.loading_uri (loc.get_uri ()); /* Updates labels as well */
/* Do not update top menu (or record uri) unless folder loads successfully */
}

Expand All @@ -368,15 +381,13 @@ namespace Marlin.View {
} catch (ConvertError e) {}

if (tab_name == null) {
tab_name = Path.get_basename (slot_path);
tab_name = location.get_parse_name ();
}
}
}

if (tab_name == null) {
tab_name = Marlin.INVALID_TAB_NAME;
} else if (Posix.getuid () == 0) {
tab_name = tab_name + " " + _("(as Administrator)");
}

this.tab_name = tab_name;
Expand Down
94 changes: 60 additions & 34 deletions src/View/Window.vala
Expand Up @@ -133,6 +133,7 @@ namespace Marlin.View {
}
}

loading_uri.connect (update_labels);
present ();
}

Expand Down Expand Up @@ -414,12 +415,13 @@ namespace Marlin.View {
var tab = new Granite.Widgets.Tab ("", null, content);
tab.ellipsize_mode = Pango.EllipsizeMode.MIDDLE;

/* Capturing ViewContainer reference in closure prevents its proper destruction */
change_tab ((int)tabs.insert_tab (tab, -1));
tabs.current = tab;
/* Capturing ViewContainer object reference in closure prevents its proper destruction
* so capture its unique id instead */
var id = content.id;
content.tab_name_changed.connect ((tab_name) => {
Idle.add (() => {
tab.label = check_for_tab_with_same_name ();
return false;
});
set_tab_label (check_for_tab_with_same_name (id, tab_name), tab, tab_name);
});

content.loading.connect ((is_loading) => {
Expand All @@ -432,43 +434,65 @@ namespace Marlin.View {
});

content.add_view (mode, location);

change_tab ((int)tabs.insert_tab (tab, -1));
tabs.current = tab;
}

private string check_for_tab_with_same_name () {
assert_nonnull (current_tab);

var vc = current_tab;
var name = vc.tab_name;

if (name == Marlin.INVALID_TAB_NAME) {
return name;
private string check_for_tab_with_same_name (int id, string path) {
if (path == Marlin.INVALID_TAB_NAME) {
return path;
}

var path = Uri.unescape_string (vc.uri);
var new_name = name;

var new_label = Path.get_basename (path);
foreach (Granite.Widgets.Tab tab in tabs.tabs) {
var content = (ViewContainer)(tab.page);
if (content != vc) {
string content_path = Uri.unescape_string (content.uri);
if (content.tab_name == name && content_path != path) {
if (content.tab_name == tab.label) {
Idle.add_full (GLib.Priority.LOW, () => {
/* Trigger relabelling of conflicting tab (but not before this function finishes) */
content.tab_name_changed (content.tab_name);
return false;
});
if (content.id != id) {
string content_path = content.tab_name;
string content_label = Path.get_basename (content_path);
if (tab.label == new_label) {
if (content_path != path) {
new_label = disambiguate_name (new_label, path, content_path); /*Relabel calling tab */
if (content_label == tab.label) {
/* Also relabel conflicting tab (but not before this function finishes) */
Idle.add_full (GLib.Priority.LOW, () => {
set_tab_label (disambiguate_name (content_label, content_path, path), tab, content_path);
return false;
});
}
}
} else if (content_label == new_label &&
content_path == path &&
content_label != tab.label) {

new_name = disambiguate_name (name, path, content_path); /*Also relabel this tab */
/* Revert to short label when possible */
Idle.add_full (GLib.Priority.LOW, () => {
set_tab_label (content_label, tab, content_path);
return false;
});
}
}
}

return new_name;
return new_label;
}

/* Just to append "as Administrator" when appropriate */
private void set_tab_label (string label, Granite.Widgets.Tab tab, string? tooltip = null) {
string lab = label;
if (Posix.getuid () == 0) {
lab += (" " + _("(as Administrator)"));
}

tab.label = lab;

/* Needs change to Granite to allow (visible) tooltip amendment.
* This compiles because tab is a widget but the tootip is overridden by that set internally */
if (tooltip != null) {
var tt = tooltip;
if (Posix.getuid () == 0) {
tt += (" " + _("(as Administrator)"));
}

tab.set_tooltip_text (tt);
}
}

private string disambiguate_name (string name, string path, string conflict_path) {
Expand Down Expand Up @@ -903,6 +927,7 @@ namespace Marlin.View {
/* Prevent too rapid loading of tabs which can cause crashes
* This may not be necessary with the Vala version of the views but does no harm
*/
/*TODO Remove this after sufficient testing */
Thread.usleep (100000);
}

Expand Down Expand Up @@ -988,10 +1013,11 @@ namespace Marlin.View {
Preferences.settings.set_enum ("default-viewmode", mode);
}

public void update_labels (string new_path, string tab_name) {
assert (new_path != null && new_path != "");
set_title (tab_name);
top_menu.update_location_bar (new_path);
private void update_labels (string uri) {
if (current_tab != null) { /* Can happen during restore */
set_title (current_tab.tab_name); /* Not actually visible on elementaryos */
top_menu.update_location_bar (uri);
}
}

public void mount_removed (Mount mount) {
Expand Down

0 comments on commit bcec244

Please sign in to comment.