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/app_list_view_controller.h"
7 #include "base/mac/foundation_util.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "skia/ext/skia_utils_mac.h"
11 #include "ui/app_list/app_list_constants.h"
12 #include "ui/app_list/app_list_model.h"
13 #include "ui/app_list/app_list_view_delegate.h"
14 #include "ui/app_list/app_list_view_delegate_observer.h"
15 #include "ui/app_list/signin_delegate.h"
16 #import "ui/app_list/cocoa/app_list_pager_view.h"
17 #import "ui/app_list/cocoa/apps_grid_controller.h"
18 #import "ui/app_list/cocoa/signin_view_controller.h"
19 #import "ui/base/cocoa/flipped_view.h"
20 #include "ui/app_list/search_box_model.h"
21 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
25 // The roundedness of the corners of the bubble.
26 const CGFloat kBubbleCornerRadius = 3;
28 // Height of the pager.
29 const CGFloat kPagerPreferredHeight = 57;
31 // Height of separator line drawn between the searchbox and grid view.
32 const CGFloat kTopSeparatorSize = 1;
34 // Height of the search input.
35 const CGFloat kSearchInputHeight = 48;
37 // Minimum margin on either side of the pager. If the pager grows beyond this,
38 // the segment size is reduced.
39 const CGFloat kMinPagerMargin = 40;
40 // Maximum width of a single segment.
41 const CGFloat kMaxSegmentWidth = 80;
43 // Duration of the animation for sliding in and out search results.
44 const NSTimeInterval kResultsAnimationDuration = 0.2;
48 @interface BackgroundView : FlippedView;
51 @implementation BackgroundView
53 - (void)drawRect:(NSRect)dirtyRect {
54 gfx::ScopedNSGraphicsContextSaveGState context;
55 NSRect boundsRect = [self bounds];
56 NSRect searchAreaRect = NSMakeRect(0, 0,
57 NSWidth(boundsRect), kSearchInputHeight);
58 NSRect separatorRect = NSMakeRect(0, NSMaxY(searchAreaRect),
59 NSWidth(boundsRect), kTopSeparatorSize);
61 [[NSBezierPath bezierPathWithRoundedRect:boundsRect
62 xRadius:kBubbleCornerRadius
63 yRadius:kBubbleCornerRadius] addClip];
65 [gfx::SkColorToSRGBNSColor(app_list::kContentsBackgroundColor) set];
66 NSRectFill(boundsRect);
67 [gfx::SkColorToSRGBNSColor(app_list::kSearchBoxBackground) set];
68 NSRectFill(searchAreaRect);
69 [gfx::SkColorToSRGBNSColor(app_list::kTopSeparatorColor) set];
70 NSRectFill(separatorRect);
75 @interface AppListViewController ()
77 - (void)loadAndSetView;
78 - (void)revealSearchResults:(BOOL)show;
84 class AppListModelObserverBridge : public AppListViewDelegateObserver {
86 AppListModelObserverBridge(AppListViewController* parent);
87 virtual ~AppListModelObserverBridge();
90 // Overridden from app_list::AppListViewDelegateObserver:
91 virtual void OnProfilesChanged() OVERRIDE;
93 AppListViewController* parent_; // Weak. Owns us.
95 DISALLOW_COPY_AND_ASSIGN(AppListModelObserverBridge);
98 AppListModelObserverBridge::AppListModelObserverBridge(
99 AppListViewController* parent)
101 [parent_ delegate]->AddObserver(this);
104 AppListModelObserverBridge::~AppListModelObserverBridge() {
105 [parent_ delegate]->RemoveObserver(this);
108 void AppListModelObserverBridge::OnProfilesChanged() {
109 [parent_ onProfilesChanged];
112 } // namespace app_list
114 @implementation AppListViewController
117 if ((self = [super init])) {
118 appsGridController_.reset([[AppsGridController alloc] init]);
119 [self loadAndSetView];
121 [self totalPagesChanged];
122 [self selectedPageChanged:0];
123 [appsGridController_ setPaginationObserver:self];
129 // Ensure that setDelegate(NULL) has been called before destruction, because
130 // dealloc can be called at odd times, and Objective C destruction order does
131 // not properly tear down these dependencies.
132 DCHECK(delegate_ == NULL);
133 [appsGridController_ setPaginationObserver:nil];
137 - (AppsSearchBoxController*)searchBoxController {
138 return appsSearchBoxController_;
141 - (BOOL)showingSearchResults {
142 return showingSearchResults_;
145 - (AppsGridController*)appsGridController {
146 return appsGridController_;
149 - (NSSegmentedControl*)pagerControl {
150 return pagerControl_;
153 - (NSView*)backgroundView {
154 return backgroundView_;
157 - (app_list::AppListViewDelegate*)delegate {
158 return delegate_.get();
161 - (void)setDelegate:(scoped_ptr<app_list::AppListViewDelegate>)newDelegate {
163 // Ensure the search box is cleared when switching profiles.
164 if ([self searchBoxModel])
165 [self searchBoxModel]->SetText(base::string16());
167 // First clean up, in reverse order.
168 app_list_model_observer_bridge_.reset();
169 [appsSearchResultsController_ setDelegate:nil];
170 [appsSearchBoxController_ setDelegate:nil];
171 [appsGridController_ setDelegate:nil];
173 delegate_.reset(newDelegate.release());
175 [loadingIndicator_ stopAnimation:self];
177 [loadingIndicator_ startAnimation:self];
181 [appsGridController_ setDelegate:delegate_.get()];
182 [appsSearchBoxController_ setDelegate:self];
183 [appsSearchResultsController_ setDelegate:self];
184 app_list_model_observer_bridge_.reset(
185 new app_list::AppListModelObserverBridge(self));
186 [self onProfilesChanged];
189 -(void)loadAndSetView {
190 pagerControl_.reset([[AppListPagerView alloc] init]);
191 [pagerControl_ setTarget:appsGridController_];
192 [pagerControl_ setAction:@selector(onPagerClicked:)];
194 NSRect gridFrame = [[appsGridController_ view] frame];
195 NSRect contentsRect = NSMakeRect(0, kSearchInputHeight + kTopSeparatorSize,
196 NSWidth(gridFrame), NSHeight(gridFrame) + kPagerPreferredHeight -
197 [AppsGridController scrollerPadding]);
199 contentsView_.reset([[FlippedView alloc] initWithFrame:contentsRect]);
200 backgroundView_.reset(
201 [[BackgroundView alloc] initWithFrame:
202 NSMakeRect(0, 0, NSMaxX(contentsRect), NSMaxY(contentsRect))]);
203 appsSearchBoxController_.reset(
204 [[AppsSearchBoxController alloc] initWithFrame:
205 NSMakeRect(0, 0, NSWidth(contentsRect), kSearchInputHeight)]);
206 appsSearchResultsController_.reset(
207 [[AppsSearchResultsController alloc] initWithAppsSearchResultsFrameSize:
208 [contentsView_ bounds].size]);
209 base::scoped_nsobject<NSView> containerView(
210 [[NSView alloc] initWithFrame:[backgroundView_ frame]]);
212 loadingIndicator_.reset(
213 [[NSProgressIndicator alloc] initWithFrame:NSZeroRect]);
214 [loadingIndicator_ setStyle:NSProgressIndicatorSpinningStyle];
215 [loadingIndicator_ sizeToFit];
216 NSRect indicatorRect = [loadingIndicator_ frame];
217 indicatorRect.origin.x = NSWidth(contentsRect) / 2 - NSMidX(indicatorRect);
218 indicatorRect.origin.y = NSHeight(contentsRect) / 2 - NSMidY(indicatorRect);
219 [loadingIndicator_ setFrame:indicatorRect];
220 [loadingIndicator_ setDisplayedWhenStopped:NO];
221 [loadingIndicator_ startAnimation:self];
223 [contentsView_ addSubview:[appsGridController_ view]];
224 [contentsView_ addSubview:pagerControl_];
225 [contentsView_ addSubview:loadingIndicator_];
226 [backgroundView_ addSubview:contentsView_];
227 [backgroundView_ addSubview:[appsSearchResultsController_ view]];
228 [backgroundView_ addSubview:[appsSearchBoxController_ view]];
229 [containerView addSubview:backgroundView_];
230 [self setView:containerView];
233 - (void)revealSearchResults:(BOOL)show {
234 if (show == showingSearchResults_)
237 showingSearchResults_ = show;
238 NSSize contentsSize = [contentsView_ frame].size;
239 NSRect resultsTargetRect = NSMakeRect(
240 0, kSearchInputHeight + kTopSeparatorSize,
241 contentsSize.width, contentsSize.height);
242 NSRect contentsTargetRect = resultsTargetRect;
244 // Shows results by sliding the grid and pager down to the bottom of the view.
245 // Hides results by collapsing the search results container to a height of 0.
247 contentsTargetRect.origin.y += NSHeight(contentsTargetRect);
249 resultsTargetRect.size.height = 0;
251 [[NSAnimationContext currentContext] setDuration:kResultsAnimationDuration];
252 [[contentsView_ animator] setFrame:contentsTargetRect];
253 [[[appsSearchResultsController_ view] animator] setFrame:resultsTargetRect];
256 - (void)totalPagesChanged {
257 size_t pageCount = [appsGridController_ pageCount];
258 [pagerControl_ setSegmentCount:pageCount];
260 NSRect viewFrame = [[pagerControl_ superview] bounds];
261 CGFloat segmentWidth = std::min(
263 (viewFrame.size.width - 2 * kMinPagerMargin) / pageCount);
265 for (size_t i = 0; i < pageCount; ++i) {
266 [pagerControl_ setWidth:segmentWidth
268 [[pagerControl_ cell] setTag:i
273 [pagerControl_ sizeToFit];
274 [pagerControl_ setFrame:
275 NSMakeRect(NSMidX(viewFrame) - NSMidX([pagerControl_ bounds]),
276 viewFrame.size.height - kPagerPreferredHeight,
277 [pagerControl_ bounds].size.width,
278 kPagerPreferredHeight)];
281 - (void)selectedPageChanged:(int)newSelected {
282 [pagerControl_ selectSegmentWithTag:newSelected];
285 - (void)pageVisibilityChanged {
286 [pagerControl_ setNeedsDisplay:YES];
289 - (NSInteger)pagerSegmentAtLocation:(NSPoint)locationInWindow {
290 return [pagerControl_ findAndHighlightSegmentAtLocation:locationInWindow];
293 - (app_list::SearchBoxModel*)searchBoxModel {
294 app_list::AppListModel* appListModel = [appsGridController_ model];
295 return appListModel ? appListModel->search_box() : NULL;
298 - (app_list::AppListViewDelegate*)appListDelegate {
299 return [self delegate];
302 - (BOOL)control:(NSControl*)control
303 textView:(NSTextView*)textView
304 doCommandBySelector:(SEL)command {
305 if (showingSearchResults_)
306 return [appsSearchResultsController_ handleCommandBySelector:command];
308 // If anything has been written, let the search view handle it.
309 if ([[control stringValue] length] > 0)
313 if (command == @selector(complete:) ||
314 command == @selector(cancel:) ||
315 command == @selector(cancelOperation:)) {
317 delegate_->Dismiss();
321 // Possibly handle grid navigation.
322 return [appsGridController_ handleCommandBySelector:command];
325 - (void)modelTextDidChange {
326 app_list::SearchBoxModel* searchBoxModel = [self searchBoxModel];
327 if (!searchBoxModel || !delegate_)
330 base::string16 query;
331 TrimWhitespace(searchBoxModel->text(), TRIM_ALL, &query);
332 BOOL shouldShowSearch = !query.empty();
333 [self revealSearchResults:shouldShowSearch];
334 if (shouldShowSearch)
335 delegate_->StartSearch();
337 delegate_->StopSearch();
340 - (app_list::AppListModel*)appListModel {
341 return [appsGridController_ model];
344 - (void)openResult:(app_list::SearchResult*)result {
346 delegate_->OpenSearchResult(result, 0 /* event flags */);
350 [self modelTextDidChange];
353 - (void)onProfilesChanged {
354 [appsSearchBoxController_ rebuildMenu];
355 app_list::SigninDelegate* signinDelegate =
356 delegate_ ? delegate_->GetSigninDelegate() : NULL;
357 BOOL showSigninView = signinDelegate && signinDelegate->NeedSignin();
359 [[signinViewController_ view] removeFromSuperview];
360 signinViewController_.reset();
362 if (!showSigninView) {
363 [backgroundView_ setHidden:NO];
367 [backgroundView_ setHidden:YES];
368 signinViewController_.reset(
369 [[SigninViewController alloc] initWithFrame:[backgroundView_ frame]
370 cornerRadius:kBubbleCornerRadius
371 delegate:signinDelegate]);
372 [[self view] addSubview:[signinViewController_ view]];