summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2021-09-21 23:04:33 -0400
committerMatthias Clasen <mclasen@redhat.com>2021-09-22 17:09:53 -0400
commit923c944abb6b5305c4f0adda3d20e9abb6381c8a (patch)
treee433124af5cb2ad6715ae5c1f0b1ad395067acb5
parent0b9f6985b1a255fc044e1eb279c19ff62406eb0b (diff)
downloadgtk+-923c944abb6b5305c4f0adda3d20e9abb6381c8a.tar.gz
Add GtkLoader to gtk4-widget-factory
Add an async-loading paintable implementation to gtk4-widget-factory, and use it to load the jpegs in the background.
-rw-r--r--demos/widget-factory/gtkloader.c215
-rw-r--r--demos/widget-factory/gtkloaderprivate.h35
-rw-r--r--demos/widget-factory/meson.build2
-rw-r--r--demos/widget-factory/widget-factory.c2
-rw-r--r--demos/widget-factory/widget-factory.ui18
5 files changed, 268 insertions, 4 deletions
diff --git a/demos/widget-factory/gtkloader.c b/demos/widget-factory/gtkloader.c
new file mode 100644
index 0000000000..be91fa1ef3
--- /dev/null
+++ b/demos/widget-factory/gtkloader.c
@@ -0,0 +1,215 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#include "gtkloaderprivate.h"
+#include <gtk/gtk.h>
+
+enum {
+ PROP_RESOURCE = 1,
+};
+
+struct _GtkLoader
+{
+ GObject parent_instance;
+
+ GdkTexture *texture;
+};
+
+struct _GtkLoaderClass
+{
+ GObjectClass parent_class;
+};
+
+static void
+gtk_loader_paintable_snapshot (GdkPaintable *paintable,
+ GdkSnapshot *snapshot,
+ double width,
+ double height)
+{
+ GtkLoader *self = GTK_LOADER (paintable);
+
+ if (self->texture)
+ gdk_paintable_snapshot (GDK_PAINTABLE (self->texture), snapshot, width, height);
+}
+
+static GdkPaintable *
+gtk_loader_paintable_get_current_image (GdkPaintable *paintable)
+{
+ GtkLoader *self = GTK_LOADER (paintable);
+
+ if (self->texture)
+ return gdk_paintable_get_current_image (GDK_PAINTABLE (self->texture));
+
+ // FIXME: return a loading image
+ return NULL;
+}
+
+static int
+gtk_loader_paintable_get_intrinsic_width (GdkPaintable *paintable)
+{
+ GtkLoader *self = GTK_LOADER (paintable);
+
+ if (self->texture)
+ return gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (self->texture));
+
+ return 0;
+}
+
+static int
+gtk_loader_paintable_get_intrinsic_height (GdkPaintable *paintable)
+{
+ GtkLoader *self = GTK_LOADER (paintable);
+
+ if (self->texture)
+ return gdk_paintable_get_intrinsic_height (GDK_PAINTABLE (self->texture));
+
+ return 0;
+}
+
+static double
+gtk_loader_paintable_get_intrinsic_aspect_ratio (GdkPaintable *paintable)
+{
+ GtkLoader *self = GTK_LOADER (paintable);
+
+ if (self->texture)
+ return gdk_paintable_get_intrinsic_aspect_ratio (GDK_PAINTABLE (self->texture));
+
+ return 0;
+};
+
+static void
+gtk_loader_paintable_init (GdkPaintableInterface *iface)
+{
+ iface->snapshot = gtk_loader_paintable_snapshot;
+ iface->get_current_image = gtk_loader_paintable_get_current_image;
+ iface->get_intrinsic_width = gtk_loader_paintable_get_intrinsic_width;
+ iface->get_intrinsic_height = gtk_loader_paintable_get_intrinsic_height;
+ iface->get_intrinsic_aspect_ratio = gtk_loader_paintable_get_intrinsic_aspect_ratio;
+}
+
+G_DEFINE_TYPE_EXTENDED (GtkLoader, gtk_loader, G_TYPE_OBJECT, 0,
+ G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
+ gtk_loader_paintable_init))
+
+static void
+gtk_loader_dispose (GObject *object)
+{
+ GtkLoader *self = GTK_LOADER (object);
+
+ g_clear_object (&self->texture);
+
+ G_OBJECT_CLASS (gtk_loader_parent_class)->dispose (object);
+}
+
+static void gtk_loader_set_resource (GtkLoader *self, const char *resource);
+
+static void
+gtk_loader_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GtkLoader *self = (GtkLoader *)object;
+
+ switch (prop_id)
+ {
+ case PROP_RESOURCE:
+ gtk_loader_set_resource (self, g_value_get_string (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gtk_loader_class_init (GtkLoaderClass *klass)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+ gobject_class->dispose = gtk_loader_dispose;
+ gobject_class->set_property = gtk_loader_set_property;
+
+ g_object_class_install_property (gobject_class, PROP_RESOURCE,
+ g_param_spec_string ("resource", "", "",
+ NULL,
+ G_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY));
+}
+
+static void
+gtk_loader_init (GtkLoader *self)
+{
+}
+
+static void
+load_texture_in_thread (GTask *task,
+ gpointer source_object,
+ gpointer task_data,
+ GCancellable *cancellable)
+{
+ const char *resource = task_data;
+ GdkTexture *texture;
+ GError *error = NULL;
+
+ texture = gdk_texture_new_from_resource (resource);
+
+ if (texture)
+ g_task_return_pointer (task, texture, g_object_unref);
+ else
+ g_task_return_error (task, error);
+}
+
+static void
+texture_finished (GObject *source,
+ GAsyncResult *result,
+ gpointer data)
+{
+ GtkLoader *self = GTK_LOADER (source);
+ GdkTexture *texture;
+ GError *error = NULL;
+
+ texture = g_task_propagate_pointer (G_TASK (result), &error);
+
+ if (texture)
+ {
+ self->texture = g_object_ref (texture);
+
+ gdk_paintable_invalidate_size (GDK_PAINTABLE (self));
+ gdk_paintable_invalidate_contents (GDK_PAINTABLE (self));
+ }
+}
+
+static void
+gtk_loader_set_resource (GtkLoader *self,
+ const char *resource)
+{
+ GTask *task;
+
+ task = g_task_new (self, NULL, texture_finished, NULL);
+ g_task_set_task_data (task, g_strdup (resource), (GDestroyNotify)g_free);
+ g_task_run_in_thread (task, load_texture_in_thread);
+ g_object_unref (task);
+}
+
+GdkPaintable *
+gtk_loader_new (void)
+{
+ return g_object_new (GTK_TYPE_LOADER, NULL);
+}
diff --git a/demos/widget-factory/gtkloaderprivate.h b/demos/widget-factory/gtkloaderprivate.h
new file mode 100644
index 0000000000..c0b2072cd3
--- /dev/null
+++ b/demos/widget-factory/gtkloaderprivate.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright © 2021 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Matthias Clasen <mclasen@redhat.com>
+ */
+
+#ifndef __GTK_LOADER_H__
+#define __GTK_LOADER_H__
+
+#include <gdk/gdk.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_LOADER (gtk_loader_get_type ())
+
+G_DECLARE_FINAL_TYPE (GtkLoader, gtk_loader, GTK, LOADER, GObject)
+
+GdkPaintable * gtk_loader_new (void);
+
+G_END_DECLS
+
+#endif /* __GTK_SCALER_H__ */
diff --git a/demos/widget-factory/meson.build b/demos/widget-factory/meson.build
index 1cebd4adc5..6b09db3e2b 100644
--- a/demos/widget-factory/meson.build
+++ b/demos/widget-factory/meson.build
@@ -6,7 +6,7 @@ widgetfactory_resources = gnome.compile_resources('widgetfactory_resources',
)
executable('gtk4-widget-factory',
- sources: ['widget-factory.c', widgetfactory_resources],
+ sources: ['widget-factory.c', 'gtkloader.c', widgetfactory_resources],
c_args: common_cflags,
dependencies: [ libgtk_dep, demo_conf_h ],
include_directories: confinc,
diff --git a/demos/widget-factory/widget-factory.c b/demos/widget-factory/widget-factory.c
index 0c4b21c81c..c18b502bcf 100644
--- a/demos/widget-factory/widget-factory.c
+++ b/demos/widget-factory/widget-factory.c
@@ -26,6 +26,7 @@
#include <gtk/gtk.h>
#include "demo_conf.h"
+#include "gtkloaderprivate.h"
static void
change_dark_state (GSimpleAction *action,
@@ -2054,6 +2055,7 @@ activate (GApplication *app)
GtkEventController *controller;
g_type_ensure (my_text_view_get_type ());
+ g_type_ensure (gtk_loader_get_type ());
provider = gtk_css_provider_new ();
gtk_css_provider_load_from_resource (provider, "/org/gtk/WidgetFactory4/widget-factory.css");
diff --git a/demos/widget-factory/widget-factory.ui b/demos/widget-factory/widget-factory.ui
index 1f2258d06d..b7a6ee442f 100644
--- a/demos/widget-factory/widget-factory.ui
+++ b/demos/widget-factory/widget-factory.ui
@@ -1251,7 +1251,11 @@ Suspendisse feugiat quam quis dolor accumsan cursus.</property>
<object class="GtkNotebookPage">
<property name="child">
<object class="GtkPicture">
- <property name="file">resource:///org/gtk/WidgetFactory4/sunset.jpg</property>
+ <property name="paintable">
+ <object class="GtkLoader">
+ <property name="resource">/org/gtk/WidgetFactory4/sunset.jpg</property>
+ </object>
+ </property>
<child>
<object class="GtkDragSource">
<property name="actions">copy</property>
@@ -1278,7 +1282,11 @@ Suspendisse feugiat quam quis dolor accumsan cursus.</property>
<object class="GtkNotebookPage">
<property name="child">
<object class="GtkPicture">
- <property name="file">resource:///org/gtk/WidgetFactory4/nyc.jpg</property>
+ <property name="paintable">
+ <object class="GtkLoader">
+ <property name="resource">/org/gtk/WidgetFactory4/nyc.jpg</property>
+ </object>
+ </property>
<child>
<object class="GtkDragSource">
<property name="actions">copy</property>
@@ -1305,7 +1313,11 @@ Suspendisse feugiat quam quis dolor accumsan cursus.</property>
<object class="GtkNotebookPage">
<property name="child">
<object class="GtkPicture">
- <property name="file">resource:///org/gtk/WidgetFactory4/beach.jpg</property>
+ <property name="paintable">
+ <object class="GtkLoader">
+ <property name="resource">/org/gtk/WidgetFactory4/beach.jpg</property>
+ </object>
+ </property>
<child>
<object class="GtkDragSource">
<property name="actions">copy</property>