blob: 2dcc36769a24d22e150a562dffd3724aa7a613e0 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/app_list/app_list_model_updater.h"
#include <algorithm>
#include "ash/public/cpp/app_list/app_list_config.h"
#include "chrome/browser/ui/app_list/chrome_app_list_item.h"
// static
syncer::StringOrdinal AppListModelUpdater::GetFirstAvailablePositionInternal(
const std::vector<ChromeAppListItem*>& top_level_items) {
// Sort the top level items by their positions.
std::vector<ChromeAppListItem*> sorted_items(top_level_items);
std::sort(sorted_items.begin(), sorted_items.end(),
[](ChromeAppListItem* const& item1,
ChromeAppListItem* const& item2) -> bool {
return item1->position().LessThan(item2->position());
});
// Find the first empty position in app list. If all pages are full, return
// the next position after last item.
int items_in_page = 0;
int page = 0;
for (size_t i = 0; i < sorted_items.size(); ++i) {
if (!sorted_items[i]->is_page_break()) {
++items_in_page;
continue;
}
// There may be multiple "page break" items at the end of page while empty
// pages will not be shown in app list, so skip them.
const int max_items_in_page =
app_list::AppListConfig::instance().GetMaxNumOfItemsPerPage(page);
if (items_in_page > 0 && items_in_page < max_items_in_page) {
return sorted_items[i - 1]->position().CreateBetween(
sorted_items[i]->position());
}
if (items_in_page > 0)
++page;
items_in_page = 0;
}
if (sorted_items.empty())
return syncer::StringOrdinal::CreateInitialOrdinal();
return sorted_items.back()->position().CreateAfter();
}