[gnome-builder] colorpicker plugin: search color in palettes
- From: Sébastien Lafargue <slafargue src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] colorpicker plugin: search color in palettes
- Date: Mon, 18 Jul 2016 16:43:45 +0000 (UTC)
commit fcdc6a42ace5d8dd3d4f4fd135302967473a7e27
Author: Sebastien Lafargue <slafargue gnome org>
Date: Mon Jul 18 18:38:30 2016 +0200
colorpicker plugin: search color in palettes
The search color by name now work with the
colors in loaded palettes.
A color can appear multiple times in the search list,
even if the name is the same, but only if the color
value is different.
contrib/gstyle/gstyle-color-panel.c | 44 ++++++++++++++-
contrib/gstyle/gstyle-palette-widget.c | 97 ++++++++++++++++++++++++++++++++
contrib/gstyle/gstyle-palette-widget.h | 2 +
contrib/gstyle/gstyle-utils.c | 24 ++++++++
contrib/gstyle/gstyle-utils.h | 32 ++++++-----
5 files changed, 183 insertions(+), 16 deletions(-)
---
diff --git a/contrib/gstyle/gstyle-color-panel.c b/contrib/gstyle/gstyle-color-panel.c
index 3cc044c..40ac644 100644
--- a/contrib/gstyle/gstyle-color-panel.c
+++ b/contrib/gstyle/gstyle-color-panel.c
@@ -599,6 +599,25 @@ component_toggled_cb (GstyleColorPanel *self,
}
}
+static gint
+search_strings_list_sort_func (GtkListBoxRow *row1,
+ GtkListBoxRow *row2,
+ gpointer user_data)
+{
+ GstyleColorWidget *cw1;
+ GstyleColorWidget *cw2;
+ const gchar *name1;
+ const gchar *name2;
+
+ cw1 = GSTYLE_COLOR_WIDGET (gtk_bin_get_child (GTK_BIN (row1)));
+ name1 = gstyle_color_get_name (gstyle_color_widget_get_color (cw1));
+
+ cw2 = GSTYLE_COLOR_WIDGET (gtk_bin_get_child (GTK_BIN (row2)));
+ name2 = gstyle_color_get_name (gstyle_color_widget_get_color (cw2));
+
+ return g_strcmp0 (name1, name2);
+}
+
static void
search_list_add_color (GstyleColorPanel *self,
GstyleColor *color)
@@ -618,11 +637,12 @@ static void
search_color_entry_changed_cb (GstyleColorPanel *self,
GtkSearchEntry *entry)
{
- g_autoptr (GPtrArray) ar = NULL;
+ GPtrArray *ar, *ar_palette;;
GstyleColor *color;
const gchar *str;
GList *children;
GList *l;
+ gint sum = 0;
g_assert (GSTYLE_IS_COLOR_PANEL (self));
g_assert (GTK_IS_SEARCH_ENTRY (entry));
@@ -650,13 +670,28 @@ search_color_entry_changed_cb (GstyleColorPanel *self,
else
{
ar = gstyle_color_fuzzy_parse_color_string (str);
+ sum += ar->len;
for (gint i = 0; i < ar->len; ++i)
{
color = g_ptr_array_index (ar, i);
search_list_add_color (self, color);
}
- gtk_widget_set_visible (self->search_strings_popover, (ar->len > 0));
+ ar_palette = gstyle_palette_widget_fuzzy_parse_color_string (self->palette_widget, str);
+ if (ar_palette != NULL && ar_palette->len > 0)
+ {
+ sum += ar_palette->len;
+ for (gint i = 0; i < ar_palette->len; ++i)
+ {
+ color = g_ptr_array_index (ar_palette, i);
+ if (ar == NULL || !gstyle_utils_is_array_contains_same_color (ar, color))
+ search_list_add_color (self, color);
+ }
+ }
+
+ g_ptr_array_unref (ar);
+ g_ptr_array_unref (ar_palette);
+ gtk_widget_set_visible (self->search_strings_popover, (sum > 0));
}
}
@@ -1414,6 +1449,11 @@ gstyle_color_panel_init (GstyleColorPanel *self)
self->percent_icon = get_percent_icon (self);
self->preferred_unit = GSTYLE_COLOR_UNIT_VALUE;
+
+ gtk_list_box_set_sort_func (GTK_LIST_BOX (self->search_strings_list),
+ search_strings_list_sort_func,
+ self,
+ NULL);
setup_ui (self);
}
diff --git a/contrib/gstyle/gstyle-palette-widget.c b/contrib/gstyle/gstyle-palette-widget.c
index 5471aa0..7d57c1e 100644
--- a/contrib/gstyle/gstyle-palette-widget.c
+++ b/contrib/gstyle/gstyle-palette-widget.c
@@ -19,6 +19,7 @@
#define G_LOG_DOMAIN "gstyle-palette-widget"
#include <cairo/cairo.h>
+#include <fuzzy.h>
#include <glib/gi18n.h>
#include <gdk/gdk.h>
#include <math.h>
@@ -73,6 +74,8 @@ G_DEFINE_TYPE (GstylePaletteWidget, gstyle_palette_widget, GTK_TYPE_BIN)
#define GSTYLE_DND_SPEED_THRESHOLD 50
#define DND_INDEX_START (G_MININT)
+#define GSTYLE_COLOR_FUZZY_SEARCH_MAX_LEN 20
+
static guint unsaved_palette_count = 0;
enum {
@@ -467,6 +470,100 @@ gstyle_palette_widget_on_drag_data_received (GtkWidget *widget,
dnd_highlight_set_from_cursor (self, -1, -1);
}
+static GPtrArray *
+fuzzy_search_lookup (GstylePaletteWidget *self,
+ Fuzzy *fuzzy,
+ const gchar *key)
+{
+ g_autoptr (GArray) results = NULL;
+ GPtrArray *ar = NULL;
+ FuzzyMatch *match;
+
+ g_assert (GSTYLE_IS_PALETTE_WIDGET (self));
+ g_assert (fuzzy != NULL);
+
+ results = fuzzy_match (fuzzy, key, 1);
+ if (ar!= NULL && ar->len > 0)
+ {
+ match = &g_array_index (results, FuzzyMatch, 0);
+ if (g_strcmp0 (match->key, key))
+ ar = match->value;
+ }
+
+ return ar;
+}
+
+/**
+ * gstyle_palette_widget_fuzzy_parse_color_string:
+ * @color_string: color name to search for
+ *
+ * Returns: (transfer full) (element-type GstyleColor): A #GPtrArray of #GstyleColor for a fuzzy search.
+ */
+GPtrArray *
+gstyle_palette_widget_fuzzy_parse_color_string (GstylePaletteWidget *self,
+ const gchar *color_string)
+{
+ g_autoptr (GArray) fuzzy_results = NULL;
+ Fuzzy *fuzzy;
+ GPtrArray *results;
+ GPtrArray *ar, *ar_list;
+ FuzzyMatch *match;
+ GstylePalette *palette;
+ GstyleColor *color, *new_color;
+ const gchar *name;
+ gint nb_palettes;
+ gint nb_colors;
+ gint len;
+
+ g_return_val_if_fail (GSTYLE_IS_PALETTE_WIDGET (self), NULL);
+
+ fuzzy = fuzzy_new (TRUE);
+ ar_list = g_ptr_array_new_with_free_func ((GDestroyNotify)g_ptr_array_unref);
+ nb_palettes = gstyle_palette_widget_get_n_palettes (self);
+ if (nb_palettes == 0)
+ return NULL;
+
+ for (gint n = 0; n < nb_palettes; ++n)
+ {
+ palette = gstyle_palette_widget_get_palette_at_index (self, n);
+ nb_colors = gstyle_palette_get_len (palette);
+ for (gint i = 0; i < nb_colors; ++i)
+ {
+ color = (GstyleColor *)gstyle_palette_get_color_at_index (palette, i);
+ name = gstyle_color_get_name (color);
+ ar = fuzzy_search_lookup (self, fuzzy, name);
+ if (ar == NULL)
+ {
+ ar = g_ptr_array_new ();
+ g_ptr_array_add (ar_list, ar);
+ fuzzy_insert (fuzzy, name, ar);
+ g_ptr_array_add (ar, color);
+ }
+ else if (!gstyle_utils_is_array_contains_same_color (ar, color))
+ g_ptr_array_add (ar, color);
+ }
+ }
+
+ results = g_ptr_array_new_with_free_func (g_object_unref);
+ fuzzy_results = fuzzy_match (fuzzy, color_string, GSTYLE_COLOR_FUZZY_SEARCH_MAX_LEN);
+ len = MIN (GSTYLE_COLOR_FUZZY_SEARCH_MAX_LEN, fuzzy_results->len);
+ for (gint n = 0; n < len; ++n)
+ {
+ match = &g_array_index (fuzzy_results, FuzzyMatch, n);
+ ar = match->value;
+ for (gint i = 0; i < ar->len; ++i)
+ {
+ color = g_ptr_array_index (ar, i);
+ new_color = gstyle_color_copy (color);
+ g_ptr_array_add (results, new_color);
+ }
+ }
+
+ fuzzy_unref (fuzzy);
+ g_ptr_array_free (ar_list, TRUE);
+ return results;
+}
+
/**
* gstyle_palette_widget_set_placeholder:
* @self: A #GstylePaletteWidget.
diff --git a/contrib/gstyle/gstyle-palette-widget.h b/contrib/gstyle/gstyle-palette-widget.h
index f27a7b7..f4b240f 100644
--- a/contrib/gstyle/gstyle-palette-widget.h
+++ b/contrib/gstyle/gstyle-palette-widget.h
@@ -63,6 +63,8 @@ GType gstyle_palette_widget_sort_mode_get_type
gboolean gstyle_palette_widget_add (GstylePaletteWidget
*self,
GstylePalette
*palette);
+GPtrArray *gstyle_palette_widget_fuzzy_parse_color_string (GstylePaletteWidget
*self,
+ const gchar
*color_string);
GstylePaletteWidgetDndLockFlags gstyle_palette_widget_get_dnd_lock (GstylePaletteWidget
*self);
GList *gstyle_palette_widget_get_list (GstylePaletteWidget
*self);
gint gstyle_palette_widget_get_n_palettes (GstylePaletteWidget
*self);
diff --git a/contrib/gstyle/gstyle-utils.c b/contrib/gstyle/gstyle-utils.c
index 7ed4b73..ff52aae 100644
--- a/contrib/gstyle/gstyle-utils.c
+++ b/contrib/gstyle/gstyle-utils.c
@@ -170,3 +170,27 @@ gstyle_utils_get_contrasted_rgba (GdkRGBA rgba,
rgba.alpha = 1.0;
}
+
+gboolean
+gstyle_utils_is_array_contains_same_color (GPtrArray *ar,
+ GstyleColor *color)
+{
+ GstyleColor *tmp_color;
+ GdkRGBA color_rgba;
+ GdkRGBA tmp_rgba;
+
+ g_return_val_if_fail (GSTYLE_IS_COLOR (color), FALSE);
+ g_return_val_if_fail (ar != NULL, FALSE);
+
+ gstyle_color_fill_rgba (color, &color_rgba);
+
+ for (gint i = 0; i < ar->len; ++i)
+ {
+ tmp_color = g_ptr_array_index (ar, i);
+ gstyle_color_fill_rgba (tmp_color, &tmp_rgba);
+ if (gdk_rgba_equal (&color_rgba, &tmp_rgba))
+ return TRUE;
+ }
+
+ return FALSE;
+}
diff --git a/contrib/gstyle/gstyle-utils.h b/contrib/gstyle/gstyle-utils.h
index 8bae505..9f49868 100644
--- a/contrib/gstyle/gstyle-utils.h
+++ b/contrib/gstyle/gstyle-utils.h
@@ -24,22 +24,26 @@
#include <gtk/gtk.h>
#include <gdk/gdk.h>
+#include "gstyle-color.h"
+
G_BEGIN_DECLS
-gboolean gstyle_str_empty0 (const gchar *str);
-gboolean gstyle_utf8_is_spaces (const gchar *str);
-void draw_cairo_round_box (cairo_t *cr,
- GdkRectangle rect,
- gint tl_radius,
- gint tr_radius,
- gint bl_radius,
- gint br_radius);
-void gstyle_utils_get_rect_resized_box (GdkRectangle src_rect,
- GdkRectangle *dst_rect,
- GtkBorder *offset);
-cairo_pattern_t *gstyle_utils_get_checkered_pattern (void);
-void gstyle_utils_get_contrasted_rgba (GdkRGBA rgba,
- GdkRGBA *dst_rgba);
+gboolean gstyle_str_empty0 (const gchar *str);
+gboolean gstyle_utf8_is_spaces (const gchar *str);
+void draw_cairo_round_box (cairo_t *cr,
+ GdkRectangle rect,
+ gint tl_radius,
+ gint tr_radius,
+ gint bl_radius,
+ gint br_radius);
+void gstyle_utils_get_rect_resized_box (GdkRectangle src_rect,
+ GdkRectangle *dst_rect,
+ GtkBorder *offset);
+cairo_pattern_t *gstyle_utils_get_checkered_pattern (void);
+void gstyle_utils_get_contrasted_rgba (GdkRGBA rgba,
+ GdkRGBA *dst_rgba);
+gboolean gstyle_utils_is_array_contains_same_color (GPtrArray *ar,
+ GstyleColor *color);
static inline guint32
pack_rgba24 (GdkRGBA *rgba)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]