From 8eb05850cbd9ee0b5dd0ecb68f2945bcf8bd081a Mon Sep 17 00:00:00 2001 From: Corey Berla Date: Wed, 21 Aug 2024 08:22:34 -0700 Subject: [PATCH] single-selection: Don't allow unselecting with autoselect According to the documentation, when autoselect is TRUE it will "will disallow unselecting the current item". --- gtk/gtksingleselection.c | 12 +++++++++--- testsuite/gtk/singleselection.c | 8 ++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/gtk/gtksingleselection.c b/gtk/gtksingleselection.c index 3b5257b2f7..40e13843c4 100644 --- a/gtk/gtksingleselection.c +++ b/gtk/gtksingleselection.c @@ -164,7 +164,7 @@ gtk_single_selection_unselect_item (GtkSelectionModel *model, { GtkSingleSelection *self = GTK_SINGLE_SELECTION (model); - if (!self->can_unselect) + if (!self->can_unselect || self->autoselect) return FALSE; if (self->selected == position) @@ -651,7 +651,8 @@ gtk_single_selection_get_selected (GtkSingleSelection *self) * value of the [property@Gtk.SingleSelection:autoselect] property: * If it is set, no change will occur and the old item will stay * selected. If it is unset, the selection will be unset and no item - * will be selected. + * will be selected. This also applies if [property@Gtk.SingleSelection:can-unselect] + * is set to %FALSE. */ void gtk_single_selection_set_selected (GtkSingleSelection *self, @@ -669,7 +670,12 @@ gtk_single_selection_set_selected (GtkSingleSelection *self, new_selected = g_list_model_get_item (self->model, position); if (new_selected == NULL) - position = GTK_INVALID_LIST_POSITION; + { + if (!self->can_unselect || self->autoselect) + return; + + position = GTK_INVALID_LIST_POSITION; + } if (self->selected == position) return; diff --git a/testsuite/gtk/singleselection.c b/testsuite/gtk/singleselection.c index 0f6aee9e6b..a1c30e24fc 100644 --- a/testsuite/gtk/singleselection.c +++ b/testsuite/gtk/singleselection.c @@ -626,6 +626,14 @@ test_can_unselect (void) gtk_single_selection_set_can_unselect (GTK_SINGLE_SELECTION (selection), TRUE); + assert_selection (selection, "1"); + ret = gtk_selection_model_unselect_item (selection, 0); + g_assert_false (ret); + assert_selection (selection, "1"); + assert_selection_changes (selection, ""); + + gtk_single_selection_set_autoselect (GTK_SINGLE_SELECTION (selection), FALSE); + assert_selection (selection, "1"); ret = gtk_selection_model_unselect_item (selection, 0); g_assert_true (ret);