BListView: Update selection on mouse down and mouse up

A version of this feature was originally implemented in hrev50495
that allowed you to scroll through a list of list items while the
mouse was held down updating the selection as you went.

This feature was removed when we switched to selecting on mouse up
in hrev52062 and was never reimplimented when we switched back to
selecting on mouse down in hrev52121.

In BeOS R5 as you scrolled through a single-selection list with the
mouse button held down the selected item appeared to change, but
the selection didn't actually update until you released the mouse
button. The selection never changes on mouse down, only on mouse
up. You could click on one item then move your mouse off the first
item to a second item releasing your mouse button and it would
select the second item without ever selecting the first item.

In this commit we replicate this behavior with one exception, we
always select on mouse down, but still allow the selection to
change on mouse up.

The big difference between this and the BeOS behavior is that on
BeOS you could only select exactly one item on mouse up, while with
this you can select one item on mouse down and a second item on
mouse up.

ScrollToSelection() in MouseMoved() if mouse button is down and
we are not not dragging. This performs auto-scroll.

Create private _DoSelection() method copied from MouseDown().
Remove Thread.h include that is no longer used.

Fixes #15009 (and doesn't cause regression for #9190 #14264 #14289)

Change-Id: Icae02b8d37ed281390647504b4efa3d694ea522a
Reviewed-on: https://review.haiku-os.org/c/haiku/+/1956
Reviewed-by: Adrien Destugues <[email protected]>
Reviewed-by: John Scipione <[email protected]>
Tested-by: Commit checker robot <[email protected]>
This commit is contained in:
John Scipione
2023-12-05 22:10:10 +00:00
parent c70de894ea
commit 6761bf581f
2 changed files with 106 additions and 49 deletions
+1
View File
@@ -188,6 +188,7 @@ private:
bool _MoveItem(int32 from, int32 to);
bool _ReplaceItem(int32 index, BListItem* item);
void _RescanSelection(int32 from, int32 to);
void _DoSelection(int32 index);
private:
BList fList;
+105 -49
View File
@@ -22,7 +22,6 @@
#include <PropertyInfo.h>
#include <ScrollBar.h>
#include <ScrollView.h>
#include <Thread.h>
#include <Window.h>
#include <binary_compatibility/Interface.h>
@@ -627,11 +626,6 @@ BListView::MouseDown(BPoint where)
}
int32 index = IndexOf(where);
int32 modifiers = 0;
BMessage* message = Looper()->CurrentMessage();
if (message != NULL)
message->FindInt32("modifiers", &modifiers);
// If the user double (or more) clicked within the current selection,
// we don't change the selection but invoke the selection.
@@ -669,41 +663,7 @@ BListView::MouseDown(BPoint where)
SetMouseEventMask(B_POINTER_EVENTS, B_NO_POINTER_HISTORY);
}
if (index >= 0) {
if (fListType == B_MULTIPLE_SELECTION_LIST) {
if ((modifiers & B_SHIFT_KEY) != 0) {
// select entire block
if (index >= fFirstSelected && index < fLastSelected)
// clicked inside of selected items block, deselect all
// but from the first selected item to the clicked item
DeselectExcept(fFirstSelected, index);
else
Select(std::min(index, fFirstSelected), std::max(index,
fLastSelected));
} else {
if ((modifiers & B_COMMAND_KEY) != 0) {
// toggle selection state of clicked item (like in Tracker)
if (ItemAt(index)->IsSelected())
Deselect(index);
else
Select(index, true);
} else if (!ItemAt(index)->IsSelected())
// To enable multi-select drag and drop, we only
// exclusively select a single item if it's not one of the
// already selected items. This behavior gives the mouse
// tracking thread the opportunity to initiate the
// multi-selection drag with all the items still selected.
Select(index);
}
} else {
// toggle selection state of clicked item (except drag & drop)
if ((modifiers & B_COMMAND_KEY) != 0 && ItemAt(index)->IsSelected())
Deselect(index);
else
Select(index);
}
} else if ((modifiers & B_COMMAND_KEY) == 0)
DeselectAll();
_DoSelection(index);
BView::MouseDown(where);
}
@@ -712,23 +672,41 @@ BListView::MouseDown(BPoint where)
void
BListView::MouseUp(BPoint where)
{
BView::MouseUp(where);
// drag is over
fTrack->try_drag = false;
fTrack->is_dragging = false;
uint32* buttons = 0;
GetMouse(&where, buttons);
// selection updating on drag is for single selection lists only
if (fListType == B_MULTIPLE_SELECTION_LIST)
return BView::MouseUp(where);
if (buttons == 0) {
fTrack->try_drag = false;
fTrack->is_dragging = false;
int32 index = IndexOf(where);
// bail out if selection hasn't changed
if (index == fTrack->item_index)
return BView::MouseUp(where);
// if mouse up selection is invalid reselect mouse down selection
if (index == -1)
index = fTrack->item_index;
// if mouse down selection also invalid deselect all
if (index == -1) {
DeselectAll();
return BView::MouseUp(where);
}
// undo fake selection and select item
ItemAt(index)->Deselect();
_DoSelection(index);
BView::MouseUp(where);
}
void
BListView::MouseMoved(BPoint where, uint32 code, const BMessage* dragMessage)
{
BView::MouseMoved(where, code, dragMessage);
if (fTrack->item_index >= 0 && fTrack->try_drag) {
// initiate a drag if the mouse was moved far enough
BPoint offset = where - fTrack->drag_start;
@@ -739,6 +717,41 @@ BListView::MouseMoved(BPoint where, uint32 code, const BMessage* dragMessage)
fTrack->item_index, fTrack->was_selected);
}
}
// get mouse buttons from current message in case of change
int32 buttons = 0;
if (Window() != NULL) {
BMessage* currentMessage = Window()->CurrentMessage();
if (currentMessage != NULL)
currentMessage->FindInt32("buttons", &buttons);
}
// only update selection if mouse button pressed and not dragging
int32 index = IndexOf(where);
if (buttons == 0 || fTrack->is_dragging)
return BView::MouseMoved(where, code, dragMessage);
// scroll to selection while button is pressed
ScrollToSelection();
// selection updating on drag is for single selection lists only
if (fListType == B_MULTIPLE_SELECTION_LIST || index == -1)
return BView::MouseMoved(where, code, dragMessage);
int32 lastIndex = fFirstSelected;
if (lastIndex != -1 && index != lastIndex) {
// mouse moved over unselected, fake selection until mouse up
ItemAt(lastIndex)->Deselect();
ItemAt(index)->Select();
// update selection index
fFirstSelected = fLastSelected = index;
// redraw items whose selection has changed
Invalidate(ItemFrame(lastIndex) | ItemFrame(index));
}
BView::MouseMoved(where, code, dragMessage);
}
@@ -2057,3 +2070,46 @@ BListView::_RecalcItemTops(int32 start, int32 end)
top += ceilf(item->Height());
}
}
void
BListView::_DoSelection(int32 index)
{
BListItem* item = ItemAt(index);
if (index >= 0 && item != NULL) {
if (fListType == B_MULTIPLE_SELECTION_LIST) {
// multiple-selection list
if ((modifiers() & B_SHIFT_KEY) != 0) {
// extend or contract selection
if (index >= fFirstSelected && index < fLastSelected) {
// clicked inside of selected items block, deselect all
// except from the first selected index to item index
DeselectExcept(fFirstSelected, index);
} else {
// extend selection up or down
Select(std::min(index, fFirstSelected),
std::max(index, fLastSelected));
}
} else {
if ((modifiers() & B_COMMAND_KEY) != 0) {
// toggle selection state (like in Tracker)
if (item->IsSelected())
Deselect(index);
else
Select(index, true);
} else if (item->IsEnabled()) // only select enabled item
Select(index);
}
} else {
// single-selection list
// toggle selection state
if ((modifiers() & B_COMMAND_KEY) != 0 && item->IsSelected())
Deselect(index);
else if (item->IsEnabled()) // only select enabled item
Select(index);
}
} else if ((modifiers() & B_COMMAND_KEY) == 0)
DeselectAll();
}