[gnome-builder/wip/gtk4-port: 383/736] plugins/editorui: add text preview subclass widget




commit 508163953bc114ccc2a786662c10b9dda7db00b1
Author: Christian Hergert <chergert redhat com>
Date:   Mon Apr 4 15:39:49 2022 -0700

    plugins/editorui: add text preview subclass widget
    
    This will allow us to bind things and keep all of that private to just
    the preview widget, rather than having that mess all over the place.

 src/libide/gui/style.css                           |   6 -
 .../editorui/gbp-editorui-preferences-addin.c      |  28 +----
 src/plugins/editorui/gbp-editorui-preview.c        | 139 +++++++++++++++++++++
 src/plugins/editorui/gbp-editorui-preview.h        |  33 +++++
 src/plugins/editorui/meson.build                   |   1 +
 5 files changed, 176 insertions(+), 31 deletions(-)
---
diff --git a/src/libide/gui/style.css b/src/libide/gui/style.css
index 67247f929..81c6c3af5 100644
--- a/src/libide/gui/style.css
+++ b/src/libide/gui/style.css
@@ -34,9 +34,3 @@ stylevariantpreview widget.header.dark {
 stylevariantpreview widget.header.light {
   background: #cccccc;
 }
-frame.text-preview,
-GtkSourceStyleSchemePreview frame {
-  box-shadow: 0 0 0 1px rgba(0,0,0,.03),
-              0 1px 3px 1px rgba(0,0,0,.07),
-              0 2px 6px 2px rgba(0,0,0,.03);
-}
diff --git a/src/plugins/editorui/gbp-editorui-preferences-addin.c 
b/src/plugins/editorui/gbp-editorui-preferences-addin.c
index 113578135..a9156eeea 100644
--- a/src/plugins/editorui/gbp-editorui-preferences-addin.c
+++ b/src/plugins/editorui/gbp-editorui-preferences-addin.c
@@ -25,6 +25,7 @@
 #include <glib/gi18n.h>
 
 #include "gbp-editorui-preferences-addin.h"
+#include "gbp-editorui-preview.h"
 
 struct _GbpEditoruiPreferencesAddin
 {
@@ -155,44 +156,21 @@ ide_preferences_builtin_add_schemes (const char                   *page_name,
                                      gpointer                      user_data)
 {
   IdePreferencesWindow *window = user_data;
-  g_autoptr(GtkSourceBuffer) buffer = NULL;
   GtkSourceStyleSchemeManager *manager;
   const char * const *scheme_ids;
-  GtkSourceLanguage *language;
-  GtkSourceView *preview;
   GtkFlowBox *flowbox;
+  GtkWidget *preview;
   GtkFrame *frame;
 
   g_assert (IDE_IS_PREFERENCES_WINDOW (window));
   g_assert (entry != NULL);
   g_assert (ADW_IS_PREFERENCES_GROUP (group));
 
-#define PREVIEW_TEXT "\
-#include <glib.h>\n\
-"
-
-  language = gtk_source_language_manager_get_language (gtk_source_language_manager_get_default (), "c");
-  buffer = g_object_new (GTK_SOURCE_TYPE_BUFFER,
-                         "language", language,
-                         "text", PREVIEW_TEXT,
-                         NULL);
-  preview = g_object_new (GTK_SOURCE_TYPE_VIEW,
-                          "buffer", buffer,
-                          "monospace", TRUE,
-                          "left-margin", 6,
-                          "top-margin", 6,
-                          "bottom-margin", 6,
-                          "right-margin", 6,
-                          "show-line-numbers", TRUE,
-                          "highlight-current-line", TRUE,
-                          "show-right-margin", TRUE,
-                          "right-margin-position", 60,
-                          NULL);
+  preview = gbp_editorui_preview_new ();
   frame = g_object_new (GTK_TYPE_FRAME,
                         "child", preview,
                         "margin-bottom", 12,
                         NULL);
-  gtk_widget_add_css_class (GTK_WIDGET (frame), "text-preview");
   adw_preferences_group_add (group, GTK_WIDGET (frame));
 
   manager = gtk_source_style_scheme_manager_get_default ();
diff --git a/src/plugins/editorui/gbp-editorui-preview.c b/src/plugins/editorui/gbp-editorui-preview.c
new file mode 100644
index 000000000..088784c3e
--- /dev/null
+++ b/src/plugins/editorui/gbp-editorui-preview.c
@@ -0,0 +1,139 @@
+/* gbp-editorui-preview.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-editorui-preview"
+
+#include "config.h"
+
+#include <libide-gui.h>
+
+#include "gbp-editorui-preview.h"
+
+struct _GbpEditoruiPreview
+{
+  GtkSourceView parent_instance;
+};
+
+G_DEFINE_TYPE (GbpEditoruiPreview, gbp_editorui_preview, GTK_SOURCE_TYPE_VIEW)
+
+static void
+gbp_editorui_preview_load_text (GbpEditoruiPreview *self)
+{
+  GtkSourceLanguageManager *manager;
+  GtkSourceLanguage *lang;
+  GtkTextBuffer *buffer;
+
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_EDITORUI_PREVIEW (self));
+
+  manager = gtk_source_language_manager_get_default ();
+  lang = gtk_source_language_manager_get_language (manager, "c");
+  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self));
+  gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (buffer), lang);
+  gtk_text_buffer_set_text (buffer, "\
+#include <glib.h>\n\
+typedef struct _type_t type_t;\n\
+type_t *type_new (int id);\n\
+void type_free (type_t *t);\
+", -1);
+
+  IDE_EXIT;
+}
+
+static void
+notify_style_scheme_cb (GbpEditoruiPreview *self,
+                        GParamSpec         *pspec,
+                        IdeApplication     *app)
+{
+  GtkSourceStyleSchemeManager *manager;
+  GtkSourceStyleScheme *scheme;
+  GtkTextBuffer *buffer;
+  const char *name;
+
+  IDE_ENTRY;
+
+  g_assert (GBP_IS_EDITORUI_PREVIEW (self));
+  g_assert (IDE_IS_APPLICATION (app));
+
+  name = ide_application_get_style_scheme (app);
+  manager = gtk_source_style_scheme_manager_get_default ();
+  scheme = gtk_source_style_scheme_manager_get_scheme (manager, name);
+  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self));
+
+  gtk_source_buffer_set_style_scheme (GTK_SOURCE_BUFFER (buffer), scheme);
+
+  IDE_EXIT;
+}
+
+static void
+gbp_editorui_preview_constructed (GObject *object)
+{
+  GbpEditoruiPreview *self = (GbpEditoruiPreview *)object;
+
+  G_OBJECT_CLASS (gbp_editorui_preview_parent_class)->constructed (object);
+
+  g_signal_connect_object (IDE_APPLICATION_DEFAULT,
+                           "notify::style-scheme",
+                           G_CALLBACK (notify_style_scheme_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  notify_style_scheme_cb (self, NULL, IDE_APPLICATION_DEFAULT);
+
+  gbp_editorui_preview_load_text (self);
+}
+
+static void
+gbp_editorui_preview_dispose (GObject *object)
+{
+  GbpEditoruiPreview *self = (GbpEditoruiPreview *)object;
+
+  G_OBJECT_CLASS (gbp_editorui_preview_parent_class)->dispose (object);
+}
+
+static void
+gbp_editorui_preview_class_init (GbpEditoruiPreviewClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->constructed = gbp_editorui_preview_constructed;
+  object_class->dispose = gbp_editorui_preview_dispose;
+}
+
+static void
+gbp_editorui_preview_init (GbpEditoruiPreview *self)
+{
+  gtk_text_view_set_monospace (GTK_TEXT_VIEW (self), TRUE);
+  gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW (self), TRUE);
+
+  g_object_set (self,
+                "left-margin", 6,
+                "top-margin", 6,
+                "bottom-margin", 6,
+                "right-margin", 6,
+                NULL);
+}
+
+GtkWidget *
+gbp_editorui_preview_new (void)
+{
+  return g_object_new (GBP_TYPE_EDITORUI_PREVIEW, NULL);
+}
diff --git a/src/plugins/editorui/gbp-editorui-preview.h b/src/plugins/editorui/gbp-editorui-preview.h
new file mode 100644
index 000000000..dbd2d673d
--- /dev/null
+++ b/src/plugins/editorui/gbp-editorui-preview.h
@@ -0,0 +1,33 @@
+/* gbp-editorui-preview.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 <gtksourceview/gtksource.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_EDITORUI_PREVIEW (gbp_editorui_preview_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpEditoruiPreview, gbp_editorui_preview, GBP, EDITORUI_PREVIEW, GtkSourceView)
+
+GtkWidget *gbp_editorui_preview_new (void);
+
+G_END_DECLS
diff --git a/src/plugins/editorui/meson.build b/src/plugins/editorui/meson.build
index 1aa291cf4..e1698fb0e 100644
--- a/src/plugins/editorui/meson.build
+++ b/src/plugins/editorui/meson.build
@@ -2,6 +2,7 @@ plugins_sources += files([
   'editorui-plugin.c',
   'gbp-editorui-position-label.c',
   'gbp-editorui-preferences-addin.c',
+  'gbp-editorui-preview.c',
   'gbp-editorui-workbench-addin.c',
   'gbp-editorui-workspace-addin.c',
 ])


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