OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" | 5 #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h" |
6 | 6 |
7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
8 #include "app/text_elider.h" | 8 #include "app/text_elider.h" |
9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
10 #include "chrome/browser/autocomplete/autocomplete_edit.h" | 10 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 // autocomplete_popup_contents_view.cc, kGlassPopupTransparency). | 37 // autocomplete_popup_contents_view.cc, kGlassPopupTransparency). |
38 const CGFloat kPopupAlpha = 240.0 / 255.0; | 38 const CGFloat kPopupAlpha = 240.0 / 255.0; |
39 | 39 |
40 // How much space to leave for the left and right margins. | 40 // How much space to leave for the left and right margins. |
41 const CGFloat kLeftRightMargin = 5.0; | 41 const CGFloat kLeftRightMargin = 5.0; |
42 | 42 |
43 // How far to offset the text column from the left. | 43 // How far to offset the text column from the left. |
44 const CGFloat kTextXOffset = 29.0; | 44 const CGFloat kTextXOffset = 29.0; |
45 | 45 |
46 // Animation duration when animating the popup window smaller. | 46 // Animation duration when animating the popup window smaller. |
47 const CGFloat kShrinkAnimationDuration = 0.1; | 47 const NSTimeInterval kShrinkAnimationDuration = 0.1; |
| 48 |
| 49 // A very short animation duration used to cancel running animations. |
| 50 const NSTimeInterval kCancelAnimationDuration = 0.00001; |
48 | 51 |
49 // Maximum fraction of the popup width that can be used to display match | 52 // Maximum fraction of the popup width that can be used to display match |
50 // contents. | 53 // contents. |
51 const float kMaxContentsFraction = 0.7; | 54 const float kMaxContentsFraction = 0.7; |
52 | 55 |
53 // NSEvent -buttonNumber for middle mouse button. | 56 // NSEvent -buttonNumber for middle mouse button. |
54 const static NSInteger kMiddleButtonNumber(2); | 57 const static NSInteger kMiddleButtonNumber(2); |
55 | 58 |
56 // Background colors for different states of the popup elements. | 59 // Background colors for different states of the popup elements. |
57 NSColor* BackgroundColor() { | 60 NSColor* BackgroundColor() { |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 [popup_ setBackgroundColor:[NSColor clearColor]]; | 292 [popup_ setBackgroundColor:[NSColor clearColor]]; |
290 [popup_ setHasShadow:YES]; | 293 [popup_ setHasShadow:YES]; |
291 [popup_ setLevel:NSNormalWindowLevel]; | 294 [popup_ setLevel:NSNormalWindowLevel]; |
292 | 295 |
293 scoped_nsobject<AutocompleteMatrix> matrix( | 296 scoped_nsobject<AutocompleteMatrix> matrix( |
294 [[AutocompleteMatrix alloc] initWithPopupView:this]); | 297 [[AutocompleteMatrix alloc] initWithPopupView:this]); |
295 [popup_ setContentView:matrix]; | 298 [popup_ setContentView:matrix]; |
296 } | 299 } |
297 } | 300 } |
298 | 301 |
| 302 void AutocompletePopupViewMac::SetPopupFrame(const NSRect frame) { |
| 303 // Do nothing if the popup is already animating to the given |frame|. |
| 304 if (NSEqualRects(frame, targetPopupFrame_)) |
| 305 return; |
| 306 |
| 307 NSRect currentPopupFrame = [popup_ frame]; |
| 308 targetPopupFrame_ = frame; |
| 309 |
| 310 // Animate the frame change if the only change is that the height got smaller. |
| 311 // Otherwise, resize immediately. |
| 312 bool animate = (NSHeight(frame) < NSHeight(currentPopupFrame) && |
| 313 NSWidth(frame) == NSWidth(currentPopupFrame)); |
| 314 NSTimeInterval duration = |
| 315 (animate ? kShrinkAnimationDuration: kCancelAnimationDuration); |
| 316 |
| 317 [NSAnimationContext beginGrouping]; |
| 318 // Don't use the GTM additon for the "Steve" slowdown because this can happen |
| 319 // async from user actions and the effects could be a surprise. |
| 320 [[NSAnimationContext currentContext] setDuration:duration]; |
| 321 [[popup_ animator] setFrame:frame display:YES]; |
| 322 [NSAnimationContext endGrouping]; |
| 323 } |
| 324 |
299 void AutocompletePopupViewMac::UpdatePopupAppearance() { | 325 void AutocompletePopupViewMac::UpdatePopupAppearance() { |
300 DCHECK([NSThread isMainThread]); | 326 DCHECK([NSThread isMainThread]); |
301 const AutocompleteResult& result = model_->result(); | 327 const AutocompleteResult& result = model_->result(); |
302 if (result.empty()) { | 328 if (result.empty()) { |
303 [[popup_ parentWindow] removeChildWindow:popup_]; | 329 [[popup_ parentWindow] removeChildWindow:popup_]; |
304 [popup_ orderOut:nil]; | 330 [popup_ orderOut:nil]; |
305 | 331 |
306 // Break references to matrix_target_ before releasing popup_. | 332 // Break references to matrix_target_ before releasing popup_. |
307 NSMatrix* matrix = [popup_ contentView]; | 333 NSMatrix* matrix = [popup_ contentView]; |
308 [matrix setTarget:nil]; | 334 [matrix setTarget:nil]; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 } | 373 } |
348 | 374 |
349 // Set the cell size to fit a line of text in the cell's font. All | 375 // Set the cell size to fit a line of text in the cell's font. All |
350 // cells should use the same font and each should layout in one | 376 // cells should use the same font and each should layout in one |
351 // line, so they should all be about the same height. | 377 // line, so they should all be about the same height. |
352 const NSSize cellSize = [[matrix cellAtRow:0 column:0] cellSize]; | 378 const NSSize cellSize = [[matrix cellAtRow:0 column:0] cellSize]; |
353 DCHECK_GT(cellSize.height, 0.0); | 379 DCHECK_GT(cellSize.height, 0.0); |
354 [matrix setCellSize:NSMakeSize(r.size.width, | 380 [matrix setCellSize:NSMakeSize(r.size.width, |
355 cellSize.height + kCellHeightAdjust)]; | 381 cellSize.height + kCellHeightAdjust)]; |
356 | 382 |
| 383 // Save the old matrix frame in order to later restore it. |
| 384 NSRect savedMatrixFrame = [matrix frame]; |
| 385 |
357 // Make the matrix big enough to hold all the cells. | 386 // Make the matrix big enough to hold all the cells. |
358 [matrix sizeToCells]; | 387 [matrix sizeToCells]; |
359 | 388 |
360 // Make the window just as big. | 389 // Make the window just as big. |
361 r.size.height = [matrix frame].size.height; | 390 r.size.height = [matrix frame].size.height; |
362 r.origin.y -= r.size.height + kPopupFieldGap; | 391 r.origin.y -= r.size.height + kPopupFieldGap; |
363 | 392 |
| 393 // Resize the matrix back to its original size, since we aren't resizing the |
| 394 // window immediately. |
| 395 [matrix setFrame:savedMatrixFrame]; |
| 396 |
364 // Update the selection. | 397 // Update the selection. |
365 PaintUpdatesNow(); | 398 PaintUpdatesNow(); |
366 | 399 |
367 // Animate the frame change if the only change is that the height got smaller. | 400 SetPopupFrame(r); |
368 // Otherwise, resize immediately. | |
369 NSRect oldFrame = [popup_ frame]; | |
370 if (r.size.height < oldFrame.size.height && | |
371 r.origin.x == oldFrame.origin.x && | |
372 r.size.width == oldFrame.size.width) { | |
373 [NSAnimationContext beginGrouping]; | |
374 // Don't use the GTM additon for the "Steve" slowdown because this can | |
375 // happen async from user actions and the effects could be a surprise. | |
376 [[NSAnimationContext currentContext] setDuration:kShrinkAnimationDuration]; | |
377 [[popup_ animator] setFrame:r display:YES]; | |
378 [NSAnimationContext endGrouping]; | |
379 } else { | |
380 [popup_ setFrame:r display:YES]; | |
381 } | |
382 | 401 |
383 if (!IsOpen()) { | 402 if (!IsOpen()) { |
384 [[field_ window] addChildWindow:popup_ ordered:NSWindowAbove]; | 403 [[field_ window] addChildWindow:popup_ ordered:NSWindowAbove]; |
385 } | 404 } |
386 } | 405 } |
387 | 406 |
388 // This is only called by model in SetSelectedLine() after updating | 407 // This is only called by model in SetSelectedLine() after updating |
389 // everything. Popup should already be visible. | 408 // everything. Popup should already be visible. |
390 void AutocompletePopupViewMac::PaintUpdatesNow() { | 409 void AutocompletePopupViewMac::PaintUpdatesNow() { |
391 AutocompleteMatrix* matrix = [popup_ contentView]; | 410 AutocompleteMatrix* matrix = [popup_ contentView]; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 titleRect.size.width -= (kTextXOffset + kLeftRightMargin); | 496 titleRect.size.width -= (kTextXOffset + kLeftRightMargin); |
478 titleRect.origin.x += kTextXOffset; | 497 titleRect.origin.x += kTextXOffset; |
479 [self drawTitle:title withFrame:titleRect inView:controlView]; | 498 [self drawTitle:title withFrame:titleRect inView:controlView]; |
480 } | 499 } |
481 } | 500 } |
482 | 501 |
483 @end | 502 @end |
484 | 503 |
485 @implementation AutocompleteMatrix | 504 @implementation AutocompleteMatrix |
486 | 505 |
| 506 |
| 507 |
487 // Remove all tracking areas and initialize the one we want. Removing | 508 // Remove all tracking areas and initialize the one we want. Removing |
488 // all might be overkill, but it's unclear why there would be others | 509 // all might be overkill, but it's unclear why there would be others |
489 // for the popup window. | 510 // for the popup window. |
490 - (void)resetTrackingArea { | 511 - (void)resetTrackingArea { |
491 for (NSTrackingArea* trackingArea in [self trackingAreas]) { | 512 for (NSTrackingArea* trackingArea in [self trackingAreas]) { |
492 [self removeTrackingArea:trackingArea]; | 513 [self removeTrackingArea:trackingArea]; |
493 } | 514 } |
494 | 515 |
495 // TODO(shess): Consider overriding -acceptsFirstMouse: and changing | 516 // TODO(shess): Consider overriding -acceptsFirstMouse: and changing |
496 // NSTrackingActiveInActiveApp to NSTrackingActiveAlways. | 517 // NSTrackingActiveInActiveApp to NSTrackingActiveAlways. |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
675 bottomRightCornerRadius:0.0]; | 696 bottomRightCornerRadius:0.0]; |
676 | 697 |
677 // Draw the matrix clipped to our border. | 698 // Draw the matrix clipped to our border. |
678 [NSGraphicsContext saveGraphicsState]; | 699 [NSGraphicsContext saveGraphicsState]; |
679 [path addClip]; | 700 [path addClip]; |
680 [super drawRect:rect]; | 701 [super drawRect:rect]; |
681 [NSGraphicsContext restoreGraphicsState]; | 702 [NSGraphicsContext restoreGraphicsState]; |
682 } | 703 } |
683 | 704 |
684 @end | 705 @end |
OLD | NEW |