From fa0295318e3760dbc7ff6802b50548ce9d39d267 Mon Sep 17 00:00:00 2001 From: Moritz Bruder Date: Tue, 18 Apr 2017 22:23:19 +0200 Subject: Run dialog: Add option to reverse history view A setting is added to show the run dialog history in reverse. --- data/org.mate.panel.gschema.xml.in | 5 ++++ mate-panel/panel-run-dialog.c | 60 ++++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/data/org.mate.panel.gschema.xml.in b/data/org.mate.panel.gschema.xml.in index c58f6e19..9c6fdb8c 100644 --- a/data/org.mate.panel.gschema.xml.in +++ b/data/org.mate.panel.gschema.xml.in @@ -30,6 +30,11 @@ Maximum history size for "Run Application" dialog Controls the maximum size of the history of the "Run Application" dialog. A value of 0 will disable the history. + + false + Reverse the history of the "Run Application" dialog + Displays the history in reverse. Provides a consistent view for terminal users as the up key will select the most recent entry. + [] Panel ID list diff --git a/mate-panel/panel-run-dialog.c b/mate-panel/panel-run-dialog.c index fc95d256..4c525519 100644 --- a/mate-panel/panel-run-dialog.c +++ b/mate-panel/panel-run-dialog.c @@ -115,6 +115,7 @@ static void panel_run_dialog_disconnect_pixmap (PanelRunDialog *dialog); #define PANEL_RUN_SCHEMA "org.mate.panel" #define PANEL_RUN_HISTORY_KEY "history-mate-run" #define PANEL_RUN_HISTORY_MAX_SIZE_KEY "history-max-size-mate-run" +#define PANEL_RUN_HISTORY_REVERSE_KEY "history-reverse-mate-run" #define PANEL_RUN_SHOW_PROGRAM_LIST_KEY "show-program-list" static GtkTreeModel * @@ -123,17 +124,23 @@ _panel_run_get_recent_programs_list (PanelRunDialog *dialog) GtkListStore *list; gchar **items; guint history_max_size; + gboolean history_reverse = FALSE; guint i = 0; list = gtk_list_store_new (1, G_TYPE_STRING); history_max_size = g_settings_get_uint (dialog->settings, PANEL_RUN_HISTORY_MAX_SIZE_KEY); + history_reverse = g_settings_get_boolean (dialog->settings, PANEL_RUN_HISTORY_REVERSE_KEY); items = g_settings_get_strv (dialog->settings, PANEL_RUN_HISTORY_KEY); for (i = 0; items[i] && i < history_max_size; i++) { GtkTreeIter iter; - gtk_list_store_append (list, &iter); + /* add history in reverse */ + if (history_reverse) + gtk_list_store_prepend (list, &iter); + else + gtk_list_store_append (list, &iter); gtk_list_store_set (list, &iter, 0, items[i], -1); } @@ -149,36 +156,45 @@ _panel_run_save_recent_programs_list (PanelRunDialog *dialog, { GtkTreeModel *model; GtkTreeIter iter; - GArray *items; guint history_max_size; - guint i = 0; + gboolean history_reverse; + history_reverse = g_settings_get_boolean (dialog->settings, PANEL_RUN_HISTORY_REVERSE_KEY); history_max_size = g_settings_get_uint (dialog->settings, PANEL_RUN_HISTORY_MAX_SIZE_KEY); - items = g_array_new (TRUE, TRUE, sizeof (gchar *)); - if (history_max_size > 0) { - g_array_append_val (items, lastcommand); - i++; - + if (history_max_size == 0) + g_settings_set_strv (dialog->settings, PANEL_RUN_HISTORY_KEY, NULL); + else { model = gtk_combo_box_get_model (GTK_COMBO_BOX (entry)); - if (history_max_size > 1) { - if (gtk_tree_model_get_iter_first (model, &iter)) { - char *command; - do { - gtk_tree_model_get (model, &iter, 0, &command, -1); - if (g_strcmp0 (command, lastcommand) == 0) - continue; - g_array_append_val (items, command); - i++; - } while (gtk_tree_model_iter_next (model, &iter) && - i < history_max_size); + /* reasonable upper bound for zero-terminated array with new command */ + gchar const *items[gtk_tree_model_iter_n_children (model, NULL) + 2]; + guint items_added = 0; + + items[0] = lastcommand; + + if (gtk_tree_model_get_iter_first (model, &iter)) { + char *command; + do { + gtk_tree_model_get (model, &iter, 0, &command, -1); + if (g_strcmp0 (command, lastcommand) == 0) + continue; + items[items_added + 1] = command; + items_added++; + } while (gtk_tree_model_iter_next (model, &iter)); + } + + if (history_reverse) { + for (guint pos = 0; pos < items_added / 2; pos++) + { + gchar const * tmp = items[pos + 1]; + items[pos + 1] = items[items_added - pos]; + items[items_added - pos] = tmp; } } + items[history_max_size < items_added + 1 ? history_max_size : items_added + 1] = NULL; + g_settings_set_strv (dialog->settings, PANEL_RUN_HISTORY_KEY, items); } - g_settings_set_strv (dialog->settings, PANEL_RUN_HISTORY_KEY, - (const gchar **) items->data); - g_array_free (items, TRUE); } static void -- cgit v1.2.1