[gnome-builder] libide/editor: use save delegate for editor pages



commit 04008ab16681fc1962b39eb03bf148a1e76a9eed
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jul 27 23:01:56 2022 -0700

    libide/editor: use save delegate for editor pages

 src/libide/editor/gtk/menus.ui               |   4 +-
 src/libide/editor/ide-editor-page-actions.c  |  85 -----------
 src/libide/editor/ide-editor-page-private.h  |   1 -
 src/libide/editor/ide-editor-page.c          |  16 ++-
 src/libide/editor/ide-editor-save-delegate.c | 208 +++++++++++++++++++++++++++
 src/libide/editor/ide-editor-save-delegate.h |  35 +++++
 src/libide/editor/meson.build                |   3 +-
 7 files changed, 261 insertions(+), 91 deletions(-)
---
diff --git a/src/libide/editor/gtk/menus.ui b/src/libide/editor/gtk/menus.ui
index 78062101a..f68fc92ef 100644
--- a/src/libide/editor/gtk/menus.ui
+++ b/src/libide/editor/gtk/menus.ui
@@ -64,12 +64,12 @@
     <section id="ide-editor-page-save-section">
       <item>
         <attribute name="label" translatable="yes">Save</attribute>
-        <attribute name="action">page.editor.save</attribute>
+        <attribute name="action">page.save</attribute>
         <attribute name="accel">&lt;ctrl&gt;s</attribute>
       </item>
       <item>
         <attribute name="label" translatable="yes">Save _As</attribute>
-        <attribute name="action">page.editor.save-as</attribute>
+        <attribute name="action">page.save-as</attribute>
         <attribute name="accel">&lt;ctrl&gt;&lt;shift&gt;s</attribute>
       </item>
     </section>
diff --git a/src/libide/editor/ide-editor-page-private.h b/src/libide/editor/ide-editor-page-private.h
index b9233a989..8130658ac 100644
--- a/src/libide/editor/ide-editor-page-private.h
+++ b/src/libide/editor/ide-editor-page-private.h
@@ -51,7 +51,6 @@ struct _IdeEditorPage
   guint                    completion_blocked : 1;
 };
 
-void _ide_editor_page_class_actions_init         (IdeEditorPageClass *klass);
 void _ide_editor_page_settings_init              (IdeEditorPage      *self);
 void _ide_editor_page_settings_reload            (IdeEditorPage      *self);
 void _ide_editor_page_settings_connect_gutter    (IdeEditorPage      *self,
diff --git a/src/libide/editor/ide-editor-page.c b/src/libide/editor/ide-editor-page.c
index 8781d49b2..dab4bcdf7 100644
--- a/src/libide/editor/ide-editor-page.c
+++ b/src/libide/editor/ide-editor-page.c
@@ -29,6 +29,7 @@
 
 #include "ide-editor-page-addin.h"
 #include "ide-editor-page-private.h"
+#include "ide-editor-save-delegate.h"
 
 enum {
   PROP_0,
@@ -371,6 +372,18 @@ search_begin_replace_action (GtkWidget  *widget,
   set_search_visible (IDE_EDITOR_PAGE (widget), TRUE, IDE_EDITOR_SEARCH_BAR_MODE_REPLACE);
 }
 
+static void
+ide_editor_page_constructed (GObject *object)
+{
+  IdeEditorPage *self = (IdeEditorPage *)object;
+  g_autoptr(PanelSaveDelegate) save_delegate = NULL;
+
+  G_OBJECT_CLASS (ide_editor_page_parent_class)->constructed (object);
+
+  save_delegate = ide_editor_save_delegate_new (self);
+  panel_widget_set_save_delegate (PANEL_WIDGET (self), save_delegate);
+}
+
 static void
 ide_editor_page_dispose (GObject *object)
 {
@@ -450,6 +463,7 @@ ide_editor_page_class_init (IdeEditorPageClass *klass)
   IdePageClass *page_class = IDE_PAGE_CLASS (klass);
   PanelWidgetClass *panel_widget_class = PANEL_WIDGET_CLASS (widget_class);
 
+  object_class->constructed = ide_editor_page_constructed;
   object_class->dispose = ide_editor_page_dispose;
   object_class->get_property = ide_editor_page_get_property;
   object_class->set_property = ide_editor_page_set_property;
@@ -513,8 +527,6 @@ ide_editor_page_class_init (IdeEditorPageClass *klass)
   panel_widget_class_install_action (panel_widget_class, "search.begin-find", NULL, 
search_begin_find_action);
   panel_widget_class_install_action (panel_widget_class, "search.begin-replace", NULL, 
search_begin_replace_action);
 
-  _ide_editor_page_class_actions_init (klass);
-
   g_type_ensure (IDE_TYPE_EDITOR_SEARCH_BAR);
 }
 
diff --git a/src/libide/editor/ide-editor-save-delegate.c b/src/libide/editor/ide-editor-save-delegate.c
new file mode 100644
index 000000000..772770a05
--- /dev/null
+++ b/src/libide/editor/ide-editor-save-delegate.c
@@ -0,0 +1,208 @@
+/* ide-editor-save-delegate.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "ide-editor-save-delegate"
+
+#include "config.h"
+
+#include <libide-threading.h>
+
+#include "ide-editor-save-delegate.h"
+
+struct _IdeEditorSaveDelegate
+{
+  PanelSaveDelegate  parent_instance;
+  IdeBuffer         *buffer;
+};
+
+enum {
+  PROP_0,
+  PROP_BUFFER,
+  N_PROPS
+};
+
+G_DEFINE_FINAL_TYPE (IdeEditorSaveDelegate, ide_editor_save_delegate, PANEL_TYPE_SAVE_DELEGATE)
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+ide_editor_save_delegate_save_cb (GObject      *object,
+                                  GAsyncResult *result,
+                                  gpointer      user_data)
+{
+  IdeBuffer *buffer = (IdeBuffer *)object;
+  g_autoptr(IdeTask) task = user_data;
+  g_autoptr(GError) error = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_BUFFER (buffer));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (IDE_IS_TASK (task));
+
+  if (!ide_buffer_save_file_finish (buffer, result, &error))
+    ide_task_return_error (task, g_steal_pointer (&error));
+  else
+    ide_task_return_boolean (task, TRUE);
+
+  IDE_EXIT;
+}
+
+static void
+ide_editor_save_delegate_save_async (PanelSaveDelegate   *delegate,
+                                     GCancellable        *cancellable,
+                                     GAsyncReadyCallback  callback,
+                                     gpointer             user_data)
+{
+  IdeEditorSaveDelegate *self = (IdeEditorSaveDelegate *)delegate;
+  g_autoptr(IdeNotification) notif = NULL;
+  g_autoptr(IdeTask) task = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_EDITOR_SAVE_DELEGATE (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = ide_task_new (self, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, ide_editor_save_delegate_save_async);
+
+  ide_buffer_save_file_async (self->buffer,
+                              NULL,
+                              NULL,
+                              &notif,
+                              ide_editor_save_delegate_save_cb,
+                              g_steal_pointer (&task));
+
+  g_object_bind_property (notif, "progress", self, "progress", G_BINDING_SYNC_CREATE);
+
+  IDE_EXIT;
+}
+
+static gboolean
+ide_editor_save_delegate_save_finish (PanelSaveDelegate  *delegate,
+                                      GAsyncResult       *result,
+                                      GError            **error)
+{
+  gboolean ret;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_EDITOR_SAVE_DELEGATE (delegate));
+  g_assert (IDE_IS_TASK (result));
+
+  ret = ide_task_propagate_boolean (IDE_TASK (result), error);
+
+  IDE_RETURN (ret);
+}
+
+static void
+ide_editor_save_delegate_dispose (GObject *object)
+{
+  IdeEditorSaveDelegate *self = (IdeEditorSaveDelegate *)object;
+
+  g_clear_object (&self->buffer);
+
+  G_OBJECT_CLASS (ide_editor_save_delegate_parent_class)->dispose (object);
+}
+
+static void
+ide_editor_save_delegate_get_property (GObject    *object,
+                                       guint       prop_id,
+                                       GValue     *value,
+                                       GParamSpec *pspec)
+{
+  IdeEditorSaveDelegate *self = IDE_EDITOR_SAVE_DELEGATE (object);
+
+  switch (prop_id)
+    {
+    case PROP_BUFFER:
+      g_value_set_object (value, self->buffer);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_editor_save_delegate_set_property (GObject      *object,
+                                       guint         prop_id,
+                                       const GValue *value,
+                                       GParamSpec   *pspec)
+{
+  IdeEditorSaveDelegate *self = IDE_EDITOR_SAVE_DELEGATE (object);
+
+  switch (prop_id)
+    {
+    case PROP_BUFFER:
+      self->buffer = g_value_dup_object (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_editor_save_delegate_class_init (IdeEditorSaveDelegateClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  PanelSaveDelegateClass *save_delegate_class = PANEL_SAVE_DELEGATE_CLASS (klass);
+
+  object_class->dispose = ide_editor_save_delegate_dispose;
+  object_class->get_property = ide_editor_save_delegate_get_property;
+  object_class->set_property = ide_editor_save_delegate_set_property;
+
+  save_delegate_class->save_async = ide_editor_save_delegate_save_async;
+  save_delegate_class->save_finish = ide_editor_save_delegate_save_finish;
+
+  properties[PROP_BUFFER] =
+    g_param_spec_object ("buffer", NULL, NULL,
+                         IDE_TYPE_BUFFER,
+                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_editor_save_delegate_init (IdeEditorSaveDelegate *self)
+{
+}
+
+PanelSaveDelegate *
+ide_editor_save_delegate_new (IdeEditorPage *page)
+{
+  IdeEditorSaveDelegate *ret;
+  IdeBuffer *buffer;
+
+  g_return_val_if_fail (IDE_IS_EDITOR_PAGE (page), NULL);
+
+  buffer = ide_editor_page_get_buffer (page);
+
+  ret = g_object_new (IDE_TYPE_EDITOR_SAVE_DELEGATE,
+                      "buffer", buffer,
+                      NULL);
+
+  g_object_bind_property (page, "title", ret, "title", G_BINDING_SYNC_CREATE);
+  g_object_bind_property (page, "icon", ret, "icon", G_BINDING_SYNC_CREATE);
+
+  return PANEL_SAVE_DELEGATE (ret);
+}
diff --git a/src/libide/editor/ide-editor-save-delegate.h b/src/libide/editor/ide-editor-save-delegate.h
new file mode 100644
index 000000000..b41e2f887
--- /dev/null
+++ b/src/libide/editor/ide-editor-save-delegate.h
@@ -0,0 +1,35 @@
+/* ide-editor-save-delegate.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <libpanel.h>
+
+#include "ide-editor-page.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_EDITOR_SAVE_DELEGATE (ide_editor_save_delegate_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeEditorSaveDelegate, ide_editor_save_delegate, IDE, EDITOR_SAVE_DELEGATE, 
PanelSaveDelegate)
+
+PanelSaveDelegate *ide_editor_save_delegate_new (IdeEditorPage *page);
+
+G_END_DECLS
diff --git a/src/libide/editor/meson.build b/src/libide/editor/meson.build
index c1105c696..f0f1959a0 100644
--- a/src/libide/editor/meson.build
+++ b/src/libide/editor/meson.build
@@ -20,6 +20,7 @@ libide_editor_public_headers = [
 ]
 
 libide_editor_private_headers = [
+  'ide-editor-save-delegate.h',
   'ide-editor-page-private.h',
 ]
 
@@ -40,8 +41,8 @@ libide_editor_public_sources = [
 
 libide_editor_private_sources = [
   'ide-editor-init.c',
-  'ide-editor-page-actions.c',
   'ide-editor-page-settings.c',
+  'ide-editor-save-delegate.c',
   'ide-editor-search-bar.c',
 ]
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]