[gnome-system-monitor] Port page-switching to GAction



commit 2450d8af2d81e5d4b5a0827375b2419501df7015
Author: Stefano Facchini <stefano facchini gmail com>
Date:   Wed Jul 17 20:34:17 2013 +0200

    Port page-switching to GAction
    
    The code is reworked to consistently make use of GAction and
    GSettings.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=704467

 src/callbacks.cpp                                 |   71 --------------
 src/callbacks.h                                   |    6 -
 src/interface.cpp                                 |  106 +++++++++++++++------
 src/org.gnome.gnome-system-monitor.gschema.xml.in |    1 +
 src/procdialogs.cpp                               |    2 +-
 src/procman-app.cpp                               |   23 ++---
 src/procman-app.h                                 |    1 -
 7 files changed, 87 insertions(+), 123 deletions(-)
---
diff --git a/src/callbacks.cpp b/src/callbacks.cpp
index 543ed0f..6204790 100644
--- a/src/callbacks.cpp
+++ b/src/callbacks.cpp
@@ -376,77 +376,6 @@ cb_tree_popup_menu (GtkWidget *widget, gpointer data)
 }
 
 
-void
-cb_switch_page (GtkNotebook *nb, GtkWidget *page,
-                gint num, gpointer data)
-{
-    cb_change_current_page (nb, num, data);
-}
-
-void
-cb_change_current_page (GtkNotebook *nb, gint num, gpointer data)
-{
-    ProcmanApp * const app = static_cast<ProcmanApp *>(data);
-
-    app->config.current_tab = num;
-
-
-    if (num == PROCMAN_TAB_PROCESSES) {
-
-        cb_timeout (app);
-
-        if (!app->timeout)
-            app->timeout = g_timeout_add (
-                app->config.update_interval,
-                cb_timeout, app);
-
-        update_sensitivity(app);
-        gtk_widget_grab_focus(app->tree);
-    }
-    else {
-        if (app->timeout) {
-            g_source_remove (app->timeout);
-            app->timeout = 0;
-        }
-
-        update_sensitivity(app);
-    }
-
-
-    if (num == PROCMAN_TAB_RESOURCES) {
-        load_graph_start (app->cpu_graph);
-        load_graph_start (app->mem_graph);
-        load_graph_start (app->net_graph);
-    }
-    else {
-        load_graph_stop (app->cpu_graph);
-        load_graph_stop (app->mem_graph);
-        load_graph_stop (app->net_graph);
-    }
-
-
-    if (num == PROCMAN_TAB_DISKS) {
-
-        cb_update_disks (app);
-
-        if(!app->disk_timeout) {
-            app->disk_timeout =
-                g_timeout_add (app->config.disks_update_interval,
-                               cb_update_disks,
-                               app);
-        }
-    }
-    else {
-        if(app->disk_timeout) {
-            g_source_remove (app->disk_timeout);
-            app->disk_timeout = 0;
-        }
-    }
-
-}
-
-
-
 gint
 cb_user_refresh (GtkAction*, gpointer data)
 {
diff --git a/src/callbacks.h b/src/callbacks.h
index 3be5220..4ec4730 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -56,12 +56,6 @@ gboolean        cb_tree_popup_menu (GtkWidget *widget, gpointer data);
 gboolean        cb_tree_button_pressed (GtkWidget *widget, GdkEventButton *event,
                                         gpointer data);
 
-
-void            cb_change_current_page (GtkNotebook *nb,
-                                        gint num, gpointer data);
-void            cb_switch_page (GtkNotebook *nb, GtkWidget *page,
-                                gint num, gpointer data);
-
 gint            cb_update_disks (gpointer data);
 gint            cb_user_refresh (GtkAction* action, gpointer data);
 gint            cb_timeout (gpointer data);
diff --git a/src/interface.cpp b/src/interface.cpp
index 8e3b535..370257c 100644
--- a/src/interface.cpp
+++ b/src/interface.cpp
@@ -40,7 +40,6 @@
 #include "gsm_color_button.h"
 
 static void     cb_toggle_tree (GtkAction *action, gpointer data);
-static void     cb_proc_goto_tab (gint tab);
 
 static const GtkActionEntry menu_entries[] =
 {
@@ -308,6 +307,78 @@ on_activate_about (GSimpleAction *, GVariant *, gpointer data)
     cb_about (NULL, data);
 }
 
+static void
+on_activate_radio (GSimpleAction *action, GVariant *parameter, gpointer data)
+{
+    g_action_change_state (G_ACTION (action), parameter);
+}
+
+static void
+change_show_page_state (GSimpleAction *action, GVariant *state, gpointer data)
+{
+    ProcmanApp *app = (ProcmanApp *) data;
+
+    g_simple_action_set_state (action, state);
+    g_settings_set_value (app->settings, "current-tab", state);
+}
+
+void
+update_page_activities (ProcmanApp *app)
+{
+    int current_page = gtk_notebook_get_current_page (GTK_NOTEBOOK (app->notebook));
+
+    if (current_page == PROCMAN_TAB_PROCESSES) {
+        cb_timeout (app);
+
+        if (!app->timeout) {
+            app->timeout = g_timeout_add (app->config.update_interval,
+                                          cb_timeout, app);
+        }
+
+        update_sensitivity (app);
+
+        gtk_widget_grab_focus (app->tree);
+    } else {
+        if (app->timeout) {
+            g_source_remove (app->timeout);
+            app->timeout = 0;
+        }
+
+        update_sensitivity (app);
+    }
+
+    if (current_page == PROCMAN_TAB_RESOURCES) {
+        load_graph_start (app->cpu_graph);
+        load_graph_start (app->mem_graph);
+        load_graph_start (app->net_graph);
+    } else {
+        load_graph_stop (app->cpu_graph);
+        load_graph_stop (app->mem_graph);
+        load_graph_stop (app->net_graph);
+    }
+
+    if (current_page == PROCMAN_TAB_DISKS) {
+        cb_update_disks (app);
+
+        if (!app->disk_timeout) {
+            app->disk_timeout = g_timeout_add (app->config.disks_update_interval,
+                                               cb_update_disks,
+                                               app);
+        }
+    } else {
+        if (app->disk_timeout) {
+            g_source_remove (app->disk_timeout);
+            app->disk_timeout = 0;
+        }
+    }
+}
+
+static void
+cb_change_current_page (GtkNotebook *notebook, GParamSpec *pspec, gpointer data)
+{
+    update_page_activities ((ProcmanApp *)data);
+}
+
 void
 create_main_window (ProcmanApp *app)
 {
@@ -325,7 +396,8 @@ create_main_window (ProcmanApp *app)
     gtk_widget_set_name (main_window, "gnome-system-monitor");
 
     GActionEntry win_action_entries[] = {
-        { "about", on_activate_about, NULL, NULL, NULL }
+        { "about", on_activate_about, NULL, NULL, NULL },
+        { "show-page", on_activate_radio, "i", "0", change_show_page_state }
     };
 
     g_action_map_add_action_entries (G_ACTION_MAP (main_window),
@@ -393,37 +465,22 @@ create_main_window (ProcmanApp *app)
     /* create the main notebook */
     app->notebook = notebook = GTK_WIDGET (gtk_builder_get_object (builder, "notebook"));
 
-
     create_proc_view(app, builder);
 
     create_sys_view (app, builder);
     
     create_disk_view (app, builder);
 
+    g_settings_bind (app->settings, "current-tab", notebook, "page", G_SETTINGS_BIND_DEFAULT);
 
-    g_signal_connect (G_OBJECT (notebook), "switch-page",
-                      G_CALLBACK (cb_switch_page), app);
-    g_signal_connect (G_OBJECT (notebook), "change-current-page",
+    g_signal_connect (G_OBJECT (notebook), "notify::page",
                       G_CALLBACK (cb_change_current_page), app);
+    update_page_activities (app);
 
-    gtk_widget_show_all(notebook); // need to make page switch work
-    gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), app->config.current_tab);
-    cb_change_current_page (GTK_NOTEBOOK (notebook), app->config.current_tab, app);
     g_signal_connect (G_OBJECT (main_window), "delete_event",
                       G_CALLBACK (cb_main_window_delete),
                       app);
 
-    GtkAccelGroup *accel_group;
-    GClosure *goto_tab_closure[4];
-    accel_group = gtk_accel_group_new ();
-    gtk_window_add_accel_group (GTK_WINDOW(main_window), accel_group);
-    for (i = 0; i < 4; ++i) {
-        goto_tab_closure[i] = g_cclosure_new_swap (G_CALLBACK (cb_proc_goto_tab),
-                                                   GINT_TO_POINTER (i), NULL);
-        gtk_accel_group_connect (accel_group, '0'+(i+1),
-                                 GDK_MOD1_MASK, GTK_ACCEL_VISIBLE,
-                                 goto_tab_closure[i]);
-    }
     action = gtk_action_group_get_action (app->action_group, "ShowDependencies");
     gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
                                   app->config.show_tree);
@@ -479,7 +536,7 @@ update_sensitivity(ProcmanApp *app)
     gboolean processes_sensitivity, selected_sensitivity;
     GtkAction *action;
 
-    processes_sensitivity = (app->config.current_tab == PROCMAN_TAB_PROCESSES);
+    processes_sensitivity = (g_settings_get_int (app->settings, "current-tab") == PROCMAN_TAB_PROCESSES);
     selected_sensitivity = (processes_sensitivity && app->selected_process != NULL);
 
     if(app->endprocessbutton) {
@@ -533,10 +590,3 @@ cb_toggle_tree (GtkAction *action, gpointer data)
 
     g_settings_set_boolean (settings, "show-tree", show);
 }
-
-static void
-cb_proc_goto_tab (gint tab)
-{
-    Glib::RefPtr<ProcmanApp> app = ProcmanApp::get();
-    gtk_notebook_set_current_page (GTK_NOTEBOOK (app->notebook), tab);
-}
diff --git a/src/org.gnome.gnome-system-monitor.gschema.xml.in 
b/src/org.gnome.gnome-system-monitor.gschema.xml.in
index 998ba10..314faa6 100644
--- a/src/org.gnome.gnome-system-monitor.gschema.xml.in
+++ b/src/org.gnome.gnome-system-monitor.gschema.xml.in
@@ -111,6 +111,7 @@
     </key>
 
     <key name="current-tab" type="i">
+      <range min="0" max="2"/>
       <default>1
       </default>
       <_summary>Saves the currently viewed tab
diff --git a/src/procdialogs.cpp b/src/procdialogs.cpp
index a5878dd..b5548eb 100644
--- a/src/procdialogs.cpp
+++ b/src/procdialogs.cpp
@@ -468,7 +468,7 @@ procdialog_create_preferences_dialog (ProcmanApp *app)
     g_signal_connect (G_OBJECT (prefs_dialog), "response",
                       G_CALLBACK (prefs_dialog_button_pressed), app);
 
-    switch (app->config.current_tab) {
+    switch (g_settings_get_int (app->settings, "current-tab")) {
         case PROCMAN_TAB_PROCESSES:
             gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 0);
             break;
diff --git a/src/procman-app.cpp b/src/procman-app.cpp
index a829730..671077b 100644
--- a/src/procman-app.cpp
+++ b/src/procman-app.cpp
@@ -291,7 +291,6 @@ ProcmanApp::load_settings()
 
     config.whose_process = g_settings_get_int (settings, "view-as");
     g_signal_connect (G_OBJECT(settings), "changed::view-as", G_CALLBACK(view_as_changed_cb), this);
-    config.current_tab = g_settings_get_int (settings, "current-tab");
 
     /* Determine number of cpus since libgtop doesn't really tell you*/
     config.num_cpus = 0;
@@ -351,9 +350,6 @@ ProcmanApp::load_settings()
     config.graph_update_interval = MAX (config.graph_update_interval, 250);
     config.disks_update_interval = MAX (config.disks_update_interval, 1000);
     config.whose_process = CLAMP (config.whose_process, 0, 2);
-    config.current_tab = CLAMP(config.current_tab,
-                               PROCMAN_TAB_PROCESSES,
-                               PROCMAN_TAB_DISKS);
 }
 
 ProcmanApp::ProcmanApp() : Gtk::Application("org.gnome.SystemMonitor", Gio::APPLICATION_HANDLES_COMMAND_LINE)
@@ -376,13 +372,6 @@ void ProcmanApp::on_activate()
     gtk_window_present (GTK_WINDOW (main_window));
 }
 
-static void
-set_tab(GtkNotebook* notebook, gint tab, ProcmanApp *app)
-{
-    gtk_notebook_set_current_page(notebook, tab);
-    cb_change_current_page(notebook, tab, app);
-}
-
 gboolean
 procman_get_tree_state (GSettings *settings, GtkWidget *tree, const gchar *child_schema)
 {
@@ -561,8 +550,6 @@ ProcmanApp::save_config ()
     g_settings_set_int (settings, "y-position", config.ypos);
     g_settings_set_boolean (settings, "maximized", config.maximized);
 
-    g_settings_set_int (settings, "current-tab", config.current_tab);
-
     g_settings_sync ();
 }
 
@@ -587,13 +574,13 @@ int ProcmanApp::on_command_line(const Glib::RefPtr<Gio::ApplicationCommandLine>&
 
     if (option_group.show_processes_tab) {
         procman_debug("Starting with PROCMAN_TAB_PROCESSES by commandline request");
-        set_tab(GTK_NOTEBOOK(notebook), PROCMAN_TAB_PROCESSES, this);
+        g_settings_set_int (settings, "current-tab", PROCMAN_TAB_PROCESSES);
     } else if (option_group.show_resources_tab) {
         procman_debug("Starting with PROCMAN_TAB_RESOURCES by commandline request");
-        set_tab(GTK_NOTEBOOK(notebook), PROCMAN_TAB_RESOURCES, this);
+        g_settings_set_int (settings, "current-tab", PROCMAN_TAB_RESOURCES);
     } else if (option_group.show_file_systems_tab) {
         procman_debug("Starting with PROCMAN_TAB_DISKS by commandline request");
-        set_tab(GTK_NOTEBOOK(notebook), PROCMAN_TAB_DISKS, this);
+        g_settings_set_int (settings, "current-tab", PROCMAN_TAB_DISKS);
     } else if (option_group.print_version) {
         g_print("%s %s\n", _("GNOME System Monitor"), VERSION);
        exit (EXIT_SUCCESS);
@@ -688,6 +675,10 @@ void ProcmanApp::on_startup()
 
     init_volume_monitor (this);
 
+    add_accelerator ("<Alt>1", "win.show-page", g_variant_new_int32 (PROCMAN_TAB_PROCESSES));
+    add_accelerator ("<Alt>2", "win.show-page", g_variant_new_int32 (PROCMAN_TAB_RESOURCES));
+    add_accelerator ("<Alt>3", "win.show-page", g_variant_new_int32 (PROCMAN_TAB_DISKS));
+
     gtk_widget_show (main_window);
 }
 
diff --git a/src/procman-app.h b/src/procman-app.h
index f24b30a..3fd2258 100644
--- a/src/procman-app.h
+++ b/src/procman-app.h
@@ -55,7 +55,6 @@ struct ProcConfig
     int             graph_update_interval;
     int             disks_update_interval;
     gint            whose_process;
-    gint            current_tab;
     GdkRGBA         cpu_color[GLIBTOP_NCPU];
     GdkRGBA         mem_color;
     GdkRGBA         swap_color;


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