Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Unified Diff: chrome/browser/custom_home_pages_table_model.cc

Issue 7044136: Support drag&drop for startup pages (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/custom_home_pages_table_model.cc
diff --git a/chrome/browser/custom_home_pages_table_model.cc b/chrome/browser/custom_home_pages_table_model.cc
index 5f2cf6124e8e41fd4e9b907ee1f983742663d153..ffa565de62b9cd4141851dd186f74517bd4c007d 100644
--- a/chrome/browser/custom_home_pages_table_model.cc
+++ b/chrome/browser/custom_home_pages_table_model.cc
@@ -66,6 +66,63 @@ void CustomHomePagesTableModel::SetURLs(const std::vector<GURL>& urls) {
observer_->OnModelChanged();
}
+/**
+ * Move a number of existing entries to a new position, reordering the table.
+ *
+ * We determine the range of elements affected by the move, save the moved
+ * elements, compact the remaining ones, and re-insert moved elements.
+ */
+void CustomHomePagesTableModel::MoveURLs(int insert_before,
+ const std::vector<int>& index_list)
csilv 2011/06/13 22:57:02 Should we be concerned about this not working prop
groby-ooo-7-16 2011/06/13 23:45:27 Since the underlying SelectionModel uses Object.ke
csilv 2011/06/14 00:06:12 A comment is prefect, thanks. On 2011/06/13 23:45
+{
+ DCHECK(insert_before >= 0 && insert_before <= RowCount());
+
+ // The range of elements that needs to be reshuffled is [ |first|, |last| ).
csilv 2011/06/13 22:57:02 nit picky: the end of the comment opens with a bra
groby-ooo-7-16 2011/06/13 23:45:27 Actually intentional - that's the standard range n
csilv 2011/06/14 00:06:12 Ah, that thought crossed my mind. (I'm not famili
+ int first = std::min(insert_before,index_list.front());
csilv 2011/06/13 22:57:02 style nit: space after comma
groby-ooo-7-16 2011/06/13 23:45:27 Done.
+ int last = std::max(insert_before, index_list.back() + 1);
+
+ // Save the dragged elements. Also, adjust insertion point if it is before a
+ // dragged element.
+ std::vector<Entry> moved_entries;
+ for (size_t i = 0; i < index_list.size(); ++i) {
+ moved_entries.push_back(entries_[index_list[i]]);
+ if (index_list[i] == insert_before)
+ insert_before++;
+ }
+
+ // Compact the range between beginning and insertion point, moving downwards.
+ size_t skip_count = 0;
+ for (int i = first; i < insert_before; ++i) {
+ if (skip_count < index_list.size() && index_list[skip_count] == i)
+ skip_count++;
+ else
+ entries_[i - skip_count]=entries_[i];
+ }
+
+ // Moving items down created a gap. We start compacting up after it.
+ first = insert_before;
+ insert_before -= skip_count;
+
+ // Now compact up for elements after the insertion point.
+ skip_count = 0;
+ for (int i = last - 1; i >= first; --i) {
+ if (skip_count < index_list.size() &&
+ index_list[index_list.size() - skip_count - 1] == i) {
+ skip_count++;
+ } else {
+ entries_[i + skip_count] = entries_[i];
+ }
+ }
+
+ // Insert moved elements.
+ std::copy(moved_entries.begin(), moved_entries.end(),
+ entries_.begin() + insert_before);
+
+ // Possibly large change, so tell the view to just rebuild itself.
+ if (observer_)
+ observer_->OnModelChanged();
+}
+
void CustomHomePagesTableModel::Add(int index, const GURL& url) {
DCHECK(index >= 0 && index <= RowCount());
entries_.insert(entries_.begin() + static_cast<size_t>(index), Entry());

Powered by Google App Engine
This is Rietveld 408576698