Chromium Code Reviews| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) { | 79 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) { |
| 80 DCHECK_LE(rr, 255); | 80 DCHECK_LE(rr, 255); |
| 81 DCHECK_LE(bb, 255); | 81 DCHECK_LE(bb, 255); |
| 82 DCHECK_LE(gg, 255); | 82 DCHECK_LE(gg, 255); |
| 83 return [NSColor colorWithCalibratedRed:static_cast<float>(rr)/255.0 | 83 return [NSColor colorWithCalibratedRed:static_cast<float>(rr)/255.0 |
| 84 green:static_cast<float>(gg)/255.0 | 84 green:static_cast<float>(gg)/255.0 |
| 85 blue:static_cast<float>(bb)/255.0 | 85 blue:static_cast<float>(bb)/255.0 |
| 86 alpha:1.0]; | 86 alpha:1.0]; |
| 87 } | 87 } |
| 88 | 88 |
| 89 NSColor* HostTextColor(bool inDarkMode) { | 89 NSColor* HostTextColor(bool in_dark_mode) { |
| 90 if (!ui::MaterialDesignController::IsModeMaterial()) { | 90 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 91 return [NSColor blackColor]; | 91 return [NSColor blackColor]; |
| 92 } | 92 } |
| 93 return inDarkMode ? [NSColor whiteColor] : [NSColor blackColor]; | 93 return in_dark_mode ? [NSColor whiteColor] : [NSColor blackColor]; |
| 94 } | 94 } |
| 95 NSColor* SecureSchemeColor(bool inDarkMode) { | 95 NSColor* SecureSchemeColor(bool in_dark_mode) { |
| 96 if (!ui::MaterialDesignController::IsModeMaterial()) { | 96 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 97 return ColorWithRGBBytes(0x07, 0x95, 0x00); | 97 return ColorWithRGBBytes(0x07, 0x95, 0x00); |
| 98 } | 98 } |
| 99 return inDarkMode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : | 99 return in_dark_mode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : |
| 100 skia::SkColorToCalibratedNSColor(gfx::kGoogleGreen700); | 100 skia::SkColorToCalibratedNSColor(gfx::kGoogleGreen700); |
| 101 } | 101 } |
| 102 NSColor* SecurityWarningSchemeColor(bool inDarkMode) { | 102 NSColor* SecurityWarningSchemeColor(bool in_dark_mode) { |
| 103 return inDarkMode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : | 103 return in_dark_mode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : |
| 104 skia::SkColorToCalibratedNSColor(gfx::kGoogleYellow700); | 104 skia::SkColorToCalibratedNSColor(gfx::kGoogleYellow700); |
| 105 } | 105 } |
| 106 NSColor* SecurityErrorSchemeColor(bool inDarkMode) { | 106 NSColor* SecurityErrorSchemeColor(bool in_dark_mode) { |
| 107 if (!ui::MaterialDesignController::IsModeMaterial()) { | 107 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 108 return ColorWithRGBBytes(0xa2, 0x00, 0x00); | 108 return ColorWithRGBBytes(0xa2, 0x00, 0x00); |
| 109 } | 109 } |
| 110 return inDarkMode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : | 110 return in_dark_mode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : |
| 111 skia::SkColorToCalibratedNSColor(gfx::kGoogleRed700); | 111 skia::SkColorToCalibratedNSColor(gfx::kGoogleRed700); |
| 112 } | 112 } |
| 113 | 113 |
| 114 const char kOmniboxViewMacStateKey[] = "OmniboxViewMacState"; | 114 const char kOmniboxViewMacStateKey[] = "OmniboxViewMacState"; |
| 115 | 115 |
| 116 // Store's the model and view state across tab switches. | 116 // Store's the model and view state across tab switches. |
| 117 struct OmniboxViewMacState : public base::SupportsUserData::Data { | 117 struct OmniboxViewMacState : public base::SupportsUserData::Data { |
| 118 OmniboxViewMacState(const OmniboxEditModel::State model_state, | 118 OmniboxViewMacState(const OmniboxEditModel::State model_state, |
| 119 const bool has_focus, | 119 const bool has_focus, |
| 120 const NSRange& selection) | 120 const NSRange& selection) |
| 121 : model_state(model_state), | 121 : model_state(model_state), |
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 538 [attributedString addAttribute:NSParagraphStyleAttributeName | 538 [attributedString addAttribute:NSParagraphStyleAttributeName |
| 539 value:paragraph_style | 539 value:paragraph_style |
| 540 range:NSMakeRange(0, [attributedString length])]; | 540 range:NSMakeRange(0, [attributedString length])]; |
| 541 } | 541 } |
| 542 | 542 |
| 543 void OmniboxViewMac::ApplyTextAttributes( | 543 void OmniboxViewMac::ApplyTextAttributes( |
| 544 const base::string16& display_text, | 544 const base::string16& display_text, |
| 545 NSMutableAttributedString* attributedString) { | 545 NSMutableAttributedString* attributedString) { |
| 546 NSUInteger as_length = [attributedString length]; | 546 NSUInteger as_length = [attributedString length]; |
| 547 NSRange as_entire_string = NSMakeRange(0, as_length); | 547 NSRange as_entire_string = NSMakeRange(0, as_length); |
| 548 bool inDarkMode = [[field_ window] inIncognitoModeWithSystemTheme]; | 548 bool in_dark_mode = [[field_ window] inIncognitoModeWithSystemTheme]; |
| 549 | 549 |
| 550 ApplyTextStyle(attributedString); | 550 ApplyTextStyle(attributedString); |
| 551 | 551 |
| 552 // A kinda hacky way to add breaking at periods. This is what Safari does. | 552 // A kinda hacky way to add breaking at periods. This is what Safari does. |
| 553 // This works for IDNs too, despite the "en_US". | 553 // This works for IDNs too, despite the "en_US". |
| 554 [attributedString addAttribute:@"NSLanguage" | 554 [attributedString addAttribute:@"NSLanguage" |
| 555 value:@"en_US_POSIX" | 555 value:@"en_US_POSIX" |
| 556 range:as_entire_string]; | 556 range:as_entire_string]; |
| 557 | 557 |
| 558 // Under Material Design, force the text to be a single color white editing. | |
| 559 if (ui::MaterialDesignController::IsModeMaterial() && | |
| 560 [field_ currentEditor]) { | |
| 561 if (as_length) { | |
|
tapted
2016/05/02 23:49:03
nit: other places in this method don't check for a
shrike
2016/05/03 00:00:33
The other blocks of code do check for an empty str
| |
| 562 [attributedString addAttribute:NSForegroundColorAttributeName | |
| 563 value:HostTextColor(in_dark_mode) | |
| 564 range:as_entire_string]; | |
| 565 } | |
| 566 return; | |
| 567 } | |
| 568 | |
| 558 url::Component scheme, host; | 569 url::Component scheme, host; |
| 559 AutocompleteInput::ParseForEmphasizeComponents( | 570 AutocompleteInput::ParseForEmphasizeComponents( |
| 560 display_text, ChromeAutocompleteSchemeClassifier(profile_), &scheme, | 571 display_text, ChromeAutocompleteSchemeClassifier(profile_), &scheme, |
| 561 &host); | 572 &host); |
| 562 bool grey_out_url = display_text.substr(scheme.begin, scheme.len) == | 573 bool grey_out_url = display_text.substr(scheme.begin, scheme.len) == |
| 563 base::UTF8ToUTF16(extensions::kExtensionScheme); | 574 base::UTF8ToUTF16(extensions::kExtensionScheme); |
| 564 if (model()->CurrentTextIsURL() && | 575 if (model()->CurrentTextIsURL() && |
| 565 (host.is_nonempty() || grey_out_url)) { | 576 (host.is_nonempty() || grey_out_url)) { |
| 566 [attributedString addAttribute:NSForegroundColorAttributeName | 577 [attributedString addAttribute:NSForegroundColorAttributeName |
| 567 value:BaseTextColor(inDarkMode) | 578 value:BaseTextColor(in_dark_mode) |
| 568 range:as_entire_string]; | 579 range:as_entire_string]; |
| 569 | 580 |
| 570 if (!grey_out_url) { | 581 if (!grey_out_url) { |
| 571 [attributedString addAttribute:NSForegroundColorAttributeName | 582 [attributedString addAttribute:NSForegroundColorAttributeName |
| 572 value:HostTextColor(inDarkMode) | 583 value:HostTextColor(in_dark_mode) |
| 573 range:ComponentToNSRange(host)]; | 584 range:ComponentToNSRange(host)]; |
| 574 } | 585 } |
| 575 } | 586 } |
| 576 | 587 |
| 577 // TODO(shess): GTK has this as a member var, figure out why. | 588 // TODO(shess): GTK has this as a member var, figure out why. |
| 578 // [Could it be to not change if no change? If so, I'm guessing | 589 // [Could it be to not change if no change? If so, I'm guessing |
| 579 // AppKit may already handle that.] | 590 // AppKit may already handle that.] |
| 580 const security_state::SecurityStateModel::SecurityLevel security_level = | 591 const security_state::SecurityStateModel::SecurityLevel security_level = |
| 581 controller()->GetToolbarModel()->GetSecurityLevel(false); | 592 controller()->GetToolbarModel()->GetSecurityLevel(false); |
| 582 | 593 |
| 583 // Emphasize the scheme for security UI display purposes (if necessary). | 594 // Emphasize the scheme for security UI display purposes (if necessary). |
| 584 if (!model()->user_input_in_progress() && model()->CurrentTextIsURL() && | 595 if (!model()->user_input_in_progress() && model()->CurrentTextIsURL() && |
| 585 scheme.is_nonempty() && | 596 scheme.is_nonempty() && |
| 586 (security_level != security_state::SecurityStateModel::NONE)) { | 597 (security_level != security_state::SecurityStateModel::NONE)) { |
| 587 NSColor* color; | 598 NSColor* color; |
| 588 if (security_level == security_state::SecurityStateModel::EV_SECURE || | 599 if (security_level == security_state::SecurityStateModel::EV_SECURE || |
| 589 security_level == security_state::SecurityStateModel::SECURE) { | 600 security_level == security_state::SecurityStateModel::SECURE) { |
| 590 color = SecureSchemeColor(inDarkMode); | 601 color = SecureSchemeColor(in_dark_mode); |
| 591 } else if (security_level == | 602 } else if (security_level == |
| 592 security_state::SecurityStateModel::SECURITY_ERROR) { | 603 security_state::SecurityStateModel::SECURITY_ERROR) { |
| 593 color = SecurityErrorSchemeColor(inDarkMode); | 604 color = SecurityErrorSchemeColor(in_dark_mode); |
| 594 // Add a strikethrough through the scheme. | 605 // Add a strikethrough through the scheme. |
| 595 [attributedString addAttribute:NSStrikethroughStyleAttributeName | 606 [attributedString addAttribute:NSStrikethroughStyleAttributeName |
| 596 value:[NSNumber numberWithInt:NSUnderlineStyleSingle] | 607 value:[NSNumber numberWithInt:NSUnderlineStyleSingle] |
| 597 range:ComponentToNSRange(scheme)]; | 608 range:ComponentToNSRange(scheme)]; |
| 598 } else if (security_level == | 609 } else if (security_level == |
| 599 security_state::SecurityStateModel::SECURITY_WARNING) { | 610 security_state::SecurityStateModel::SECURITY_WARNING) { |
| 600 color = SecurityWarningSchemeColor(inDarkMode); | 611 color = SecurityWarningSchemeColor(in_dark_mode); |
| 601 } else { | 612 } else { |
| 602 NOTREACHED(); | 613 NOTREACHED(); |
| 603 color = BaseTextColor(inDarkMode); | 614 color = BaseTextColor(in_dark_mode); |
| 604 } | 615 } |
| 605 [attributedString addAttribute:NSForegroundColorAttributeName | 616 [attributedString addAttribute:NSForegroundColorAttributeName |
| 606 value:color | 617 value:color |
| 607 range:ComponentToNSRange(scheme)]; | 618 range:ComponentToNSRange(scheme)]; |
| 608 } | 619 } |
| 609 } | 620 } |
| 610 | 621 |
| 611 void OmniboxViewMac::OnTemporaryTextMaybeChanged( | 622 void OmniboxViewMac::OnTemporaryTextMaybeChanged( |
| 612 const base::string16& display_text, | 623 const base::string16& display_text, |
| 613 bool save_original_selection, | 624 bool save_original_selection, |
| (...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1113 | 1124 |
| 1114 NSUInteger OmniboxViewMac::GetTextLength() const { | 1125 NSUInteger OmniboxViewMac::GetTextLength() const { |
| 1115 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : | 1126 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : |
| 1116 [[field_ stringValue] length]; | 1127 [[field_ stringValue] length]; |
| 1117 } | 1128 } |
| 1118 | 1129 |
| 1119 bool OmniboxViewMac::IsCaretAtEnd() const { | 1130 bool OmniboxViewMac::IsCaretAtEnd() const { |
| 1120 const NSRange selection = GetSelectedRange(); | 1131 const NSRange selection = GetSelectedRange(); |
| 1121 return NSMaxRange(selection) == GetTextLength(); | 1132 return NSMaxRange(selection) == GetTextLength(); |
| 1122 } | 1133 } |
| OLD | NEW |