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/auto_reset.h" |
9 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
11 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
13 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
15 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 16 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/search/search.h" | 18 #include "chrome/browser/search/search.h" |
18 #include "chrome/browser/themes/theme_service.h" | 19 #include "chrome/browser/themes/theme_service.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 void StoreStateToTab(WebContents* tab, | 123 void StoreStateToTab(WebContents* tab, |
123 OmniboxViewMacState* state) { | 124 OmniboxViewMacState* state) { |
124 tab->SetUserData(kOmniboxViewMacStateKey, state); | 125 tab->SetUserData(kOmniboxViewMacStateKey, state); |
125 } | 126 } |
126 | 127 |
127 const OmniboxViewMacState* GetStateFromTab(const WebContents* tab) { | 128 const OmniboxViewMacState* GetStateFromTab(const WebContents* tab) { |
128 return static_cast<OmniboxViewMacState*>( | 129 return static_cast<OmniboxViewMacState*>( |
129 tab->GetUserData(&kOmniboxViewMacStateKey)); | 130 tab->GetUserData(&kOmniboxViewMacStateKey)); |
130 } | 131 } |
131 | 132 |
132 // Helper to make converting url ranges to NSRange easier to | |
133 // read. | |
134 NSRange ComponentToNSRange(const url::Component& component) { | |
135 return NSMakeRange(static_cast<NSInteger>(component.begin), | |
136 static_cast<NSInteger>(component.len)); | |
137 } | |
138 | |
139 } // namespace | 133 } // namespace |
140 | 134 |
141 // static | 135 // static |
142 NSImage* OmniboxViewMac::ImageForResource(int resource_id) { | 136 NSImage* OmniboxViewMac::ImageForResource(int resource_id) { |
143 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 137 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
144 return rb.GetNativeImageNamed(resource_id).ToNSImage(); | 138 return rb.GetNativeImageNamed(resource_id).ToNSImage(); |
145 } | 139 } |
146 | 140 |
147 // static | 141 // static |
148 NSColor* OmniboxViewMac::SuggestTextColor() { | 142 NSColor* OmniboxViewMac::SuggestTextColor() { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 base::WrapUnique(new ChromeOmniboxClient(controller, profile))), | 179 base::WrapUnique(new ChromeOmniboxClient(controller, profile))), |
186 profile_(profile), | 180 profile_(profile), |
187 popup_view_(new OmniboxPopupViewMac(this, model(), field)), | 181 popup_view_(new OmniboxPopupViewMac(this, model(), field)), |
188 field_(field), | 182 field_(field), |
189 saved_temporary_selection_(NSMakeRange(0, 0)), | 183 saved_temporary_selection_(NSMakeRange(0, 0)), |
190 marked_range_before_change_(NSMakeRange(0, 0)), | 184 marked_range_before_change_(NSMakeRange(0, 0)), |
191 delete_was_pressed_(false), | 185 delete_was_pressed_(false), |
192 delete_at_end_pressed_(false), | 186 delete_at_end_pressed_(false), |
193 in_coalesced_update_block_(false), | 187 in_coalesced_update_block_(false), |
194 do_coalesced_text_update_(false), | 188 do_coalesced_text_update_(false), |
195 do_coalesced_range_update_(false) { | 189 do_coalesced_range_update_(false), |
| 190 attributing_display_string_(nil) { |
196 [field_ setObserver:this]; | 191 [field_ setObserver:this]; |
197 | 192 |
198 // Needed so that editing doesn't lose the styling. | 193 // Needed so that editing doesn't lose the styling. |
199 [field_ setAllowsEditingTextAttributes:YES]; | 194 [field_ setAllowsEditingTextAttributes:YES]; |
200 | 195 |
201 // Get the appropriate line height for the font that we use. | 196 // Get the appropriate line height for the font that we use. |
202 base::scoped_nsobject<NSLayoutManager> layoutManager( | 197 base::scoped_nsobject<NSLayoutManager> layoutManager( |
203 [[NSLayoutManager alloc] init]); | 198 [[NSLayoutManager alloc] init]); |
204 [layoutManager setUsesScreenFonts:YES]; | 199 [layoutManager setUsesScreenFonts:YES]; |
205 } | 200 } |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 // If this is a URL, set the top-level paragraph direction to LTR (avoids RTL | 533 // If this is a URL, set the top-level paragraph direction to LTR (avoids RTL |
539 // characters from making the URL render from right to left, as per RFC 3987 | 534 // characters from making the URL render from right to left, as per RFC 3987 |
540 // Section 4.1). | 535 // Section 4.1). |
541 if (model()->CurrentTextIsURL()) | 536 if (model()->CurrentTextIsURL()) |
542 [paragraph_style setBaseWritingDirection:NSWritingDirectionLeftToRight]; | 537 [paragraph_style setBaseWritingDirection:NSWritingDirectionLeftToRight]; |
543 [attributedString addAttribute:NSParagraphStyleAttributeName | 538 [attributedString addAttribute:NSParagraphStyleAttributeName |
544 value:paragraph_style | 539 value:paragraph_style |
545 range:NSMakeRange(0, [attributedString length])]; | 540 range:NSMakeRange(0, [attributedString length])]; |
546 } | 541 } |
547 | 542 |
| 543 void OmniboxViewMac::SetEmphasis(bool emphasize, const gfx::Range& range) { |
| 544 bool in_dark_mode = [[field_ window] inIncognitoModeWithSystemTheme]; |
| 545 |
| 546 NSRange ns_range = range.IsValid() |
| 547 ? range.ToNSRange() |
| 548 : NSMakeRange(0, [attributing_display_string_ length]); |
| 549 |
| 550 [attributing_display_string_ |
| 551 addAttribute:NSForegroundColorAttributeName |
| 552 value:(emphasize) ? HostTextColor(in_dark_mode) |
| 553 : BaseTextColor(in_dark_mode) |
| 554 range:ns_range]; |
| 555 } |
| 556 |
| 557 void OmniboxViewMac::UpdateSchemeStyle(const gfx::Range& range) { |
| 558 if (!range.IsValid()) |
| 559 return; |
| 560 |
| 561 const security_state::SecurityLevel security_level = |
| 562 controller()->GetToolbarModel()->GetSecurityLevel(false); |
| 563 |
| 564 if ((security_level == security_state::NONE) || |
| 565 (security_level == security_state::HTTP_SHOW_WARNING)) |
| 566 return; |
| 567 |
| 568 if (security_level == security_state::DANGEROUS) { |
| 569 // Add a strikethrough through the scheme. |
| 570 [attributing_display_string_ |
| 571 addAttribute:NSStrikethroughStyleAttributeName |
| 572 value:[NSNumber numberWithInt:NSUnderlineStyleSingle] |
| 573 range:range.ToNSRange()]; |
| 574 } |
| 575 |
| 576 bool in_dark_mode = [[field_ window] inIncognitoModeWithSystemTheme]; |
| 577 |
| 578 [attributing_display_string_ |
| 579 addAttribute:NSForegroundColorAttributeName |
| 580 value:GetSecureTextColor(security_level, in_dark_mode) |
| 581 range:range.ToNSRange()]; |
| 582 } |
| 583 |
548 void OmniboxViewMac::ApplyTextAttributes( | 584 void OmniboxViewMac::ApplyTextAttributes( |
549 const base::string16& display_text, | 585 const base::string16& display_text, |
550 NSMutableAttributedString* attributedString) { | 586 NSMutableAttributedString* attributed_string) { |
551 NSUInteger as_length = [attributedString length]; | 587 NSUInteger as_length = [attributed_string length]; |
552 if (as_length == 0) { | 588 if (as_length == 0) { |
553 return; | 589 return; |
554 } | 590 } |
555 NSRange as_entire_string = NSMakeRange(0, as_length); | |
556 bool in_dark_mode = [[field_ window] inIncognitoModeWithSystemTheme]; | |
557 | 591 |
558 ApplyTextStyle(attributedString); | 592 ApplyTextStyle(attributed_string); |
559 | 593 |
560 // A kinda hacky way to add breaking at periods. This is what Safari does. | 594 // A kinda hacky way to add breaking at periods. This is what Safari does. |
561 // This works for IDNs too, despite the "en_US". | 595 // This works for IDNs too, despite the "en_US". |
562 [attributedString addAttribute:@"NSLanguage" | 596 [attributed_string addAttribute:@"NSLanguage" |
563 value:@"en_US_POSIX" | 597 value:@"en_US_POSIX" |
564 range:as_entire_string]; | 598 range:NSMakeRange(0, as_length)]; |
565 | 599 |
566 [attributedString addAttribute:NSForegroundColorAttributeName | 600 // Cache a pointer to the attributed string to allow the superclass' |
567 value:HostTextColor(in_dark_mode) | 601 // virtual method invocations to add attributes. |
568 range:as_entire_string]; | 602 DCHECK(attributing_display_string_ == nil); |
569 | 603 base::AutoReset<NSMutableAttributedString*> resetter( |
570 url::Component scheme, host; | 604 &attributing_display_string_, attributed_string); |
571 AutocompleteInput::ParseForEmphasizeComponents( | 605 UpdateTextStyle(display_text, ChromeAutocompleteSchemeClassifier(profile_)); |
572 display_text, ChromeAutocompleteSchemeClassifier(profile_), &scheme, | |
573 &host); | |
574 bool grey_out_url = display_text.substr(scheme.begin, scheme.len) == | |
575 base::UTF8ToUTF16(extensions::kExtensionScheme); | |
576 if (model()->CurrentTextIsURL() && | |
577 (host.is_nonempty() || grey_out_url)) { | |
578 [attributedString addAttribute:NSForegroundColorAttributeName | |
579 value:BaseTextColor(in_dark_mode) | |
580 range:as_entire_string]; | |
581 | |
582 if (!grey_out_url) { | |
583 [attributedString addAttribute:NSForegroundColorAttributeName | |
584 value:HostTextColor(in_dark_mode) | |
585 range:ComponentToNSRange(host)]; | |
586 } | |
587 } | |
588 | |
589 // TODO(shess): GTK has this as a member var, figure out why. | |
590 // [Could it be to not change if no change? If so, I'm guessing | |
591 // AppKit may already handle that.] | |
592 const security_state::SecurityLevel security_level = | |
593 controller()->GetToolbarModel()->GetSecurityLevel(false); | |
594 | |
595 // Emphasize the scheme for security UI display purposes (if necessary). | |
596 if (!model()->user_input_in_progress() && model()->CurrentTextIsURL() && | |
597 scheme.is_nonempty() && | |
598 (security_level != security_state::NONE) && | |
599 (security_level != security_state::HTTP_SHOW_WARNING)) { | |
600 if (security_level == security_state::DANGEROUS) { | |
601 // Add a strikethrough through the scheme. | |
602 [attributedString addAttribute:NSStrikethroughStyleAttributeName | |
603 value:[NSNumber numberWithInt:NSUnderlineStyleSingle] | |
604 range:ComponentToNSRange(scheme)]; | |
605 } | |
606 [attributedString | |
607 addAttribute:NSForegroundColorAttributeName | |
608 value:GetSecureTextColor(security_level, in_dark_mode) | |
609 range:ComponentToNSRange(scheme)]; | |
610 } | |
611 } | 606 } |
612 | 607 |
613 void OmniboxViewMac::OnTemporaryTextMaybeChanged( | 608 void OmniboxViewMac::OnTemporaryTextMaybeChanged( |
614 const base::string16& display_text, | 609 const base::string16& display_text, |
615 bool save_original_selection, | 610 bool save_original_selection, |
616 bool notify_text_changed) { | 611 bool notify_text_changed) { |
617 if (save_original_selection) | 612 if (save_original_selection) |
618 saved_temporary_selection_ = GetSelectedRange(); | 613 saved_temporary_selection_ = GetSelectedRange(); |
619 | 614 |
620 SetWindowTextAndCaretPos(display_text, display_text.size(), false, false); | 615 SetWindowTextAndCaretPos(display_text, display_text.size(), false, false); |
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1088 display_text); | 1083 display_text); |
1089 NSDictionary* notification_info = @{ | 1084 NSDictionary* notification_info = @{ |
1090 NSAccessibilityAnnouncementKey : announcement, | 1085 NSAccessibilityAnnouncementKey : announcement, |
1091 NSAccessibilityPriorityKey : @(NSAccessibilityPriorityHigh) | 1086 NSAccessibilityPriorityKey : @(NSAccessibilityPriorityHigh) |
1092 }; | 1087 }; |
1093 NSAccessibilityPostNotificationWithUserInfo( | 1088 NSAccessibilityPostNotificationWithUserInfo( |
1094 [field_ window], | 1089 [field_ window], |
1095 NSAccessibilityAnnouncementRequestedNotification, | 1090 NSAccessibilityAnnouncementRequestedNotification, |
1096 notification_info); | 1091 notification_info); |
1097 } | 1092 } |
OLD | NEW |