From c63dea6b91508bc87dc1423260ce1ebab3910f27 Mon Sep 17 00:00:00 2001
From: Merov Linden <merov@lindenlab.com>
Date: Thu, 9 Aug 2012 16:26:37 -0700
Subject: [PATCH] MAINT-1197 : Fix inventory deselection/reselection when
 cutting items

---
 indra/newview/llfolderview.cpp      | 23 +++++++++++++++++++++--
 indra/newview/llfolderview.h        |  2 ++
 indra/newview/llfolderviewitem.cpp  |  4 ++++
 indra/newview/llfolderviewitem.h    |  1 +
 indra/newview/llinventorybridge.cpp |  6 ++++++
 5 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/indra/newview/llfolderview.cpp b/indra/newview/llfolderview.cpp
index 7d047ec67ec..3ffa08ec325 100644
--- a/indra/newview/llfolderview.cpp
+++ b/indra/newview/llfolderview.cpp
@@ -2254,7 +2254,7 @@ void LLFolderView::doIdle()
 		LLFastTimer t3(FTM_AUTO_SELECT);
 		// select new item only if a filtered item not currently selected
 		LLFolderViewItem* selected_itemp = mSelectedItems.empty() ? NULL : mSelectedItems.back();
-		if (!mAutoSelectOverride && (!selected_itemp || !selected_itemp->potentiallyFiltered()))
+		if (!mAutoSelectOverride && (!selected_itemp || !selected_itemp->potentiallyVisible()))
 		{
 			// these are named variables to get around gcc not binding non-const references to rvalues
 			// and functor application is inherently non-const to allow for stateful functors
@@ -2264,7 +2264,7 @@ void LLFolderView::doIdle()
 
 		// Open filtered folders for folder views with mAutoSelectOverride=TRUE.
 		// Used by LLPlacesFolderView.
-		if (mAutoSelectOverride && !mFilter->getFilterSubString().empty())
+		if (!mFilter->getFilterSubString().empty())
 		{
 			// these are named variables to get around gcc not binding non-const references to rvalues
 			// and functor application is inherently non-const to allow for stateful functors
@@ -2556,6 +2556,25 @@ void LLFolderView::onRenamerLost()
 	}
 }
 
+LLFolderViewItem* LLFolderView::getNextUnselectedItem()
+{
+	LLFolderViewItem* last_item = *mSelectedItems.rbegin();
+	LLFolderViewItem* new_selection = last_item->getNextOpenNode(FALSE);
+	while(new_selection && new_selection->isSelected())
+	{
+		new_selection = new_selection->getNextOpenNode(FALSE);
+	}
+	if (!new_selection)
+	{
+		new_selection = last_item->getPreviousOpenNode(FALSE);
+		while (new_selection && (new_selection->isInSelection()))
+		{
+			new_selection = new_selection->getPreviousOpenNode(FALSE);
+		}
+	}
+	return new_selection;
+}
+
 LLInventoryFilter* LLFolderView::getFilter()
 {
 	return mFilter;
diff --git a/indra/newview/llfolderview.h b/indra/newview/llfolderview.h
index da8bb15f8ef..3f78312a985 100644
--- a/indra/newview/llfolderview.h
+++ b/indra/newview/llfolderview.h
@@ -207,6 +207,8 @@ class LLFolderView : public LLFolderViewFolder, public LLEditMenuHandler
 	virtual void	doDelete();
 	virtual BOOL	canDoDelete() const;
 
+	LLFolderViewItem* getNextUnselectedItem();
+	
 	// Public rename functionality - can only start the process
 	void startRenamingSelectedItem( void );
 
diff --git a/indra/newview/llfolderviewitem.cpp b/indra/newview/llfolderviewitem.cpp
index 515e544452b..3aa16b44134 100644
--- a/indra/newview/llfolderviewitem.cpp
+++ b/indra/newview/llfolderviewitem.cpp
@@ -1098,6 +1098,10 @@ void LLFolderViewItem::draw()
 	}
 }
 
+bool LLFolderViewItem::isInSelection() const
+{
+	return mIsSelected || (mParentFolder && mParentFolder->isInSelection());
+}
 
 ///----------------------------------------------------------------------------
 /// Class LLFolderViewFolder
diff --git a/indra/newview/llfolderviewitem.h b/indra/newview/llfolderviewitem.h
index 3c7592046a1..577b6b54a26 100644
--- a/indra/newview/llfolderviewitem.h
+++ b/indra/newview/llfolderviewitem.h
@@ -243,6 +243,7 @@ class LLFolderViewItem : public LLView
 	virtual void destroyView();
 
 	BOOL isSelected() const { return mIsSelected; }
+	bool isInSelection() const;
 
 	void setUnselected() { mIsSelected = FALSE; }
 
diff --git a/indra/newview/llinventorybridge.cpp b/indra/newview/llinventorybridge.cpp
index b86c453d617..b819100b9bb 100644
--- a/indra/newview/llinventorybridge.cpp
+++ b/indra/newview/llinventorybridge.cpp
@@ -1355,7 +1355,10 @@ void LLItemBridge::performAction(LLInventoryModel* model, std::string action)
 	else if ("cut" == action)
 	{
 		cutToClipboard();
+		// MAINT-1197: This is temp code to work around a deselection/reselection bug. Please discard when merging CHUI.
+		LLFolderViewItem* item_to_select = mRoot->getNextUnselectedItem();
 		LLFolderView::removeCutItems();
+		mRoot->setSelection(item_to_select, item_to_select ? item_to_select->isOpen() : false, false);
 		return;
 	}
 	else if ("copy" == action)
@@ -2743,7 +2746,10 @@ void LLFolderBridge::performAction(LLInventoryModel* model, std::string action)
 	else if ("cut" == action)
 	{
 		cutToClipboard();
+		// MAINT-1197: This is temp code to work around a deselection/reselection bug. Please discard when merging CHUI.
+		LLFolderViewItem* item_to_select = mRoot->getNextUnselectedItem();
 		LLFolderView::removeCutItems();
+		mRoot->setSelection(item_to_select, item_to_select ? item_to_select->isOpen() : false, false);
 		return;
 	}
 	else if ("copy" == action)
-- 
GitLab