Index: chrome/browser/ui/views/aura/app_list/app_list_model_builder.cc |
diff --git a/chrome/browser/ui/views/aura/app_list/app_list_model_builder.cc b/chrome/browser/ui/views/aura/app_list/app_list_model_builder.cc |
index 6bd1ef1806f046e065f8b1ef4b21f2636130cd84..b253db22f810f777fe42694d3ac1bb854c39813b 100644 |
--- a/chrome/browser/ui/views/aura/app_list/app_list_model_builder.cc |
+++ b/chrome/browser/ui/views/aura/app_list/app_list_model_builder.cc |
@@ -4,7 +4,9 @@ |
#include "chrome/browser/ui/views/aura/app_list/app_list_model_builder.h" |
-#include "ash/app_list/app_list_item_group_model.h" |
+#include "base/string_util.h" |
+#include "base/i18n/case_conversion.h" |
+#include "base/utf_string_conversions.h" |
#include "chrome/app/chrome_command_ids.h" |
#include "chrome/browser/extensions/extension_service.h" |
#include "chrome/browser/prefs/pref_service.h" |
@@ -22,32 +24,23 @@ |
namespace { |
-// Gets or creates group for given |extension| in |model|. The created group |
-// is added to |model| and owned by it. |
-ash::AppListItemGroupModel* GetOrCreateGroup( |
- int page_index, |
- const ListValue* app_page_names, |
- ash::AppListModel* model) { |
- if (page_index >= model->group_count()) { |
- for (int i = model->group_count(); i <= page_index; ++i) { |
- std::string title; |
- if (!app_page_names->GetString(i, &title)) |
- title = l10n_util::GetStringUTF8(IDS_APP_DEFAULT_PAGE_NAME); |
- |
- ash::AppListItemGroupModel* group = |
- new ash::AppListItemGroupModel(title); |
- model->AddGroup(group); |
- } |
- } |
- |
- return model->GetGroup(page_index); |
+// Data struct used to define BrowserCommand. |
+typedef struct { |
tfarina
2012/03/01 01:05:23
nit: please no typedef struct, C++ doesn't need th
xiyuan
2012/03/01 19:58:47
Done.
|
+ int command_id; |
+ int title_id; |
+ int icon_id; |
+} BrowserCommandData; |
+ |
+// Binary predict to sort app list items alphabetically. |
+bool AppPrecedes(const ash::AppListItemModel* a, |
+ const ash::AppListItemModel* b) { |
+ return a->title() < b->title(); |
} |
-// Binary predict to sort extension apps. Smaller launch ordinal takes |
-// precedence. |
-bool ExtensionAppPrecedes(const ExtensionAppItem* a, |
- const ExtensionAppItem* b) { |
- return a->launch_ordinal().LessThan(b->launch_ordinal()); |
+// Returns true if given |str| matches |query|. Currently, it returns |
+// if |query| is a substr of |str|. |
+bool MatchesQuery(const string16 query, const string16 str) { |
tfarina
2012/03/01 01:05:23
nit: const string16& ?
xiyuan
2012/03/01 19:58:47
Done.
|
+ return query.empty() || str.find(query) != string16::npos; |
} |
} // namespace |
@@ -61,60 +54,65 @@ AppListModelBuilder::AppListModelBuilder(Profile* profile, |
AppListModelBuilder::~AppListModelBuilder() { |
} |
-void AppListModelBuilder::Build() { |
- GetExtensionApps(); |
- GetBrowserCommands(); |
+void AppListModelBuilder::Build(const std::string& query) { |
+ string16 normalized_query(base::i18n::ToLower(UTF8ToUTF16(query))); |
+ |
+ Items items; |
+ GetExtensionApps(normalized_query, &items); |
+ GetBrowserCommands(normalized_query, &items); |
+ |
+ std::sort(items.begin(), items.end(), &AppPrecedes); |
+ for (Items::const_iterator it = items.begin(); it != items.end(); ++it) { |
+ model_->AddItem(*it); |
+ } |
} |
-void AppListModelBuilder::GetExtensionApps() { |
+void AppListModelBuilder::GetExtensionApps(const string16& query, |
+ Items* items) { |
DCHECK(profile_); |
ExtensionService* service = profile_->GetExtensionService(); |
if (!service) |
return; |
// Get extension apps. |
- std::vector<ExtensionAppItem*> items; |
const ExtensionSet* extensions = service->extensions(); |
for (ExtensionSet::const_iterator app = extensions->begin(); |
app != extensions->end(); ++app) { |
- if ((*app)->ShouldDisplayInLauncher()) |
- items.push_back(new ExtensionAppItem(profile_, *app)); |
- } |
- |
- // Sort by launch ordinal. |
- std::sort(items.begin(), items.end(), &ExtensionAppPrecedes); |
- |
- // Put all items into model and group them by page ordinal. |
- PrefService* prefs = profile_->GetPrefs(); |
- const ListValue* app_page_names = prefs->GetList(prefs::kNtpAppPageNames); |
- for (size_t i = 0; i < items.size(); ++i) { |
- ExtensionAppItem* item = items[i]; |
- |
- ash::AppListItemGroupModel* group = GetOrCreateGroup( |
- item->page_index(), |
- app_page_names, |
- model_); |
- |
- group->AddItem(item); |
+ if ((*app)->ShouldDisplayInLauncher() && |
+ MatchesQuery(query, |
+ base::i18n::ToLower(UTF8ToUTF16((*app)->name())))) { |
+ items->push_back(new ExtensionAppItem(profile_, *app)); |
+ } |
} |
} |
-void AppListModelBuilder::GetBrowserCommands() { |
+void AppListModelBuilder::GetBrowserCommands(const string16& query, |
+ Items* items) { |
Browser* browser = BrowserList::GetLastActiveWithProfile(profile_); |
if (!browser) |
return; |
- // Uses the first group to put browser commands |
- if (model_->group_count() == 0) |
- model_->AddGroup(new ash::AppListItemGroupModel("")); |
- ash::AppListItemGroupModel* group = model_->GetGroup(0); |
- |
- group->AddItem(new BrowserCommandItem(browser, |
- IDC_NEW_INCOGNITO_WINDOW, |
- IDS_APP_LIST_INCOGNITO, |
- IDR_APP_LIST_INCOGNITO)); |
- group->AddItem(new BrowserCommandItem(browser, |
- IDC_OPTIONS, |
- IDS_APP_LIST_SETTINGS, |
- IDR_APP_LIST_SETTINGS)); |
+ const BrowserCommandData kBrowserCommands[] = { |
+ { |
+ IDC_NEW_INCOGNITO_WINDOW, |
+ IDS_APP_LIST_INCOGNITO, |
+ IDR_APP_LIST_INCOGNITO, |
+ }, |
+ { |
+ IDC_OPTIONS, |
+ IDS_APP_LIST_SETTINGS, |
+ IDR_APP_LIST_SETTINGS, |
+ |
+ }, |
+ }; |
+ |
+ for (size_t i = 0; i < arraysize(kBrowserCommands); ++i) { |
+ string16 title = l10n_util::GetStringUTF16(kBrowserCommands[i].title_id); |
+ if (MatchesQuery(query, base::i18n::ToLower(title))) { |
+ items->push_back(new BrowserCommandItem(browser, |
+ kBrowserCommands[i].command_id, |
+ kBrowserCommands[i].title_id, |
+ kBrowserCommands[i].icon_id)); |
+ } |
+ } |
} |