1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ui/app_list/cocoa/apps_search_results_model_bridge.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/strings/sys_string_conversions.h"
10 #include "ui/app_list/app_list_model.h"
11 #import "ui/app_list/cocoa/apps_search_results_controller.h"
12 #include "ui/app_list/search_result.h"
13 #include "ui/app_list/search_result_observer.h"
14 #import "ui/base/cocoa/menu_controller.h"
18 class AppsSearchResultsModelBridge::ItemObserver : public SearchResultObserver {
20 ItemObserver(AppsSearchResultsModelBridge* bridge, size_t index)
21 : bridge_(bridge), row_in_view_(index) {
22 // Cache the result, because the results array is updated before notifying
23 // observers (which happens before deleting the SearchResult).
24 result_ = [bridge_->parent_ results]->GetItemAt(index);
25 result_->AddObserver(this);
28 ~ItemObserver() override { result_->RemoveObserver(this); }
30 NSMenu* GetContextMenu() {
31 if (!context_menu_controller_) {
32 ui::MenuModel* menu_model = result_->GetContextMenuModel();
36 context_menu_controller_.reset(
37 [[MenuController alloc] initWithModel:menu_model
38 useWithPopUpButtonCell:NO]);
40 return [context_menu_controller_ menu];
43 // SearchResultObserver overrides:
44 void OnIconChanged() override {
45 bridge_->ReloadDataForItems(row_in_view_, 1);
47 void OnActionsChanged() override {}
48 void OnIsInstallingChanged() override {}
49 void OnPercentDownloadedChanged() override {}
50 void OnItemInstalled() override {}
53 AppsSearchResultsModelBridge* bridge_; // Weak. Owns us.
54 SearchResult* result_; // Weak. Owned by AppListModel::SearchResults.
56 base::scoped_nsobject<MenuController> context_menu_controller_;
58 DISALLOW_COPY_AND_ASSIGN(ItemObserver);
61 AppsSearchResultsModelBridge::AppsSearchResultsModelBridge(
62 AppsSearchResultsController* results_controller)
63 : parent_(results_controller) {
64 UpdateItemObservers();
65 [parent_ results]->AddObserver(this);
68 AppsSearchResultsModelBridge::~AppsSearchResultsModelBridge() {
69 [parent_ results]->RemoveObserver(this);
72 NSMenu* AppsSearchResultsModelBridge::MenuForItem(size_t index) {
73 DCHECK_LT(index, item_observers_.size());
74 return item_observers_[index]->GetContextMenu();
77 void AppsSearchResultsModelBridge::UpdateItemObservers() {
78 DCHECK(item_observers_.empty());
79 const size_t itemCount = [parent_ results]->item_count();
80 for (size_t i = 0 ; i < itemCount; ++i)
81 item_observers_.push_back(new ItemObserver(this, i));
84 void AppsSearchResultsModelBridge::ReloadDataForItems(
85 size_t start, size_t count) const {
86 NSIndexSet* column = [NSIndexSet indexSetWithIndex:0];
88 [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(start, count)];
89 [[parent_ tableView] reloadDataForRowIndexes:rows
90 columnIndexes:column];
93 void AppsSearchResultsModelBridge::ListItemsAdded(
94 size_t start, size_t count) {
95 item_observers_.clear();
96 if (start == static_cast<size_t>([[parent_ tableView] numberOfRows]))
97 [[parent_ tableView] noteNumberOfRowsChanged];
99 [[parent_ tableView] reloadData];
100 UpdateItemObservers();
103 void AppsSearchResultsModelBridge::ListItemsRemoved(
104 size_t start, size_t count) {
105 item_observers_.clear();
106 if (start == [parent_ results]->item_count())
107 [[parent_ tableView] noteNumberOfRowsChanged];
109 [[parent_ tableView] reloadData];
110 UpdateItemObservers();
113 void AppsSearchResultsModelBridge::ListItemMoved(
114 size_t index, size_t target_index) {
118 void AppsSearchResultsModelBridge::ListItemsChanged(
119 size_t start, size_t count) {
120 item_observers_.clear();
121 ReloadDataForItems(start, count);
122 UpdateItemObservers();
125 } // namespace app_list