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