From 529a92230b6b709a1544f003a00a7873850e9cfe Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 28 Jan 2025 17:40:47 +0000 Subject: [PATCH 1/7] Load children asynchronously --- src/FolderManager/FolderItem.vala | 45 ++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/FolderManager/FolderItem.vala b/src/FolderManager/FolderItem.vala index 33f8e130de..8fc4bf2dd9 100644 --- a/src/FolderManager/FolderItem.vala +++ b/src/FolderManager/FolderItem.vala @@ -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); @@ -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); } From a4e94d57a620a912c3c5da62e7a22d942c3b7088 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 28 Jan 2025 18:31:08 +0000 Subject: [PATCH 2/7] Load docs async; expand folder when all loaded --- src/MainWindow.vala | 34 ++++++++++++++++------------------ src/Widgets/DocumentView.vala | 27 +++++++++++---------------- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 34fe7d93db..592edb7c79 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -657,11 +657,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; @@ -677,31 +678,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 ()); - } } } } } - 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) { @@ -757,16 +755,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) { 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); + 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) { @@ -776,7 +774,7 @@ namespace Scratch { FolderManager.ProjectFolderItem? project = folder_manager_view.get_project_for_file (doc.file); doc.source_view.project = project; - document_view.open_document (doc, focus, 0, range); + yield document_view.open_document (doc, focus, 0, range); } // Close a document diff --git a/src/Widgets/DocumentView.vala b/src/Widgets/DocumentView.vala index ed19bb6510..e1fee6dea9 100644 --- a/src/Widgets/DocumentView.vala +++ b/src/Widgets/DocumentView.vala @@ -316,7 +316,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) { @@ -347,24 +347,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 () { From 01eb4f6f38d91f96cc1394dd42cd8b71c233896a Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 7 Feb 2025 10:26:04 +0000 Subject: [PATCH 3/7] Explicit .begin --- src/Application.vala | 4 ++-- src/FolderManager/FileView.vala | 2 +- src/MainWindow.vala | 12 ++++++------ src/Services/PluginManager.vala | 2 +- src/Widgets/DocumentView.vala | 8 ++++---- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 10c131d380..210fbf7bfb 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -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); } } } diff --git a/src/FolderManager/FileView.vala b/src/FolderManager/FileView.vala index d251e03966..0df3b48f81 100644 --- a/src/FolderManager/FileView.vala +++ b/src/FolderManager/FileView.vala @@ -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 () { diff --git a/src/MainWindow.vala b/src/MainWindow.vala index c2e8d1c061..e20355ddb4 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -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); } } @@ -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); } @@ -804,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 (); } }); } @@ -984,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); } } } @@ -999,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) { @@ -1167,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; }); } 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 1ae5c90b72..d46d233721 100644 --- a/src/Widgets/DocumentView.vala +++ b/src/Widgets/DocumentView.vala @@ -294,7 +294,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); } @@ -309,7 +309,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) { @@ -515,7 +515,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++) { @@ -644,7 +644,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); From 9c0f5ab72fe8825ebb1aaf2ddb8910b39b5fcd35 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 7 Feb 2025 10:56:26 +0000 Subject: [PATCH 4/7] Missed one --- plugins/fuzzy-search/fuzzy-search.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 (); }); From b5a5dc22733641dea24746de74bef10e4ebe9947 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sat, 19 Apr 2025 14:07:48 +0100 Subject: [PATCH 5/7] Fix warning re null focused file --- src/MainWindow.vala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 1f23fc435f..a6dcde3bae 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -684,7 +684,9 @@ namespace Scratch { document_view.request_placeholder_if_empty (); restore_override = null; - folder_manager_view.expand_to_path (focused_file.get_path ()); + 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) { From 937a79fe4f09a44cb18ff0d9aa284bf4bf293e66 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sat, 19 Apr 2025 14:08:05 +0100 Subject: [PATCH 6/7] Fix warning re null search context --- src/Widgets/SearchBar.vala | 40 ++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 19 deletions(-) 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 (); } From 7000a944fea0bc0bfa600d086bc7fbca76a678b1 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 9 May 2025 14:55:23 +0100 Subject: [PATCH 7/7] Call quit not destroy --- src/MainWindow.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 5c9a24ac9b..01240d6060 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -956,7 +956,7 @@ namespace Scratch { handle_quit (); check_unsaved_changes.begin ((obj, res) => { if (check_unsaved_changes.end (res)) { - destroy (); + app.quit (); } }); }