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