From 916bea7ec5b81daf1de61aea97930d1aba8df6d9 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 15 Mar 2023 14:58:33 +0000 Subject: [PATCH 1/5] Provide option to load invalid text into new document --- src/MainWindow.vala | 12 ++++++++++++ src/Services/Document.vala | 25 +++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index e82b46d80a..f7af990446 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -95,6 +95,7 @@ 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_TAB = "action_close_tab"; 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"; @@ -141,6 +142,7 @@ namespace Scratch { { ACTION_PREVIOUS_TAB, action_previous_tab }, { ACTION_CLEAR_LINES, action_clear_lines }, { ACTION_NEW_BRANCH, action_new_branch, "s" }, + { ACTION_CLOSE_TAB, action_close_tab, "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"} @@ -953,6 +955,16 @@ namespace Scratch { } } + private void action_close_tab (SimpleAction action, Variant? param) { + var close_path = get_target_path_for_actions (param); + unowned var docs = document_view.docs; + docs.foreach ((doc) => { + if (doc.file.get_path () == close_path) { + document_view.close_document (doc); + } + }); + } + private void action_hide_project_docs (SimpleAction action, Variant? param) { close_project_docs (get_target_path_for_actions (param), true); } diff --git a/src/Services/Document.vala b/src/Services/Document.vala index d03306d467..f83c8c37bd 100644 --- a/src/Services/Document.vala +++ b/src/Services/Document.vala @@ -328,12 +328,11 @@ namespace Scratch.Services { } else { source_view.buffer.text = buffer.text; } - } catch (Error e) { critical (e.message); source_view.buffer.text = ""; - show_default_load_error_view (); working = false; + show_default_load_error_view (buffer.text); return; } finally { load_cancellable = null; @@ -357,7 +356,6 @@ namespace Scratch.Services { original_content = source_view.buffer.text; last_save_content = source_view.buffer.text; set_saved_status (true); - doc_opened (); source_view.sensitive = true; @@ -733,10 +731,29 @@ namespace Scratch.Services { } // Show an error view which says "Hey, I cannot read that file!" - private void show_default_load_error_view () { + private void show_default_load_error_view (string? invalid_content = null) { var title = _("File \"%s\" Cannot Be Read").printf (get_basename ()); var description = _("It may be corrupt or you don't have permission to read it."); var alert_view = new Granite.Widgets.AlertView (title, description, "dialog-error"); + if (invalid_content != null) { + alert_view.show_action (_("Show as text")); + alert_view.action_activated.connect (() => { + main_stack.set_visible_child_name ("content"); + Idle.add (() => { + var clipboard = Gtk.Clipboard.get_for_display (get_display (), Gdk.SELECTION_CLIPBOARD); + clipboard.set_text (invalid_content, -1); + var clipboard_action = Utils.action_from_group (MainWindow.ACTION_NEW_FROM_CLIPBOARD, actions); + clipboard_action.set_enabled (true); + clipboard_action.activate (null); + + var close_tab_action = Utils.action_from_group (MainWindow.ACTION_CLOSE_TAB, actions); + close_tab_action.set_enabled (true); + close_tab_action.activate (new Variant ("s", file.get_path ())); + return false; + }); + }); + } + alert_view.show_all (); main_stack.add_named (alert_view, "error_alert"); main_stack.set_visible_child (alert_view); From 3c050a0b4ede2246c7370de8b80910af15326df7 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 15 Mar 2023 20:17:27 +0000 Subject: [PATCH 2/5] Open new doc after create from clipboard --- src/Widgets/DocumentView.vala | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Widgets/DocumentView.vala b/src/Widgets/DocumentView.vala index 30ed6016e6..cbf6a335ef 100644 --- a/src/Widgets/DocumentView.vala +++ b/src/Widgets/DocumentView.vala @@ -207,11 +207,8 @@ public class Scratch.Widgets.DocumentView : Granite.Widgets.DynamicNotebook { var doc = new Services.Document (window.actions, file); - insert_document (doc, -1); - current_document = doc; + open_document (doc); - doc.focus (); - save_opened_files (); } catch (Error e) { critical ("Cannot insert clipboard: %s", clipboard); } From f69aa1b7fdd71d6b8b883d4fe7aa3f78e7f689fe Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 15 Mar 2023 20:18:18 +0000 Subject: [PATCH 3/5] Only give option to open in new doc if there is content --- src/Services/Document.vala | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Services/Document.vala b/src/Services/Document.vala index f83c8c37bd..ba6387b69e 100644 --- a/src/Services/Document.vala +++ b/src/Services/Document.vala @@ -731,12 +731,19 @@ namespace Scratch.Services { } // Show an error view which says "Hey, I cannot read that file!" - private void show_default_load_error_view (string? invalid_content = null) { - var title = _("File \"%s\" Cannot Be Read").printf (get_basename ()); - var description = _("It may be corrupt or you don't have permission to read it."); + private void show_default_load_error_view (string invalid_content = "") { + var title = _("Cannot read text in file \"%s\"").printf (get_basename ()); + string description; + if (invalid_content == "") { + description = _("You may not have permission to read the file."); + } else { + description = _("The file may be corrupt or may not be a text file"); + } var alert_view = new Granite.Widgets.AlertView (title, description, "dialog-error"); - if (invalid_content != null) { - alert_view.show_action (_("Show as text")); + // Lack of read permission results in empty content string. Do not give option to open + // in new document in that case. + if (invalid_content != "") { + alert_view.show_action (_("Convert to text and show in a new document")); alert_view.action_activated.connect (() => { main_stack.set_visible_child_name ("content"); Idle.add (() => { From 74a5bea59d8b50deab62b3ff2d970a2d63fcadbb Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 17 Mar 2023 19:35:21 +0000 Subject: [PATCH 4/5] Use typographical quotes --- src/Services/Document.vala | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Services/Document.vala b/src/Services/Document.vala index ba6387b69e..5ab07cdda8 100644 --- a/src/Services/Document.vala +++ b/src/Services/Document.vala @@ -262,7 +262,7 @@ namespace Scratch.Services { try { FileUtils.set_contents (file.get_path (), ""); } catch (FileError e) { - warning ("Cannot create file \"%s\": %s", get_basename (), e.message); + warning ("Cannot create file “%s”: %s", get_basename (), e.message); return; } } @@ -297,7 +297,7 @@ namespace Scratch.Services { load_timout_id = Timeout.add_seconds_full (GLib.Priority.HIGH, 5, () => { if (load_cancellable != null && !load_cancellable.is_cancelled ()) { - var title = _("Loading File \"%s\" Is Taking a Long Time").printf (get_basename ()); + var title = _("Loading File “%s” Is Taking a Long Time").printf (get_basename ()); var description = _("Please wait while Code is loading the file."); var alert_view = new Granite.Widgets.AlertView (title, description, "dialog-information"); alert_view.show_action (_("Cancel Loading")); @@ -372,7 +372,7 @@ namespace Scratch.Services { } public bool do_close (bool app_closing = false) { - debug ("Closing \"%s\"", get_basename ()); + debug ("Closing “%s”", get_basename ()); if (!loaded) { load_cancellable.cancel (); @@ -391,7 +391,7 @@ namespace Scratch.Services { var parent_window = source_view.get_toplevel () as Gtk.Window; var dialog = new Granite.MessageDialog ( - _("Save changes to \"%s\" before closing?").printf (this.get_basename ()), + _("Save changes to “%s” before closing?").printf (this.get_basename ()), _("If you don't save, changes will be permanently lost."), new ThemedIcon ("dialog-warning"), Gtk.ButtonsType.NONE @@ -483,7 +483,7 @@ namespace Scratch.Services { } catch (Error e) { // We don't need to send an error message at cancellation (corresponding to error code 19) if (e.code != 19) { - warning ("Cannot save \"%s\": %s", get_basename (), e.message); + warning ("Cannot save “%s”: %s", get_basename (), e.message); } return false; } @@ -497,7 +497,7 @@ namespace Scratch.Services { this.set_saved_status (true); last_save_content = source_view.buffer.text; - debug ("File \"%s\" saved successfully", get_basename ()); + debug ("File “%s” saved successfully", get_basename ()); return true; } @@ -732,7 +732,7 @@ namespace Scratch.Services { // Show an error view which says "Hey, I cannot read that file!" private void show_default_load_error_view (string invalid_content = "") { - var title = _("Cannot read text in file \"%s\"").printf (get_basename ()); + var title = _("Cannot read text in file “%s”").printf (get_basename ()); string description; if (invalid_content == "") { description = _("You may not have permission to read the file."); @@ -772,7 +772,7 @@ namespace Scratch.Services { if (!exists ()) { if (mounted == false) { string message = _( - "The location containing the file \"%s\" was unmounted. Do you want to save somewhere else?" + "The location containing the file “%s” was unmounted. Do you want to save somewhere else?" ).printf ("%s".printf (get_basename ())); set_message (Gtk.MessageType.WARNING, message, _("Save As…"), () => { @@ -781,7 +781,7 @@ namespace Scratch.Services { }); } else { string message = _( - "File \"%s\" was deleted. Do you want to save it anyway?" + "File “%s” was deleted. Do you want to save it anyway?" ).printf ("%s".printf (get_basename ())); set_message (Gtk.MessageType.WARNING, message, _("Save"), () => { @@ -798,7 +798,7 @@ namespace Scratch.Services { // If the file can't be written if (!can_write ()) { string message = _( - "You cannot save changes to the file \"%s\". Do you want to save the changes somewhere else?" + "You cannot save changes to the file “%s”. Do you want to save the changes somewhere else?" ).printf ("%s".printf (get_basename ())); set_message (Gtk.MessageType.WARNING, message, _("Save changes elsewhere"), () => { @@ -835,7 +835,7 @@ namespace Scratch.Services { source_view.set_text (new_buffer.text, false); } else { string message = _( - "File \"%s\" was modified by an external application. Do you want to load it again or continue your editing?" + "File “%s” was modified by an external application. Do you want to load it again or continue your editing?" ).printf ("%s".printf (get_basename ())); set_message (Gtk.MessageType.WARNING, message, _("Load"), () => { @@ -887,7 +887,7 @@ namespace Scratch.Services { try { file.copy (backup, FileCopyFlags.NONE); } catch (Error e) { - warning ("Cannot create backup copy for file \"%s\": %s", get_basename (), e.message); + warning ("Cannot create backup copy for file “%s”: %s", get_basename (), e.message); } } } @@ -912,7 +912,7 @@ namespace Scratch.Services { backup.delete (); debug ("Backup file deleted: %s", backup_file); } catch (Error e) { - warning ("Cannot delete backup for file \"%s\": %s", get_basename (), e.message); + warning ("Cannot delete backup for file “%s”: %s", get_basename (), e.message); } } @@ -925,7 +925,7 @@ namespace Scratch.Services { file.delete (); return true; } catch (Error e) { - warning ("Cannot delete temporary file \"%s\": %s", file.get_uri (), e.message); + warning ("Cannot delete temporary file “%s”: %s", file.get_uri (), e.message); } return false; From 83087da690334c511d22a1f4d8de56980fd371be Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 17 Mar 2023 19:41:41 +0000 Subject: [PATCH 5/5] Revise label text --- src/Services/Document.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/Document.vala b/src/Services/Document.vala index 5ab07cdda8..81854683e2 100644 --- a/src/Services/Document.vala +++ b/src/Services/Document.vala @@ -743,7 +743,7 @@ namespace Scratch.Services { // Lack of read permission results in empty content string. Do not give option to open // in new document in that case. if (invalid_content != "") { - alert_view.show_action (_("Convert to text and show in a new document")); + alert_view.show_action (_("Show Anyway")); alert_view.action_activated.connect (() => { main_stack.set_visible_child_name ("content"); Idle.add (() => {