This new feature shows a color tip if the mouse is hovered over a color value like e.g. #fff or #ffffff. If a color value is double clicked, then the Color Chooser is started. Both (showing the color tip and starting the Color Chooser) can be enabled and disabled via the plugin preferences. See #663.
I hope it can be added to the AddOns plugin as I think it's not worth writing a new complete own plugin for it. You can view, comment on, or merge this pull request online at:
https://github.com/geany/geany-plugins/pull/664
-- Commit Summary --
* addons: show color tip and start Color Chooser with double click * addons: fixed typo.
-- File Changes --
M addons/src/Makefile.am (3) M addons/src/addons.c (42) A addons/src/ao_colortip.c (397) A addons/src/ao_colortip.h (51)
-- Patch Links --
https://github.com/geany/geany-plugins/pull/664.patch https://github.com/geany/geany-plugins/pull/664.diff
A little mp4 screencast showing how it looks: [Screencast-Addons-ColorTip.mp4.zip](https://github.com/geany/geany-plugins/files/1578924/Screencast-Addons-Color...)
I made a plugin a lot like this before, except the tooltip had a square colour swatch and next to it it listed the hex, rgb, hsv, values. I have no clue where I put it though.
@codebrainz: well, if you find it we can take whatever is preferred. Or I can change the look of this one if you got any requests/suggestions for improvement.
b4n commented on this pull request.
- $Id$
+ */ + + +#include <gtk/gtk.h> +#include <glib-object.h> + +#include "geanyplugin.h" + +#include "addons.h" +#include "ao_colortip.h" + +typedef struct _AoColorTipPrivate AoColorTipPrivate; + +#define AO_COLORTIP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj),\ + AO_COLORTIP_TYPE, AoColorTipPrivate))
it is recommended nowadays to have a `priv` member in the object struct that points to the private structure because it's faster. It might not be very important here, but that's a good practice. But having a private struct is not very useful here at all, as the "public" struct itself is also private anyway, so actually I'd rather get rid of it and but everything in the regular struct.
+static void connect_documents_button_press_signal_handler(AoColorTip *colortip)
+{ + guint i = 0; + /* connect the button-press event for all open documents */ + foreach_document(i) + { + connect_document_button_press_signal_handler(colortip, documents[i]); + } +} + + +static void ao_color_tip_finalize(GObject *object) +{ + g_return_if_fail(object != NULL); + g_return_if_fail(IS_AO_COLORTIP(object)); +
shouldn't you disconnect the signal handler (`on_editor_button_press_event`) here? as you use `plugin_signal_connect()` it'll be disconnected when the plugin is unloaded, but you still have to take care it's not called after the AoColortip is destroyed.
+ * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $Id$
I guess that's a c&p leftover? doesn't seem very useful with Git, is it :)
+ +#include <gtk/gtk.h> +#include <glib-object.h> + +#include "geanyplugin.h" + +#include "addons.h" +#include "ao_colortip.h" + +typedef struct _AoColorTipPrivate AoColorTipPrivate; + +#define AO_COLORTIP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj),\ + AO_COLORTIP_TYPE, AoColorTipPrivate)) + +struct _AoColorTip
naming convention: normally GObjects names is `FooBar` and `FOO_BAR`; so here it would be `AoColortip` and `AO_COLORTIP`, or `AoColorTip` and `AO_COLOR_TIP`. That's more a FYI comment, it doesn't actually matter here, it's only rather important to follow if you use automated tools relying on these conventions (like possibly API extractors & co)
- */
+ + +#ifndef __AO_COLORTIP_H__ +#define __AO_COLORTIP_H__ + +G_BEGIN_DECLS + +#define AO_COLORTIP_TYPE (ao_color_tip_get_type()) +#define AO_COLORTIP(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\ + AO_COLORTIP_TYPE, AoColorTip)) +#define AO_COLORTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),\ + AO_COLORTIP_TYPE, AoColorTipClass)) +#define IS_AO_COLORTIP(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),\ + AO_COLORTIP_TYPE)) +#define IS_AO_COLORTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),\
Also about naming, the conventional naming would be `AO_IS_COLORTIP` (or `AO_IS_COLOR_TIP`) -- e.g. `AO` is the namespace. But anyway, following the convention the plugin already has makes sense.
- if (end > max)
+ { + end = max; + } + + /* Get text in range and examine it */ + subtext = sci_get_contents_range(doc->editor->sci, start, end); + if (subtext != NULL) + { + pos = pos - start; + color = contains_color_value(subtext, pos, 1); + g_free(subtext); + } + + return color; +}
any reason why you don't simply use `editor_get_word_at_pos()`? Maybe something similar to this or whatnot.
```c gchar *word = editor_get_word_at_pos(doc->editor, -1, "0123456789abcdefABCDEF");
if (word) { gint color = -1;
switch (strlen (color)) { case 3: color = ((g_ascii_xdigit_value(word[0]) * 0x11) << 16 | (g_ascii_xdigit_value(word[1]) * 0x11) << 8 | (g_ascii_xdigit_value(word[2]) * 0x11) << 0); break; case 6: color = (g_ascii_xdigit_value(word[0]) << 20 | g_ascii_xdigit_value(word[1]) << 16 | g_ascii_xdigit_value(word[2]) << 12 | g_ascii_xdigit_value(word[3]) << 8 | g_ascii_xdigit_value(word[4]) << 4 | g_ascii_xdigit_value(word[5]) << 0); break; default: /* invalid color or other format */ break; } // … ````
LarsGit223 commented on this pull request.
+ * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $Id$
Yes, copied from ```ao_markword.c```, maybe that should be changed to.
LarsGit223 commented on this pull request.
+static void connect_documents_button_press_signal_handler(AoColorTip *colortip)
+{ + guint i = 0; + /* connect the button-press event for all open documents */ + foreach_document(i) + { + connect_document_button_press_signal_handler(colortip, documents[i]); + } +} + + +static void ao_color_tip_finalize(GObject *object) +{ + g_return_if_fail(object != NULL); + g_return_if_fail(IS_AO_COLORTIP(object)); +
The AddOns-Plugin is calling ```plugin_module_make_resident()``` so do I really need to handle that?
codebrainz commented on this pull request.
+static void connect_documents_button_press_signal_handler(AoColorTip *colortip)
+{ + guint i = 0; + /* connect the button-press event for all open documents */ + foreach_document(i) + { + connect_document_button_press_signal_handler(colortip, documents[i]); + } +} + + +static void ao_color_tip_finalize(GObject *object) +{ + g_return_if_fail(object != NULL); + g_return_if_fail(IS_AO_COLORTIP(object)); +
I'm not familiar with Addons machinery, but I think he means when you disable this feature and this GObject instance is deleted, there will still be connections to the dangling instance pointer. You should be able to test by enabling the feature, opening a few documents, disabling the feature, and then triggering button press events on the editor widgets.
b4n commented on this pull request.
+static void connect_documents_button_press_signal_handler(AoColorTip *colortip)
+{ + guint i = 0; + /* connect the button-press event for all open documents */ + foreach_document(i) + { + connect_document_button_press_signal_handler(colortip, documents[i]); + } +} + + +static void ao_color_tip_finalize(GObject *object) +{ + g_return_if_fail(object != NULL); + g_return_if_fail(IS_AO_COLORTIP(object)); +
As @codebrainz said it depends how addons actually work, if the AoColortip object lives as long as the plugin does it would be fine I guess.
LarsGit223 commented on this pull request.
+static void connect_documents_button_press_signal_handler(AoColorTip *colortip)
+{ + guint i = 0; + /* connect the button-press event for all open documents */ + foreach_document(i) + { + connect_document_button_press_signal_handler(colortip, documents[i]); + } +} + + +static void ao_color_tip_finalize(GObject *object) +{ + g_return_if_fail(object != NULL); + g_return_if_fail(IS_AO_COLORTIP(object)); +
@codebrainz, @b4n: if I understood it right all object should live as long as the plugin. But I will test it to verify it and report the result (as soon as I got time for it).
elextr commented on this pull request.
+static void connect_documents_button_press_signal_handler(AoColorTip *colortip)
+{ + guint i = 0; + /* connect the button-press event for all open documents */ + foreach_document(i) + { + connect_document_button_press_signal_handler(colortip, documents[i]); + } +} + + +static void ao_color_tip_finalize(GObject *object) +{ + g_return_if_fail(object != NULL); + g_return_if_fail(IS_AO_COLORTIP(object)); +
Shouldn't the object be deleted (or not created in the first place) if the feature is disabled?
It would be great when writing CSS if the color chooser was able to insert/edit rgb(...) and rgba(...) color values as well as hex color codes. :-)
@LarsGit223 pushed 1 commit.
44200fc addons: corrected file header comments.
LarsGit223 commented on this pull request.
+ * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $Id$
Fixed.
@LarsGit223 pushed 2 commits.
d7e345e addons: re-factored function 'get_color_value_at_current_doc_position()' (thanks to b4n). 5309ece addons: fixed missing SCI_SETMOUSEDWELLTIME.
LarsGit223 commented on this pull request.
- if (end > max)
+ { + end = max; + } + + /* Get text in range and examine it */ + subtext = sci_get_contents_range(doc->editor->sci, start, end); + if (subtext != NULL) + { + pos = pos - start; + color = contains_color_value(subtext, pos, 1); + g_free(subtext); + } + + return color; +}
Thanks, used your code snippet.
LarsGit223 commented on this pull request.
+static void connect_documents_button_press_signal_handler(AoColorTip *colortip)
+{ + guint i = 0; + /* connect the button-press event for all open documents */ + foreach_document(i) + { + connect_document_button_press_signal_handler(colortip, documents[i]); + } +} + + +static void ao_color_tip_finalize(GObject *object) +{ + g_return_if_fail(object != NULL); + g_return_if_fail(IS_AO_COLORTIP(object)); +
I tested what @codebrainz described above and nothing happens. So I assume that everything is fine. If not I would have expected a crash if an old/invalid pointer would still be used.
Then I read @b4n's first comment again: AoColortip is only destroyed in function ```plugin_cleanup()```, so it should be OK.
LarsGit223 commented on this pull request.
+static void connect_documents_button_press_signal_handler(AoColorTip *colortip)
+{ + guint i = 0; + /* connect the button-press event for all open documents */ + foreach_document(i) + { + connect_document_button_press_signal_handler(colortip, documents[i]); + } +} + + +static void ao_color_tip_finalize(GObject *object) +{ + g_return_if_fail(object != NULL); + g_return_if_fail(IS_AO_COLORTIP(object)); +
I wonder if I should throw away the whole GObject stuff and pass the config values directly from the ```ao_info``` struct in ```addons.c```. I only used it because I saw it in ```ao_markword.c```. I think it's quite complicated stuff for just saving to values. Is there any reason for not removing it?
@nicktrosnant: good idea but I guess it will not be supported by now. Geany is still supporting Gtk2 and is therefore using the function ```gdk_color_parse()``` which doesn't support rgb/rgba values. If in the future GTk2 support is dropped then the function ```gdk_rgba_parse ()``` from Gtk3 could be used to do the job. It also supports rgb/rgba values.
I merge this as I think it looks okish even might improvements can be done. ... e.g. I didn't see any README-update .... @LarsGit223 Can you follow up here with a new PR?
Merged #664.
@frlan: I agree, basically the rest is as copied, e.g. naming convention should be as in the existing addons code.
Can you follow up here with a new PR?
Completely forgot it, will post a README in a new PR.
github-comments@lists.geany.org