[gnome-text-editor] app: disable clear-history action when unavailable



commit 6ecec5b8b9f1534e851b05020627a5490a29b8af
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jan 28 13:00:42 2022 -0800

    app: disable clear-history action when unavailable

 src/editor-application-actions.c |  6 ++++
 src/editor-session-private.h     |  1 +
 src/editor-session.c             | 63 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+)
---
diff --git a/src/editor-application-actions.c b/src/editor-application-actions.c
index 13faa26..1b84247 100644
--- a/src/editor-application-actions.c
+++ b/src/editor-application-actions.c
@@ -346,6 +346,7 @@ _editor_application_actions_init (EditorApplication *self)
     { "clear-history", editor_application_actions_clear_history },
   };
   g_autoptr(GPropertyAction) style_scheme = NULL;
+  GAction *action;
 
   g_action_map_add_action_entries (G_ACTION_MAP (self),
                                    actions,
@@ -354,4 +355,9 @@ _editor_application_actions_init (EditorApplication *self)
 
   style_scheme = g_property_action_new ("style-scheme", self, "style-scheme");
   g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (style_scheme));
+
+  action = g_action_map_lookup_action (G_ACTION_MAP (self), "clear-history");
+  g_object_bind_property (self->session, "can-clear-history",
+                          action, "enabled",
+                          G_BINDING_SYNC_CREATE);
 }
diff --git a/src/editor-session-private.h b/src/editor-session-private.h
index 5080bff..639f6d8 100644
--- a/src/editor-session-private.h
+++ b/src/editor-session-private.h
@@ -52,6 +52,7 @@ struct _EditorSession
   guint               did_restore : 1;
   guint               restore_pages : 1;
   guint               dirty : 1;
+  guint               can_clear_history : 1;
 };
 
 EditorSession *_editor_session_new                    (void);
diff --git a/src/editor-session.c b/src/editor-session.c
index 7b0d0aa..113586b 100644
--- a/src/editor-session.c
+++ b/src/editor-session.c
@@ -70,6 +70,7 @@ enum {
   PROP_AUTO_SAVE,
   PROP_AUTO_SAVE_DELAY,
   PROP_RECENTS,
+  PROP_CAN_CLEAR_HISTORY,
   N_PROPS
 };
 
@@ -325,6 +326,14 @@ find_page_for_file (EditorSession *self,
   return NULL;
 }
 
+static gboolean
+_editor_session_get_can_clear_history (EditorSession *self)
+{
+  g_assert (EDITOR_IS_SESSION (self));
+
+  return self->can_clear_history;
+}
+
 static void
 editor_session_dispose (GObject *object)
 {
@@ -385,6 +394,10 @@ editor_session_get_property (GObject    *object,
       g_value_set_uint (value, editor_session_get_auto_save_delay (self));
       break;
 
+    case PROP_CAN_CLEAR_HISTORY:
+      g_value_set_boolean (value, _editor_session_get_can_clear_history (self));
+      break;
+
     case PROP_RECENTS:
       g_value_set_object (value, editor_session_get_recents (self));
       break;
@@ -458,6 +471,18 @@ editor_session_class_init (EditorSessionClass *klass)
                        1, MAX_AUTO_SAVE_TIMEOUT_SECONDS, DEFAULT_AUTO_SAVE_TIMEOUT_SECONDS,
                        (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  /**
+   * EditorSession:can-clear-history:
+   *
+   * If the history can be cleared.
+   */
+  properties [PROP_CAN_CLEAR_HISTORY] =
+    g_param_spec_boolean ("can-clear-history",
+                          "Can Clear History",
+                          "If the history can be cleared",
+                          FALSE,
+                          (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
   /**
    * EditorSession:recents:
    *
@@ -1754,6 +1779,38 @@ failure:
   editor_session_create_window (self);
 }
 
+static void
+editor_session_recents_items_changed_cb (EditorSession      *self,
+                                         guint               position,
+                                         guint               removed,
+                                         guint               added,
+                                         EditorSidebarModel *model)
+{
+  gboolean can_clear_history = FALSE;
+  guint n_items;
+
+  g_assert (EDITOR_IS_SESSION (self));
+
+  n_items = g_list_model_get_n_items (G_LIST_MODEL (model));
+
+  for (guint i = 0; i < n_items; i++)
+    {
+      g_autoptr(EditorSidebarItem) item = g_list_model_get_item (G_LIST_MODEL (model), i);
+
+      if (!_editor_sidebar_item_get_is_modified (item))
+        {
+          can_clear_history = TRUE;
+          break;
+        }
+    }
+
+  if (can_clear_history != self->can_clear_history)
+    {
+      self->can_clear_history = can_clear_history;
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CAN_CLEAR_HISTORY]);
+    }
+}
+
 static void
 editor_session_restore (EditorSession *self,
                         GVariant      *state)
@@ -1766,6 +1823,12 @@ editor_session_restore (EditorSession *self,
 
   self->recents = _editor_sidebar_model_new (self);
 
+  g_signal_connect_object (self->recents,
+                           "items-changed",
+                           G_CALLBACK (editor_session_recents_items_changed_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+
   if (g_variant_lookup (state, "version", "u", &version) && version == 1)
     editor_session_restore_v1 (self, state);
   else


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