app_list_view_controller.mm revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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. 4 5#import "ui/app_list/cocoa/app_list_view_controller.h" 6 7#include "base/mac/foundation_util.h" 8#include "skia/ext/skia_utils_mac.h" 9#include "ui/app_list/app_list_constants.h" 10#include "ui/app_list/app_list_view_delegate.h" 11#import "ui/app_list/cocoa/app_list_pager_view.h" 12#import "ui/app_list/cocoa/apps_grid_controller.h" 13 14namespace { 15 16// The roundedness of the corners of the bubble. 17const CGFloat kBubbleCornerRadius = 3; 18 19// Height of the pager. 20const CGFloat kPagerPreferredHeight = 57; 21// Padding between the bottom of the grid and the bottom of the view. 22const CGFloat kViewGridOffsetY = 38; 23 24// Minimum margin on either side of the pager. If the pager grows beyond this, 25// the segment size is reduced. 26const CGFloat kMinPagerMargin = 40; 27// Maximum width of a single segment. 28const CGFloat kMaxSegmentWidth = 80; 29 30} // namespace 31 32@interface BackgroundView : NSView; 33@end 34 35@implementation BackgroundView 36 37- (void)drawRect:(NSRect)dirtyRect { 38 [NSGraphicsContext saveGraphicsState]; 39 [gfx::SkColorToCalibratedNSColor(app_list::kContentsBackgroundColor) set]; 40 [[NSBezierPath bezierPathWithRoundedRect:[self bounds] 41 xRadius:kBubbleCornerRadius 42 yRadius:kBubbleCornerRadius] addClip]; 43 NSRectFill([self bounds]); 44 [NSGraphicsContext restoreGraphicsState]; 45} 46 47@end 48 49@interface AppListViewController () 50 51- (void)loadAndSetView; 52 53@end 54 55@implementation AppListViewController 56 57- (id)init { 58 if ((self = [super init])) { 59 appsGridController_.reset([[AppsGridController alloc] init]); 60 [self loadAndSetView]; 61 62 [self totalPagesChanged]; 63 [self selectedPageChanged:0]; 64 [appsGridController_ setPaginationObserver:self]; 65 } 66 return self; 67} 68 69- (void)dealloc { 70 [appsGridController_ setPaginationObserver:nil]; 71 [appsGridController_ setDelegate:NULL]; 72 [super dealloc]; 73} 74 75- (AppsGridController*)appsGridController { 76 return appsGridController_; 77} 78 79- (NSSegmentedControl*)pagerControl { 80 return pagerControl_; 81} 82 83- (app_list::AppListViewDelegate*)delegate { 84 return delegate_.get(); 85} 86 87- (void)setDelegate:(scoped_ptr<app_list::AppListViewDelegate>)newDelegate { 88 delegate_.reset(newDelegate.release()); 89 [appsGridController_ setDelegate:delegate_.get()]; 90} 91 92-(void)loadAndSetView { 93 pagerControl_.reset([[AppListPagerView alloc] init]); 94 [pagerControl_ setTarget:appsGridController_]; 95 [pagerControl_ setAction:@selector(onPagerClicked:)]; 96 97 [[appsGridController_ view] setFrameOrigin:NSMakePoint(0, kViewGridOffsetY)]; 98 99 NSRect backgroundRect = [[appsGridController_ view] bounds]; 100 backgroundRect.size.height += kViewGridOffsetY; 101 scoped_nsobject<BackgroundView> backgroundView( 102 [[BackgroundView alloc] initWithFrame:backgroundRect]); 103 104 [backgroundView addSubview:[appsGridController_ view]]; 105 [backgroundView addSubview:pagerControl_]; 106 [self setView:backgroundView]; 107} 108 109- (void)totalPagesChanged { 110 size_t pageCount = [appsGridController_ pageCount]; 111 [pagerControl_ setSegmentCount:pageCount]; 112 113 NSRect viewFrame = [[self view] bounds]; 114 CGFloat segmentWidth = std::min( 115 kMaxSegmentWidth, 116 (viewFrame.size.width - 2 * kMinPagerMargin) / pageCount); 117 118 for (size_t i = 0; i < pageCount; ++i) { 119 [pagerControl_ setWidth:segmentWidth 120 forSegment:i]; 121 [[pagerControl_ cell] setTag:i 122 forSegment:i]; 123 } 124 125 // Center in view. 126 [pagerControl_ sizeToFit]; 127 [pagerControl_ setFrame: 128 NSMakeRect(NSMidX(viewFrame) - NSMidX([pagerControl_ bounds]), 129 0, 130 [pagerControl_ bounds].size.width, 131 kPagerPreferredHeight)]; 132} 133 134- (void)selectedPageChanged:(int)newSelected { 135 [pagerControl_ selectSegmentWithTag:newSelected]; 136} 137 138- (void)pageVisibilityChanged { 139 [pagerControl_ setNeedsDisplay:YES]; 140} 141 142@end 143