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 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 // security level. Dig in and figure out why this isn't a no-op | 234 // security level. Dig in and figure out why this isn't a no-op |
235 // that should go away. | 235 // that should go away. |
236 EmphasizeURLComponents(); | 236 EmphasizeURLComponents(); |
237 } | 237 } |
238 } | 238 } |
239 | 239 |
240 void OmniboxViewMac::UpdatePlaceholderText() { | 240 void OmniboxViewMac::UpdatePlaceholderText() { |
241 if (chrome::ShouldDisplayOriginChip() || | 241 if (chrome::ShouldDisplayOriginChip() || |
242 OmniboxFieldTrial::DisplayHintTextWhenPossible()) { | 242 OmniboxFieldTrial::DisplayHintTextWhenPossible()) { |
243 NSDictionary* placeholder_attributes = @{ | 243 NSDictionary* placeholder_attributes = @{ |
244 NSFontAttributeName : GetFieldFont(gfx::Font::NORMAL), | |
245 NSForegroundColorAttributeName : [NSColor disabledControlTextColor] | 244 NSForegroundColorAttributeName : [NSColor disabledControlTextColor] |
246 }; | 245 }; |
247 base::scoped_nsobject<NSMutableAttributedString> placeholder_text( | 246 base::scoped_nsobject<NSMutableAttributedString> placeholder_text( |
248 [[NSMutableAttributedString alloc] | 247 [[NSMutableAttributedString alloc] |
249 initWithString:base::SysUTF16ToNSString(GetHintText()) | 248 initWithString:base::SysUTF16ToNSString(GetHintText()) |
250 attributes:placeholder_attributes]); | 249 attributes:placeholder_attributes]); |
| 250 ApplyTextStyle(placeholder_text); |
251 [[field_ cell] setPlaceholderAttributedString:placeholder_text]; | 251 [[field_ cell] setPlaceholderAttributedString:placeholder_text]; |
252 } | 252 } |
253 } | 253 } |
254 | 254 |
255 void OmniboxViewMac::OpenMatch(const AutocompleteMatch& match, | 255 void OmniboxViewMac::OpenMatch(const AutocompleteMatch& match, |
256 WindowOpenDisposition disposition, | 256 WindowOpenDisposition disposition, |
257 const GURL& alternate_nav_url, | 257 const GURL& alternate_nav_url, |
258 const base::string16& pasted_text, | 258 const base::string16& pasted_text, |
259 size_t selected_line) { | 259 size_t selected_line) { |
260 // Coalesce text and selection updates from the following function. If we | 260 // Coalesce text and selection updates from the following function. If we |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 // This function can be called during the editor's -resignFirstResponder. If | 474 // This function can be called during the editor's -resignFirstResponder. If |
475 // that happens, |storage| and |field_| will not be synced automatically any | 475 // that happens, |storage| and |field_| will not be synced automatically any |
476 // more. Calling -stringValue ensures that |field_| reflects the changes to | 476 // more. Calling -stringValue ensures that |field_| reflects the changes to |
477 // |storage|. | 477 // |storage|. |
478 [field_ stringValue]; | 478 [field_ stringValue]; |
479 } else { | 479 } else { |
480 SetText(GetText()); | 480 SetText(GetText()); |
481 } | 481 } |
482 } | 482 } |
483 | 483 |
484 void OmniboxViewMac::ApplyTextAttributes(const base::string16& display_text, | 484 void OmniboxViewMac::ApplyTextStyle(NSMutableAttributedString* as) { |
485 NSMutableAttributedString* as) { | |
486 NSUInteger as_length = [as length]; | |
487 NSRange as_entire_string = NSMakeRange(0, as_length); | |
488 | |
489 [as addAttribute:NSFontAttributeName value:GetFieldFont(gfx::Font::NORMAL) | 485 [as addAttribute:NSFontAttributeName value:GetFieldFont(gfx::Font::NORMAL) |
490 range:as_entire_string]; | 486 range:NSMakeRange(0, [as length])]; |
491 | |
492 // A kinda hacky way to add breaking at periods. This is what Safari does. | |
493 // This works for IDNs too, despite the "en_US". | |
494 [as addAttribute:@"NSLanguage" value:@"en_US_POSIX" | |
495 range:as_entire_string]; | |
496 | 487 |
497 // Make a paragraph style locking in the standard line height as the maximum, | 488 // Make a paragraph style locking in the standard line height as the maximum, |
498 // otherwise the baseline may shift "downwards". | 489 // otherwise the baseline may shift "downwards". |
499 base::scoped_nsobject<NSMutableParagraphStyle> paragraph_style( | 490 base::scoped_nsobject<NSMutableParagraphStyle> paragraph_style( |
500 [[NSMutableParagraphStyle alloc] init]); | 491 [[NSMutableParagraphStyle alloc] init]); |
501 CGFloat line_height = [[field_ cell] lineHeight]; | 492 CGFloat line_height = [[field_ cell] lineHeight]; |
502 [paragraph_style setMaximumLineHeight:line_height]; | 493 [paragraph_style setMaximumLineHeight:line_height]; |
503 [paragraph_style setMinimumLineHeight:line_height]; | 494 [paragraph_style setMinimumLineHeight:line_height]; |
504 [paragraph_style setLineBreakMode:NSLineBreakByTruncatingTail]; | 495 [paragraph_style setLineBreakMode:NSLineBreakByTruncatingTail]; |
505 [as addAttribute:NSParagraphStyleAttributeName value:paragraph_style | 496 [as addAttribute:NSParagraphStyleAttributeName value:paragraph_style |
| 497 range:NSMakeRange(0, [as length])]; |
| 498 } |
| 499 |
| 500 void OmniboxViewMac::ApplyTextAttributes(const base::string16& display_text, |
| 501 NSMutableAttributedString* as) { |
| 502 NSUInteger as_length = [as length]; |
| 503 NSRange as_entire_string = NSMakeRange(0, as_length); |
| 504 |
| 505 ApplyTextStyle(as); |
| 506 |
| 507 // A kinda hacky way to add breaking at periods. This is what Safari does. |
| 508 // This works for IDNs too, despite the "en_US". |
| 509 [as addAttribute:@"NSLanguage" value:@"en_US_POSIX" |
506 range:as_entire_string]; | 510 range:as_entire_string]; |
507 | 511 |
508 url::Component scheme, host; | 512 url::Component scheme, host; |
509 AutocompleteInput::ParseForEmphasizeComponents( | 513 AutocompleteInput::ParseForEmphasizeComponents( |
510 display_text, ChromeAutocompleteSchemeClassifier(profile()), | 514 display_text, ChromeAutocompleteSchemeClassifier(profile()), |
511 &scheme, &host); | 515 &scheme, &host); |
512 bool grey_out_url = display_text.substr(scheme.begin, scheme.len) == | 516 bool grey_out_url = display_text.substr(scheme.begin, scheme.len) == |
513 base::UTF8ToUTF16(extensions::kExtensionScheme); | 517 base::UTF8ToUTF16(extensions::kExtensionScheme); |
514 if (model()->CurrentTextIsURL() && | 518 if (model()->CurrentTextIsURL() && |
515 (host.is_nonempty() || grey_out_url)) { | 519 (host.is_nonempty() || grey_out_url)) { |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1025 | 1029 |
1026 NSUInteger OmniboxViewMac::GetTextLength() const { | 1030 NSUInteger OmniboxViewMac::GetTextLength() const { |
1027 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : | 1031 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : |
1028 [[field_ stringValue] length]; | 1032 [[field_ stringValue] length]; |
1029 } | 1033 } |
1030 | 1034 |
1031 bool OmniboxViewMac::IsCaretAtEnd() const { | 1035 bool OmniboxViewMac::IsCaretAtEnd() const { |
1032 const NSRange selection = GetSelectedRange(); | 1036 const NSRange selection = GetSelectedRange(); |
1033 return NSMaxRange(selection) == GetTextLength(); | 1037 return NSMaxRange(selection) == GetTextLength(); |
1034 } | 1038 } |
OLD | NEW |