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/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 // | 71 // |
72 // Other platforms don't appear to have the sense of "key window" that | 72 // Other platforms don't appear to have the sense of "key window" that |
73 // Mac does (I believe their fields lose focus when the window loses | 73 // Mac does (I believe their fields lose focus when the window loses |
74 // focus). Rather than modifying focus outside the control's edit | 74 // focus). Rather than modifying focus outside the control's edit |
75 // scope, when the window resigns key the autocomplete popup is | 75 // scope, when the window resigns key the autocomplete popup is |
76 // closed. model() still believes it has focus, and the popup will | 76 // closed. model() still believes it has focus, and the popup will |
77 // be regenerated on the user's next edit. That seems to match how | 77 // be regenerated on the user's next edit. That seems to match how |
78 // things work on other platforms. | 78 // things work on other platforms. |
79 | 79 |
80 namespace { | 80 namespace { |
| 81 const int kOmniboxLargeFontSizeDelta = 9; |
| 82 const int kOmniboxNormalFontSizeDelta = 1; |
| 83 const int kOmniboxSmallFontSizeDelta = 0; |
| 84 const int kOmniboxSmallMaterialFontSizeDelta = -1; |
81 | 85 |
82 // TODO(shess): This is ugly, find a better way. Using it right now | 86 // TODO(shess): This is ugly, find a better way. Using it right now |
83 // so that I can crib from gtk and still be able to see that I'm using | 87 // so that I can crib from gtk and still be able to see that I'm using |
84 // the same values easily. | 88 // the same values easily. |
85 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) { | 89 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) { |
86 DCHECK_LE(rr, 255); | 90 DCHECK_LE(rr, 255); |
87 DCHECK_LE(bb, 255); | 91 DCHECK_LE(bb, 255); |
88 DCHECK_LE(gg, 255); | 92 DCHECK_LE(gg, 255); |
89 return [NSColor colorWithCalibratedRed:static_cast<float>(rr)/255.0 | 93 return [NSColor colorWithCalibratedRed:static_cast<float>(rr)/255.0 |
90 green:static_cast<float>(gg)/255.0 | 94 green:static_cast<float>(gg)/255.0 |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 // |storage|. | 547 // |storage|. |
544 [field_ stringValue]; | 548 [field_ stringValue]; |
545 } else { | 549 } else { |
546 SetText(GetText()); | 550 SetText(GetText()); |
547 } | 551 } |
548 } | 552 } |
549 | 553 |
550 void OmniboxViewMac::ApplyTextStyle( | 554 void OmniboxViewMac::ApplyTextStyle( |
551 NSMutableAttributedString* attributedString) { | 555 NSMutableAttributedString* attributedString) { |
552 [attributedString addAttribute:NSFontAttributeName | 556 [attributedString addAttribute:NSFontAttributeName |
553 value:GetFieldFont(gfx::Font::NORMAL) | 557 value:GetNormalFieldFont() |
554 range:NSMakeRange(0, [attributedString length])]; | 558 range:NSMakeRange(0, [attributedString length])]; |
555 | 559 |
556 // Make a paragraph style locking in the standard line height as the maximum, | 560 // Make a paragraph style locking in the standard line height as the maximum, |
557 // otherwise the baseline may shift "downwards". | 561 // otherwise the baseline may shift "downwards". |
558 base::scoped_nsobject<NSMutableParagraphStyle> paragraph_style( | 562 base::scoped_nsobject<NSMutableParagraphStyle> paragraph_style( |
559 [[NSMutableParagraphStyle alloc] init]); | 563 [[NSMutableParagraphStyle alloc] init]); |
560 CGFloat line_height = [[field_ cell] lineHeight]; | 564 CGFloat line_height = [[field_ cell] lineHeight]; |
561 [paragraph_style setMaximumLineHeight:line_height]; | 565 [paragraph_style setMaximumLineHeight:line_height]; |
562 [paragraph_style setMinimumLineHeight:line_height]; | 566 [paragraph_style setMinimumLineHeight:line_height]; |
563 [paragraph_style setLineBreakMode:NSLineBreakByTruncatingTail]; | 567 [paragraph_style setLineBreakMode:NSLineBreakByTruncatingTail]; |
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1098 // If the text field has a field editor, it's the first responder, meaning | 1102 // If the text field has a field editor, it's the first responder, meaning |
1099 // that it's already focused. makeFirstResponder: will select all, so only | 1103 // that it's already focused. makeFirstResponder: will select all, so only |
1100 // call it if this behavior is desired. | 1104 // call it if this behavior is desired. |
1101 if (select_all || ![field_ currentEditor]) | 1105 if (select_all || ![field_ currentEditor]) |
1102 [[field_ window] makeFirstResponder:field_]; | 1106 [[field_ window] makeFirstResponder:field_]; |
1103 DCHECK_EQ([field_ currentEditor], [[field_ window] firstResponder]); | 1107 DCHECK_EQ([field_ currentEditor], [[field_ window] firstResponder]); |
1104 } | 1108 } |
1105 } | 1109 } |
1106 | 1110 |
1107 // static | 1111 // static |
1108 NSFont* OmniboxViewMac::GetFieldFont(int style) { | 1112 NSFont* OmniboxViewMac::GetNormalFieldFont() { |
1109 // This value should be kept in sync with InstantPage::InitializeFonts. | |
1110 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 1113 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
1111 return rb.GetFontList(ui::ResourceBundle::BaseFont).Derive(1, style) | 1114 return rb |
1112 .GetPrimaryFont().GetNativeFont(); | 1115 .GetFontWithDelta(kOmniboxNormalFontSizeDelta, gfx::Font::NORMAL, |
1113 } | 1116 gfx::Font::Weight::NORMAL) |
1114 | |
1115 NSFont* OmniboxViewMac::GetLargeFont(int style) { | |
1116 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
1117 return rb.GetFontList(ui::ResourceBundle::LargeFont) | |
1118 .Derive(1, style) | |
1119 .GetPrimaryFont() | |
1120 .GetNativeFont(); | 1117 .GetNativeFont(); |
1121 } | 1118 } |
1122 | 1119 |
1123 NSFont* OmniboxViewMac::GetSmallFont(int style) { | 1120 NSFont* OmniboxViewMac::GetBoldFieldFont() { |
1124 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 1121 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
1125 if (!ui::MaterialDesignController::IsModeMaterial()) { | 1122 return rb |
1126 return rb.GetFontList(ui::ResourceBundle::SmallFont) | 1123 .GetFontWithDelta(kOmniboxNormalFontSizeDelta, gfx::Font::NORMAL, |
1127 .Derive(1, style) | 1124 gfx::Font::Weight::BOLD) |
1128 .GetPrimaryFont() | |
1129 .GetNativeFont(); | |
1130 } | |
1131 return rb.GetFontListWithDelta(-2, gfx::Font::NORMAL) | |
1132 .Derive(1, style) | |
1133 .GetPrimaryFont() | |
1134 .GetNativeFont(); | 1125 .GetNativeFont(); |
1135 } | 1126 } |
1136 | 1127 |
| 1128 NSFont* OmniboxViewMac::GetLargeFont() { |
| 1129 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 1130 return rb |
| 1131 .GetFontWithDelta(kOmniboxLargeFontSizeDelta, gfx::Font::NORMAL, |
| 1132 gfx::Font::Weight::NORMAL) |
| 1133 .GetNativeFont(); |
| 1134 } |
| 1135 |
| 1136 NSFont* OmniboxViewMac::GetSmallFont() { |
| 1137 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 1138 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 1139 return rb |
| 1140 .GetFontWithDelta(kOmniboxSmallFontSizeDelta, gfx::Font::NORMAL, |
| 1141 gfx::Font::Weight::NORMAL) |
| 1142 .GetNativeFont(); |
| 1143 } |
| 1144 |
| 1145 return rb |
| 1146 .GetFontWithDelta(kOmniboxSmallMaterialFontSizeDelta, gfx::Font::NORMAL, |
| 1147 gfx::Font::Weight::NORMAL) |
| 1148 .GetNativeFont(); |
| 1149 } |
| 1150 |
1137 int OmniboxViewMac::GetOmniboxTextLength() const { | 1151 int OmniboxViewMac::GetOmniboxTextLength() const { |
1138 return static_cast<int>(GetTextLength()); | 1152 return static_cast<int>(GetTextLength()); |
1139 } | 1153 } |
1140 | 1154 |
1141 NSUInteger OmniboxViewMac::GetTextLength() const { | 1155 NSUInteger OmniboxViewMac::GetTextLength() const { |
1142 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : | 1156 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : |
1143 [[field_ stringValue] length]; | 1157 [[field_ stringValue] length]; |
1144 } | 1158 } |
1145 | 1159 |
1146 bool OmniboxViewMac::IsCaretAtEnd() const { | 1160 bool OmniboxViewMac::IsCaretAtEnd() const { |
1147 const NSRange selection = GetSelectedRange(); | 1161 const NSRange selection = GetSelectedRange(); |
1148 return NSMaxRange(selection) == GetTextLength(); | 1162 return NSMaxRange(selection) == GetTextLength(); |
1149 } | 1163 } |
1150 | 1164 |
1151 void OmniboxViewMac::AnnounceAutocompleteForScreenReader( | 1165 void OmniboxViewMac::AnnounceAutocompleteForScreenReader( |
1152 const base::string16& display_text) { | 1166 const base::string16& display_text) { |
1153 NSString* announcement = | 1167 NSString* announcement = |
1154 l10n_util::GetNSStringF(IDS_ANNOUNCEMENT_COMPLETION_AVAILABLE_MAC, | 1168 l10n_util::GetNSStringF(IDS_ANNOUNCEMENT_COMPLETION_AVAILABLE_MAC, |
1155 display_text); | 1169 display_text); |
1156 NSDictionary* notification_info = @{ | 1170 NSDictionary* notification_info = @{ |
1157 NSAccessibilityAnnouncementKey : announcement, | 1171 NSAccessibilityAnnouncementKey : announcement, |
1158 NSAccessibilityPriorityKey : @(NSAccessibilityPriorityHigh) | 1172 NSAccessibilityPriorityKey : @(NSAccessibilityPriorityHigh) |
1159 }; | 1173 }; |
1160 NSAccessibilityPostNotificationWithUserInfo( | 1174 NSAccessibilityPostNotificationWithUserInfo( |
1161 [field_ window], | 1175 [field_ window], |
1162 NSAccessibilityAnnouncementRequestedNotification, | 1176 NSAccessibilityAnnouncementRequestedNotification, |
1163 notification_info); | 1177 notification_info); |
1164 } | 1178 } |
OLD | NEW |