File: app_list_view_controller.mm

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (368 lines) | stat: -rw-r--r-- 11,700 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ui/app_list/cocoa/app_list_view_controller.h"

#include "base/mac/foundation_util.h"
#include "base/mac/mac_util.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "skia/ext/skia_utils_mac.h"
#include "ui/app_list/app_list_constants.h"
#include "ui/app_list/app_list_model.h"
#include "ui/app_list/app_list_view_delegate.h"
#include "ui/app_list/app_list_view_delegate_observer.h"
#import "ui/app_list/cocoa/app_list_pager_view.h"
#import "ui/app_list/cocoa/apps_grid_controller.h"
#import "ui/base/cocoa/flipped_view.h"
#include "ui/app_list/search_box_model.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"

namespace {

// The roundedness of the corners of the bubble.
const CGFloat kBubbleCornerRadius = 3;

// Height of the pager.
const CGFloat kPagerPreferredHeight = 57;

// Height of separator line drawn between the searchbox and grid view.
const CGFloat kTopSeparatorSize = 1;

// Height of the search input.
const CGFloat kSearchInputHeight = 48;

// Minimum margin on either side of the pager. If the pager grows beyond this,
// the segment size is reduced.
const CGFloat kMinPagerMargin = 40;
// Maximum width of a single segment.
const CGFloat kMaxSegmentWidth = 80;

// Duration of the animation for sliding in and out search results.
const NSTimeInterval kResultsAnimationDuration = 0.2;

}  // namespace

@interface BackgroundView : FlippedView;
@end

@implementation BackgroundView

- (void)drawRect:(NSRect)dirtyRect {
  gfx::ScopedNSGraphicsContextSaveGState context;
  NSRect boundsRect = [self bounds];
  NSRect searchAreaRect = NSMakeRect(0, 0,
                                     NSWidth(boundsRect), kSearchInputHeight);
  NSRect separatorRect = NSMakeRect(0, NSMaxY(searchAreaRect),
                                    NSWidth(boundsRect), kTopSeparatorSize);

  [[NSBezierPath bezierPathWithRoundedRect:boundsRect
                                   xRadius:kBubbleCornerRadius
                                   yRadius:kBubbleCornerRadius] addClip];

  [gfx::SkColorToSRGBNSColor(app_list::kContentsBackgroundColor) set];
  NSRectFill(boundsRect);
  [gfx::SkColorToSRGBNSColor(app_list::kSearchBoxBackground) set];
  NSRectFill(searchAreaRect);
  [gfx::SkColorToSRGBNSColor(app_list::kTopSeparatorColor) set];
  NSRectFill(separatorRect);
}

@end

@interface AppListViewController ()

- (void)loadAndSetView;
- (void)revealSearchResults:(BOOL)show;

@end

namespace app_list {

class AppListModelObserverBridge : public AppListViewDelegateObserver {
 public:
  AppListModelObserverBridge(AppListViewController* parent);
  ~AppListModelObserverBridge() override;

 private:
  // Overridden from app_list::AppListViewDelegateObserver:
  void OnProfilesChanged() override;
  void OnShutdown() override;

  AppListViewController* parent_;  // Weak. Owns us.

  DISALLOW_COPY_AND_ASSIGN(AppListModelObserverBridge);
};

AppListModelObserverBridge::AppListModelObserverBridge(
    AppListViewController* parent)
    : parent_(parent) {
  [parent_ delegate]->AddObserver(this);
}

AppListModelObserverBridge::~AppListModelObserverBridge() {
  [parent_ delegate]->RemoveObserver(this);
}

void AppListModelObserverBridge::OnProfilesChanged() {
  [parent_ onProfilesChanged];
}

void AppListModelObserverBridge::OnShutdown() {
  [parent_ setDelegate:nil];
}

}  // namespace app_list

@implementation AppListViewController

- (id)init {
  if ((self = [super init])) {
    appsGridController_.reset([[AppsGridController alloc] init]);
    [self loadAndSetView];

    [self totalPagesChanged];
    [self selectedPageChanged:0];
    [appsGridController_ setPaginationObserver:self];
  }
  return self;
}

- (void)dealloc {
  // Ensure that setDelegate(NULL) has been called before destruction, because
  // dealloc can be called at odd times, and Objective C destruction order does
  // not properly tear down these dependencies.
  DCHECK(delegate_ == NULL);
  [appsGridController_ setPaginationObserver:nil];
  [super dealloc];
}

- (AppsSearchBoxController*)searchBoxController {
  return appsSearchBoxController_;
}

- (BOOL)showingSearchResults {
  return showingSearchResults_;
}

- (AppsGridController*)appsGridController {
  return appsGridController_;
}

- (NSSegmentedControl*)pagerControl {
  return pagerControl_;
}

- (NSView*)backgroundView {
  return backgroundView_;
}

- (app_list::AppListViewDelegate*)delegate {
  return delegate_;
}

- (void)setDelegate:(app_list::AppListViewDelegate*)newDelegate {
  if (delegate_) {
    // Ensure the search box is cleared when switching profiles.
    if ([self searchBoxModel])
      [self searchBoxModel]->SetText(base::string16());

    // First clean up, in reverse order.
    app_list_model_observer_bridge_.reset();
    [appsSearchResultsController_ setDelegate:nil];
    [appsSearchBoxController_ setDelegate:nil];
    [appsGridController_ setDelegate:nil];
  }
  delegate_ = newDelegate;
  if (delegate_) {
    [loadingIndicator_ stopAnimation:self];
  } else {
    [loadingIndicator_ startAnimation:self];
    return;
  }

  [appsGridController_ setDelegate:delegate_];
  [appsSearchBoxController_ setDelegate:self];
  [appsSearchResultsController_ setDelegate:self];
  app_list_model_observer_bridge_.reset(
      new app_list::AppListModelObserverBridge(self));
  [self onProfilesChanged];
}

-(void)loadAndSetView {
  pagerControl_.reset([[AppListPagerView alloc] init]);
  [pagerControl_ setTarget:appsGridController_];
  [pagerControl_ setAction:@selector(onPagerClicked:)];

  NSRect gridFrame = [[appsGridController_ view] frame];
  NSRect contentsRect = NSMakeRect(0, kSearchInputHeight + kTopSeparatorSize,
      NSWidth(gridFrame), NSHeight(gridFrame) + kPagerPreferredHeight -
          [AppsGridController scrollerPadding]);

  contentsView_.reset([[FlippedView alloc] initWithFrame:contentsRect]);

  // The contents view contains animations both from an NSCollectionView and the
  // app list's own transitive drag layers. On Mavericks, the subviews need to
  // have access to a compositing layer they can share. Otherwise the compositor
  // makes tearing artifacts. However, doing this on Mountain Lion or earler
  // results in flickering whilst an item is installing.
  if (base::mac::IsOSMavericksOrLater())
    [contentsView_ setWantsLayer:YES];

  backgroundView_.reset(
      [[BackgroundView alloc] initWithFrame:
              NSMakeRect(0, 0, NSMaxX(contentsRect), NSMaxY(contentsRect))]);
  appsSearchBoxController_.reset(
      [[AppsSearchBoxController alloc] initWithFrame:
          NSMakeRect(0, 0, NSWidth(contentsRect), kSearchInputHeight)]);
  appsSearchResultsController_.reset(
      [[AppsSearchResultsController alloc] initWithAppsSearchResultsFrameSize:
          [contentsView_ bounds].size]);
  base::scoped_nsobject<NSView> containerView(
      [[NSView alloc] initWithFrame:[backgroundView_ frame]]);

  loadingIndicator_.reset(
      [[NSProgressIndicator alloc] initWithFrame:NSZeroRect]);
  [loadingIndicator_ setStyle:NSProgressIndicatorSpinningStyle];
  [loadingIndicator_ sizeToFit];
  NSRect indicatorRect = [loadingIndicator_ frame];
  indicatorRect.origin.x = NSWidth(contentsRect) / 2 - NSMidX(indicatorRect);
  indicatorRect.origin.y = NSHeight(contentsRect) / 2 - NSMidY(indicatorRect);
  [loadingIndicator_ setFrame:indicatorRect];
  [loadingIndicator_ setDisplayedWhenStopped:NO];
  [loadingIndicator_ startAnimation:self];

  [contentsView_ addSubview:[appsGridController_ view]];
  [contentsView_ addSubview:pagerControl_];
  [contentsView_ addSubview:loadingIndicator_];
  [backgroundView_ addSubview:contentsView_];
  [backgroundView_ addSubview:[appsSearchResultsController_ view]];
  [backgroundView_ addSubview:[appsSearchBoxController_ view]];
  [containerView addSubview:backgroundView_];
  [self setView:containerView];
}

- (void)revealSearchResults:(BOOL)show {
  if (show == showingSearchResults_)
    return;

  showingSearchResults_ = show;
  NSSize contentsSize = [contentsView_ frame].size;
  NSRect resultsTargetRect = NSMakeRect(
      0, kSearchInputHeight + kTopSeparatorSize,
      contentsSize.width, contentsSize.height);
  NSRect contentsTargetRect = resultsTargetRect;

  // Shows results by sliding the grid and pager down to the bottom of the view.
  // Hides results by collapsing the search results container to a height of 0.
  if (show)
    contentsTargetRect.origin.y += NSHeight(contentsTargetRect);
  else
    resultsTargetRect.size.height = 0;

  [[NSAnimationContext currentContext] setDuration:kResultsAnimationDuration];
  [[contentsView_ animator] setFrame:contentsTargetRect];
  [[[appsSearchResultsController_ view] animator] setFrame:resultsTargetRect];
}

- (void)totalPagesChanged {
  size_t pageCount = [appsGridController_ pageCount];
  [pagerControl_ setSegmentCount:pageCount];

  NSRect viewFrame = [[pagerControl_ superview] bounds];
  CGFloat segmentWidth = std::min(
      kMaxSegmentWidth,
      (viewFrame.size.width - 2 * kMinPagerMargin) / pageCount);

  for (size_t i = 0; i < pageCount; ++i) {
    [pagerControl_ setWidth:segmentWidth
                 forSegment:i];
    [[pagerControl_ cell] setTag:i
                      forSegment:i];
  }

  // Center in view.
  [pagerControl_ sizeToFit];
  [pagerControl_ setFrame:
      NSMakeRect(NSMidX(viewFrame) - NSMidX([pagerControl_ bounds]),
                 viewFrame.size.height - kPagerPreferredHeight,
                 [pagerControl_ bounds].size.width,
                 kPagerPreferredHeight)];
}

- (void)selectedPageChanged:(int)newSelected {
  [pagerControl_ selectSegmentWithTag:newSelected];
}

- (void)pageVisibilityChanged {
  [pagerControl_ setNeedsDisplay:YES];
}

- (NSInteger)pagerSegmentAtLocation:(NSPoint)locationInWindow {
  return [pagerControl_ findAndHighlightSegmentAtLocation:locationInWindow];
}

- (app_list::SearchBoxModel*)searchBoxModel {
  app_list::AppListModel* appListModel = [appsGridController_ model];
  return appListModel ? appListModel->search_box() : NULL;
}

- (app_list::AppListViewDelegate*)appListDelegate {
  return [self delegate];
}

- (BOOL)control:(NSControl*)control
               textView:(NSTextView*)textView
    doCommandBySelector:(SEL)command {
  if (showingSearchResults_)
    return [appsSearchResultsController_ handleCommandBySelector:command];

  // If anything has been written, let the search view handle it.
  if ([[control stringValue] length] > 0)
    return NO;

  // Handle escape.
  if (command == @selector(complete:) ||
      command == @selector(cancel:) ||
      command == @selector(cancelOperation:)) {
    if (delegate_)
      delegate_->Dismiss();
    return YES;
  }

  // Possibly handle grid navigation.
  return [appsGridController_ handleCommandBySelector:command];
}

- (void)modelTextDidChange {
  app_list::SearchBoxModel* searchBoxModel = [self searchBoxModel];
  if (!searchBoxModel || !delegate_)
    return;

  base::string16 query;
  base::TrimWhitespace(searchBoxModel->text(), base::TRIM_ALL, &query);
  BOOL shouldShowSearch = !query.empty();
  [self revealSearchResults:shouldShowSearch];
  if (shouldShowSearch)
    delegate_->StartSearch();
  else
    delegate_->StopSearch();
}

- (app_list::AppListModel*)appListModel {
  return [appsGridController_ model];
}

- (void)openResult:(app_list::SearchResult*)result {
  if (delegate_) {
    delegate_->OpenSearchResult(
        result, false /* auto_launch */, 0 /* event flags */);
  }
}

- (void)onProfilesChanged {
  [appsSearchBoxController_ rebuildMenu];
}

@end