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_view_mac.h" | 5 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" |
6 | 6 |
7 #include <Carbon/Carbon.h> // kVK_Return | 7 #include <Carbon/Carbon.h> // kVK_Return |
8 | 8 |
9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // | 64 // |
65 // Other platforms don't appear to have the sense of "key window" that | 65 // Other platforms don't appear to have the sense of "key window" that |
66 // Mac does (I believe their fields lose focus when the window loses | 66 // Mac does (I believe their fields lose focus when the window loses |
67 // focus). Rather than modifying focus outside the control's edit | 67 // focus). Rather than modifying focus outside the control's edit |
68 // scope, when the window resigns key the autocomplete popup is | 68 // scope, when the window resigns key the autocomplete popup is |
69 // closed. model() still believes it has focus, and the popup will | 69 // closed. model() still believes it has focus, and the popup will |
70 // be regenerated on the user's next edit. That seems to match how | 70 // be regenerated on the user's next edit. That seems to match how |
71 // things work on other platforms. | 71 // things work on other platforms. |
72 | 72 |
73 namespace { | 73 namespace { |
| 74 const int kOmniboxLargeFontSizeDelta = 9; |
| 75 const int kOmniboxNormalFontSizeDelta = 1; |
| 76 const int kOmniboxSmallFontSizeDelta = 0; |
74 | 77 |
75 // TODO(shess): This is ugly, find a better way. Using it right now | 78 // TODO(shess): This is ugly, find a better way. Using it right now |
76 // so that I can crib from gtk and still be able to see that I'm using | 79 // so that I can crib from gtk and still be able to see that I'm using |
77 // the same values easily. | 80 // the same values easily. |
78 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) { | 81 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) { |
79 DCHECK_LE(rr, 255); | 82 DCHECK_LE(rr, 255); |
80 DCHECK_LE(bb, 255); | 83 DCHECK_LE(bb, 255); |
81 DCHECK_LE(gg, 255); | 84 DCHECK_LE(gg, 255); |
82 return [NSColor colorWithCalibratedRed:static_cast<float>(rr)/255.0 | 85 return [NSColor colorWithCalibratedRed:static_cast<float>(rr)/255.0 |
83 green:static_cast<float>(gg)/255.0 | 86 green:static_cast<float>(gg)/255.0 |
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 // |storage|. | 514 // |storage|. |
512 [field_ stringValue]; | 515 [field_ stringValue]; |
513 } else { | 516 } else { |
514 SetText(GetText()); | 517 SetText(GetText()); |
515 } | 518 } |
516 } | 519 } |
517 | 520 |
518 void OmniboxViewMac::ApplyTextStyle( | 521 void OmniboxViewMac::ApplyTextStyle( |
519 NSMutableAttributedString* attributedString) { | 522 NSMutableAttributedString* attributedString) { |
520 [attributedString addAttribute:NSFontAttributeName | 523 [attributedString addAttribute:NSFontAttributeName |
521 value:GetFieldFont(gfx::Font::NORMAL) | 524 value:GetNormalFieldFont() |
522 range:NSMakeRange(0, [attributedString length])]; | 525 range:NSMakeRange(0, [attributedString length])]; |
523 | 526 |
524 // Make a paragraph style locking in the standard line height as the maximum, | 527 // Make a paragraph style locking in the standard line height as the maximum, |
525 // otherwise the baseline may shift "downwards". | 528 // otherwise the baseline may shift "downwards". |
526 base::scoped_nsobject<NSMutableParagraphStyle> paragraph_style( | 529 base::scoped_nsobject<NSMutableParagraphStyle> paragraph_style( |
527 [[NSMutableParagraphStyle alloc] init]); | 530 [[NSMutableParagraphStyle alloc] init]); |
528 CGFloat line_height = [[field_ cell] lineHeight]; | 531 CGFloat line_height = [[field_ cell] lineHeight]; |
529 [paragraph_style setMaximumLineHeight:line_height]; | 532 [paragraph_style setMaximumLineHeight:line_height]; |
530 [paragraph_style setMinimumLineHeight:line_height]; | 533 [paragraph_style setMinimumLineHeight:line_height]; |
531 [paragraph_style setLineBreakMode:NSLineBreakByTruncatingTail]; | 534 [paragraph_style setLineBreakMode:NSLineBreakByTruncatingTail]; |
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1071 // If the text field has a field editor, it's the first responder, meaning | 1074 // If the text field has a field editor, it's the first responder, meaning |
1072 // that it's already focused. makeFirstResponder: will select all, so only | 1075 // that it's already focused. makeFirstResponder: will select all, so only |
1073 // call it if this behavior is desired. | 1076 // call it if this behavior is desired. |
1074 if (select_all || ![field_ currentEditor]) | 1077 if (select_all || ![field_ currentEditor]) |
1075 [[field_ window] makeFirstResponder:field_]; | 1078 [[field_ window] makeFirstResponder:field_]; |
1076 DCHECK_EQ([field_ currentEditor], [[field_ window] firstResponder]); | 1079 DCHECK_EQ([field_ currentEditor], [[field_ window] firstResponder]); |
1077 } | 1080 } |
1078 } | 1081 } |
1079 | 1082 |
1080 // static | 1083 // static |
1081 NSFont* OmniboxViewMac::GetFieldFont(int style) { | 1084 NSFont* OmniboxViewMac::GetNormalFieldFont() { |
1082 // This value should be kept in sync with InstantPage::InitializeFonts. | |
1083 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 1085 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
1084 return rb.GetFontList(ui::ResourceBundle::BaseFont).Derive(1, style) | 1086 return rb |
1085 .GetPrimaryFont().GetNativeFont(); | 1087 .GetFontWithDelta(kOmniboxNormalFontSizeDelta, gfx::Font::NORMAL, |
1086 } | 1088 gfx::Font::Weight::NORMAL) |
1087 | |
1088 NSFont* OmniboxViewMac::GetLargeFont(int style) { | |
1089 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
1090 return rb.GetFontList(ui::ResourceBundle::LargeFont) | |
1091 .Derive(1, style) | |
1092 .GetPrimaryFont() | |
1093 .GetNativeFont(); | 1089 .GetNativeFont(); |
1094 } | 1090 } |
1095 | 1091 |
1096 NSFont* OmniboxViewMac::GetSmallFont(int style) { | 1092 NSFont* OmniboxViewMac::GetBoldFieldFont() { |
1097 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 1093 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
1098 return rb.GetFontList(ui::ResourceBundle::SmallFont) | 1094 return rb |
1099 .Derive(1, style) | 1095 .GetFontWithDelta(kOmniboxNormalFontSizeDelta, gfx::Font::NORMAL, |
1100 .GetPrimaryFont() | 1096 gfx::Font::Weight::BOLD) |
1101 .GetNativeFont(); | 1097 .GetNativeFont(); |
1102 } | 1098 } |
1103 | 1099 |
| 1100 NSFont* OmniboxViewMac::GetLargeFont() { |
| 1101 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 1102 return rb |
| 1103 .GetFontWithDelta(kOmniboxLargeFontSizeDelta, gfx::Font::NORMAL, |
| 1104 gfx::Font::Weight::NORMAL) |
| 1105 .GetNativeFont(); |
| 1106 } |
| 1107 |
| 1108 NSFont* OmniboxViewMac::GetSmallFont() { |
| 1109 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 1110 return rb |
| 1111 .GetFontWithDelta(kOmniboxSmallFontSizeDelta, gfx::Font::NORMAL, |
| 1112 gfx::Font::Weight::NORMAL) |
| 1113 .GetNativeFont(); |
| 1114 } |
| 1115 |
1104 int OmniboxViewMac::GetOmniboxTextLength() const { | 1116 int OmniboxViewMac::GetOmniboxTextLength() const { |
1105 return static_cast<int>(GetTextLength()); | 1117 return static_cast<int>(GetTextLength()); |
1106 } | 1118 } |
1107 | 1119 |
1108 NSUInteger OmniboxViewMac::GetTextLength() const { | 1120 NSUInteger OmniboxViewMac::GetTextLength() const { |
1109 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : | 1121 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : |
1110 [[field_ stringValue] length]; | 1122 [[field_ stringValue] length]; |
1111 } | 1123 } |
1112 | 1124 |
1113 bool OmniboxViewMac::IsCaretAtEnd() const { | 1125 bool OmniboxViewMac::IsCaretAtEnd() const { |
1114 const NSRange selection = GetSelectedRange(); | 1126 const NSRange selection = GetSelectedRange(); |
1115 return NSMaxRange(selection) == GetTextLength(); | 1127 return NSMaxRange(selection) == GetTextLength(); |
1116 } | 1128 } |
OLD | NEW |