| 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 14 matching lines...) Expand all Loading... |
| 25 #include "chrome/grit/generated_resources.h" | 25 #include "chrome/grit/generated_resources.h" |
| 26 #include "components/omnibox/browser/autocomplete_input.h" | 26 #include "components/omnibox/browser/autocomplete_input.h" |
| 27 #include "components/omnibox/browser/autocomplete_match.h" | 27 #include "components/omnibox/browser/autocomplete_match.h" |
| 28 #include "components/omnibox/browser/omnibox_edit_controller.h" | 28 #include "components/omnibox/browser/omnibox_edit_controller.h" |
| 29 #include "components/omnibox/browser/omnibox_field_trial.h" | 29 #include "components/omnibox/browser/omnibox_field_trial.h" |
| 30 #include "components/omnibox/browser/omnibox_popup_model.h" | 30 #include "components/omnibox/browser/omnibox_popup_model.h" |
| 31 #include "components/security_state/security_state_model.h" | 31 #include "components/security_state/security_state_model.h" |
| 32 #include "components/toolbar/toolbar_model.h" | 32 #include "components/toolbar/toolbar_model.h" |
| 33 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
| 34 #include "extensions/common/constants.h" | 34 #include "extensions/common/constants.h" |
| 35 #include "grit/components_scaled_resources.h" |
| 36 #include "grit/theme_resources.h" |
| 35 #import "skia/ext/skia_utils_mac.h" | 37 #import "skia/ext/skia_utils_mac.h" |
| 36 #import "third_party/mozilla/NSPasteboard+Utils.h" | 38 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 37 #include "ui/base/clipboard/clipboard.h" | 39 #include "ui/base/clipboard/clipboard.h" |
| 38 #include "ui/base/clipboard/clipboard_util_mac.h" | 40 #include "ui/base/clipboard/clipboard_util_mac.h" |
| 39 #import "ui/base/cocoa/cocoa_base_utils.h" | 41 #import "ui/base/cocoa/cocoa_base_utils.h" |
| 40 #include "ui/base/material_design/material_design_controller.h" | 42 #include "ui/base/material_design/material_design_controller.h" |
| 41 #include "ui/base/resource/resource_bundle.h" | 43 #include "ui/base/resource/resource_bundle.h" |
| 42 #include "ui/gfx/color_palette.h" | 44 #include "ui/gfx/color_palette.h" |
| 43 #include "ui/gfx/font.h" | 45 #include "ui/gfx/font.h" |
| 44 #include "ui/gfx/font_list.h" | 46 #include "ui/gfx/font_list.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) { | 81 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) { |
| 80 DCHECK_LE(rr, 255); | 82 DCHECK_LE(rr, 255); |
| 81 DCHECK_LE(bb, 255); | 83 DCHECK_LE(bb, 255); |
| 82 DCHECK_LE(gg, 255); | 84 DCHECK_LE(gg, 255); |
| 83 return [NSColor colorWithCalibratedRed:static_cast<float>(rr)/255.0 | 85 return [NSColor colorWithCalibratedRed:static_cast<float>(rr)/255.0 |
| 84 green:static_cast<float>(gg)/255.0 | 86 green:static_cast<float>(gg)/255.0 |
| 85 blue:static_cast<float>(bb)/255.0 | 87 blue:static_cast<float>(bb)/255.0 |
| 86 alpha:1.0]; | 88 alpha:1.0]; |
| 87 } | 89 } |
| 88 | 90 |
| 89 NSColor* HostTextColor(bool inDarkMode) { | 91 NSColor* HostTextColor(bool in_dark_mode) { |
| 90 if (!ui::MaterialDesignController::IsModeMaterial()) { | 92 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 91 return [NSColor blackColor]; | 93 return [NSColor blackColor]; |
| 92 } | 94 } |
| 93 return inDarkMode ? [NSColor whiteColor] : [NSColor blackColor]; | 95 return in_dark_mode ? [NSColor whiteColor] : [NSColor blackColor]; |
| 94 } | 96 } |
| 95 NSColor* SecureSchemeColor(bool inDarkMode) { | 97 NSColor* SecureSchemeColor(bool in_dark_mode) { |
| 96 if (!ui::MaterialDesignController::IsModeMaterial()) { | 98 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 97 return ColorWithRGBBytes(0x07, 0x95, 0x00); | 99 return ColorWithRGBBytes(0x07, 0x95, 0x00); |
| 98 } | 100 } |
| 99 return inDarkMode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : | 101 return in_dark_mode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : |
| 100 skia::SkColorToCalibratedNSColor(gfx::kGoogleGreen700); | 102 skia::SkColorToCalibratedNSColor(gfx::kGoogleGreen700); |
| 101 } | 103 } |
| 102 NSColor* SecurityWarningSchemeColor(bool inDarkMode) { | 104 NSColor* SecurityWarningSchemeColor(bool in_dark_mode) { |
| 103 return inDarkMode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : | 105 return in_dark_mode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : |
| 104 skia::SkColorToCalibratedNSColor(gfx::kGoogleYellow700); | 106 skia::SkColorToCalibratedNSColor(gfx::kGoogleYellow700); |
| 105 } | 107 } |
| 106 NSColor* SecurityErrorSchemeColor(bool inDarkMode) { | 108 NSColor* SecurityErrorSchemeColor(bool in_dark_mode) { |
| 107 if (!ui::MaterialDesignController::IsModeMaterial()) { | 109 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 108 return ColorWithRGBBytes(0xa2, 0x00, 0x00); | 110 return ColorWithRGBBytes(0xa2, 0x00, 0x00); |
| 109 } | 111 } |
| 110 return inDarkMode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : | 112 return in_dark_mode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] : |
| 111 skia::SkColorToCalibratedNSColor(gfx::kGoogleRed700); | 113 skia::SkColorToCalibratedNSColor(gfx::kGoogleRed700); |
| 112 } | 114 } |
| 113 | 115 |
| 114 const char kOmniboxViewMacStateKey[] = "OmniboxViewMacState"; | 116 const char kOmniboxViewMacStateKey[] = "OmniboxViewMacState"; |
| 115 | 117 |
| 116 // Store's the model and view state across tab switches. | 118 // Store's the model and view state across tab switches. |
| 117 struct OmniboxViewMacState : public base::SupportsUserData::Data { | 119 struct OmniboxViewMacState : public base::SupportsUserData::Data { |
| 118 OmniboxViewMacState(const OmniboxEditModel::State model_state, | 120 OmniboxViewMacState(const OmniboxEditModel::State model_state, |
| 119 const bool has_focus, | 121 const bool has_focus, |
| 120 const NSRange& selection) | 122 const NSRange& selection) |
| 121 : model_state(model_state), | 123 : model_state(model_state), |
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 [attributedString addAttribute:NSParagraphStyleAttributeName | 540 [attributedString addAttribute:NSParagraphStyleAttributeName |
| 539 value:paragraph_style | 541 value:paragraph_style |
| 540 range:NSMakeRange(0, [attributedString length])]; | 542 range:NSMakeRange(0, [attributedString length])]; |
| 541 } | 543 } |
| 542 | 544 |
| 543 void OmniboxViewMac::ApplyTextAttributes( | 545 void OmniboxViewMac::ApplyTextAttributes( |
| 544 const base::string16& display_text, | 546 const base::string16& display_text, |
| 545 NSMutableAttributedString* attributedString) { | 547 NSMutableAttributedString* attributedString) { |
| 546 NSUInteger as_length = [attributedString length]; | 548 NSUInteger as_length = [attributedString length]; |
| 547 NSRange as_entire_string = NSMakeRange(0, as_length); | 549 NSRange as_entire_string = NSMakeRange(0, as_length); |
| 548 bool inDarkMode = [[field_ window] inIncognitoModeWithSystemTheme]; | 550 bool in_dark_mode = [[field_ window] inIncognitoModeWithSystemTheme]; |
| 549 | 551 |
| 550 ApplyTextStyle(attributedString); | 552 ApplyTextStyle(attributedString); |
| 551 | 553 |
| 552 // A kinda hacky way to add breaking at periods. This is what Safari does. | 554 // A kinda hacky way to add breaking at periods. This is what Safari does. |
| 553 // This works for IDNs too, despite the "en_US". | 555 // This works for IDNs too, despite the "en_US". |
| 554 [attributedString addAttribute:@"NSLanguage" | 556 [attributedString addAttribute:@"NSLanguage" |
| 555 value:@"en_US_POSIX" | 557 value:@"en_US_POSIX" |
| 556 range:as_entire_string]; | 558 range:as_entire_string]; |
| 557 | 559 |
| 558 url::Component scheme, host; | 560 url::Component scheme, host; |
| 559 AutocompleteInput::ParseForEmphasizeComponents( | 561 AutocompleteInput::ParseForEmphasizeComponents( |
| 560 display_text, ChromeAutocompleteSchemeClassifier(profile_), &scheme, | 562 display_text, ChromeAutocompleteSchemeClassifier(profile_), &scheme, |
| 561 &host); | 563 &host); |
| 562 bool grey_out_url = display_text.substr(scheme.begin, scheme.len) == | 564 bool grey_out_url = display_text.substr(scheme.begin, scheme.len) == |
| 563 base::UTF8ToUTF16(extensions::kExtensionScheme); | 565 base::UTF8ToUTF16(extensions::kExtensionScheme); |
| 564 if (model()->CurrentTextIsURL() && | 566 if (model()->CurrentTextIsURL() && |
| 565 (host.is_nonempty() || grey_out_url)) { | 567 (host.is_nonempty() || grey_out_url)) { |
| 566 [attributedString addAttribute:NSForegroundColorAttributeName | 568 [attributedString addAttribute:NSForegroundColorAttributeName |
| 567 value:BaseTextColor(inDarkMode) | 569 value:BaseTextColor(in_dark_mode) |
| 568 range:as_entire_string]; | 570 range:as_entire_string]; |
| 569 | 571 |
| 570 if (!grey_out_url) { | 572 if (!grey_out_url) { |
| 571 [attributedString addAttribute:NSForegroundColorAttributeName | 573 [attributedString addAttribute:NSForegroundColorAttributeName |
| 572 value:HostTextColor(inDarkMode) | 574 value:HostTextColor(in_dark_mode) |
| 573 range:ComponentToNSRange(host)]; | 575 range:ComponentToNSRange(host)]; |
| 574 } | 576 } |
| 575 } | 577 } |
| 576 | 578 |
| 577 // TODO(shess): GTK has this as a member var, figure out why. | 579 // 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 | 580 // [Could it be to not change if no change? If so, I'm guessing |
| 579 // AppKit may already handle that.] | 581 // AppKit may already handle that.] |
| 580 const security_state::SecurityStateModel::SecurityLevel security_level = | 582 const security_state::SecurityStateModel::SecurityLevel security_level = |
| 581 controller()->GetToolbarModel()->GetSecurityLevel(false); | 583 controller()->GetToolbarModel()->GetSecurityLevel(false); |
| 582 | 584 |
| 583 // Emphasize the scheme for security UI display purposes (if necessary). | 585 // Emphasize the scheme for security UI display purposes (if necessary). |
| 584 if (!model()->user_input_in_progress() && model()->CurrentTextIsURL() && | 586 if (!model()->user_input_in_progress() && model()->CurrentTextIsURL() && |
| 585 scheme.is_nonempty() && | 587 scheme.is_nonempty() && |
| 586 (security_level != security_state::SecurityStateModel::NONE)) { | 588 (security_level != security_state::SecurityStateModel::NONE)) { |
| 587 NSColor* color; | 589 NSColor* color; |
| 588 if (security_level == security_state::SecurityStateModel::EV_SECURE || | 590 if (security_level == security_state::SecurityStateModel::EV_SECURE || |
| 589 security_level == security_state::SecurityStateModel::SECURE) { | 591 security_level == security_state::SecurityStateModel::SECURE) { |
| 590 color = SecureSchemeColor(inDarkMode); | 592 color = SecureSchemeColor(in_dark_mode); |
| 591 } else if (security_level == | 593 } else if (security_level == |
| 592 security_state::SecurityStateModel::SECURITY_ERROR) { | 594 security_state::SecurityStateModel::SECURITY_ERROR) { |
| 593 color = SecurityErrorSchemeColor(inDarkMode); | 595 color = SecurityErrorSchemeColor(in_dark_mode); |
| 594 // Add a strikethrough through the scheme. | 596 // Add a strikethrough through the scheme. |
| 595 [attributedString addAttribute:NSStrikethroughStyleAttributeName | 597 [attributedString addAttribute:NSStrikethroughStyleAttributeName |
| 596 value:[NSNumber numberWithInt:NSUnderlineStyleSingle] | 598 value:[NSNumber numberWithInt:NSUnderlineStyleSingle] |
| 597 range:ComponentToNSRange(scheme)]; | 599 range:ComponentToNSRange(scheme)]; |
| 598 } else if (security_level == | 600 } else if (security_level == |
| 599 security_state::SecurityStateModel::SECURITY_WARNING) { | 601 security_state::SecurityStateModel::SECURITY_WARNING) { |
| 600 color = SecurityWarningSchemeColor(inDarkMode); | 602 color = SecurityWarningSchemeColor(in_dark_mode); |
| 601 } else { | 603 } else { |
| 602 NOTREACHED(); | 604 NOTREACHED(); |
| 603 color = BaseTextColor(inDarkMode); | 605 color = BaseTextColor(in_dark_mode); |
| 604 } | 606 } |
| 605 [attributedString addAttribute:NSForegroundColorAttributeName | 607 [attributedString addAttribute:NSForegroundColorAttributeName |
| 606 value:color | 608 value:color |
| 607 range:ComponentToNSRange(scheme)]; | 609 range:ComponentToNSRange(scheme)]; |
| 608 } | 610 } |
| 609 } | 611 } |
| 610 | 612 |
| 611 void OmniboxViewMac::OnTemporaryTextMaybeChanged( | 613 void OmniboxViewMac::OnTemporaryTextMaybeChanged( |
| 612 const base::string16& display_text, | 614 const base::string16& display_text, |
| 613 bool save_original_selection, | 615 bool save_original_selection, |
| (...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1094 NSFont* OmniboxViewMac::GetLargeFont(int style) { | 1096 NSFont* OmniboxViewMac::GetLargeFont(int style) { |
| 1095 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 1097 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 1096 return rb.GetFontList(ui::ResourceBundle::LargeFont) | 1098 return rb.GetFontList(ui::ResourceBundle::LargeFont) |
| 1097 .Derive(1, style) | 1099 .Derive(1, style) |
| 1098 .GetPrimaryFont() | 1100 .GetPrimaryFont() |
| 1099 .GetNativeFont(); | 1101 .GetNativeFont(); |
| 1100 } | 1102 } |
| 1101 | 1103 |
| 1102 NSFont* OmniboxViewMac::GetSmallFont(int style) { | 1104 NSFont* OmniboxViewMac::GetSmallFont(int style) { |
| 1103 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 1105 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 1104 return rb.GetFontList(ui::ResourceBundle::SmallFont) | 1106 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 1107 return rb.GetFontList(ui::ResourceBundle::SmallFont) |
| 1108 .Derive(1, style) |
| 1109 .GetPrimaryFont() |
| 1110 .GetNativeFont(); |
| 1111 } |
| 1112 return rb.GetFontListWithDelta(-2, gfx::Font::NORMAL) |
| 1105 .Derive(1, style) | 1113 .Derive(1, style) |
| 1106 .GetPrimaryFont() | 1114 .GetPrimaryFont() |
| 1107 .GetNativeFont(); | 1115 .GetNativeFont(); |
| 1108 } | 1116 } |
| 1109 | 1117 |
| 1110 int OmniboxViewMac::GetOmniboxTextLength() const { | 1118 int OmniboxViewMac::GetOmniboxTextLength() const { |
| 1111 return static_cast<int>(GetTextLength()); | 1119 return static_cast<int>(GetTextLength()); |
| 1112 } | 1120 } |
| 1113 | 1121 |
| 1114 NSUInteger OmniboxViewMac::GetTextLength() const { | 1122 NSUInteger OmniboxViewMac::GetTextLength() const { |
| 1115 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : | 1123 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : |
| 1116 [[field_ stringValue] length]; | 1124 [[field_ stringValue] length]; |
| 1117 } | 1125 } |
| 1118 | 1126 |
| 1119 bool OmniboxViewMac::IsCaretAtEnd() const { | 1127 bool OmniboxViewMac::IsCaretAtEnd() const { |
| 1120 const NSRange selection = GetSelectedRange(); | 1128 const NSRange selection = GetSelectedRange(); |
| 1121 return NSMaxRange(selection) == GetTextLength(); | 1129 return NSMaxRange(selection) == GetTextLength(); |
| 1122 } | 1130 } |
| OLD | NEW |