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/search_result.h"
11 #include "ui/app_list/search_result_observer.h"
12 #import "ui/base/cocoa/menu_controller.h"
16 class AppsSearchResultsModelBridge::ItemObserver : public SearchResultObserver {
18 ItemObserver(AppsSearchResultsModelBridge* bridge, size_t index)
19 : bridge_(bridge), row_in_view_(index) {
20 // Cache the result, because the results array is updated before notifying
21 // observers (which happens before deleting the SearchResult).
22 result_ = bridge_->results_->GetItemAt(index);
23 result_->AddObserver(this);
26 virtual ~ItemObserver() {
27 result_->RemoveObserver(this);
30 NSMenu* GetContextMenu() {
31 if (!context_menu_controller_) {
32 context_menu_controller_.reset(
33 [[MenuController alloc] initWithModel:result_->GetContextMenuModel()
34 useWithPopUpButtonCell:NO]);
36 return [context_menu_controller_ menu];
39 // SearchResultObserver overrides:
40 virtual void OnIconChanged() OVERRIDE {
41 bridge_->ReloadDataForItems(row_in_view_, 1);
43 virtual void OnActionsChanged() OVERRIDE {}
44 virtual void OnIsInstallingChanged() OVERRIDE {}
45 virtual void OnPercentDownloadedChanged() OVERRIDE {}
46 virtual void OnItemInstalled() OVERRIDE {}
47 virtual void OnItemUninstalled() OVERRIDE {}
50 AppsSearchResultsModelBridge* bridge_; // Weak. Owns us.
51 SearchResult* result_; // Weak. Owned by AppListModel::SearchResults.
53 base::scoped_nsobject<MenuController> context_menu_controller_;
55 DISALLOW_COPY_AND_ASSIGN(ItemObserver);
58 AppsSearchResultsModelBridge::AppsSearchResultsModelBridge(
59 AppListModel::SearchResults* results_model,
60 NSTableView* results_table_view)
61 : results_(results_model),
62 table_view_([results_table_view retain]) {
63 UpdateItemObservers();
64 results_->AddObserver(this);
67 AppsSearchResultsModelBridge::~AppsSearchResultsModelBridge() {
68 results_->RemoveObserver(this);
71 NSMenu* AppsSearchResultsModelBridge::MenuForItem(size_t index) {
72 DCHECK_LT(index, item_observers_.size());
73 return item_observers_[index]->GetContextMenu();
76 void AppsSearchResultsModelBridge::UpdateItemObservers() {
77 DCHECK(item_observers_.empty());
78 const size_t itemCount = results_->item_count();
79 for (size_t i = 0 ; i < itemCount; ++i)
80 item_observers_.push_back(new ItemObserver(this, i));
83 void AppsSearchResultsModelBridge::ReloadDataForItems(
84 size_t start, size_t count) const {
85 NSIndexSet* column = [NSIndexSet indexSetWithIndex:0];
87 [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(start, count)];
88 [table_view_ reloadDataForRowIndexes:rows
89 columnIndexes:column];
92 void AppsSearchResultsModelBridge::ListItemsAdded(
93 size_t start, size_t count) {
94 item_observers_.clear();
95 if (start == static_cast<size_t>([table_view_ numberOfRows]))
96 [table_view_ noteNumberOfRowsChanged];
98 [table_view_ reloadData];
99 UpdateItemObservers();
102 void AppsSearchResultsModelBridge::ListItemsRemoved(
103 size_t start, size_t count) {
104 item_observers_.clear();
105 if (start == results_->item_count())
106 [table_view_ noteNumberOfRowsChanged];
108 [table_view_ reloadData];
109 UpdateItemObservers();
112 void AppsSearchResultsModelBridge::ListItemMoved(
113 size_t index, size_t target_index) {
117 void AppsSearchResultsModelBridge::ListItemsChanged(
118 size_t start, size_t count) {
119 item_observers_.clear();
120 ReloadDataForItems(start, count);
121 UpdateItemObservers();
124 } // namespace app_list