[gnome-control-center] wifi: Use Paintables for QR code
- From: Mohammed Sadiq <pksadiq src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-control-center] wifi: Use Paintables for QR code
- Date: Thu, 20 Jan 2022 13:45:27 +0000 (UTC)
commit 99d4947c6347ddf8a98a04da81b8947b349f5541
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date: Thu Jan 20 12:37:50 2022 +0530
wifi: Use Paintables for QR code
This fixes showing qr code when the window is scaled.
panels/network/cc-qr-code.c | 90 ++++++++++++++++++------------------
panels/network/cc-qr-code.h | 6 +--
panels/network/cc-wifi-panel.c | 22 +++++----
panels/network/cc-wifi-panel.ui | 4 +-
panels/network/network.gresource.xml | 1 +
panels/network/wifi-panel.css | 4 ++
6 files changed, 68 insertions(+), 59 deletions(-)
---
diff --git a/panels/network/cc-qr-code.c b/panels/network/cc-qr-code.c
index b803011bc..a277585dd 100644
--- a/panels/network/cc-qr-code.c
+++ b/panels/network/cc-qr-code.c
@@ -41,14 +41,15 @@
* Generate a QR image from a given text.
*/
+#define BYTES_PER_R8G8B8 3
+
struct _CcQrCode
{
GObject parent_instance;
gchar *text;
- cairo_surface_t *surface;
+ GdkTexture *texture;
gint size;
- gint scale;
};
G_DEFINE_TYPE (CcQrCode, cc_qr_code, G_TYPE_OBJECT)
@@ -59,7 +60,7 @@ cc_qr_code_finalize (GObject *object)
{
CcQrCode *self = (CcQrCode *)object;
- g_clear_pointer (&self->surface, cairo_surface_destroy);
+ g_clear_object (&self->texture);
g_clear_pointer (&self->text, g_free);
G_OBJECT_CLASS (cc_qr_code_parent_class)->finalize (object);
@@ -94,8 +95,7 @@ cc_qr_code_set_text (CcQrCode *self,
if (g_strcmp0 (text, self->text) == 0)
return FALSE;
- /* Clear cairo surface that is cached */
- g_clear_pointer (&self->surface, cairo_surface_destroy);
+ g_clear_object (&self->texture);
g_free (self->text);
self->text = g_strdup (text);
@@ -103,33 +103,33 @@ cc_qr_code_set_text (CcQrCode *self,
}
static void
-cc_cairo_fill_pixel (cairo_t *cr,
- int x,
- int y,
- int padding,
- int scale)
+cc_fill_pixel (GByteArray *array,
+ guint8 value,
+ int pixel_size)
{
- cairo_rectangle (cr,
- x * scale + padding,
- y * scale + padding,
- scale, scale);
- cairo_fill (cr);
+ guint i;
+
+ for (i = 0; i < pixel_size; i++) {
+ g_byte_array_append (array, &value, 1); /* R */
+ g_byte_array_append (array, &value, 1); /* G */
+ g_byte_array_append (array, &value, 1); /* B */
+ }
}
-cairo_surface_t *
-cc_qr_code_get_surface (CcQrCode *self,
- gint size,
- gint scale)
+GdkPaintable *
+cc_qr_code_get_paintable (CcQrCode *self,
+ gint size)
{
uint8_t qr_code[qrcodegen_BUFFER_LEN_FOR_VERSION (qrcodegen_VERSION_MAX)];
uint8_t temp_buf[qrcodegen_BUFFER_LEN_FOR_VERSION (qrcodegen_VERSION_MAX)];
- cairo_t *cr;
- gint pixel_size, padding, qr_size;
+ g_autoptr(GBytes) bytes = NULL;
+ GByteArray *qr_matrix;
+ gint pixel_size, padding, qr_size, total_size;
+ gint column, row, i;
gboolean success = FALSE;
g_return_val_if_fail (CC_IS_QR_CODE (self), NULL);
g_return_val_if_fail (size > 0, NULL);
- g_return_val_if_fail (scale > 0, NULL);
if (!self->text || !*self->text)
{
@@ -137,14 +137,10 @@ cc_qr_code_get_surface (CcQrCode *self,
cc_qr_code_set_text (self, "invalid text");
}
- if (self->surface &&
- self->size == size &&
- self->scale == scale)
- return self->surface;
+ if (self->texture && self->size == size)
+ return GDK_PAINTABLE (self->texture);
self->size = size;
- self->scale = scale;
- g_clear_pointer (&self->surface, cairo_surface_destroy);
success = qrcodegen_encodeText (self->text,
temp_buf,
@@ -158,16 +154,6 @@ cc_qr_code_get_surface (CcQrCode *self,
if (!success)
return NULL;
- self->surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, size * scale, size * scale);
- cairo_surface_set_device_scale (self->surface, scale, scale);
- cr = cairo_create (self->surface);
- cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
-
- /* Draw white background */
- cairo_set_source_rgba (cr, 1, 1, 1, 1);
- cairo_rectangle (cr, 0, 0, size * scale, size * scale);
- cairo_fill (cr);
-
qr_size = qrcodegen_getSize(qr_code);
pixel_size = MAX (1, size / (qr_size));
padding = (size - qr_size * pixel_size) / 2;
@@ -180,18 +166,30 @@ cc_qr_code_get_surface (CcQrCode *self,
padding = (size - qr_size * pixel_size) / 2;
}
- /* Now draw the black QR code pixels */
- cairo_set_source_rgba (cr, 0, 0, 0, 1);
- for (int row = 0; row < qr_size; row++)
+ total_size = qr_size * pixel_size;
+ qr_matrix = g_byte_array_sized_new (total_size * total_size * pixel_size * BYTES_PER_R8G8B8);
+
+ for (column = 0; column < total_size; column++)
{
- for (int column = 0; column < qr_size; column++)
+ for (i = 0; i < pixel_size; i++)
{
- if (qrcodegen_getModule (qr_code, row, column))
- cc_cairo_fill_pixel (cr, column, row, padding, pixel_size);
+ for (row = 0; row < total_size / pixel_size; row++)
+ {
+ if (qrcodegen_getModule (qr_code, column, row))
+ cc_fill_pixel (qr_matrix, 0x00, pixel_size);
+ else
+ cc_fill_pixel (qr_matrix, 0xff, pixel_size);
+ }
}
}
- cairo_destroy (cr);
+ bytes = g_byte_array_free_to_bytes (qr_matrix);
+
+ self->texture = gdk_memory_texture_new (total_size,
+ total_size,
+ GDK_MEMORY_R8G8B8,
+ bytes,
+ total_size * BYTES_PER_R8G8B8);
- return self->surface;
+ return GDK_PAINTABLE (self->texture);
}
diff --git a/panels/network/cc-qr-code.h b/panels/network/cc-qr-code.h
index 9965b0f37..844dd2ab0 100644
--- a/panels/network/cc-qr-code.h
+++ b/panels/network/cc-qr-code.h
@@ -24,6 +24,7 @@
#pragma once
+#include <gtk/gtk.h>
#include <glib-object.h>
#include <cairo.h>
@@ -36,8 +37,7 @@ G_DECLARE_FINAL_TYPE (CcQrCode, cc_qr_code, CC, QR_CODE, GObject)
CcQrCode *cc_qr_code_new (void);
gboolean cc_qr_code_set_text (CcQrCode *self,
const gchar *text);
-cairo_surface_t *cc_qr_code_get_surface (CcQrCode *self,
- gint size,
- gint scale);
+GdkPaintable *cc_qr_code_get_paintable (CcQrCode *self,
+ gint size);
G_END_DECLS
diff --git a/panels/network/cc-wifi-panel.c b/panels/network/cc-wifi-panel.c
index 146124f84..b2d93746f 100644
--- a/panels/network/cc-wifi-panel.c
+++ b/panels/network/cc-wifi-panel.c
@@ -60,7 +60,7 @@ struct _CcWifiPanel
GtkStack *main_stack;
GtkWidget *spinner;
GtkStack *stack;
- GtkImage *wifi_qr_image;
+ GtkPicture *wifi_qr_image;
CcQrCode *qr_code;
NMClient *client;
@@ -355,17 +355,12 @@ wifi_panel_update_qr_image_cb (CcWifiPanel *self)
str = get_qr_string_for_hotspot (self->client, hotspot);
if (cc_qr_code_set_text (self->qr_code, str))
{
- g_autoptr(GdkPixbuf) pixbuf = NULL;
- cairo_surface_t *surface;
+ GdkPaintable *paintable;
gint scale;
scale = gtk_widget_get_scale_factor (GTK_WIDGET (self->wifi_qr_image));
- surface = cc_qr_code_get_surface (self->qr_code, QR_IMAGE_SIZE, scale);
- pixbuf = gdk_pixbuf_get_from_surface (surface,
- 0, 0,
- QR_IMAGE_SIZE,
- QR_IMAGE_SIZE);
- gtk_image_set_from_pixbuf (self->wifi_qr_image, pixbuf);
+ paintable = cc_qr_code_get_paintable (self->qr_code, QR_IMAGE_SIZE * scale);
+ gtk_picture_set_paintable (self->wifi_qr_image, paintable);
}
}
@@ -1018,6 +1013,8 @@ cc_wifi_panel_class_init (CcWifiPanelClass *klass)
static void
cc_wifi_panel_init (CcWifiPanel *self)
{
+ g_autoptr(GtkCssProvider) provider = NULL;
+
g_resources_register (cc_network_get_resource ());
gtk_widget_init_template (GTK_WIDGET (self));
@@ -1067,4 +1064,11 @@ cc_wifi_panel_init (CcWifiPanel *self)
/* Handle comment-line arguments after loading devices */
handle_argv (self);
+
+ /* use custom CSS */
+ provider = gtk_css_provider_new ();
+ gtk_css_provider_load_from_resource (provider, "/org/gnome/control-center/network/wifi-panel.css");
+ gtk_style_context_add_provider_for_display (gdk_display_get_default (),
+ GTK_STYLE_PROVIDER (provider),
+ GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
diff --git a/panels/network/cc-wifi-panel.ui b/panels/network/cc-wifi-panel.ui
index 570c3355f..2a3405ae7 100644
--- a/panels/network/cc-wifi-panel.ui
+++ b/panels/network/cc-wifi-panel.ui
@@ -181,12 +181,14 @@
<!-- Hotspot QR code -->
<child>
- <object class="GtkImage" id="wifi_qr_image">
+ <object class="GtkPicture" id="wifi_qr_image">
<property name="halign">center</property>
+ <property name="valign">center</property>
<property name="width-request">180</property>
<property name="height-request">180</property>
<style>
<class name="frame"/>
+ <class name="qr-image"/>
</style>
</object>
</child>
diff --git a/panels/network/network.gresource.xml b/panels/network/network.gresource.xml
index a67ab60de..8fb44ae57 100644
--- a/panels/network/network.gresource.xml
+++ b/panels/network/network.gresource.xml
@@ -14,6 +14,7 @@
<!-- Wi-Fi panel -->
<file preprocess="xml-stripblanks">cc-wifi-panel.ui</file>
+ <file>wifi-panel.css</file>
</gresource>
<gresource prefix="/org/gnome/ControlCenter/icons/scalable/actions">
<!-- Wi-Fi panel icons -->
diff --git a/panels/network/wifi-panel.css b/panels/network/wifi-panel.css
new file mode 100644
index 000000000..cacb24fae
--- /dev/null
+++ b/panels/network/wifi-panel.css
@@ -0,0 +1,4 @@
+.qr-image {
+ padding: 12px;
+}
+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]