OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ui/cocoa/omnibox/omnibox_popup_view_mac.h" | 5 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
11 #include "chrome/browser/autocomplete/autocomplete_match.h" | 11 #include "chrome/browser/autocomplete/autocomplete_match.h" |
12 #include "chrome/browser/ui/cocoa/event_utils.h" | 12 #include "chrome/browser/ui/cocoa/event_utils.h" |
13 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" | 13 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" |
14 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" | 14 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
15 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" | 15 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
16 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
17 #include "skia/ext/skia_utils_mac.h" | 17 #include "skia/ext/skia_utils_mac.h" |
18 #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h" | 18 #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h" |
19 #include "ui/base/cocoa/window_size_constants.h" | 19 #include "ui/base/cocoa/window_size_constants.h" |
20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
21 #include "ui/base/text/text_elider.h" | 21 #include "ui/base/text/text_elider.h" |
22 #include "ui/gfx/rect.h" | 22 #include "ui/gfx/rect.h" |
23 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 23 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
24 | 24 |
25 | |
26 #if !defined(MAC_OS_X_VERSION_10_7) || \ | |
27 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | |
28 @interface NSWindow (LionAPI) | |
29 - (CGFloat)backingScaleFactor; | |
30 @end | |
31 #endif // 10.7 | |
32 | |
33 | |
34 // The size delta between the font used for the edit and the result | 25 // The size delta between the font used for the edit and the result |
35 // rows. | 26 // rows. |
36 const int kEditFontAdjust = -1; | 27 const int kEditFontAdjust = -1; |
37 | 28 |
38 // How much to adjust the cell sizing up from the default determined | 29 // How much to adjust the cell sizing up from the default determined |
39 // by the font. | 30 // by the font. |
40 const int kCellHeightAdjust = 6.0; | 31 const int kCellHeightAdjust = 6.0; |
41 | 32 |
42 // How to round off the popup's corners. Goal is to match star and go | 33 // How to round off the popup's corners. Goal is to match star and go |
43 // buttons. | 34 // buttons. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 NSColor* ContentTextColor() { | 83 NSColor* ContentTextColor() { |
93 return [NSColor blackColor]; | 84 return [NSColor blackColor]; |
94 } | 85 } |
95 NSColor* DimContentTextColor() { | 86 NSColor* DimContentTextColor() { |
96 return [NSColor darkGrayColor]; | 87 return [NSColor darkGrayColor]; |
97 } | 88 } |
98 NSColor* URLTextColor() { | 89 NSColor* URLTextColor() { |
99 return [NSColor colorWithCalibratedRed:0.0 green:0.55 blue:0.0 alpha:1.0]; | 90 return [NSColor colorWithCalibratedRed:0.0 green:0.55 blue:0.0 alpha:1.0]; |
100 } | 91 } |
101 | 92 |
102 CGFloat ScaleFactor(NSWindow* window) { | |
103 if ([window respondsToSelector:@selector(backingScaleFactor)]) | |
104 return [window backingScaleFactor]; | |
105 return [window userSpaceScaleFactor]; | |
106 } | |
107 | |
108 } // namespace | 93 } // namespace |
109 | 94 |
110 // Helper for MatchText() to allow sharing code between the contents | 95 // Helper for MatchText() to allow sharing code between the contents |
111 // and description cases. Returns NSMutableAttributedString as a | 96 // and description cases. Returns NSMutableAttributedString as a |
112 // convenience for MatchText(). | 97 // convenience for MatchText(). |
113 NSMutableAttributedString* OmniboxPopupViewMac::DecorateMatchedString( | 98 NSMutableAttributedString* OmniboxPopupViewMac::DecorateMatchedString( |
114 const string16 &matchString, | 99 const string16 &matchString, |
115 const AutocompleteMatch::ACMatchClassifications &classifications, | 100 const AutocompleteMatch::ACMatchClassifications &classifications, |
116 NSColor* textColor, NSColor* dimTextColor, gfx::Font& font) { | 101 NSColor* textColor, NSColor* dimTextColor, gfx::Font& font) { |
117 // Cache for on-demand computation of the bold version of |font|. | 102 // Cache for on-demand computation of the bold version of |font|. |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 // Calculate the popup's position on the screen. It should abut the | 335 // Calculate the popup's position on the screen. It should abut the |
351 // field's visual border vertically, and be below the bounds | 336 // field's visual border vertically, and be below the bounds |
352 // horizontally. | 337 // horizontally. |
353 | 338 |
354 // Start with the field's rect on the screen. | 339 // Start with the field's rect on the screen. |
355 NSRect popupFrame = NSInsetRect([field_ bounds], 0.0, kFieldVisualInset); | 340 NSRect popupFrame = NSInsetRect([field_ bounds], 0.0, kFieldVisualInset); |
356 popupFrame = [field_ convertRect:popupFrame toView:nil]; | 341 popupFrame = [field_ convertRect:popupFrame toView:nil]; |
357 popupFrame.origin = [[field_ window] convertBaseToScreen:popupFrame.origin]; | 342 popupFrame.origin = [[field_ window] convertBaseToScreen:popupFrame.origin]; |
358 | 343 |
359 // Size to fit the matrix, and shift down by the size plus the top | 344 // Size to fit the matrix, and shift down by the size plus the top |
360 // window border. Would prefer -convertSize:fromView: to | 345 // window border. |
361 // -userSpaceScaleFactor for the scale conversion, but until the | 346 popupFrame.size.height = matrixHeight; |
362 // window is on-screen that doesn't work right (bug?). | |
363 popupFrame.size.height = matrixHeight * ScaleFactor(popup_); | |
364 popupFrame.origin.y -= NSHeight(popupFrame) + kWindowBorderWidth; | 347 popupFrame.origin.y -= NSHeight(popupFrame) + kWindowBorderWidth; |
365 | 348 |
366 // Inset to account for the horizontal border drawn by the window. | 349 // Inset to account for the horizontal border drawn by the window. |
367 popupFrame = NSInsetRect(popupFrame, kWindowBorderWidth, 0.0); | 350 popupFrame = NSInsetRect(popupFrame, kWindowBorderWidth, 0.0); |
368 | 351 |
369 // Leave a gap between the popup and the field. | 352 // Leave a gap between the popup and the field. |
370 popupFrame.origin.y -= kPopupFieldGap * ScaleFactor(popup_); | 353 popupFrame.origin.y -= kPopupFieldGap; |
371 | 354 |
372 // Do nothing if the popup is already animating to the given |frame|. | 355 // Do nothing if the popup is already animating to the given |frame|. |
373 if (NSEqualRects(popupFrame, targetPopupFrame_)) | 356 if (NSEqualRects(popupFrame, targetPopupFrame_)) |
374 return; | 357 return; |
375 | 358 |
376 NSRect currentPopupFrame = [popup_ frame]; | 359 NSRect currentPopupFrame = [popup_ frame]; |
377 targetPopupFrame_ = popupFrame; | 360 targetPopupFrame_ = popupFrame; |
378 | 361 |
379 // Animate the frame change if the only change is that the height got smaller. | 362 // Animate the frame change if the only change is that the height got smaller. |
380 // Otherwise, resize immediately. | 363 // Otherwise, resize immediately. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 | 430 |
448 // The popup's font is a slightly smaller version of the field's. | 431 // The popup's font is a slightly smaller version of the field's. |
449 NSFont* fieldFont = OmniboxViewMac::GetFieldFont(); | 432 NSFont* fieldFont = OmniboxViewMac::GetFieldFont(); |
450 const CGFloat resultFontSize = [fieldFont pointSize] + kEditFontAdjust; | 433 const CGFloat resultFontSize = [fieldFont pointSize] + kEditFontAdjust; |
451 gfx::Font resultFont(base::SysNSStringToUTF8([fieldFont fontName]), | 434 gfx::Font resultFont(base::SysNSStringToUTF8([fieldFont fontName]), |
452 static_cast<int>(resultFontSize)); | 435 static_cast<int>(resultFontSize)); |
453 | 436 |
454 AutocompleteMatrix* matrix = [popup_ contentView]; | 437 AutocompleteMatrix* matrix = [popup_ contentView]; |
455 | 438 |
456 // Calculate the width of the matrix based on backing out the | 439 // Calculate the width of the matrix based on backing out the |
457 // popup's border from the width of the field. Would prefer to use | 440 // popup's border from the width of the field. |
458 // [matrix convertSize:fromView:] for converting from screen size, | |
459 // but that doesn't work until the popup is on-screen (bug?). | |
460 const NSRect fieldRectBase = [field_ convertRect:[field_ bounds] toView:nil]; | 441 const NSRect fieldRectBase = [field_ convertRect:[field_ bounds] toView:nil]; |
461 const CGFloat popupWidth = NSWidth(fieldRectBase) - 2 * kWindowBorderWidth; | 442 const CGFloat popupWidth = NSWidth(fieldRectBase) - 2 * kWindowBorderWidth; |
462 DCHECK_GT(popupWidth, 0.0); | 443 DCHECK_GT(popupWidth, 0.0); |
463 const CGFloat matrixWidth = popupWidth / ScaleFactor(popup_); | 444 const CGFloat matrixWidth = popupWidth; |
464 | 445 |
465 // Load the results into the popup's matrix. | 446 // Load the results into the popup's matrix. |
466 const size_t rows = model_->result().size(); | 447 const size_t rows = model_->result().size(); |
467 DCHECK_GT(rows, 0U); | 448 DCHECK_GT(rows, 0U); |
468 [matrix renewRows:rows columns:1]; | 449 [matrix renewRows:rows columns:1]; |
469 for (size_t ii = 0; ii < rows; ++ii) { | 450 for (size_t ii = 0; ii < rows; ++ii) { |
470 AutocompleteButtonCell* cell = [matrix cellAtRow:ii column:0]; | 451 AutocompleteButtonCell* cell = [matrix cellAtRow:ii column:0]; |
471 const AutocompleteMatch& match = model_->result().match_at(ii); | 452 const AutocompleteMatch& match = model_->result().match_at(ii); |
472 [cell setImage:ImageForMatch(match)]; | 453 [cell setImage:ImageForMatch(match)]; |
473 [cell setAttributedTitle:MatchText(match, resultFont, matrixWidth)]; | 454 [cell setAttributedTitle:MatchText(match, resultFont, matrixWidth)]; |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
775 xRadius:kPopupRoundingRadius | 756 xRadius:kPopupRoundingRadius |
776 yRadius:kPopupRoundingRadius]; | 757 yRadius:kPopupRoundingRadius]; |
777 | 758 |
778 // Draw the matrix clipped to our border. | 759 // Draw the matrix clipped to our border. |
779 gfx::ScopedNSGraphicsContextSaveGState scopedGState; | 760 gfx::ScopedNSGraphicsContextSaveGState scopedGState; |
780 [path addClip]; | 761 [path addClip]; |
781 [super drawRect:rect]; | 762 [super drawRect:rect]; |
782 } | 763 } |
783 | 764 |
784 @end | 765 @end |
OLD | NEW |