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