diff --git a/plugins/fuzzy-search/fuzzy-search.vala b/plugins/fuzzy-search/fuzzy-search.vala index 2cff1cf2b7..8226a164bd 100644 --- a/plugins/fuzzy-search/fuzzy-search.vala +++ b/plugins/fuzzy-search/fuzzy-search.vala @@ -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 (); }); diff --git a/src/Application.vala b/src/Application.vala index cc809dab35..afbf15e31f 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -185,9 +185,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); } } } diff --git a/src/FolderManager/FileView.vala b/src/FolderManager/FileView.vala index b8b614a3d0..61ea037892 100644 --- a/src/FolderManager/FileView.vala +++ b/src/FolderManager/FileView.vala @@ -159,7 +159,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 () { diff --git a/src/FolderManager/FolderItem.vala b/src/FolderManager/FolderItem.vala index 9613841178..f00ce4c1ea 100644 --- a/src/FolderManager/FolderItem.vala +++ b/src/FolderManager/FolderItem.vala @@ -55,31 +55,48 @@ 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) { - Code.Widgets.SourceList.Item? item = null; + foreach (var child in file.children) { + Idle.add (() => { + Code.Widgets.SourceList.Item item = null; if (child.is_valid_directory) { item = new FolderItem (child, view); } else if (child.is_valid_textfile) { item = new FileItem (child, view); } - add (item); // ignores null parameter - } + 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); } diff --git a/src/MainWindow.vala b/src/MainWindow.vala index c24e9c3004..01240d6060 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -469,7 +469,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); } } @@ -489,7 +489,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); } @@ -653,11 +653,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; @@ -673,32 +674,30 @@ 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; + if (focused_file != null) { + folder_manager_view.expand_to_path (focused_file.get_path ()); + } } private bool on_key_pressed (uint keyval, uint keycode, Gdk.ModifierType state) { @@ -773,15 +772,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) { @@ -790,7 +790,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 @@ -819,7 +819,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 (); } }); } @@ -956,7 +956,7 @@ namespace Scratch { handle_quit (); check_unsaved_changes.begin ((obj, res) => { if (check_unsaved_changes.end (res)) { - destroy (); + app.quit (); } }); } @@ -992,7 +992,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); } } } @@ -1007,7 +1007,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) { @@ -1175,7 +1175,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; }); } diff --git a/src/Services/PluginManager.vala b/src/Services/PluginManager.vala index 26729a2400..749a4768ae 100644 --- a/src/Services/PluginManager.vala +++ b/src/Services/PluginManager.vala @@ -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; } diff --git a/src/Widgets/DocumentView.vala b/src/Widgets/DocumentView.vala index a98553980b..30fa7f56cd 100644 --- a/src/Widgets/DocumentView.vala +++ b/src/Widgets/DocumentView.vala @@ -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); } @@ -313,7 +313,7 @@ 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) { @@ -321,7 +321,7 @@ public class Scratch.Widgets.DocumentView : Gtk.Box { } } - 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) { @@ -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 () { @@ -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++) { @@ -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); diff --git a/src/Widgets/SearchBar.vala b/src/Widgets/SearchBar.vala index 56a718631d..45ce9d5629 100644 --- a/src/Widgets/SearchBar.vala +++ b/src/Widgets/SearchBar.vala @@ -326,26 +326,28 @@ namespace Scratch.Widgets { } // Called when one of the settings buttons or the search term changes - private void on_search_parameters_changed () requires (search_context != null) { - var search_string = search_entry.text; - search_context.settings.search_text = search_string; - var case_mode = (CaseSensitiveMode)(case_sensitive_search_button.active); - switch (case_mode) { - case CaseSensitiveMode.NEVER: - search_context.settings.case_sensitive = false; - break; - case CaseSensitiveMode.MIXED: - search_context.settings.case_sensitive = !((search_string.up () == search_string) || (search_string.down () == search_string)); - break; - case CaseSensitiveMode.ALWAYS: - search_context.settings.case_sensitive = true; - break; - default: - assert_not_reached (); - } + private void on_search_parameters_changed () { + if (search_context != null) { + var search_string = search_entry.text; + search_context.settings.search_text = search_string; + var case_mode = (CaseSensitiveMode)(case_sensitive_search_button.active); + switch (case_mode) { + case CaseSensitiveMode.NEVER: + search_context.settings.case_sensitive = false; + break; + case CaseSensitiveMode.MIXED: + search_context.settings.case_sensitive = !((search_string.up () == search_string) || (search_string.down () == search_string)); + break; + case CaseSensitiveMode.ALWAYS: + search_context.settings.case_sensitive = true; + break; + default: + assert_not_reached (); + } - search_context.settings.at_word_boundaries = whole_word_search_button.active; - search_context.settings.regex_enabled = regex_search_button.active; + search_context.settings.at_word_boundaries = whole_word_search_button.active; + search_context.settings.regex_enabled = regex_search_button.active; + } update_search_widgets (); }