[gnome-builder/wip/mwleeds/replace: 183/185] editor-frame: Only enable replace/replace-all at appropriate times



commit a5a483170bea2d8dd1426cf43e1870949ed35cb1
Author: Matthew Leeds <mleeds redhat com>
Date:   Fri Jul 1 11:58:15 2016 -0400

    editor-frame: Only enable replace/replace-all at appropriate times
    
    When the search entry field is empty, the replace and replace-all
    actions don't make sense, so the buttons should be insensitive.
    Similarly, if no search occurrence is selected, the replace action
    doesn't make sense. This commit sets up callbacks to try to ensure the
    buttons are only sensitive (clickable) at appropriate times.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=765635

 libide/editor/ide-editor-frame-actions.c |   34 ++++++++++++++++++
 libide/editor/ide-editor-frame.c         |   57 ++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+), 0 deletions(-)
---
diff --git a/libide/editor/ide-editor-frame-actions.c b/libide/editor/ide-editor-frame-actions.c
index c449e23..42935ea 100644
--- a/libide/editor/ide-editor-frame-actions.c
+++ b/libide/editor/ide-editor-frame-actions.c
@@ -93,6 +93,8 @@ ide_editor_frame_actions_next_search_result (GSimpleAction *action,
                                             gpointer       user_data)
 {
   IdeEditorFrame *self = user_data;
+  GActionGroup *group;
+  GAction *replace_action;
 
   g_assert (IDE_IS_EDITOR_FRAME (self));
 
@@ -100,6 +102,11 @@ ide_editor_frame_actions_next_search_result (GSimpleAction *action,
 
   IDE_SOURCE_VIEW_GET_CLASS (self->source_view)->move_search
     (self->source_view, GTK_DIR_DOWN, FALSE, TRUE, TRUE, FALSE, -1);
+
+  group = gtk_widget_get_action_group (GTK_WIDGET (self->search_frame), "search-entry");
+  replace_action = g_action_map_lookup_action (G_ACTION_MAP (group), "replace");
+
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (replace_action), TRUE);
 }
 
 static void
@@ -108,6 +115,8 @@ ide_editor_frame_actions_previous_search_result (GSimpleAction *action,
                                                 gpointer       user_data)
 {
   IdeEditorFrame *self = user_data;
+  GActionGroup *group;
+  GAction *replace_action;
 
   g_assert (IDE_IS_EDITOR_FRAME (self));
 
@@ -115,6 +124,11 @@ ide_editor_frame_actions_previous_search_result (GSimpleAction *action,
 
   IDE_SOURCE_VIEW_GET_CLASS (self->source_view)->move_search
     (self->source_view, GTK_DIR_UP, FALSE, TRUE, TRUE, FALSE, -1);
+
+  group = gtk_widget_get_action_group (GTK_WIDGET (self->search_frame), "search-entry");
+  replace_action = g_action_map_lookup_action (G_ACTION_MAP (group), "replace");
+
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (replace_action), TRUE);
 }
 
 static void
@@ -222,6 +236,9 @@ ide_editor_frame_actions_exit_search (GSimpleAction *action,
 {
   IdeEditorFrame *self = user_data;
   GtkTextBuffer *buffer;
+  GActionGroup *group;
+  GAction *replace_action;
+  GAction *replace_all_action;
 
   g_assert (IDE_IS_EDITOR_FRAME (self));
 
@@ -229,6 +246,13 @@ ide_editor_frame_actions_exit_search (GSimpleAction *action,
   g_free (self->previous_search_string);
   g_object_get (self->search_entry, "text", &self->previous_search_string, NULL);
 
+  /* disable the replace and replace all actions */
+  group = gtk_widget_get_action_group (GTK_WIDGET (self->search_frame), "search-entry");
+  replace_action = g_action_map_lookup_action (G_ACTION_MAP (group), "replace");
+  replace_all_action = g_action_map_lookup_action (G_ACTION_MAP (group), "replace-all");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (replace_action), FALSE);
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (replace_all_action), FALSE);
+
   /* clear the highlights in the source view */
   ide_source_view_clear_search (self->source_view);
 
@@ -357,6 +381,7 @@ void
 ide_editor_frame_actions_init (IdeEditorFrame *self)
 {
   GSimpleActionGroup *group;
+  GAction *action;
 
   g_assert (IDE_IS_EDITOR_FRAME (self));
 
@@ -369,6 +394,15 @@ ide_editor_frame_actions_init (IdeEditorFrame *self)
   group = g_simple_action_group_new ();
   g_action_map_add_action_entries (G_ACTION_MAP (group), IdeEditorFrameSearchActions,
                                    G_N_ELEMENTS (IdeEditorFrameSearchActions), self);
+
+  /* Disable replace and replace-all by default; they should only be enabled
+   * when the corresponding operations would make sense.
+   */
+  action = g_action_map_lookup_action (G_ACTION_MAP (group), "replace");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
+  action = g_action_map_lookup_action (G_ACTION_MAP (group), "replace-all");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
+
   gtk_widget_insert_action_group (GTK_WIDGET (self->search_frame), "search-entry", G_ACTION_GROUP (group));
 
   g_object_unref (group);
diff --git a/libide/editor/ide-editor-frame.c b/libide/editor/ide-editor-frame.c
index c452239..84e89b7 100644
--- a/libide/editor/ide-editor-frame.c
+++ b/libide/editor/ide-editor-frame.c
@@ -404,6 +404,50 @@ ide_editor_frame_add_search_actions (IdeEditorFrame *self,
   g_object_unref (prop_action);
 }
 
+static void
+on_buffer_has_selection_changed (IdeEditorFrame *self,
+                                 GParamSpec     *pspec,
+                                 IdeBuffer      *buffer)
+{
+  GActionGroup *group;
+  GAction *replace_action;
+
+  g_assert (IDE_IS_EDITOR_FRAME (self));
+  g_assert (IDE_IS_BUFFER (buffer));
+
+  if (!gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (buffer)))
+  {
+    group = gtk_widget_get_action_group (GTK_WIDGET (self->search_frame), "search-entry");
+    replace_action = g_action_map_lookup_action (G_ACTION_MAP (group), "replace");
+    g_simple_action_set_enabled (G_SIMPLE_ACTION (replace_action), FALSE);
+  }
+}
+
+static void
+on_search_text_changed (IdeEditorFrame          *self,
+                        GParamSpec              *pspec,
+                        GtkSourceSearchSettings *search_settings)
+{
+  GActionGroup *group;
+  GAction *replace_action;
+  GAction *replace_all_action;
+  const gchar *search_text;
+
+  g_assert (IDE_IS_EDITOR_FRAME (self));
+  g_assert (GTK_SOURCE_IS_SEARCH_SETTINGS (search_settings));
+
+  group = gtk_widget_get_action_group (GTK_WIDGET (self->search_frame), "search-entry");
+  replace_action = g_action_map_lookup_action (G_ACTION_MAP (group), "replace");
+  replace_all_action = g_action_map_lookup_action (G_ACTION_MAP (group), "replace-all");
+
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (replace_action), FALSE);
+
+  search_text = gtk_source_search_settings_get_search_text (search_settings);
+
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (replace_all_action),
+                               ide_str_empty0 (search_text) ? FALSE : TRUE);
+}
+
 void
 ide_editor_frame_set_document (IdeEditorFrame *self,
                                IdeBuffer      *buffer)
@@ -412,6 +456,7 @@ ide_editor_frame_set_document (IdeEditorFrame *self,
   GtkSourceSearchSettings *search_settings;
   GtkTextMark *mark;
   GtkTextIter iter;
+  GActionGroup *group;
 
   g_return_if_fail (IDE_IS_EDITOR_FRAME (self));
   g_return_if_fail (IDE_IS_BUFFER (buffer));
@@ -424,6 +469,12 @@ ide_editor_frame_set_document (IdeEditorFrame *self,
                            self,
                            G_CONNECT_SWAPPED);
 
+  g_signal_connect_object (buffer,
+                           "notify::has-selection",
+                           G_CALLBACK (on_buffer_has_selection_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
+
   self->cursor_moved_handler =
     g_signal_connect (buffer,
                       "cursor-moved",
@@ -453,6 +504,12 @@ ide_editor_frame_set_document (IdeEditorFrame *self,
    */
   group = gtk_widget_get_action_group (GTK_WIDGET (self->search_frame), "search-entry");
   ide_editor_frame_add_search_actions (self, group);
+
+  g_signal_connect_object (search_settings,
+                           "notify::search-text",
+                           G_CALLBACK (on_search_text_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
 }
 
 static gboolean


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