From 8d1956921deadc2736d2d3434351f01815f7ec83 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Mon, 29 Nov 2021 23:58:28 +0100 Subject: [PATCH] node-editor: Display errors When opening a file or pasting DND fails, display the error as the actual node. --- demos/node-editor/node-editor-window.c | 41 +++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/demos/node-editor/node-editor-window.c b/demos/node-editor/node-editor-window.c index 995d2969f6..84baae7809 100644 --- a/demos/node-editor/node-editor-window.c +++ b/demos/node-editor/node-editor-window.c @@ -336,12 +336,39 @@ text_view_query_tooltip_cb (GtkWidget *widget, } } +static gboolean +load_bytes (NodeEditorWindow *self, + GBytes *bytes); + +static void +load_error (NodeEditorWindow *self, + const char *error_message) +{ + PangoLayout *layout; + GtkSnapshot *snapshot; + GskRenderNode *node; + GBytes *bytes; + + layout = gtk_widget_create_pango_layout (GTK_WIDGET (self), error_message); + pango_layout_set_width (layout, 300 * PANGO_SCALE); + snapshot = gtk_snapshot_new (); + gtk_snapshot_append_layout (snapshot, layout, &(GdkRGBA) { 0.7, 0.13, 0.13, 1.0 }); + node = gtk_snapshot_free_to_node (snapshot); + bytes = gsk_render_node_serialize (node); + + load_bytes (self, bytes); + + gsk_render_node_unref (node); + g_object_unref (layout); +} + static gboolean load_bytes (NodeEditorWindow *self, GBytes *bytes) { if (!g_utf8_validate (g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), NULL)) { + load_error (self, "Invalid UTF-8"); g_bytes_unref (bytes); return FALSE; } @@ -359,11 +386,16 @@ static gboolean load_file_contents (NodeEditorWindow *self, GFile *file) { + GError *error = NULL; GBytes *bytes; - bytes = g_file_load_bytes (file, NULL, NULL, NULL); + bytes = g_file_load_bytes (file, NULL, NULL, &error); if (bytes == NULL) - return FALSE; + { + load_error (self, error->message); + g_clear_error (&error); + return FALSE; + } return load_bytes (self, bytes); } @@ -473,17 +505,18 @@ node_editor_window_load (NodeEditorWindow *self, { GError *error = NULL; + g_clear_object (&self->file_monitor); + if (!load_file_contents (self, file)) return FALSE; - g_clear_object (&self->file_monitor); self->file_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error); - if (error) { g_warning ("couldn't monitor file: %s", error->message); g_error_free (error); + g_clear_object (&self->file_monitor); } else {