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

Load sidebar folder children asynchronously after docs finished loading #1522

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion plugins/fuzzy-search/fuzzy-search.vala
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public class Scratch.Plugins.FuzzySearch: Peas.ExtensionBase, Peas.Activatable {
var file = new Scratch.FolderManager.File (filepath);
var doc = new Scratch.Services.Document (window.actions, file.file);

window.open_document (doc);
window.open_document.begin (doc);
popover.popdown ();
});

Expand Down
4 changes: 2 additions & 2 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ namespace Scratch {
debug ("Files length: %d\n", files.length);
var doc = new Scratch.Services.Document (window.actions, file);
if (location_jump_manager.has_selection_range != null && files.length == 1) {
window.open_document_at_selected_range (doc, true, location_jump_manager.range);
window.open_document_at_selected_range.begin (doc, true, location_jump_manager.range);
} else {
window.open_document (doc);
window.open_document.begin (doc);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/FolderManager/FileView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
return;
}

add_folder (folder, true);
add_folder.begin (folder, true);
}

public void collapse_all () {
Expand Down
45 changes: 30 additions & 15 deletions src/FolderManager/FolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,10 @@ namespace Scratch.FolderManager {
}
}

private void on_toggled () {
private async void load_children () {
var root = get_root_folder ();
if (!children_loaded &&
expanded &&
n_children <= 1 &&
file.children.size > 0) {

foreach (var child in file.children) {
foreach (var child in file.children) {
Idle.add (() => {
Code.Widgets.SourceList.Item item = null;
if (child.is_valid_directory ()) {
item = new FolderItem (child, view);
Expand All @@ -73,15 +69,34 @@ namespace Scratch.FolderManager {
if (item != null) {
add (item);
}
}

children_loaded = true;
if (root != null) {
root.child_folder_loaded (this);
}
} else if (!expanded &&
root != null &&
root.monitored_repo != null) {
load_children.callback ();
return Source.REMOVE;
});

yield;
}

children_loaded = true;
if (root != null) {
root.child_folder_loaded (this);
}
}

private void on_toggled () {
if (!children_loaded &&
expanded &&
n_children <= 1 &&
file.children.size > 0) {

load_children.begin ();
return;
}

var root = get_root_folder ();
if (!expanded &&
root != null &&
root.monitored_repo != null) {
//When toggled closed, update status to reflect hidden contents
root.update_item_status (this);
}
Expand Down
50 changes: 24 additions & 26 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ namespace Scratch {
//TODO Handle folders dropped here
if (Scratch.Services.FileHandler.can_open_file (file, out is_folder) && !is_folder) {
Scratch.Services.Document doc = new Scratch.Services.Document (actions, file);
document_view.open_document (doc);
document_view.open_document.begin (doc);
}
}

Expand All @@ -494,7 +494,7 @@ namespace Scratch {
var doc = new Scratch.Services.Document (actions, file.file);

if (file.is_valid_textfile) {
open_document (doc);
open_document.begin (doc);
} else {
open_binary (file.file);
}
Expand Down Expand Up @@ -659,11 +659,12 @@ namespace Scratch {
}
}

private void restore_opened_documents () {
private async void restore_opened_documents () {
File? focused_file = null;
if (privacy_settings.get_boolean ("remember-recent-files")) {
var doc_infos = settings.get_value ("opened-files");
var doc_info_iter = new VariantIter (doc_infos);
string focused_document = settings.get_string ("focused-document");
string focused_uri = settings.get_string ("focused-document");
string uri;
int pos;
bool was_restore_overriden = false;
Expand All @@ -679,32 +680,28 @@ namespace Scratch {
But for files that do not exist we need to make sure that doc won't create a new file
*/
if (file.query_exists ()) {
var is_focused = uri == focused_uri;
if (is_focused) {
focused_file = file;
}
//TODO Check files valid (settings could have been manually altered)
var doc = new Scratch.Services.Document (actions, file);
bool is_focused = file.get_uri () == focused_document;
if (doc.exists () || !doc.is_file_temporary) {
if (restore_override != null && (file.get_path () == restore_override.file.get_path ())) {
open_document_at_selected_range (doc, true, restore_override.range, true);
yield open_document_at_selected_range (doc, true, restore_override.range, true);
was_restore_overriden = true;
} else {
open_document (doc, was_restore_overriden ? false : is_focused, pos);
yield open_document (doc, was_restore_overriden ? false : is_focused, pos);
}
}

if (is_focused) { //Maybe expand to show all opened documents?
folder_manager_view.expand_to_path (file.get_path ());
}
}
}
}
}

// DocumentView's number of docs updates asychronously so need Idle
Idle.add (() => {
document_view.request_placeholder_if_empty ();
restore_override = null;
return Source.REMOVE;
});
document_view.request_placeholder_if_empty ();
restore_override = null;
folder_manager_view.expand_to_path (focused_file.get_path ());
}

// private bool on_key_pressed (Gdk.EventKey event) {
Expand Down Expand Up @@ -760,15 +757,16 @@ namespace Scratch {
folder_manager_view.open_folder (foldermanager_file);
}

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

doc.source_view.project = folder_manager_view.get_project_for_file (doc.file);
document_view.open_document (doc, focus, cursor_position);
FolderManager.ProjectFolderItem? project = folder_manager_view.get_project_for_file (doc.file);
doc.source_view.project = project;
yield document_view.open_document (doc, focus, cursor_position);
}

public void open_document_at_selected_range (Scratch.Services.Document doc,
public async void open_document_at_selected_range (Scratch.Services.Document doc,
bool focus = true,
SelectionRange range = SelectionRange.EMPTY,
bool is_override = false) {
Expand All @@ -777,7 +775,7 @@ namespace Scratch {
}

doc.source_view.project = folder_manager_view.get_project_for_file (doc.file);
document_view.open_document (doc, focus, 0, range);
yield document_view.open_document (doc, focus, 0, range);
}

// Close a document
Expand Down Expand Up @@ -806,7 +804,7 @@ namespace Scratch {
folder_manager_view.restore_saved_state.begin ((obj, res) => {
folder_manager_view.restore_saved_state.end (res);
if (restore_docs) {
restore_opened_documents ();
restore_opened_documents.begin ();
}
});
}
Expand Down Expand Up @@ -986,7 +984,7 @@ namespace Scratch {
// Open the file
var file = File.new_for_uri (uri);
var doc = new Scratch.Services.Document (actions, file);
open_document (doc);
open_document.begin (doc);
}
}
}
Expand All @@ -1001,7 +999,7 @@ namespace Scratch {
var file = File.new_for_path (path);
var doc = new Scratch.Services.Document (new_window.actions, file);

new_window.open_document (doc, true);
new_window.open_document.begin (doc, true);
}

private void action_open_folder (SimpleAction action, Variant? param) {
Expand Down Expand Up @@ -1169,7 +1167,7 @@ namespace Scratch {
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.
open_document.begin (doc); // Use this to reassociate project and document.
return true;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Services/PluginManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Scratch.Services {

public Document open_file (File file) {
var doc = new Document (manager.window.actions, file);
manager.window.open_document (doc);
manager.window.open_document.begin (doc);
return doc;
}

Expand Down
35 changes: 15 additions & 20 deletions src/Widgets/DocumentView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {

var doc = new Services.Document (window.actions, file);
// Must open document in order to unlock it.
open_document (doc);
open_document.begin (doc);
} catch (Error e) {
critical (e.message);
}
Expand All @@ -313,15 +313,15 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {
file.replace_contents (clipboard.data, null, false, 0, null);
var doc = new Services.Document (window.actions, file);

open_document (doc);
open_document.begin (doc);


} catch (Error e) {
critical ("Cannot insert clipboard: %s", clipboard);
}
}

public void open_document (Services.Document doc, bool focus = true, int cursor_position = 0, SelectionRange range = SelectionRange.EMPTY) {
public async void open_document (Services.Document doc, bool focus = true, int cursor_position = 0, SelectionRange range = SelectionRange.EMPTY) {
for (int n = 0; n <= docs.length (); n++) {
var nth_doc = docs.nth_data (n);
if (nth_doc == null) {
Expand Down Expand Up @@ -352,24 +352,19 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {
current_document = doc;
}

Idle.add_full (GLib.Priority.LOW, () => { // This helps ensures new tab is drawn before opening document.
doc.open.begin (false, (obj, res) => {
doc.open.end (res);
if (focus && doc == current_document) {
doc.focus ();
}
yield doc.open (false);

if (range != SelectionRange.EMPTY) {
doc.source_view.select_range (range);
} else if (cursor_position > 0) {
doc.source_view.cursor_position = cursor_position;
}
if (focus && doc == current_document) {
doc.focus ();
}

save_opened_files ();
});
if (range != SelectionRange.EMPTY) {
doc.source_view.select_range (range);
} else if (cursor_position > 0) {
doc.source_view.cursor_position = cursor_position;
}

return false;
});
save_opened_files ();
}

public void next_document () {
Expand Down Expand Up @@ -524,7 +519,7 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {
public void restore_closed_tab (string path) {
var file = File.new_for_path (path);
var doc = new Services.Document (window.actions, file);
open_document (doc);
open_document.begin (doc);

var menu = (Menu) tab_history_button.menu_model;
for (var i = 0; i < menu.get_n_items (); i++) {
Expand Down Expand Up @@ -653,7 +648,7 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {
foreach (var filename in uris) {
var file = File.new_for_uri (filename);
var doc = new Services.Document (window.actions, file);
open_document (doc);
open_document.begin (doc);
}

Gtk.drag_finish (ctx, true, false, time);
Expand Down
Loading