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

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: Fixed review issues 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..8380230c682d8205a6e5f64647b39ef11d3301ca 100644
--- a/chrome/browser/custom_home_pages_table_model.cc
+++ b/chrome/browser/custom_home_pages_table_model.cc
@@ -66,6 +66,64 @@ 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.
+ * Expects |index_list| to be ordered ascending.
+ */
+void CustomHomePagesTableModel::MoveURLs(int insert_before,
+ const std::vector<int>& index_list)
+{
+ DCHECK(insert_before >= 0 && insert_before <= RowCount());
+
+ // The range of elements that needs to be reshuffled is [ |first|, |last| ).
+ int first = std::min(insert_before, index_list.front());
+ 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());
« no previous file with comments | « chrome/browser/custom_home_pages_table_model.h ('k') | chrome/browser/resources/options/browser_options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698