Avoiding the extra ListBoxModel callback when the selection has not changed


#1

It's likely not a big deal, but if you perform a click that ends up with the same exact selection (or lack thereof), you get extra callbacks through selectedRowsChanged().

In my case, I've set up the backgroundClicked() method to always clear the selection.

setSelectedRows (SparseSet<int>())

Now, every time I click the background, it clears the selection as expected - except it always results in a selectedRowsChanged() call per click.

It seems like the best route is to simply check if the selected rows have changed in ListBox::setSelectedRows() before doing any updating and selectedRowsChanged() call.


void ListBox::setSelectedRows (const SparseSet<int>& setOfRowsToBeSelected,
                               const NotificationType sendNotificationEventToModel)
{
    if (selected != setOfRowsToBeSelected) //ie: doing this
    {
        selected = setOfRowsToBeSelected;
        selected.removeRange (Range<int> (totalItems, std::numeric_limits<int>::max()));

        if (! isRowSelected (lastRowSelected))
            lastRowSelected = getSelectedRow (0);

        viewport->updateContents();

        if (model != nullptr && sendNotificationEventToModel == sendNotification)
            model->selectedRowsChanged (lastRowSelected);
    }
}

#2

I'd love to say I'd change this, but I just know that if I did, people will have written code that depends on it being called, and it'll break it for them!