[gnome-builder] preferences: update action state upon window changes



commit 858254bda38341a53c3a7ea39fd8dca901e297cc
Author: Christian Hergert <chergert redhat com>
Date:   Mon Oct 10 13:43:47 2016 -0700

    preferences: update action state upon window changes
    
    When a window is added or removed, or the current perspective
    changes, we might need to toggle the sensitivity of the
    preferences action.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=772696

 libide/application/ide-application-actions.c |   34 ++++++++++++++++++++++++++
 libide/application/ide-application-actions.h |    3 +-
 libide/application/ide-application.c         |   32 ++++++++++++++++++++++++
 libide/workbench/ide-workbench.c             |    6 ++++
 4 files changed, 74 insertions(+), 1 deletions(-)
---
diff --git a/libide/application/ide-application-actions.c b/libide/application/ide-application-actions.c
index ebeb975..fb8b872 100644
--- a/libide/application/ide-application-actions.c
+++ b/libide/application/ide-application-actions.c
@@ -348,4 +348,38 @@ ide_application_actions_init (IdeApplication *self)
   gtk_application_set_accels_for_action (GTK_APPLICATION (self), "win.global-search", global_search);
   gtk_application_set_accels_for_action (GTK_APPLICATION (self), "win.show-command-bar", command_bar);
   gtk_application_set_accels_for_action (GTK_APPLICATION (self), "build-manager.build", build);
+
+  ide_application_actions_update (self);
+}
+
+void
+ide_application_actions_update (IdeApplication *self)
+{
+  GList *windows;
+  GAction *action;
+  gboolean enabled;
+
+  g_assert (IDE_IS_APPLICATION (self));
+
+  /*
+   * We only enable the preferences action if we have a workbench open
+   * that is past the greeter.
+   */
+  action = g_action_map_lookup_action (G_ACTION_MAP (self), "preferences");
+  enabled = FALSE;
+  for (windows = gtk_application_get_windows (GTK_APPLICATION (self));
+       windows != NULL;
+       windows = windows->next)
+    {
+      GtkWindow *window = windows->data;
+
+      if (IDE_IS_WORKBENCH (window) &&
+          !ide_str_equal0 ("greeter",
+                           ide_workbench_get_visible_perspective_name (IDE_WORKBENCH (window))))
+        {
+          enabled = TRUE;
+          break;
+        }
+    }
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
 }
diff --git a/libide/application/ide-application-actions.h b/libide/application/ide-application-actions.h
index e4cb961..88f4e4d 100644
--- a/libide/application/ide-application-actions.h
+++ b/libide/application/ide-application-actions.h
@@ -23,7 +23,8 @@
 
 G_BEGIN_DECLS
 
-void ide_application_actions_init (IdeApplication *self);
+void ide_application_actions_init   (IdeApplication *self);
+void ide_application_actions_update (IdeApplication *self);
 
 G_END_DECLS
 
diff --git a/libide/application/ide-application.c b/libide/application/ide-application.c
index 2bb6178..fe003b1 100644
--- a/libide/application/ide-application.c
+++ b/libide/application/ide-application.c
@@ -412,6 +412,34 @@ ide_application_shutdown (GApplication *application)
 }
 
 static void
+ide_application_window_added (GtkApplication *application,
+                              GtkWindow      *window)
+{
+  IdeApplication *self = (IdeApplication *)application;
+
+  g_assert (IDE_IS_APPLICATION (self));
+  g_assert (GTK_IS_WINDOW (window));
+
+  GTK_APPLICATION_CLASS (ide_application_parent_class)->window_added (application, window);
+
+  ide_application_actions_update (self);
+}
+
+static void
+ide_application_window_removed (GtkApplication *application,
+                                GtkWindow      *window)
+{
+  IdeApplication *self = (IdeApplication *)application;
+
+  g_assert (IDE_IS_APPLICATION (self));
+  g_assert (GTK_IS_WINDOW (window));
+
+  GTK_APPLICATION_CLASS (ide_application_parent_class)->window_removed (application, window);
+
+  ide_application_actions_update (self);
+}
+
+static void
 ide_application_finalize (GObject *object)
 {
   IdeApplication *self = (IdeApplication *)object;
@@ -437,6 +465,7 @@ ide_application_class_init (IdeApplicationClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GApplicationClass *g_app_class = G_APPLICATION_CLASS (klass);
+  GtkApplicationClass *gtk_app_class = GTK_APPLICATION_CLASS (klass);
 
   object_class->finalize = ide_application_finalize;
 
@@ -446,6 +475,9 @@ ide_application_class_init (IdeApplicationClass *klass)
   g_app_class->startup = ide_application_startup;
   g_app_class->shutdown = ide_application_shutdown;
 
+  gtk_app_class->window_added = ide_application_window_added;
+  gtk_app_class->window_removed = ide_application_window_removed;
+
   main_thread = g_thread_self ();
 }
 
diff --git a/libide/workbench/ide-workbench.c b/libide/workbench/ide-workbench.c
index 189b685..7f8e93b 100644
--- a/libide/workbench/ide-workbench.c
+++ b/libide/workbench/ide-workbench.c
@@ -24,6 +24,7 @@
 #include "ide-macros.h"
 
 #include "application/ide-application.h"
+#include "application/ide-application-actions.h"
 #include "editor/ide-editor-perspective.h"
 #include "greeter/ide-greeter-perspective.h"
 #include "preferences/ide-preferences-perspective.h"
@@ -878,6 +879,11 @@ ide_workbench_set_visible_perspective (IdeWorkbench   *self,
 
   if (restore_duration != 0)
     gtk_stack_set_transition_duration (self->perspectives_stack, restore_duration);
+
+  /* Notify the application to possibly update actions such
+   * as the preferences state.
+   */
+  ide_application_actions_update (IDE_APPLICATION_DEFAULT);
 }
 
 const gchar *


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