[gnome-builder/wip/gtk4-port] libide/gui: add script for keybindings to reuse



commit 396112c0fbc4e054a4654701b1258970495be3d8
Author: Christian Hergert <chergert redhat com>
Date:   Fri May 6 11:04:15 2022 -0700

    libide/gui: add script for keybindings to reuse
    
    The goal here is to keep our keybindings.json file a bit more succinct.

 src/libide/gui/gtk/keybindings.gsl      | 42 +++++++++++++++++++++++++++++++++
 src/libide/gui/ide-shortcut-bundle.c    | 35 ++++++++++-----------------
 src/libide/gui/libide-gui.gresource.xml |  1 +
 3 files changed, 55 insertions(+), 23 deletions(-)
---
diff --git a/src/libide/gui/gtk/keybindings.gsl b/src/libide/gui/gtk/keybindings.gsl
new file mode 100644
index 000000000..0f88b442c
--- /dev/null
+++ b/src/libide/gui/gtk/keybindings.gsl
@@ -0,0 +1,42 @@
+require Ide
+
+require Adw       version "1"
+require GLib      version "2.0"
+require Gdk       version "4.0"
+require Gio       version "2.0"
+require Gsk       version "4.0"
+require Gtk       version "4.0"
+require GtkSource version "5"
+require Json      version "1.0"
+require Jsonrpc   version "1.0"
+require Panel     version "1"
+require Template  version "1.0"
+require Vte       version "3.91"
+
+def inEditor()
+  focus != null && typeof(focus).is_a(typeof(Ide.SourceView))
+end
+
+def inTerminal()
+  focus != null && typeof(focus).is_a(typeof(Ide.Terminal))
+end
+
+def inGrid()
+  focus != null && (focus.get_ancestor(typeof(Ide.Grid)) != null)
+end
+
+def hasProject()
+  workbench != null && workbench.has_project()
+end
+
+def isBuilding()
+  workbench != null && workbench.has_project() && Ide.BuildManager.from_context(workbench.context).get_busy()
+end
+
+def isRunning()
+  workbench != null && workbench.has_project() && Ide.RunManager.from_context(workbench.context).get_busy()
+end
+
+def isDebugging()
+  workbench != null && workbench.has_project() && 
Ide.DebugManager.from_context(workbench.context).get_active()
+end
diff --git a/src/libide/gui/ide-shortcut-bundle.c b/src/libide/gui/ide-shortcut-bundle.c
index 89f54981a..10690d51e 100644
--- a/src/libide/gui/ide-shortcut-bundle.c
+++ b/src/libide/gui/ide-shortcut-bundle.c
@@ -27,6 +27,7 @@
 #include <tmpl-glib.h>
 
 #include "ide-gui-global.h"
+#include "ide-gui-resources.h"
 #include "ide-shortcut-bundle-private.h"
 #include "ide-workbench.h"
 #include "ide-workspace.h"
@@ -46,23 +47,6 @@ typedef struct
 } IdeShortcut;
 
 static TmplScope *imports_scope;
-static const struct {
-  const char *name;
-  const char *version;
-} imports[] = {
-  { "Adw", "1" },
-  { "GLib", "2.0" },
-  { "Gdk", "4.0" },
-  { "Gio", "2.0" },
-  { "Gsk", "4.0" },
-  { "Gtk", "4.0" },
-  { "GtkSource", "5" },
-  { "Ide", PACKAGE_ABI_S },
-  { "Json", "1.0" },
-  { "Jsonrpc", "1.0" },
-  { "Template", "1.0" },
-  { "Vte", "3.91" },
-};
 
 static IdeShortcut *
 ide_shortcut_new (const char          *action,
@@ -221,6 +205,8 @@ ide_shortcut_bundle_class_init (IdeShortcutBundleClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
   object_class->dispose = ide_shortcut_bundle_dispose;
+
+  g_resources_register (ide_gui_get_resource ());
 }
 
 static void
@@ -228,14 +214,17 @@ ide_shortcut_bundle_init (IdeShortcutBundle *self)
 {
   if (g_once_init_enter (&imports_scope))
     {
+      g_autoptr(GBytes) bytes = g_resources_lookup_data ("/org/gnome/libide-gui/gtk/keybindings.gsl", 0, 
NULL);
+      const char *str = (const char *)g_bytes_get_data (bytes, NULL);
+      g_autoptr(TmplExpr) expr = NULL;
+      g_autoptr(GError) error = NULL;
+      g_auto(GValue) return_value = G_VALUE_INIT;
       TmplScope *scope = tmpl_scope_new ();
 
-      for (guint i = 0; i < G_N_ELEMENTS (imports); i++)
-        {
-          if (!tmpl_scope_require (scope, imports[i].name, imports[i].version))
-            g_warning ("Failed to import %s@%s into template scope",
-                       imports[i].name, imports[i].version);
-        }
+      if (!(expr = tmpl_expr_from_string (str, &error)))
+        g_critical ("Failed to parse keybindings.gsl: %s", error->message);
+      else if (!tmpl_expr_eval (expr, scope, &return_value, &error))
+        g_critical ("Failed to eval keybindings.gsl: %s", error->message);
 
       g_once_init_leave (&imports_scope, scope);
     }
diff --git a/src/libide/gui/libide-gui.gresource.xml b/src/libide/gui/libide-gui.gresource.xml
index 76756b3b5..faa3a83bf 100644
--- a/src/libide/gui/libide-gui.gresource.xml
+++ b/src/libide/gui/libide-gui.gresource.xml
@@ -5,6 +5,7 @@
   </gresource>
   <gresource prefix="/org/gnome/libide-gui">
     <file preprocess="xml-stripblanks">gtk/menus.ui</file>
+    <file>gtk/keybindings.gsl</file>
     <file>gtk/keybindings.json</file>
     <file>images/style-preview-dark.png</file>
     <file>images/style-preview-default.png</file>


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