gnome-control-center/backport-bugfix-power-Correctly-lookup-or-insert-new-item.patch
Chen Wei 1488053d66 power: Correctly lookup or insert new items into combobox
From b5711c59eccb90a547921af6e0f11e5e3dd249f5 Mon Sep 17 00:00:00 2001
From: Benjamin Berg <bberg@redhat.com>
Date: Mon, 5 Nov 2018 13:27:12 +0100
Subject: [PATCH] power: Correctly lookup or insert new items into combobox

The code to lookup or insert items into the combobox had a few issues.
It would assume that the items are sorted, causing existing items to not
be found and be inserted instead. It also would simply forget to insert
an item if it was larger than all existing items.

This code is now changed to iterate over all items, finding the best
insertion point in the process (next item has a larger value, or the
values are not increasing anymore). The item will only be inserted if it
has not been found.

https://gitlab.gnome.org/GNOME/gnome-control-center/-/issues/261

Signed-off-by: Chen Wei <chenwei@xfusion.com>
2022-12-12 16:58:06 +08:00

91 lines
3.3 KiB
Diff

From ff677952619771e0f3c97dd5626f4303ffc1b264 Mon Sep 17 00:00:00 2001
From: Chen Wei <chenwei@xfusion.com>
Date: Sat, 10 Dec 2022 10:15:12 +0800
Subject: [PATCH] power: Correctly lookup or insert new items into combobox
From b5711c59eccb90a547921af6e0f11e5e3dd249f5 Mon Sep 17 00:00:00 2001
From: Benjamin Berg <bberg@redhat.com>
Date: Mon, 5 Nov 2018 13:27:12 +0100
Subject: [PATCH] power: Correctly lookup or insert new items into combobox
The code to lookup or insert items into the combobox had a few issues.
It would assume that the items are sorted, causing existing items to not
be found and be inserted instead. It also would simply forget to insert
an item if it was larger than all existing items.
This code is now changed to iterate over all items, finding the best
insertion point in the process (next item has a larger value, or the
values are not increasing anymore). The item will only be inserted if it
has not been found.
https://gitlab.gnome.org/GNOME/gnome-control-center/-/issues/261
Signed-off-by: Chen Wei <chenwei@xfusion.com>
---
panels/power/cc-power-panel.c | 38 ++++++++++++++++++-----------------
1 file changed, 20 insertions(+), 18 deletions(-)
diff --git a/panels/power/cc-power-panel.c b/panels/power/cc-power-panel.c
index bfac953..d30f834 100644
--- a/panels/power/cc-power-panel.c
+++ b/panels/power/cc-power-panel.c
@@ -1197,9 +1197,12 @@ static void
set_value_for_combo (GtkComboBox *combo_box, gint value)
{
GtkTreeIter iter;
- GtkTreeIter last;
+ g_autoptr(GtkTreeIter) insert = NULL;
+ GtkTreeIter new;
GtkTreeModel *model;
gint value_tmp;
+ gint value_last = 0;
+ g_autofree gchar *text = NULL;
gboolean ret;
/* get entry */
@@ -1219,25 +1222,24 @@ set_value_for_combo (GtkComboBox *combo_box, gint value)
gtk_combo_box_set_active_iter (combo_box, &iter);
return;
}
- else if (value_tmp > value)
- {
- GtkTreeIter new;
- g_autofree gchar *text = NULL;
-
- /* This is an unlisted value, add it to the drop-down */
- gtk_list_store_insert_before (GTK_LIST_STORE (model), &new, &iter);
- text = time_to_string_text (value * 1000);
- gtk_list_store_set (GTK_LIST_STORE (model), &new,
- ACTION_MODEL_TEXT, text,
- ACTION_MODEL_VALUE, value,
- -1);
- gtk_combo_box_set_active_iter (combo_box, &new);
- return;
- }
- last = iter;
+
+ /* Insert before if the next value is larger or the value is lower
+ * again (i.e. "Never" is zero and last). */
+ if (!insert && (value_tmp > value || value_last > value_tmp))
+ insert = gtk_tree_iter_copy (&iter);
+
+ value_last = value_tmp;
} while (gtk_tree_model_iter_next (model, &iter));
- gtk_combo_box_set_active_iter (combo_box, &last);
+ /* The value is not listed, so add it at the best point (or the end). */
+ gtk_list_store_insert_before (GTK_LIST_STORE (model), &new, insert);
+
+ text = time_to_string_text (value * 1000);
+ gtk_list_store_set (GTK_LIST_STORE (model), &new,
+ ACTION_MODEL_TEXT, text,
+ ACTION_MODEL_VALUE, value,
+ -1);
+ gtk_combo_box_set_active_iter (combo_box, &new);
}
static void
--
2.33.0