[tomboy] Add an option for setting custom search match highlight color



commit 477e0541ac72285e004961f514a53f4d58e016f1
Author: Alex Tereschenko <frozen and blue gmail com>
Date:   Sun Jan 22 02:14:33 2017 +0100

    Add an option for setting custom search match highlight color
    
    Closes #33 and bgo748661.
    
    Signed-off-by: Alex Tereschenko <frozen and blue gmail com>

 Tomboy/NoteEditor.cs        |   31 ++++++++++++++++
 Tomboy/NoteTag.cs           |    4 ++-
 Tomboy/Preferences.cs       |    8 ++++
 Tomboy/PreferencesDialog.cs |   46 +++++++++++++++++++++++-
 Tomboy/Utils.cs             |   85 +++++++++++++++++++++++++++++++++++++++++++
 data/tomboy.schemas.in      |   31 ++++++++++++++++
 6 files changed, 203 insertions(+), 2 deletions(-)
---
diff --git a/Tomboy/NoteEditor.cs b/Tomboy/NoteEditor.cs
index 9e75910..633e34a 100644
--- a/Tomboy/NoteEditor.cs
+++ b/Tomboy/NoteEditor.cs
@@ -35,7 +35,11 @@ namespace Tomboy
                                ModifyFont (GetGnomeDocumentFontDescription ());
                        }
 
+                       // Update search match color
+                       UpdateSearchMatchColor ();
+
                        Preferences.SettingChanged += OnFontSettingChanged;
+                       Preferences.SettingChanged += OnSearchMatchColorSettingChanged;
 
                        // Set extra editor drag targets supported (in addition
                        // to the default TextView's various text formats)...
@@ -88,6 +92,33 @@ namespace Tomboy
                                break;
                        }
                }
+
+               //
+               // Update the search match highlight color based on the changed Preference dialog setting.
+               //
+               void OnSearchMatchColorSettingChanged (object sender, NotifyEventArgs args)
+               {
+                       switch (args.Key) {
+                               case Preferences.ENABLE_CUSTOM_SEARCH_MATCH_COLOR:
+                               case Preferences.CUSTOM_SEARCH_MATCH_COLOR:
+                                       UpdateSearchMatchColor ();
+                                       break;
+                       }
+               }
+
+               void UpdateSearchMatchColor ()
+               {
+                       Gdk.Color search_match_color = GuiUtils.GetSearchMatchColor ();
+                       string search_match_color_str = GuiUtils.GetHexRgbHashStringFromGdkColor 
(search_match_color);
+
+                       if ((bool) Preferences.Get (Preferences.ENABLE_CUSTOM_SEARCH_MATCH_COLOR)) {
+                               Logger.Debug ("Switching search match highlight color to {0}", 
search_match_color_str);
+                       } else {
+                               Logger.Debug ("Switching search match highlight color to default {0}", 
search_match_color_str);
+                       }
+
+                       NoteTagTable.Instance.SearchMatchTag.BackgroundGdk = search_match_color;
+               }
                
                void UpdateCustomFontSetting ()
                {
diff --git a/Tomboy/NoteTag.cs b/Tomboy/NoteTag.cs
index 4d341ff..8f0eae6 100644
--- a/Tomboy/NoteTag.cs
+++ b/Tomboy/NoteTag.cs
@@ -521,6 +521,7 @@ namespace Tomboy
                public NoteTag UrlTag { get; private set; }
                public NoteTag LinkTag { get; private set; }
                public NoteTag BrokenLinkTag { get; private set; }
+               public NoteTag SearchMatchTag { get; private set; }
 
                void InitCommonTags ()
                {
@@ -564,11 +565,12 @@ namespace Tomboy
                        Add (tag);
 
                        tag = new NoteTag ("find-match");
-                       tag.Background = "lawngreen";
+                       tag.BackgroundGdk = GuiUtils.GetSearchMatchColor ();
                        tag.CanSerialize = false;
                        tag.CanSpellCheck = true;
                        tag.SaveType = TagSaveType.Meta;
                        Add (tag);
+                       SearchMatchTag = tag;
 
                        tag = new NoteTag ("note-title");
                        tag.Underline = Pango.Underline.Single;
diff --git a/Tomboy/Preferences.cs b/Tomboy/Preferences.cs
index 801583c..eaee68c 100644
--- a/Tomboy/Preferences.cs
+++ b/Tomboy/Preferences.cs
@@ -10,6 +10,7 @@ namespace Tomboy
                public const string ENABLE_SPELLCHECKING = "/apps/tomboy/enable_spellchecking";
                public const string ENABLE_WIKIWORDS = "/apps/tomboy/enable_wikiwords";
                public const string ENABLE_CUSTOM_FONT = "/apps/tomboy/enable_custom_font";
+               public const string ENABLE_CUSTOM_SEARCH_MATCH_COLOR = 
"/apps/tomboy/enable_custom_search_match_color";
                public const string ENABLE_KEYBINDINGS = "/apps/tomboy/enable_keybindings";
                public const string ENABLE_STARTUP_NOTES = "/apps/tomboy/enable_startup_notes";
                public const string ENABLE_AUTO_BULLETED_LISTS = "/apps/tomboy/enable_bulleted_lists";
@@ -20,6 +21,7 @@ namespace Tomboy
 
                public const string START_NOTE_URI = "/apps/tomboy/start_note";
                public const string CUSTOM_FONT_FACE = "/apps/tomboy/custom_font_face";
+               public const string CUSTOM_SEARCH_MATCH_COLOR = "/apps/tomboy/custom_search_match_color";
                public const string MENU_NOTE_COUNT = "/apps/tomboy/menu_note_count";
                public const string MENU_MAX_NOTE_COUNT = "/apps/tomboy/menu_max_note_count";
                public const string MENU_PINNED_NOTES = "/apps/tomboy/menu_pinned_notes";
@@ -78,6 +80,9 @@ namespace Tomboy
                        case ENABLE_CUSTOM_FONT:
                                return false;
 
+                       case ENABLE_CUSTOM_SEARCH_MATCH_COLOR:
+                               return false;
+
                        case ENABLE_WIKIWORDS:
                                return false;
 
@@ -102,6 +107,9 @@ namespace Tomboy
                        case CUSTOM_FONT_FACE:
                                return "Serif 11";
 
+                       case CUSTOM_SEARCH_MATCH_COLOR:
+                               return "#7cfc00"; // "lawngreen", provides good contrast with default font 
color
+
                        case MENU_NOTE_COUNT:
                                return 10;
 
diff --git a/Tomboy/PreferencesDialog.cs b/Tomboy/PreferencesDialog.cs
index 5a67ffd..6e3823c 100644
--- a/Tomboy/PreferencesDialog.cs
+++ b/Tomboy/PreferencesDialog.cs
@@ -27,6 +27,8 @@ namespace Tomboy
                Gtk.Label font_face;
                Gtk.Label font_size;
 
+               Gtk.ColorButton search_match_color_button;
+
                Mono.Addins.Gui.AddinTreeWidget addin_tree;
 
                Gtk.Button enable_addin_button;
@@ -166,7 +168,7 @@ namespace Tomboy
                        Gtk.Label label;
                        Gtk.CheckButton check;
                        Gtk.Alignment align;
-                       IPropertyEditorBool peditor, font_peditor, bullet_peditor;
+                       IPropertyEditorBool peditor, font_peditor, bullet_peditor, search_match_color_peditor;
 
                        Gtk.VBox options_list = new Gtk.VBox (false, 12);
                        options_list.BorderWidth = 12;
@@ -238,6 +240,23 @@ namespace Tomboy
 
                        font_peditor.AddGuard (font_button);
 
+                       // Custom search match highlight color
+                       Gtk.HBox search_match_color_box = new Gtk.HBox (false, 0);
+                       check = MakeCheckButton (Catalog.GetString ("Use custom search match highlight 
_color"));
+                       search_match_color_box.PackStart (check);
+
+                       search_match_color_peditor =
+                               Services.Factory.CreatePropertyEditorToggleButton 
(Preferences.ENABLE_CUSTOM_SEARCH_MATCH_COLOR, check);
+                       SetupPropertyEditor (search_match_color_peditor);
+
+                       search_match_color_button = MakeSearchMatchColorButton ();
+                       search_match_color_button.Sensitive = check.Active;
+                       search_match_color_box.PackStart (search_match_color_button);
+                       search_match_color_box.ShowAll ();
+                       options_list.PackStart (search_match_color_box, false, false, 0);
+
+                       search_match_color_peditor.AddGuard (search_match_color_button);
+
                        // Note renaming bahvior
                        Gtk.HBox rename_behavior_box = new Gtk.HBox (false, 0);
                        label = MakeLabel (Catalog.GetString ("When renaming a linked note: "));
@@ -313,6 +332,15 @@ namespace Tomboy
                        return button;
                }
 
+               Gtk.ColorButton MakeSearchMatchColorButton ()
+               {
+                       Gtk.ColorButton button = new Gtk.ColorButton (GuiUtils.GetPrefSearchMatchColor ());
+                       button.ColorSet += OnSearchMatchColorSet;
+                       button.Show ();
+
+                       return button;
+               }
+
                // Page 2
                // List of Hotkey options
                public Gtk.Widget MakeHotkeysPane ()
@@ -969,6 +997,22 @@ namespace Tomboy
                                                          desc.ToString ());
                }
 
+               // Search match highlight color change handler
+               void OnSearchMatchColorSet (object sender, EventArgs args)
+               {
+                       Gdk.Color color_pref = GuiUtils.GetSearchMatchColor ();
+                       Gdk.Color color_selected = search_match_color_button.Color;
+                       string color_selected_str = GuiUtils.GetHexRgbHashStringFromGdkColor (color_selected);
+
+                       if (!color_pref.Equal (color_selected)) {
+                               Logger.Debug ("New search match highlight color selected: {0}", 
color_selected_str);
+                               Preferences.Set (Preferences.CUSTOM_SEARCH_MATCH_COLOR, color_selected_str);
+                               NoteTagTable.Instance.SearchMatchTag.BackgroundGdk = color_selected;
+                       } else {
+                               Logger.Debug ("Same search match highlight color selected: {0}", 
color_selected_str);
+                       }
+               }
+
                private void OnAdvancedSyncConfigButton (object sender, EventArgs args)
                {
                        // Get saved behavior
diff --git a/Tomboy/Utils.cs b/Tomboy/Utils.cs
index 262b657..6df1e0a 100644
--- a/Tomboy/Utils.cs
+++ b/Tomboy/Utils.cs
@@ -308,6 +308,91 @@ namespace Tomboy
                        if (mainThreadException != null)
                                throw mainThreadException;
                }
+
+               /// <summary>
+               /// Returns Gdk.Color used as the background for the Search Match Highlight tag.
+               /// If current preference value is unparsable or custom option is not selected,
+               /// default preference value is returned instead.
+               /// </summary>
+               /// <returns>Gdk.Color for search match highlight</returns>
+               public static Gdk.Color GetSearchMatchColor ()
+               {
+                       Gdk.Color search_match_color;
+
+                       if ((bool) Preferences.Get (Preferences.ENABLE_CUSTOM_SEARCH_MATCH_COLOR)) {
+                               search_match_color = GetPrefSearchMatchColor ();
+                       } else {
+                               search_match_color = GetDefaultSearchMatchColor ();
+                       }
+
+                       return search_match_color;
+               }
+
+               /// <summary>
+               /// Gets the search match highlight color from the preference.
+               /// </summary>
+               /// <returns>The preference search match highlight color or default if unparsable.</returns>
+               public static Gdk.Color GetPrefSearchMatchColor ()
+               {
+                       string color_pref = (string) Preferences.Get (Preferences.CUSTOM_SEARCH_MATCH_COLOR);
+                       try {
+                               Gdk.Color search_match_color = GetGdkColorFromHexRgbHashString (color_pref);
+                               return search_match_color;
+                       } catch (InvalidDataException) {
+                               Logger.Error ("Cannot parse search match color from preferences {0}, will use 
defaults", color_pref);
+                               return GetDefaultSearchMatchColor ();
+                       }
+               }
+
+               /// <summary>
+               /// Gets the default highlight color of the search match.
+               /// </summary>
+               /// <returns>The default search match highlight color.</returns>
+               public static Gdk.Color GetDefaultSearchMatchColor ()
+               {
+                       string color_pref = (string) Preferences.GetDefault 
(Preferences.CUSTOM_SEARCH_MATCH_COLOR);
+
+                       // The function may return throw an exception if the color is unparsable,
+                       // but our precondition is that our default value is always parsable.
+                       return GetGdkColorFromHexRgbHashString (color_pref);
+               }
+
+               /// <summary>
+               /// Converts hex RGB hash string to Gdk.Color, see Gdk.Color.Parse() docs for acceptable 
formats.
+               /// </summary>
+               /// <returns>The Gdk.Color or throws an InvalidDataException if unparsable.</returns>
+               /// <param name="hash_string">Hex RGB hash string.</param>
+               public static Gdk.Color GetGdkColorFromHexRgbHashString (string hash_string)
+               {
+                       Gdk.Color color = new Gdk.Color ();
+
+                       if (Gdk.Color.Parse (hash_string, ref color)) {
+                               return color;
+                       } else {
+                               throw new InvalidDataException ("Search match color from preference is 
unparsable");
+                       }
+               }
+
+               /// <summary>
+               /// Converts Gdk.Color to hex RGB hash string using ToString() and some custom parsing to 
alleviate
+               /// GTK# 2.12 limitation of ToString() result being unparsable with Parse().
+               /// </summary>
+               /// <returns>The hex RGB hash string.</returns>
+               /// <param name="color">Color as Gdk.Color</param>
+               public static string GetHexRgbHashStringFromGdkColor (Gdk.Color color)
+               {
+                       // In gtk-sharp 2.12 the result of ToString() is not parsable with Parse(), need to 
pre-process.
+                       // It looks like this: "rgb:rrrr/gggg/bbbb", we want "#rrrrggggbbbb".
+                       char[] delim_chars = { ':', '/' };
+                       // First - split the string to pieces
+                       string[] color_selected_str_parts = color.ToString ().Split (delim_chars);
+                       // Replace "rgb" with "#"
+                       color_selected_str_parts[0] = "#";
+                       // Assemble it all back together
+                       string color_selected_str = string.Join (null, color_selected_str_parts);
+
+                       return color_selected_str;
+               }
        }
 
        public class GlobalKeybinder
diff --git a/data/tomboy.schemas.in b/data/tomboy.schemas.in
index 031aac3..9ac7b5f 100644
--- a/data/tomboy.schemas.in
+++ b/data/tomboy.schemas.in
@@ -91,6 +91,37 @@
     </schema>
 
     <schema>
+      <key>/schemas/apps/tomboy/enable_custom_search_match_color</key>
+      <applyto>/apps/tomboy/enable_custom_search_match_color</applyto>
+      <owner>tomboy</owner>
+      <type>bool</type>
+      <default>false</default>
+      <locale name="C">
+         <short>Enable custom search match highlight color</short>
+         <long>
+       If true, the color set in custom_search_match_color will be used as
+       the text background for search matches. Otherwise some default one
+       will be used.
+         </long>
+      </locale>
+    </schema>
+
+    <schema>
+      <key>/schemas/apps/tomboy/custom_search_match_color</key>
+      <applyto>/apps/tomboy/custom_search_match_color</applyto>
+      <owner>tomboy</owner>
+      <type>string</type>
+      <default>#7cfc00</default>
+      <locale name="C">
+         <short>Custom search match highlight color</short>
+         <long>
+       If enable_custom_search_match_color is true, the color set here
+       will be used as the text background for search matches.
+         </long>
+      </locale>
+    </schema>
+
+    <schema>
       <key>/schemas/apps/tomboy/enable_keybindings</key>
       <applyto>/apps/tomboy/enable_keybindings</applyto>
       <owner>tomboy</owner>


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