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

Hide non-active documents when project manually chosen #1114

Merged
merged 15 commits into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
43 changes: 38 additions & 5 deletions src/FolderManager/FileView.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2017 elementary LLC. (https://elementary.io),
* Copyright (c) 2017 - 2022 elementary LLC. (https://elementary.io),
* 2013 Julien Spautz <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -23,15 +23,19 @@
*/
public class Scratch.FolderManager.FileView : Granite.Widgets.SourceList, Code.PaneSwitcher {
private GLib.Settings settings;
private Scratch.Services.GitManager git_manager;
private ActionGroup? toplevel_action_group = null;

public signal void select (string file);
public signal void close_all_docs_from_path (string path);

// This is a workaround for SourceList silliness: you cannot remove an item
// without it automatically selecting another one.
public bool ignore_next_select { get; set; default = false; }
public string icon_name { get; set; }
public string title { get; set; }
public string active_project_path {
get {
return git_manager.active_project_path;
}
}

construct {
width_request = 180;
Expand All @@ -41,6 +45,13 @@ public class Scratch.FolderManager.FileView : Granite.Widgets.SourceList, Code.P
item_selected.connect (on_item_selected);

settings = new GLib.Settings ("io.elementary.code.folder-manager");

git_manager = Scratch.Services.GitManager.get_instance ();

realize.connect (() => {
toplevel_action_group = get_action_group (MainWindow.ACTION_GROUP);
assert_nonnull (toplevel_action_group);
});
}

private void on_item_selected (Granite.Widgets.SourceList.Item? item) {
Expand Down Expand Up @@ -104,6 +115,27 @@ public class Scratch.FolderManager.FileView : Granite.Widgets.SourceList, Code.P
item_selected.connect (on_item_selected);
}

public void collapse_other_projects (string? keep_open_path = null) {
unowned string path;
if (keep_open_path == null) {
path = git_manager.active_project_path;
} else {
path = keep_open_path;
git_manager.active_project_path = path;
}

foreach (var child in root.children) {
var project_folder = ((ProjectFolderItem) child);
if (project_folder.path != path) {
project_folder.expanded = false;
toplevel_action_group.activate_action (MainWindow.ACTION_HIDE_PROJECT_DOCS, new Variant.string (project_folder.path));
} else if (project_folder.path == path) {
project_folder.expanded = true;
toplevel_action_group.activate_action (MainWindow.ACTION_RESTORE_PROJECT_DOCS, new Variant.string (project_folder.path));
}
}
}

private unowned Granite.Widgets.SourceList.Item? find_path (Granite.Widgets.SourceList.ExpandableItem list,
string path,
bool expand = false) {
Expand Down Expand Up @@ -231,7 +263,7 @@ public class Scratch.FolderManager.FileView : Granite.Widgets.SourceList, Code.P

folder_root.expanded = expand;
folder_root.closed.connect (() => {
close_all_docs_from_path (folder_root.file.path);
toplevel_action_group.activate_action (MainWindow.ACTION_CLOSE_PROJECT_DOCS, new Variant.string (folder_root.path));
root.remove (folder_root);
foreach (var child in root.children) {
var child_folder = (ProjectFolderItem) child;
Expand All @@ -247,6 +279,7 @@ public class Scratch.FolderManager.FileView : Granite.Widgets.SourceList, Code.P
foreach (var child in root.children) {
var project_folder_item = (ProjectFolderItem)child;
if (project_folder_item != folder_root) {
toplevel_action_group.activate_action (MainWindow.ACTION_CLOSE_PROJECT_DOCS, new Variant.string (project_folder_item.path));
root.remove (project_folder_item);
Scratch.Services.GitManager.get_instance ().remove_project (project_folder_item);
}
Expand Down
65 changes: 61 additions & 4 deletions src/FolderManager/ProjectFolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,58 @@ namespace Scratch.FolderManager {
}

public override Gtk.Menu? get_context_menu () {
var close_item = new Gtk.MenuItem.with_label (_("Close Folder"));
close_item.activate.connect (() => {
var close_folder_item = new Gtk.MenuItem.with_label (_("Close Folder"));
close_folder_item.activate.connect (() => {
closed ();
});

var close_all_except_item = new Gtk.MenuItem.with_label (_("Close Other Folders"));
close_all_except_item.activate.connect (() => { close_all_except (); });
close_all_except_item.sensitive = view.root.children.size > 1;

var n_open = Scratch.Services.DocumentManager.get_instance ().open_for_project (path);
var open_text = ngettext (_("Close %u Open Document"),
_("Close %u Open Documents"),
n_open).printf (n_open);

var close_accellabel = new Granite.AccelLabel.from_action_name (
open_text,
MainWindow.ACTION_PREFIX + MainWindow.ACTION_CLOSE_PROJECT_DOCS + "::"
);
var close_item = new Gtk.MenuItem () {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_CLOSE_PROJECT_DOCS,
action_target = new Variant.string (file.file.get_path ())
};
close_item.add (close_accellabel);

var hide_text = ngettext (_("Hide %u Open Document"),
_("Hide %u Open Documents"),
n_open).printf (n_open);

var hide_accellabel = new Granite.AccelLabel.from_action_name (
hide_text,
MainWindow.ACTION_PREFIX + MainWindow.ACTION_HIDE_PROJECT_DOCS + "::"
);
var hide_item = new Gtk.MenuItem () {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_HIDE_PROJECT_DOCS,
action_target = new Variant.string (file.file.get_path ())
};
hide_item.add (hide_accellabel);

var n_restorable = Scratch.Services.DocumentManager.get_instance ().restorable_for_project (path);
var restore_text = ngettext (_("Restore %u Hidden Document"),
_("Restore %u Hidden Documents"),
n_restorable).printf (n_restorable);
var restore_accellabel = new Granite.AccelLabel.from_action_name (
restore_text,
MainWindow.ACTION_PREFIX + MainWindow.ACTION_RESTORE_PROJECT_DOCS + "::"
);
var restore_item = new Gtk.MenuItem () {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_RESTORE_PROJECT_DOCS,
action_target = new Variant.string (file.file.get_path ())
};
restore_item.add (restore_accellabel);

var delete_item = new Gtk.MenuItem.with_label (_("Move to Trash"));
delete_item.activate.connect (() => {
closed ();
Expand All @@ -129,7 +172,7 @@ namespace Scratch.FolderManager {
);

var search_item = new Gtk.MenuItem () {
action_name = "win.action_find_global",
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_FIND_GLOBAL,
action_target = new Variant.string (file.file.get_path ())
};
search_item.add (search_accellabel);
Expand Down Expand Up @@ -157,8 +200,22 @@ namespace Scratch.FolderManager {
}

menu.append (new Gtk.SeparatorMenuItem ());
menu.append (close_item);
menu.append (close_folder_item);
menu.append (close_all_except_item);
menu.append (new Gtk.SeparatorMenuItem ());
if (n_restorable > 0) {
menu.append (restore_item);
}

if (n_open > 0) {
menu.append (hide_item);
menu.append (close_item);
}

if (n_restorable + n_open > 1) {
menu.append (new Gtk.SeparatorMenuItem ());
}

menu.append (delete_item);
menu.append (new Gtk.SeparatorMenuItem ());
menu.append (search_item);
Expand Down
77 changes: 62 additions & 15 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Scratch {
public Scratch.Widgets.SearchBar search_bar;
private Code.WelcomeView welcome_view;
private FolderManager.FileView folder_manager_view;
private Scratch.Services.DocumentManager document_manager;

// Plugins
private Scratch.Services.PluginsManager plugins;
Expand All @@ -54,7 +55,8 @@ namespace Scratch {

public SimpleActionGroup actions { get; construct; }

public const string ACTION_PREFIX = "win.";
public const string ACTION_GROUP = "win";
public const string ACTION_PREFIX = ACTION_GROUP + ".";
public const string ACTION_FIND = "action_find";
public const string ACTION_FIND_NEXT = "action_find_next";
public const string ACTION_FIND_PREVIOUS = "action_find_previous";
Expand Down Expand Up @@ -90,6 +92,9 @@ namespace Scratch {
public const string ACTION_PREVIOUS_TAB = "action_previous_tab";
public const string ACTION_CLEAR_LINES = "action_clear_lines";
public const string ACTION_NEW_BRANCH = "action_new_branch";
public const string ACTION_CLOSE_PROJECT_DOCS = "action_close_project_docs";
public const string ACTION_HIDE_PROJECT_DOCS = "action_hide_project_docs";
public const string ACTION_RESTORE_PROJECT_DOCS = "action_restore_project_docs";

public static Gee.MultiMap<string, string> action_accelerators = new Gee.HashMultiMap<string, string> ();

Expand Down Expand Up @@ -129,7 +134,10 @@ namespace Scratch {
{ ACTION_NEXT_TAB, action_next_tab },
{ ACTION_PREVIOUS_TAB, action_previous_tab },
{ ACTION_CLEAR_LINES, action_clear_lines },
{ ACTION_NEW_BRANCH, action_new_branch, "s" }
{ ACTION_NEW_BRANCH, action_new_branch, "s" },
{ ACTION_HIDE_PROJECT_DOCS, action_hide_project_docs, "s"},
{ ACTION_CLOSE_PROJECT_DOCS, action_close_project_docs, "s"},
{ ACTION_RESTORE_PROJECT_DOCS, action_restore_project_docs, "s"}
};

public MainWindow (Scratch.Application scratch_app) {
Expand Down Expand Up @@ -176,6 +184,8 @@ namespace Scratch {
action_accelerators.set (ACTION_PREVIOUS_TAB, "<Control><Shift>Tab");
action_accelerators.set (ACTION_CLEAR_LINES, "<Control>K"); //Geany
action_accelerators.set (ACTION_NEW_BRANCH + "::", "<Control>B");
action_accelerators.set (ACTION_HIDE_PROJECT_DOCS + "::", "<Control><Shift>h");
action_accelerators.set (ACTION_RESTORE_PROJECT_DOCS + "::", "<Control><Shift>r");

var provider = new Gtk.CssProvider ();
provider.load_from_resource ("io/elementary/code/Application.css");
Expand All @@ -190,9 +200,11 @@ namespace Scratch {
weak Gtk.IconTheme default_theme = Gtk.IconTheme.get_default ();
default_theme.add_resource_path ("/io/elementary/code");

document_manager = Scratch.Services.DocumentManager.get_instance ();

actions = new SimpleActionGroup ();
actions.add_action_entries (ACTION_ENTRIES, this);
insert_action_group ("win", actions);
insert_action_group (ACTION_GROUP, actions);

actions.action_state_changed.connect ((name, new_state) => {
if (name == ACTION_SHOW_FIND) {
Expand Down Expand Up @@ -279,6 +291,10 @@ namespace Scratch {
toolbar = new Scratch.Widgets.HeaderBar ();
toolbar.title = title;

toolbar.choose_project_button.project_chosen.connect (() => {
folder_manager_view.collapse_other_projects ();
});

// SearchBar
search_bar = new Scratch.Widgets.SearchBar (this);
search_revealer = new Gtk.Revealer ();
Expand Down Expand Up @@ -339,14 +355,6 @@ namespace Scratch {
}
});

folder_manager_view.close_all_docs_from_path.connect ((a) => {
var docs = document_view.docs.copy ();
docs.foreach ((doc) => {
if (doc.file.get_path ().has_prefix (a)) {
document_view.close_document (doc);
}
});
});

folder_manager_view.restore_saved_state ();

Expand Down Expand Up @@ -564,7 +572,10 @@ namespace Scratch {
folder_manager_view.open_folder (foldermanager_file);
}

public void open_document (Scratch.Services.Document doc, bool focus = true, int cursor_position = 0) {
public void open_document (Scratch.Services.Document doc,
bool focus = true,
int cursor_position = 0) {

FolderManager.ProjectFolderItem? project = folder_manager_view.get_project_for_file (doc.file);
doc.source_view.project = project;
document_view.open_document (doc, focus, cursor_position);
Expand Down Expand Up @@ -862,6 +873,42 @@ namespace Scratch {
}
}

private void action_hide_project_docs (SimpleAction action, Variant? param) {
close_project_docs (get_target_path_for_actions (param), true);
}

private void action_close_project_docs (SimpleAction action, Variant? param) {
close_project_docs (get_target_path_for_actions (param), false);
}

private void action_restore_project_docs (SimpleAction action, Variant? param) {
restore_project_docs (get_target_path_for_actions (param));
}

private void close_project_docs (string project_path, bool make_restorable) {
unowned var docs = document_view.docs;
docs.foreach ((doc) => {
if (doc.file.get_path ().has_prefix (project_path)) {
document_view.close_document (doc);
if (make_restorable) {
document_manager.make_restorable (doc);
}
}
});

if (!make_restorable) {
document_manager.remove_project (project_path);
}
}

private void restore_project_docs (string project_path) {
document_manager.take_restorable_paths (project_path).@foreach ((doc_path) => {
var doc = new Scratch.Services.Document (actions, File.new_for_path (doc_path));
open_document (doc); // Use this to reassociate project and document.
return true;
});
}

/** Not a toggle action - linked to keyboard short cut (Ctrl-f). **/
private string current_search_term = "";
private void action_fetch (SimpleAction action, Variant? param) {
Expand Down Expand Up @@ -905,7 +952,7 @@ namespace Scratch {
term = search_bar.search_entry.text;
}

folder_manager_view.search_global (get_target_path_for_git_actions (param), term);
folder_manager_view.search_global (get_target_path_for_actions (param), term);
}

private void set_search_text () {
Expand Down Expand Up @@ -1023,10 +1070,10 @@ namespace Scratch {
}

private void action_new_branch (SimpleAction action, Variant? param) {
folder_manager_view.new_branch (get_target_path_for_git_actions (param));
folder_manager_view.new_branch (get_target_path_for_actions (param));
}

private string? get_target_path_for_git_actions (Variant? path_variant) {
private string? get_target_path_for_actions (Variant? path_variant) {
string? path = "";
if (path_variant != null) {
path = path_variant.get_string ();
Expand Down
Loading