[gnome-builder] plugins/ls: restore folder listings across project sessions



commit d13c8531cea4914e8a6af8d21dc7659969f8316c
Author: Christian Hergert <chergert redhat com>
Date:   Fri Sep 16 14:45:23 2022 -0700

    plugins/ls: restore folder listings across project sessions

 src/plugins/ls/gbp-ls-workspace-addin.c | 181 ++++++++++++++++++++++++++++++++
 src/plugins/ls/gbp-ls-workspace-addin.h |  31 ++++++
 src/plugins/ls/ls-plugin.c              |   5 +
 src/plugins/ls/ls.plugin                |   1 +
 src/plugins/ls/meson.build              |   1 +
 5 files changed, 219 insertions(+)
---
diff --git a/src/plugins/ls/gbp-ls-workspace-addin.c b/src/plugins/ls/gbp-ls-workspace-addin.c
new file mode 100644
index 000000000..132fc1a68
--- /dev/null
+++ b/src/plugins/ls/gbp-ls-workspace-addin.c
@@ -0,0 +1,181 @@
+/* gbp-ls-workspace-addin.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 "gbp-ls-workspace-addin"
+
+#include "config.h"
+
+#include "gbp-ls-page.h"
+#include "gbp-ls-workspace-addin.h"
+
+struct _GbpLsWorkspaceAddin
+{
+  GObject       parent_instance;
+  IdeWorkspace *workspace;
+};
+
+static void
+gbp_ls_workspace_addin_load (IdeWorkspaceAddin *addin,
+                             IdeWorkspace      *workspace)
+{
+  GbpLsWorkspaceAddin *self = (GbpLsWorkspaceAddin *)addin;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_LS_WORKSPACE_ADDIN (self));
+  g_assert (IDE_IS_WORKSPACE (workspace));
+
+  self->workspace = workspace;
+}
+
+static void
+gbp_ls_workspace_addin_unload (IdeWorkspaceAddin *addin,
+                               IdeWorkspace      *workspace)
+{
+  GbpLsWorkspaceAddin *self = (GbpLsWorkspaceAddin *)addin;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_LS_WORKSPACE_ADDIN (self));
+  g_assert (IDE_IS_WORKSPACE (workspace));
+
+  self->workspace = NULL;
+}
+
+static void
+gbp_ls_workspace_addin_save_session_page_cb (IdePage  *page,
+                                             gpointer  user_data)
+{
+  IdeSession *session = user_data;
+
+  g_assert (IDE_IS_PAGE (page));
+  g_assert (IDE_IS_SESSION (session));
+
+  if (GBP_IS_LS_PAGE (page))
+    {
+      g_autoptr(PanelPosition) position = ide_page_get_position (page);
+      g_autoptr(IdeSessionItem) item = ide_session_item_new ();
+      GFile *file = gbp_ls_page_get_directory (GBP_LS_PAGE (page));
+      IdeWorkspace *workspace = ide_widget_get_workspace (GTK_WIDGET (page));
+      const char *id = ide_workspace_get_id (workspace);
+      g_autofree char *uri = g_file_get_uri (file);
+
+      ide_session_item_set_module_name (item, "ls");
+      ide_session_item_set_type_hint (item, "GbpLsPage");
+      ide_session_item_set_workspace (item, id);
+      ide_session_item_set_position (item, position);
+      ide_session_item_set_metadata (item, "uri", "s", uri);
+
+      if (page == ide_workspace_get_most_recent_page (workspace))
+        ide_session_item_set_metadata (item, "has-focus", "b", TRUE);
+
+      ide_session_append (session, item);
+    }
+}
+
+static void
+gbp_ls_workspace_addin_save_session (IdeWorkspaceAddin *addin,
+                                     IdeSession        *session)
+{
+  GbpLsWorkspaceAddin *self = (GbpLsWorkspaceAddin *)addin;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_LS_WORKSPACE_ADDIN (self));
+  g_assert (IDE_IS_WORKSPACE (self->workspace));
+  g_assert (IDE_IS_SESSION (session));
+
+  ide_workspace_foreach_page (self->workspace,
+                              gbp_ls_workspace_addin_save_session_page_cb,
+                              session);
+}
+
+static void
+gbp_ls_workspace_addin_restore_page (GbpLsWorkspaceAddin *self,
+                                     IdeSessionItem      *item)
+{
+  g_autofree char *uri = NULL;
+  g_autoptr(GFile) file = NULL;
+  PanelPosition *position;
+  GtkWidget *page;
+  gboolean has_focus;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_LS_WORKSPACE_ADDIN (self));
+  g_assert (IDE_IS_SESSION_ITEM (item));
+
+  if (!(position = ide_session_item_get_position (item)) ||
+      !ide_session_item_get_metadata (item, "uri", "s", &uri))
+    return;
+
+  file = g_file_new_for_uri (uri);
+  page = gbp_ls_page_new ();
+  gbp_ls_page_set_directory (GBP_LS_PAGE (page), file);
+
+  ide_workspace_add_page (self->workspace, IDE_PAGE (page), position);
+
+  if (ide_session_item_get_metadata (item, "has-focus", "b", &has_focus) && has_focus)
+    {
+      panel_widget_raise (PANEL_WIDGET (page));
+      gtk_widget_grab_focus (GTK_WIDGET (page));
+    }
+}
+
+static void
+gbp_ls_workspace_addin_restore_session_item (IdeWorkspaceAddin *addin,
+                                             IdeSession        *session,
+                                             IdeSessionItem    *item)
+{
+  GbpLsWorkspaceAddin *self = (GbpLsWorkspaceAddin *)addin;
+  const char *type_hint;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_LS_WORKSPACE_ADDIN (self));
+  g_assert (IDE_IS_SESSION (session));
+  g_assert (IDE_IS_WORKSPACE (self->workspace));
+
+  type_hint = ide_session_item_get_type_hint (item);
+
+  if (ide_str_equal0 (type_hint, "GbpLsPage"))
+    gbp_ls_workspace_addin_restore_page (self, item);
+
+  IDE_EXIT;
+}
+
+static void
+workspace_addin_iface_init (IdeWorkspaceAddinInterface *iface)
+{
+  iface->load = gbp_ls_workspace_addin_load;
+  iface->unload = gbp_ls_workspace_addin_unload;
+  iface->save_session = gbp_ls_workspace_addin_save_session;
+  iface->restore_session_item = gbp_ls_workspace_addin_restore_session_item;
+}
+
+G_DEFINE_FINAL_TYPE_WITH_CODE (GbpLsWorkspaceAddin, gbp_ls_workspace_addin, G_TYPE_OBJECT,
+                               G_IMPLEMENT_INTERFACE (IDE_TYPE_WORKSPACE_ADDIN, workspace_addin_iface_init))
+
+static void
+gbp_ls_workspace_addin_class_init (GbpLsWorkspaceAddinClass *klass)
+{
+}
+
+static void
+gbp_ls_workspace_addin_init (GbpLsWorkspaceAddin *self)
+{
+}
diff --git a/src/plugins/ls/gbp-ls-workspace-addin.h b/src/plugins/ls/gbp-ls-workspace-addin.h
new file mode 100644
index 000000000..cf4e7dbbd
--- /dev/null
+++ b/src/plugins/ls/gbp-ls-workspace-addin.h
@@ -0,0 +1,31 @@
+/* gbp-ls-workspace-addin.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 <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_LS_WORKSPACE_ADDIN (gbp_ls_workspace_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpLsWorkspaceAddin, gbp_ls_workspace_addin, GBP, LS_WORKSPACE_ADDIN, GObject)
+
+G_END_DECLS
diff --git a/src/plugins/ls/ls-plugin.c b/src/plugins/ls/ls-plugin.c
index 1117ed0ba..1c52993ed 100644
--- a/src/plugins/ls/ls-plugin.c
+++ b/src/plugins/ls/ls-plugin.c
@@ -21,11 +21,13 @@
 #include "config.h"
 
 #include <libpeas/peas.h>
+
 #include <libide-editor.h>
 #include <libide-gui.h>
 
 #include "gbp-ls-editor-page-addin.h"
 #include "gbp-ls-workbench-addin.h"
+#include "gbp-ls-workspace-addin.h"
 
 _IDE_EXTERN void
 _gbp_ls_register_types (PeasObjectModule *module)
@@ -36,4 +38,7 @@ _gbp_ls_register_types (PeasObjectModule *module)
   peas_object_module_register_extension_type (module,
                                               IDE_TYPE_WORKBENCH_ADDIN,
                                               GBP_TYPE_LS_WORKBENCH_ADDIN);
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_WORKSPACE_ADDIN,
+                                              GBP_TYPE_LS_WORKSPACE_ADDIN);
 }
diff --git a/src/plugins/ls/ls.plugin b/src/plugins/ls/ls.plugin
index 37db84d12..b47b97528 100644
--- a/src/plugins/ls/ls.plugin
+++ b/src/plugins/ls/ls.plugin
@@ -8,3 +8,4 @@ Embedded=_gbp_ls_register_types
 Hidden=true
 Module=ls
 Name=View Directory Listings
+X-Workspace-Kind=editor;primary;
diff --git a/src/plugins/ls/meson.build b/src/plugins/ls/meson.build
index e74df7ebe..f1a866a99 100644
--- a/src/plugins/ls/meson.build
+++ b/src/plugins/ls/meson.build
@@ -3,6 +3,7 @@ plugins_sources += files([
   'gbp-ls-page.c',
   'gbp-ls-editor-page-addin.c',
   'gbp-ls-workbench-addin.c',
+  'gbp-ls-workspace-addin.c',
   'gbp-ls-tree-view.c',
   'ls-plugin.c',
 ])


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