[gnome-builder/wip/gtk4-port: 855/1774] libide/gui: stub out shortcut infrastructure




commit 0219544dcf40a52c9e7f4d32fc2c043e6bc0b1f9
Author: Christian Hergert <chergert redhat com>
Date:   Mon May 2 21:24:46 2022 -0700

    libide/gui: stub out shortcut infrastructure
    
    This just gets some machinery in place, but doesn't do anything yet.

 src/libide/gui/ide-shortcut-controller-private.h |  29 ++++
 src/libide/gui/ide-shortcut-controller.c         |  62 ++++++++
 src/libide/gui/ide-shortcut-model-private.h      |  41 +++++
 src/libide/gui/ide-shortcut-model.c              | 187 +++++++++++++++++++++++
 src/libide/gui/ide-shortcut-private.h            |  31 ++++
 src/libide/gui/ide-shortcut.c                    |  92 +++++++++++
 src/libide/gui/ide-workspace.c                   |   6 +
 src/libide/gui/meson.build                       |   6 +
 8 files changed, 454 insertions(+)
---
diff --git a/src/libide/gui/ide-shortcut-controller-private.h 
b/src/libide/gui/ide-shortcut-controller-private.h
new file mode 100644
index 000000000..9215dff23
--- /dev/null
+++ b/src/libide/gui/ide-shortcut-controller-private.h
@@ -0,0 +1,29 @@
+/* ide-shortcut-controller-private.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 <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+GtkEventController *ide_shortcut_controller_new_for_window (GtkWindow *window);
+
+G_END_DECLS
diff --git a/src/libide/gui/ide-shortcut-controller.c b/src/libide/gui/ide-shortcut-controller.c
new file mode 100644
index 000000000..c355ffb72
--- /dev/null
+++ b/src/libide/gui/ide-shortcut-controller.c
@@ -0,0 +1,62 @@
+/* ide-shortcut-controller.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-shortcut-controller"
+
+#include "config.h"
+
+#include "ide-gui-global.h"
+#include "ide-shortcut-controller-private.h"
+#include "ide-shortcut-model-private.h"
+
+static gboolean
+has_context_property (GObject *object)
+{
+  GParamSpec *pspec;
+
+  if (object == NULL)
+    return FALSE;
+
+  if (!(pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), "context")))
+    return FALSE;
+
+  return G_IS_PARAM_SPEC_OBJECT (pspec) && g_type_is_a (pspec->value_type, IDE_TYPE_CONTEXT);
+}
+
+
+GtkEventController *
+ide_shortcut_controller_new_for_window (GtkWindow *window)
+{
+  g_autoptr(IdeShortcutModel) model = NULL;
+  IdeContext *context;
+
+  g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
+
+  model = ide_shortcut_model_new ();
+  context = ide_widget_get_context (GTK_WIDGET (window));
+
+  if (has_context_property (G_OBJECT (window)))
+    g_object_bind_property (window, "context", model, "context", 0);
+
+  if (context != NULL)
+    ide_shortcut_model_set_context (model, context);
+
+  return gtk_shortcut_controller_new_for_model (G_LIST_MODEL (model));
+}
diff --git a/src/libide/gui/ide-shortcut-model-private.h b/src/libide/gui/ide-shortcut-model-private.h
new file mode 100644
index 000000000..7fa153223
--- /dev/null
+++ b/src/libide/gui/ide-shortcut-model-private.h
@@ -0,0 +1,41 @@
+/* ide-shortcut-model-private.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 <libide-core.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_SHORTCUT_MODEL (ide_shortcut_model_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeShortcutModel, ide_shortcut_model, IDE, SHORTCUT_MODEL, GObject)
+
+IdeShortcutModel *ide_shortcut_model_new          (void);
+IdeContext       *ide_shortcut_model_get_context  (IdeShortcutModel *self);
+void              ide_shortcut_model_set_context  (IdeShortcutModel *self,
+                                                   IdeContext       *context);
+const char       *ide_shortcut_model_get_variable (IdeShortcutModel *self,
+                                                   const char       *name);
+void              ide_shortcut_model_set_variable (IdeShortcutModel *self,
+                                                   const char       *name,
+                                                   const char       *value);
+
+G_END_DECLS
diff --git a/src/libide/gui/ide-shortcut-model.c b/src/libide/gui/ide-shortcut-model.c
new file mode 100644
index 000000000..d4960e131
--- /dev/null
+++ b/src/libide/gui/ide-shortcut-model.c
@@ -0,0 +1,187 @@
+/* ide-shortcut-model.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-shortcut-model"
+
+#include "config.h"
+
+#include <gtk/gtk.h>
+
+#include "ide-shortcut-model-private.h"
+
+struct _IdeShortcutModel
+{
+  GObject     parent_instance;
+  IdeContext *context;
+};
+
+enum {
+  PROP_0,
+  PROP_CONTEXT,
+  N_PROPS
+};
+
+static guint
+ide_shortcut_model_get_n_items (GListModel *model)
+{
+  return 0;
+}
+
+static gpointer
+ide_shortcut_model_get_item (GListModel *model,
+                             guint       position)
+{
+  return NULL;
+}
+
+static GType
+ide_shortcut_model_get_item_type (GListModel *model)
+{
+  return GTK_TYPE_SHORTCUT;
+}
+
+static void
+list_model_iface_init (GListModelInterface *iface)
+{
+  iface->get_n_items = ide_shortcut_model_get_n_items;
+  iface->get_item = ide_shortcut_model_get_item;
+  iface->get_item_type = ide_shortcut_model_get_item_type;
+}
+
+G_DEFINE_FINAL_TYPE_WITH_CODE (IdeShortcutModel, ide_shortcut_model, G_TYPE_OBJECT,
+                               G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+ide_shortcut_model_finalize (GObject *object)
+{
+  IdeShortcutModel *self = (IdeShortcutModel *)object;
+
+  g_clear_object (&self->context);
+
+  G_OBJECT_CLASS (ide_shortcut_model_parent_class)->finalize (object);
+}
+
+static void
+ide_shortcut_model_get_property (GObject    *object,
+                                 guint       prop_id,
+                                 GValue     *value,
+                                 GParamSpec *pspec)
+{
+  IdeShortcutModel *self = IDE_SHORTCUT_MODEL (object);
+
+  switch (prop_id)
+    {
+    case PROP_CONTEXT:
+      g_value_set_object (value, ide_shortcut_model_get_context (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_shortcut_model_set_property (GObject      *object,
+                                 guint         prop_id,
+                                 const GValue *value,
+                                 GParamSpec   *pspec)
+{
+  IdeShortcutModel *self = IDE_SHORTCUT_MODEL (object);
+
+  switch (prop_id)
+    {
+    case PROP_CONTEXT:
+      ide_shortcut_model_set_context (self, g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_shortcut_model_class_init (IdeShortcutModelClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = ide_shortcut_model_finalize;
+  object_class->get_property = ide_shortcut_model_get_property;
+  object_class->set_property = ide_shortcut_model_set_property;
+
+  properties [PROP_CONTEXT] =
+    g_param_spec_object ("context",
+                         "Context",
+                         "The IdeContext if any",
+                         IDE_TYPE_CONTEXT,
+                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_shortcut_model_init (IdeShortcutModel *self)
+{
+}
+
+/**
+ * ide_shortcut_model_get_context:
+ * @self: a #IdeShortcutModel
+ *
+ * Gets the #IdeContext associated with a model, if any.
+ *
+ * Returns: (transfer none): an #IdeContext or %NULL
+ */
+IdeContext *
+ide_shortcut_model_get_context (IdeShortcutModel *self)
+{
+  g_return_val_if_fail (IDE_IS_SHORTCUT_MODEL (self), NULL);
+
+  return self->context;
+}
+
+/**
+ * ide_shortcut_model_set_context:
+ * @self: a #IdeShortcutModel
+ * @context: (nullable): an #IdeContext or %NULL
+ *
+ * Sets the context for a shortcut model, if any.
+ *
+ * Setting the context for the model will cause custom shortcuts to be loaded
+ * that have been loaded for the project.
+ */
+void
+ide_shortcut_model_set_context (IdeShortcutModel *self,
+                                IdeContext       *context)
+{
+  g_return_if_fail (IDE_IS_SHORTCUT_MODEL (self));
+  g_return_if_fail (!context || IDE_IS_CONTEXT (context));
+
+  /* TODO: Load context-specific shortcuts */
+  if (g_set_object (&self->context, context))
+    g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CONTEXT]);
+}
+
+IdeShortcutModel *
+ide_shortcut_model_new (void)
+{
+  return g_object_new (IDE_TYPE_SHORTCUT_MODEL, NULL);
+}
diff --git a/src/libide/gui/ide-shortcut-private.h b/src/libide/gui/ide-shortcut-private.h
new file mode 100644
index 000000000..c22d0330d
--- /dev/null
+++ b/src/libide/gui/ide-shortcut-private.h
@@ -0,0 +1,31 @@
+/* ide-shortcut-private.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 <libide-core.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_SHORTCUT (ide_shortcut_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeShortcut, ide_shortcut, IDE, SHORTCUT, GObject)
+
+G_END_DECLS
diff --git a/src/libide/gui/ide-shortcut.c b/src/libide/gui/ide-shortcut.c
new file mode 100644
index 000000000..30aa9f793
--- /dev/null
+++ b/src/libide/gui/ide-shortcut.c
@@ -0,0 +1,92 @@
+/* ide-shortcut.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-shortcut"
+
+#include "config.h"
+
+#include "ide-shortcut-private.h"
+
+struct _IdeShortcut
+{
+  GObject parent_instance;
+};
+
+enum {
+  PROP_0,
+  N_PROPS
+};
+
+G_DEFINE_FINAL_TYPE (IdeShortcut, ide_shortcut, G_TYPE_OBJECT)
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+ide_shortcut_dispose (GObject *object)
+{
+  IdeShortcut *self = (IdeShortcut *)object;
+
+  G_OBJECT_CLASS (ide_shortcut_parent_class)->dispose (object);
+}
+
+static void
+ide_shortcut_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
+{
+  IdeShortcut *self = IDE_SHORTCUT (object);
+
+  switch (prop_id)
+    {
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_shortcut_set_property (GObject      *object,
+                           guint         prop_id,
+                           const GValue *value,
+                           GParamSpec   *pspec)
+{
+  IdeShortcut *self = IDE_SHORTCUT (object);
+
+  switch (prop_id)
+    {
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_shortcut_class_init (IdeShortcutClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = ide_shortcut_dispose;
+  object_class->get_property = ide_shortcut_get_property;
+  object_class->set_property = ide_shortcut_set_property;
+}
+
+static void
+ide_shortcut_init (IdeShortcut *self)
+{
+}
diff --git a/src/libide/gui/ide-workspace.c b/src/libide/gui/ide-workspace.c
index 02bbdca83..2c998a6c8 100644
--- a/src/libide/gui/ide-workspace.c
+++ b/src/libide/gui/ide-workspace.c
@@ -27,6 +27,7 @@
 
 #include "ide-gui-global.h"
 #include "ide-search-popover-private.h"
+#include "ide-shortcut-controller-private.h"
 #include "ide-workspace-addin.h"
 #include "ide-workspace-private.h"
 #include "ide-workbench-private.h"
@@ -564,6 +565,7 @@ static void
 ide_workspace_init (IdeWorkspace *self)
 {
   IdeWorkspacePrivate *priv = ide_workspace_get_instance_private (self);
+  GtkEventController *shortcuts;
   g_autofree gchar *app_id = NULL;
 
 #ifdef DEVELOPMENT_BUILD
@@ -598,6 +600,10 @@ ide_workspace_init (IdeWorkspace *self)
 
   /* Initialize GActions for workspace */
   _ide_workspace_init_actions (self);
+
+  /* Initialize shortcuts for the window */
+  shortcuts = ide_shortcut_controller_new_for_window (GTK_WINDOW (self));
+  gtk_widget_add_controller (GTK_WIDGET (self), shortcuts);
 }
 
 GList *
diff --git a/src/libide/gui/meson.build b/src/libide/gui/meson.build
index 3dc9822b6..514018cde 100644
--- a/src/libide/gui/meson.build
+++ b/src/libide/gui/meson.build
@@ -54,6 +54,9 @@ libide_gui_private_headers = [
   'ide-recoloring-private.h',
   'ide-search-popover-private.h',
   'ide-session-private.h',
+  'ide-shortcut-private.h',
+  'ide-shortcut-model-private.h',
+  'ide-shortcut-controller-private.h',
   'ide-style-variant-preview-private.h',
 ]
 
@@ -71,6 +74,9 @@ libide_gui_private_sources = [
   'ide-recoloring.c',
   'ide-search-popover.c',
   'ide-session.c',
+  'ide-shortcut.c',
+  'ide-shortcut-model.c',
+  'ide-shortcut-controller.c',
   'ide-style-variant-preview.c',
   'ide-workspace-actions.c',
 ]


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