single-selection: Don't allow unselecting with autoselect

According to the documentation, when autoselect is TRUE it will
"will disallow unselecting the current item".
This commit is contained in:
Corey Berla 2024-08-21 08:22:34 -07:00 committed by Benjamin Otte
parent 98eaec99ee
commit 8eb05850cb
2 changed files with 17 additions and 3 deletions

View File

@ -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)
{
if (!self->can_unselect || self->autoselect)
return;
position = GTK_INVALID_LIST_POSITION;
}
if (self->selected == position)
return;

View File

@ -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);