[tepl] WindowActionsEdit: move code to update basic edit actions sensitivity



commit a02038c7147019a74d124ab1b17179be8067b8bb
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon May 4 04:40:20 2020 +0200

    WindowActionsEdit: move code to update basic edit actions sensitivity

 tepl/tepl-application-window.c  | 88 +----------------------------------------
 tepl/tepl-window-actions-edit.c | 72 +++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 87 deletions(-)
---
diff --git a/tepl/tepl-application-window.c b/tepl/tepl-application-window.c
index 029299e..3aadef0 100644
--- a/tepl/tepl-application-window.c
+++ b/tepl/tepl-application-window.c
@@ -194,78 +194,9 @@ goto_line_change_state_cb (GSimpleAction *action,
        update_goto_line (tepl_window);
 }
 
-static void
-update_basic_edit_actions_sensitivity (TeplApplicationWindow *tepl_window)
-{
-       TeplView *view;
-       TeplBuffer *buffer;
-       gboolean view_is_editable = FALSE;
-       gboolean buffer_has_selection = FALSE;
-       GActionMap *action_map;
-       GAction *action;
-
-       view = tepl_tab_group_get_active_view (TEPL_TAB_GROUP (tepl_window));
-       buffer = tepl_tab_group_get_active_buffer (TEPL_TAB_GROUP (tepl_window));
-
-       if (view != NULL)
-       {
-               view_is_editable = gtk_text_view_get_editable (GTK_TEXT_VIEW (view));
-       }
-
-       if (buffer != NULL)
-       {
-               buffer_has_selection = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (buffer));
-       }
-
-       action_map = G_ACTION_MAP (tepl_window->priv->gtk_window);
-
-       action = g_action_map_lookup_action (action_map, "tepl-cut");
-       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
-                                    view_is_editable && buffer_has_selection);
-
-       action = g_action_map_lookup_action (action_map, "tepl-copy");
-       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
-                                    buffer_has_selection);
-
-       /* tepl-paste is treated separately with
-        * update_paste_action_sensitivity(), to request the clipboard only when
-        * necessary.
-        */
-
-       action = g_action_map_lookup_action (action_map, "tepl-delete");
-       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
-                                    view_is_editable && buffer_has_selection);
-
-       action = g_action_map_lookup_action (action_map, "tepl-select-all");
-       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
-                                    buffer != NULL);
-
-       action = g_action_map_lookup_action (action_map, "tepl-indent");
-       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
-                                    view_is_editable);
-
-       action = g_action_map_lookup_action (action_map, "tepl-unindent");
-       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
-                                    view_is_editable);
-}
-
-static void
-update_actions_sensitivity (TeplApplicationWindow *tepl_window)
-{
-       update_basic_edit_actions_sensitivity (tepl_window);
-       update_goto_line_action_sensitivity (tepl_window);
-}
-
 static void
 add_actions (TeplApplicationWindow *tepl_window)
 {
-       /* The actions need to be namespaced, to not conflict with the
-        * application or other libraries.
-        *
-        * Do not forget to document each action in the TeplApplicationWindow
-        * class description, and to add the corresponding AmtkActionInfoEntry
-        * in tepl-application.c.
-        */
        const GActionEntry entries[] = {
                /* Search menu */
                { "tepl-goto-line", goto_line_activate_cb, NULL, "false", goto_line_change_state_cb },
@@ -281,7 +212,7 @@ add_actions (TeplApplicationWindow *tepl_window)
        g_assert (tepl_window->priv->window_actions_edit == NULL);
        tepl_window->priv->window_actions_edit = _tepl_window_actions_edit_new (tepl_window);
 
-       update_actions_sensitivity (tepl_window);
+       update_goto_line_action_sensitivity (tepl_window);
 }
 
 static void
@@ -620,7 +551,6 @@ tepl_application_window_get_application_window (TeplApplicationWindow *tepl_wind
 static void
 active_tab_changed (TeplApplicationWindow *tepl_window)
 {
-       update_basic_edit_actions_sensitivity (tepl_window);
        update_goto_line (tepl_window);
        update_title (tepl_window);
 }
@@ -630,7 +560,6 @@ active_view_editable_notify_cb (GtkTextView           *active_view,
                                GParamSpec            *pspec,
                                TeplApplicationWindow *tepl_window)
 {
-       update_basic_edit_actions_sensitivity (tepl_window);
        update_title (tepl_window);
 }
 
@@ -656,14 +585,6 @@ active_view_changed (TeplApplicationWindow *tepl_window)
                                                  tepl_window));
 }
 
-static void
-active_buffer_has_selection_notify_cb (GtkTextBuffer         *buffer,
-                                      GParamSpec            *pspec,
-                                      TeplApplicationWindow *tepl_window)
-{
-       update_basic_edit_actions_sensitivity (tepl_window);
-}
-
 static void
 active_buffer_full_title_notify_cb (TeplBuffer            *buffer,
                                    GParamSpec            *pspec,
@@ -687,12 +608,6 @@ active_buffer_changed (TeplApplicationWindow *tepl_window)
 
        tepl_window->priv->buffer_signal_group = _tepl_signal_group_new (G_OBJECT (active_buffer));
 
-       _tepl_signal_group_add (tepl_window->priv->buffer_signal_group,
-                               g_signal_connect (active_buffer,
-                                                 "notify::has-selection",
-                                                 G_CALLBACK (active_buffer_has_selection_notify_cb),
-                                                 tepl_window));
-
        _tepl_signal_group_add (tepl_window->priv->buffer_signal_group,
                                g_signal_connect (active_buffer,
                                                  "notify::tepl-full-title",
@@ -700,7 +615,6 @@ active_buffer_changed (TeplApplicationWindow *tepl_window)
                                                  tepl_window));
 
 end:
-       update_basic_edit_actions_sensitivity (tepl_window);
        update_title (tepl_window);
 }
 
diff --git a/tepl/tepl-window-actions-edit.c b/tepl/tepl-window-actions-edit.c
index bbe5b14..b048628 100644
--- a/tepl/tepl-window-actions-edit.c
+++ b/tepl/tepl-window-actions-edit.c
@@ -332,6 +332,61 @@ update_paste_action_sensitivity (TeplApplicationWindow *tepl_window)
                                       tepl_window);
 }
 
+static void
+update_basic_edit_actions_sensitivity (TeplApplicationWindow *tepl_window)
+{
+       TeplView *view;
+       TeplBuffer *buffer;
+       gboolean view_is_editable = FALSE;
+       gboolean buffer_has_selection = FALSE;
+       GActionMap *action_map;
+       GAction *action;
+
+       view = tepl_tab_group_get_active_view (TEPL_TAB_GROUP (tepl_window));
+       buffer = tepl_tab_group_get_active_buffer (TEPL_TAB_GROUP (tepl_window));
+
+       if (view != NULL)
+       {
+               view_is_editable = gtk_text_view_get_editable (GTK_TEXT_VIEW (view));
+       }
+
+       if (buffer != NULL)
+       {
+               buffer_has_selection = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (buffer));
+       }
+
+       action_map = G_ACTION_MAP (tepl_application_window_get_application_window (tepl_window));
+
+       action = g_action_map_lookup_action (action_map, "tepl-cut");
+       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                                    view_is_editable && buffer_has_selection);
+
+       action = g_action_map_lookup_action (action_map, "tepl-copy");
+       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                                    buffer_has_selection);
+
+       /* tepl-paste is treated separately with
+        * update_paste_action_sensitivity(), to request the clipboard only when
+        * necessary.
+        */
+
+       action = g_action_map_lookup_action (action_map, "tepl-delete");
+       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                                    view_is_editable && buffer_has_selection);
+
+       action = g_action_map_lookup_action (action_map, "tepl-select-all");
+       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                                    buffer != NULL);
+
+       action = g_action_map_lookup_action (action_map, "tepl-indent");
+       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                                    view_is_editable);
+
+       action = g_action_map_lookup_action (action_map, "tepl-unindent");
+       g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                                    view_is_editable);
+}
+
 static void
 active_view_editable_notify_cb (GtkTextView           *active_view,
                                GParamSpec            *pspec,
@@ -339,6 +394,7 @@ active_view_editable_notify_cb (GtkTextView           *active_view,
 {
        update_undo_redo_actions_sensitivity (window_actions_edit->tepl_window);
        update_paste_action_sensitivity (window_actions_edit->tepl_window);
+       update_basic_edit_actions_sensitivity (window_actions_edit->tepl_window);
 }
 
 static void
@@ -365,6 +421,7 @@ active_view_changed (TeplWindowActionsEdit *window_actions_edit)
 end:
        update_undo_redo_actions_sensitivity (window_actions_edit->tepl_window);
        update_paste_action_sensitivity (window_actions_edit->tepl_window);
+       update_basic_edit_actions_sensitivity (window_actions_edit->tepl_window);
 }
 
 static void
@@ -391,6 +448,14 @@ active_buffer_can_redo_notify_cb (GtkSourceBuffer       *buffer,
        update_undo_redo_actions_sensitivity (window_actions_edit->tepl_window);
 }
 
+static void
+active_buffer_has_selection_notify_cb (GtkTextBuffer         *buffer,
+                                      GParamSpec            *pspec,
+                                      TeplWindowActionsEdit *window_actions_edit)
+{
+       update_basic_edit_actions_sensitivity (window_actions_edit->tepl_window);
+}
+
 static void
 active_buffer_changed (TeplWindowActionsEdit *window_actions_edit)
 {
@@ -418,8 +483,15 @@ active_buffer_changed (TeplWindowActionsEdit *window_actions_edit)
                                                  G_CALLBACK (active_buffer_can_redo_notify_cb),
                                                  window_actions_edit));
 
+       _tepl_signal_group_add (window_actions_edit->buffer_signal_group,
+                               g_signal_connect (active_buffer,
+                                                 "notify::has-selection",
+                                                 G_CALLBACK (active_buffer_has_selection_notify_cb),
+                                                 window_actions_edit));
+
 end:
        update_undo_redo_actions_sensitivity (window_actions_edit->tepl_window);
+       update_basic_edit_actions_sensitivity (window_actions_edit->tepl_window);
 }
 
 static void


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