From e44f2e847ddb5db5b6944f6c2f993981adbd3ad4 Mon Sep 17 00:00:00 2001 From: monsta Date: Wed, 2 Mar 2016 14:44:15 +0300 Subject: disks: implement saving and loading width and visibility of columns adapted from https://git.gnome.org/browse/gnome-system-monitor/commit/?id=f589439a966151b2589bca21a4d6b40455eb201a with necessary changes from https://github.com/mate-desktop/mate-system-monitor/commit/eebc190a5aeb6ebaf5c3a868b68434679adddc74 --- src/disks.cpp | 41 ++++++++++++++++++++++ src/org.mate.system-monitor.gschema.xml.in | 56 ++++++++++++++++++++++++++++++ src/procdialogs.cpp | 29 +++++++++++----- src/procman.cpp | 25 +++++++------ 4 files changed, 130 insertions(+), 21 deletions(-) diff --git a/src/disks.cpp b/src/disks.cpp index 2431727..302098a 100644 --- a/src/disks.cpp +++ b/src/disks.cpp @@ -285,6 +285,44 @@ open_dir(GtkTreeView *tree_view, g_free(dir); } +static guint timeout_id = 0; +static GtkTreeViewColumn *current_column; + +static gboolean +save_column_width (gpointer data) +{ + gint width; + gchar *key; + int id; + GSettings *settings; + + settings = g_settings_get_child (G_SETTINGS (data), "disktreenew"); + id = gtk_tree_view_column_get_sort_column_id (current_column); + width = gtk_tree_view_column_get_width (current_column); + + key = g_strdup_printf ("col-%d-width", id); + g_settings_set_int(settings, key, width); + g_free (key); + + if (timeout_id) { + g_source_remove (timeout_id); + timeout_id = 0; + } + + return FALSE; +} + +static void +cb_disks_column_resized(GtkWidget *widget, GParamSpec *pspec, gpointer data) +{ + current_column = GTK_TREE_VIEW_COLUMN(widget); + + if (timeout_id) + g_source_remove (timeout_id); + + timeout_id = g_timeout_add (250, save_column_width, data); +} + GtkWidget * create_disk_view(ProcData *procdata) { @@ -355,6 +393,7 @@ create_disk_view(ProcData *procdata) gtk_tree_view_column_set_resizable(col, TRUE); gtk_tree_view_column_set_min_width (col, 30); gtk_tree_view_column_set_sizing(col, GTK_TREE_VIEW_COLUMN_FIXED); + g_signal_connect(G_OBJECT(col), "notify::fixed-width", G_CALLBACK(cb_disks_column_resized), procdata->settings); gtk_tree_view_append_column(GTK_TREE_VIEW(disk_tree), col); /* sizes - used */ @@ -369,6 +408,7 @@ create_disk_view(ProcData *procdata) gtk_tree_view_column_set_resizable(col, TRUE); gtk_tree_view_column_set_min_width (col, 30); gtk_tree_view_column_set_sizing(col, GTK_TREE_VIEW_COLUMN_FIXED); + g_signal_connect(G_OBJECT(col), "notify::fixed-width", G_CALLBACK(cb_disks_column_resized), procdata->settings); gtk_tree_view_append_column(GTK_TREE_VIEW(disk_tree), col); switch (i) { @@ -411,6 +451,7 @@ create_disk_view(ProcData *procdata) gtk_tree_view_column_set_resizable(col, TRUE); gtk_tree_view_column_set_min_width (col, 150); gtk_tree_view_column_set_sizing(col, GTK_TREE_VIEW_COLUMN_FIXED); + g_signal_connect(G_OBJECT(col), "notify::fixed-width", G_CALLBACK(cb_disks_column_resized), procdata->settings); gtk_tree_view_append_column(GTK_TREE_VIEW(disk_tree), col); /* numeric sort */ diff --git a/src/org.mate.system-monitor.gschema.xml.in b/src/org.mate.system-monitor.gschema.xml.in index 063ffa4..c69bee0 100644 --- a/src/org.mate.system-monitor.gschema.xml.in +++ b/src/org.mate.system-monitor.gschema.xml.in @@ -353,6 +353,62 @@ [ 0 ] Disk view columns order + + 100 + Width of disk view 'Device' column + + + true + Show disk view 'Device' column on startup + + + 100 + Width of disk view 'Directory' column + + + true + Show disk view 'Directory' column on startup + + + 50 + Width of disk view 'Type' column + + + true + Show disk view 'Type' column on startup + + + 80 + Width of disk view 'Total' column + + + true + Show disk view 'Total' column on startup + + + 80 + Width of disk view 'Free' column + + + true + Show disk view 'Free' column on startup + + + 80 + Width of disk view 'Available' column + + + true + Show disk view 'Available' column on startup + + + 300 + Width of disk view 'Used' column + + + true + Show disk view 'Used' column on startup + diff --git a/src/procdialogs.cpp b/src/procdialogs.cpp index f76d639..96cf39c 100644 --- a/src/procdialogs.cpp +++ b/src/procdialogs.cpp @@ -355,17 +355,15 @@ private: }; - - static void -field_toggled (GtkCellRendererToggle *cell, gchar *path_str, gpointer data) +field_toggled (const gchar *child_schema, GtkCellRendererToggle *cell, gchar *path_str, gpointer data) { GtkTreeModel *model = static_cast(data); GtkTreePath *path = gtk_tree_path_new_from_string (path_str); GtkTreeIter iter; GtkTreeViewColumn *column; gboolean toggled; - GSettings *settings = g_settings_get_child (ProcData::get_instance()->settings, "proctree"); + GSettings *settings = g_settings_get_child (ProcData::get_instance()->settings, child_schema); gchar *key; int id; @@ -387,11 +385,22 @@ field_toggled (GtkCellRendererToggle *cell, gchar *path_str, gpointer data) g_free (key); gtk_tree_path_free (path); +} + +static void +proc_field_toggled (GtkCellRendererToggle *cell, gchar *path_str, gpointer data) +{ + field_toggled ("proctree", cell, path_str, data); +} +static void +disk_field_toggled (GtkCellRendererToggle *cell, gchar *path_str, gpointer data) +{ + field_toggled ("disktreenew", cell, path_str, data); } static GtkWidget * -create_field_page(GtkWidget *tree, const char* text) +create_field_page(GtkWidget *tree, const gchar *child_schema, const gchar *text) { GtkWidget *vbox; GtkWidget *scrolled; @@ -433,7 +442,11 @@ create_field_page(GtkWidget *tree, const char* text) gtk_tree_view_column_set_attributes (column, cell, "active", 0, NULL); - g_signal_connect (G_OBJECT (cell), "toggled", G_CALLBACK (field_toggled), model); + if (g_strcmp0 (child_schema, "proctree") == 0) + g_signal_connect (G_OBJECT (cell), "toggled", G_CALLBACK (proc_field_toggled), model); + else if (g_strcmp0 (child_schema, "disktreenew") == 0) + g_signal_connect (G_OBJECT (cell), "toggled", G_CALLBACK (disk_field_toggled), model); + gtk_tree_view_column_set_clickable (column, TRUE); gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column); @@ -663,7 +676,7 @@ procdialog_create_preferences_dialog (ProcData *procdata) label = gtk_label_new (" "); gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); - vbox2 = create_field_page (procdata->tree, _("Process i_nformation shown in list:")); + vbox2 = create_field_page (procdata->tree, "proctree", _("Process i_nformation shown in list:")); gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 0); sys_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12); @@ -823,7 +836,7 @@ procdialog_create_preferences_dialog (ProcData *procdata) label = gtk_label_new (" "); gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); - vbox3 = create_field_page (procdata->disk_list, _("File system i_nformation shown in list:")); + vbox3 = create_field_page (procdata->disk_list, "disktreenew", _("File system i_nformation shown in list:")); gtk_box_pack_start (GTK_BOX (hbox), vbox3, TRUE, TRUE, 0); gtk_widget_show_all (dialog); diff --git a/src/procman.cpp b/src/procman.cpp index b9343eb..ea50cb2 100644 --- a/src/procman.cpp +++ b/src/procman.cpp @@ -399,7 +399,8 @@ procman_get_tree_state (GSettings *settings, GtkWidget *tree, const gchar *child columns = gtk_tree_view_get_columns (GTK_TREE_VIEW (tree)); - if(!g_strcmp0(child_schema, "proctree")) + if (g_strcmp0(child_schema, "proctree") == 0 || + g_strcmp0(child_schema, "disktreenew") == 0) { for (it = columns; it; it = it->next) { @@ -421,19 +422,17 @@ procman_get_tree_state (GSettings *settings, GtkWidget *tree, const gchar *child g_free (key); column = gtk_tree_view_get_column (GTK_TREE_VIEW (tree), id); - if(!column) continue; - gtk_tree_view_column_set_visible (column, visible); - if (visible) { - /* ensure column is really visible */ - width = MAX(width, 20); - gtk_tree_view_column_set_fixed_width(column, width); - } - } - } + if (column == NULL) + continue; + + gtk_tree_view_column_set_visible (column, visible); + if (visible) { + /* ensure column is really visible */ + width = MAX(width, 20); + gtk_tree_view_column_set_fixed_width(column, width); + } + } - if(!g_strcmp0(child_schema, "proctree") || - !g_strcmp0(child_schema, "disktreenew")) - { GVariant *value; GVariantIter iter; int sortIndex; -- cgit v1.2.1