[gnome-builder/wip/gtk4-port: 378/736] libide/editor: add basic save action




commit f15bbb9773790d7c17d46e30a537f48f3cab5ad7
Author: Christian Hergert <chergert redhat com>
Date:   Mon Apr 4 14:29:15 2022 -0700

    libide/editor: add basic save action
    
    Long term I think we might want to manage these shortcuts differently
    so that we can wrap actions in commands (with descriptions, shortcut
    themes, and more) but this is just basic wiring to get things started.

 src/libide/editor/ide-editor-page-actions.c | 85 +++++++++++++++++++++++++++++
 src/libide/editor/ide-editor-page-private.h |  5 +-
 src/libide/editor/ide-editor-page.c         |  4 ++
 src/libide/editor/meson.build               |  1 +
 4 files changed, 93 insertions(+), 2 deletions(-)
---
diff --git a/src/libide/editor/ide-editor-page-actions.c b/src/libide/editor/ide-editor-page-actions.c
new file mode 100644
index 000000000..da4e04bf2
--- /dev/null
+++ b/src/libide/editor/ide-editor-page-actions.c
@@ -0,0 +1,85 @@
+/* ide-editor-page-actions.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-page-actions"
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+
+#include "ide-editor-page-private.h"
+
+static void
+ide_editor_page_actions_save_cb (GObject      *object,
+                                 GAsyncResult *result,
+                                 gpointer      user_data)
+{
+  IdeBuffer *buffer = (IdeBuffer *)object;
+  g_autoptr(IdeEditorPage) self = user_data;
+  g_autoptr(GError) error = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_BUFFER (buffer));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (IDE_IS_EDITOR_PAGE (self));
+
+  if (!ide_buffer_save_file_finish (buffer, result, &error))
+    ide_page_report_error (IDE_PAGE (self),
+                           /* translators: %s is replaced with a technical error message */
+                           _("Failed to save file: %s"),
+                           error->message);
+
+  ide_page_set_progress (IDE_PAGE (self), NULL);
+
+  IDE_EXIT;
+}
+
+static void
+ide_editor_page_actions_save (GtkWidget  *widget,
+                              const char *action_name,
+                              GVariant   *param)
+{
+  IdeEditorPage *self = (IdeEditorPage *)widget;
+  g_autoptr(IdeNotification) notif = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_EDITOR_PAGE (self));
+
+  ide_buffer_save_file_async (self->buffer,
+                              NULL,
+                              NULL,
+                              &notif,
+                              ide_editor_page_actions_save_cb,
+                              g_object_ref (self));
+
+  ide_page_set_progress (IDE_PAGE (self), notif);
+
+  IDE_EXIT;
+}
+
+void
+_ide_editor_page_class_actions_init (IdeEditorPageClass *klass)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  gtk_widget_class_install_action (widget_class, "page.save", NULL, ide_editor_page_actions_save);
+}
diff --git a/src/libide/editor/ide-editor-page-private.h b/src/libide/editor/ide-editor-page-private.h
index 38a624b69..b7ec71431 100644
--- a/src/libide/editor/ide-editor-page-private.h
+++ b/src/libide/editor/ide-editor-page-private.h
@@ -42,7 +42,8 @@ struct _IdeEditorPage
   GtkRevealer       *map_revealer;
 };
 
-void _ide_editor_page_settings_init   (IdeEditorPage *self);
-void _ide_editor_page_settings_reload (IdeEditorPage *self);
+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);
 
 G_END_DECLS
diff --git a/src/libide/editor/ide-editor-page.c b/src/libide/editor/ide-editor-page.c
index 28b7c370e..446b06abf 100644
--- a/src/libide/editor/ide-editor-page.c
+++ b/src/libide/editor/ide-editor-page.c
@@ -209,6 +209,10 @@ ide_editor_page_class_init (IdeEditorPageClass *klass)
   gtk_widget_class_bind_template_child (widget_class, IdeEditorPage, scroller);
   gtk_widget_class_bind_template_child (widget_class, IdeEditorPage, view);
   gtk_widget_class_bind_template_callback (widget_class, ide_editor_page_focus_enter_cb);
+
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_s, GDK_CONTROL_MASK, "page.save", NULL);
+
+  _ide_editor_page_class_actions_init (klass);
 }
 
 static void
diff --git a/src/libide/editor/meson.build b/src/libide/editor/meson.build
index 5fbfb8ff7..f68ee0e29 100644
--- a/src/libide/editor/meson.build
+++ b/src/libide/editor/meson.build
@@ -34,6 +34,7 @@ libide_editor_public_sources = [
 
 libide_editor_private_sources = [
   'ide-editor-init.c',
+  'ide-editor-page-actions.c',
   'ide-editor-page-settings.c',
 ]
 


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