[gnome-builder] gstyle: Drop X11-only eyedropper in favor of portal-based color picker



commit 2e8389dfdc92365d06eb318cebe22405e19fe4d2
Author: vanadiae <vanadiae35 gmail com>
Date:   Sun Feb 7 17:23:30 2021 +0100

    gstyle: Drop X11-only eyedropper in favor of portal-based color picker
    
    Currently the color picker (eyedropper) works correctly only on X11,
    since there's no way integrated into Wayland to take screenshots/pick a
    color.
    
    So this commit switches it to use the portal through libportal, which
    means that the system is responsible for providing a convenient
    eyedropper/color picker (which includes showing a bigger zone of the
    color at the cursor, a "zoom" of it) while being able to use it with
    both X11 and Wayland.

 src/gstyle/gstyle-color-panel-private.h |   2 -
 src/gstyle/gstyle-color-panel.c         |  64 +--
 src/gstyle/gstyle-eyedropper.c          | 830 --------------------------------
 src/gstyle/gstyle-eyedropper.h          |  34 --
 src/gstyle/meson.build                  |   5 +-
 src/gstyle/theme/gstyle.css             |   8 -
 6 files changed, 34 insertions(+), 909 deletions(-)
---
diff --git a/src/gstyle/gstyle-color-panel-private.h b/src/gstyle/gstyle-color-panel-private.h
index cdd0d80c9..db70b21ab 100644
--- a/src/gstyle/gstyle-color-panel-private.h
+++ b/src/gstyle/gstyle-color-panel-private.h
@@ -32,7 +32,6 @@
 #include "gstyle-color-scale.h"
 #include "gstyle-color-widget.h"
 #include "gstyle-css-provider.h"
-#include "gstyle-eyedropper.h"
 #include "gstyle-color-filter.h"
 #include "gstyle-palette-widget.h"
 #include "gstyle-revealer.h"
@@ -72,7 +71,6 @@ struct _GstyleColorPanel
   GstyleColorWidget                   *old_swatch;
 
   GtkButton                           *picker_button;
-  GstyleEyedropper                    *eyedropper;
   GtkWidget                           *search_color_entry;
   GtkWidget                           *search_strings_popover;
   GtkWidget                           *search_strings_list;
diff --git a/src/gstyle/gstyle-color-panel.c b/src/gstyle/gstyle-color-panel.c
index 6c91a4c1e..3b2c75369 100644
--- a/src/gstyle/gstyle-color-panel.c
+++ b/src/gstyle/gstyle-color-panel.c
@@ -29,6 +29,9 @@
 #include "gstyle-revealer.h"
 #include "gstyle-color.h"
 
+#include <libportal/portal.h>
+#include <libportal/portal-gtk3.h>
+
 #define HSV_TO_SCALE_FACTOR (1.0 / 256.0)
 #define CIELAB_L_TO_SCALE_FACTOR (100.0 / 256.0)
 
@@ -585,50 +588,50 @@ update_ref_color_ramp (GstyleColorPanel *self,
 }
 
 static void
-color_picked_cb (GstyleColorPanel *self,
-                 GdkRGBA          *rgba)
+on_color_picked_cb (GObject      *object,
+                    GAsyncResult *result,
+                    gpointer      user_data)
 {
-  g_assert (GSTYLE_IS_COLOR_PANEL (self));
+  XdpPortal *portal = NULL;
+  GstyleColorPanel *color_panel = NULL;
+  g_autoptr(GError) error = NULL;
+  g_autoptr(GVariant) color_variant = NULL;
+  GdkRGBA picked_color;
 
-  gstyle_color_plane_set_rgba (self->color_plane, rgba);
-}
+  g_assert (XDP_IS_PORTAL (object));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (GSTYLE_IS_COLOR_PANEL (user_data));
 
-static void
-grab_released_cb (GstyleColorPanel *self)
-{
-  g_assert (GSTYLE_IS_COLOR_PANEL (self));
+  portal = XDP_PORTAL (object);
+  color_panel = GSTYLE_COLOR_PANEL (user_data);
 
-  g_clear_object (&self->eyedropper);
+  color_variant = xdp_portal_pick_color_finish (portal, result, &error);
+  if (error)
+    {
+      g_warning ("Couldn't pick color using the portal: %s", error->message);
+      return;
+    }
+  g_variant_get (color_variant, "(ddd)", &picked_color.red, &picked_color.green, &picked_color.blue);
+  gstyle_color_plane_set_rgba (color_panel->color_plane, &picked_color);
 }
 
 static void
 picker_button_clicked_cb (GstyleColorPanel *self,
                           GtkButton        *picker_button)
 {
-  GdkEvent *event;
+  g_autoptr(XdpPortal) portal = NULL;
+  XdpParent *portal_parent = NULL;
+  GtkWindow *window = NULL;
 
   g_assert (GSTYLE_IS_COLOR_PANEL (self));
   g_assert (GTK_IS_BUTTON (picker_button));
 
-  event = gtk_get_current_event ();
-  g_assert (event != NULL);
-
-  self->eyedropper = g_object_ref_sink (g_object_new (GSTYLE_TYPE_EYEDROPPER,
-                                        "source-event", event,
-                                        NULL));
-  gdk_event_free (event);
-
-  g_signal_connect_object (self->eyedropper,
-                           "color-picked",
-                           G_CALLBACK (color_picked_cb),
-                           self,
-                           G_CONNECT_SWAPPED);
-
-  g_signal_connect_object (self->eyedropper,
-                           "grab-released",
-                           G_CALLBACK (grab_released_cb),
-                           self,
-                           G_CONNECT_SWAPPED);
+  portal = xdp_portal_new ();
+  window = GTK_WINDOW (gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_WINDOW));
+  portal_parent = xdp_parent_new_gtk (window);
+
+  xdp_portal_pick_color (portal, portal_parent, NULL, on_color_picked_cb, self);
+  xdp_parent_free (portal_parent);
 }
 
 static void
@@ -1260,7 +1263,6 @@ gstyle_color_panel_dispose (GObject *object)
   g_clear_object (&self->default_provider);
   g_clear_object (&self->degree_icon);
   g_clear_object (&self->percent_icon);
-  g_clear_object (&self->eyedropper);
   gstyle_color_panel_set_prefs_pages (self, NULL, NULL, NULL, NULL);
 
   G_OBJECT_CLASS (gstyle_color_panel_parent_class)->dispose (object);
diff --git a/src/gstyle/meson.build b/src/gstyle/meson.build
index fb3247996..2aa92bf31 100644
--- a/src/gstyle/meson.build
+++ b/src/gstyle/meson.build
@@ -16,7 +16,6 @@ libgstyle_headers = [
   'gstyle-color-widget-actions.h',
   'gstyle-colorlexer.h',
   'gstyle-css-provider.h',
-  'gstyle-eyedropper.h',
   'gstyle-hsv.h',
   'gstyle-palette.h',
   'gstyle-palette-widget.h',
@@ -56,7 +55,6 @@ libgstyle_sources = [
   'gstyle-color-widget-actions.c',
   'gstyle-colorlexer.c',
   'gstyle-css-provider.c',
-  'gstyle-eyedropper.c',
   'gstyle-hsv.c',
   'gstyle-palette.c',
   'gstyle-palette-widget.c',
@@ -71,6 +69,7 @@ libgstyle_sources = [
 libgstyle_deps = [
   libdazzle_dep,
   libgtk_dep,
+  libportal_dep,
   libm_dep,
   libxml2_dep,
 ]
@@ -107,8 +106,6 @@ libgstyle_introspection_sources = [
   'gstyle-color-scale.c',
   'gstyle-color-widget.h',
   'gstyle-color-widget.c',
-  'gstyle-eyedropper.h',
-  'gstyle-eyedropper.c',
   'gstyle-hsv.h',
   'gstyle-hsv.c',
   'gstyle-palette.h',
diff --git a/src/gstyle/theme/gstyle.css b/src/gstyle/theme/gstyle.css
index 25cc85949..d3fa24d2c 100644
--- a/src/gstyle/theme/gstyle.css
+++ b/src/gstyle/theme/gstyle.css
@@ -118,11 +118,3 @@ gstylecolorpanel revealer button.toggle {
 gstyleslidein.shade {
   color: rgba(77, 78, 83, 0.8);
 }
-
-#gstyleeyedropper {
-  border: solid alpha(@borders, 0.75) 1px;
-}
-
-#gstyleeyedropper > box {
-  margin: 6px;
-}


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