Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm

Issue 1680773006: Implement Material Design for Mac toolbar. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@md_master
Patch Set: Ready for review. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "base/strings/sys_string_conversions.h" 12 #include "base/strings/sys_string_conversions.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" 14 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
15 #include "chrome/browser/browser_process.h" 15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/search/search.h" 16 #include "chrome/browser/search/search.h"
17 #include "chrome/browser/themes/theme_service.h"
17 #include "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" 18 #include "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
18 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h" 19 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h"
20 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
19 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" 21 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h"
20 #include "chrome/browser/ui/omnibox/chrome_omnibox_client.h" 22 #include "chrome/browser/ui/omnibox/chrome_omnibox_client.h"
21 #include "chrome/browser/ui/omnibox/clipboard_utils.h" 23 #include "chrome/browser/ui/omnibox/clipboard_utils.h"
22 #include "chrome/grit/generated_resources.h" 24 #include "chrome/grit/generated_resources.h"
23 #include "components/omnibox/browser/autocomplete_input.h" 25 #include "components/omnibox/browser/autocomplete_input.h"
24 #include "components/omnibox/browser/autocomplete_match.h" 26 #include "components/omnibox/browser/autocomplete_match.h"
25 #include "components/omnibox/browser/omnibox_edit_controller.h" 27 #include "components/omnibox/browser/omnibox_edit_controller.h"
26 #include "components/omnibox/browser/omnibox_field_trial.h" 28 #include "components/omnibox/browser/omnibox_field_trial.h"
27 #include "components/omnibox/browser/omnibox_popup_model.h" 29 #include "components/omnibox/browser/omnibox_popup_model.h"
28 #include "components/security_state/security_state_model.h" 30 #include "components/security_state/security_state_model.h"
29 #include "components/toolbar/toolbar_model.h" 31 #include "components/toolbar/toolbar_model.h"
30 #include "content/public/browser/web_contents.h" 32 #include "content/public/browser/web_contents.h"
31 #include "extensions/common/constants.h" 33 #include "extensions/common/constants.h"
32 #import "third_party/mozilla/NSPasteboard+Utils.h" 34 #import "third_party/mozilla/NSPasteboard+Utils.h"
33 #include "ui/base/clipboard/clipboard.h" 35 #include "ui/base/clipboard/clipboard.h"
34 #import "ui/base/cocoa/cocoa_base_utils.h" 36 #import "ui/base/cocoa/cocoa_base_utils.h"
37 #include "ui/base/material_design/material_design_controller.h"
35 #include "ui/base/resource/resource_bundle.h" 38 #include "ui/base/resource/resource_bundle.h"
36 #include "ui/gfx/font.h" 39 #include "ui/gfx/font.h"
37 #include "ui/gfx/font_list.h" 40 #include "ui/gfx/font_list.h"
38 #include "ui/gfx/geometry/rect.h" 41 #include "ui/gfx/geometry/rect.h"
39 42
40 using content::WebContents; 43 using content::WebContents;
41 44
42 // Focus-handling between |field_| and model() is a bit subtle. 45 // Focus-handling between |field_| and model() is a bit subtle.
43 // Other platforms detect change of focus, which is inconvenient 46 // Other platforms detect change of focus, which is inconvenient
44 // without subclassing NSTextField (even with a subclass, the use of a 47 // without subclassing NSTextField (even with a subclass, the use of a
(...skipping 27 matching lines...) Expand all
72 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) { 75 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) {
73 DCHECK_LE(rr, 255); 76 DCHECK_LE(rr, 255);
74 DCHECK_LE(bb, 255); 77 DCHECK_LE(bb, 255);
75 DCHECK_LE(gg, 255); 78 DCHECK_LE(gg, 255);
76 return [NSColor colorWithCalibratedRed:static_cast<float>(rr)/255.0 79 return [NSColor colorWithCalibratedRed:static_cast<float>(rr)/255.0
77 green:static_cast<float>(gg)/255.0 80 green:static_cast<float>(gg)/255.0
78 blue:static_cast<float>(bb)/255.0 81 blue:static_cast<float>(bb)/255.0
79 alpha:1.0]; 82 alpha:1.0];
80 } 83 }
81 84
82 NSColor* HostTextColor() { 85 NSColor* HostTextColor(bool inDarkMode) {
83 return [NSColor blackColor]; 86 if (!ui::MaterialDesignController::IsModeMaterial()) {
87 return [NSColor blackColor];
88 }
89 return inDarkMode ? [NSColor whiteColor] : [NSColor blackColor];
84 } 90 }
85 NSColor* BaseTextColor() { 91 NSColor* BaseTextColor(bool inDarkMode) {
86 return [NSColor darkGrayColor]; 92 if (!ui::MaterialDesignController::IsModeMaterial()) {
93 return [NSColor darkGrayColor];
94 }
95 return inDarkMode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] :
96 [NSColor colorWithCalibratedWhite:0 alpha:0.5];
87 } 97 }
88 NSColor* SecureSchemeColor() { 98 NSColor* SecureSchemeColor(bool inDarkMode) {
89 return ColorWithRGBBytes(0x07, 0x95, 0x00); 99 if (!ui::MaterialDesignController::IsModeMaterial()) {
100 return ColorWithRGBBytes(0x07, 0x95, 0x00);
101 }
102 return inDarkMode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] :
103 ColorWithRGBBytes(0x0B, 0x80, 0x43);
90 } 104 }
91 NSColor* SecurityErrorSchemeColor() { 105 NSColor* SecurityWarningSchemeColor(bool inDarkMode) {
92 return ColorWithRGBBytes(0xa2, 0x00, 0x00); 106 return inDarkMode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] :
107 ColorWithRGBBytes(0xF0, 0x93, 0x00);
108 }
109 NSColor* SecurityErrorSchemeColor(bool inDarkMode) {
110 if (!ui::MaterialDesignController::IsModeMaterial()) {
111 return ColorWithRGBBytes(0xa2, 0x00, 0x00);
112 }
113 return inDarkMode ? [NSColor colorWithCalibratedWhite:1 alpha:0.5] :
114 ColorWithRGBBytes(0xC5, 0x39, 0x29);
93 } 115 }
94 116
95 const char kOmniboxViewMacStateKey[] = "OmniboxViewMacState"; 117 const char kOmniboxViewMacStateKey[] = "OmniboxViewMacState";
96 118
97 // Store's the model and view state across tab switches. 119 // Store's the model and view state across tab switches.
98 struct OmniboxViewMacState : public base::SupportsUserData::Data { 120 struct OmniboxViewMacState : public base::SupportsUserData::Data {
99 OmniboxViewMacState(const OmniboxEditModel::State model_state, 121 OmniboxViewMacState(const OmniboxEditModel::State model_state,
100 const bool has_focus, 122 const bool has_focus,
101 const NSRange& selection) 123 const NSRange& selection)
102 : model_state(model_state), 124 : model_state(model_state),
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 [attributedString addAttribute:NSParagraphStyleAttributeName 527 [attributedString addAttribute:NSParagraphStyleAttributeName
506 value:paragraph_style 528 value:paragraph_style
507 range:NSMakeRange(0, [attributedString length])]; 529 range:NSMakeRange(0, [attributedString length])];
508 } 530 }
509 531
510 void OmniboxViewMac::ApplyTextAttributes( 532 void OmniboxViewMac::ApplyTextAttributes(
511 const base::string16& display_text, 533 const base::string16& display_text,
512 NSMutableAttributedString* attributedString) { 534 NSMutableAttributedString* attributedString) {
513 NSUInteger as_length = [attributedString length]; 535 NSUInteger as_length = [attributedString length];
514 NSRange as_entire_string = NSMakeRange(0, as_length); 536 NSRange as_entire_string = NSMakeRange(0, as_length);
537 bool inDarkMode = [[field_ window] inIncognitoModeWithSystemTheme];
515 538
516 ApplyTextStyle(attributedString); 539 ApplyTextStyle(attributedString);
517 540
518 // A kinda hacky way to add breaking at periods. This is what Safari does. 541 // A kinda hacky way to add breaking at periods. This is what Safari does.
519 // This works for IDNs too, despite the "en_US". 542 // This works for IDNs too, despite the "en_US".
520 [attributedString addAttribute:@"NSLanguage" 543 [attributedString addAttribute:@"NSLanguage"
521 value:@"en_US_POSIX" 544 value:@"en_US_POSIX"
522 range:as_entire_string]; 545 range:as_entire_string];
523 546
524 url::Component scheme, host; 547 url::Component scheme, host;
525 AutocompleteInput::ParseForEmphasizeComponents( 548 AutocompleteInput::ParseForEmphasizeComponents(
526 display_text, ChromeAutocompleteSchemeClassifier(profile_), &scheme, 549 display_text, ChromeAutocompleteSchemeClassifier(profile_), &scheme,
527 &host); 550 &host);
528 bool grey_out_url = display_text.substr(scheme.begin, scheme.len) == 551 bool grey_out_url = display_text.substr(scheme.begin, scheme.len) ==
529 base::UTF8ToUTF16(extensions::kExtensionScheme); 552 base::UTF8ToUTF16(extensions::kExtensionScheme);
530 if (model()->CurrentTextIsURL() && 553 if (model()->CurrentTextIsURL() &&
531 (host.is_nonempty() || grey_out_url)) { 554 (host.is_nonempty() || grey_out_url)) {
532 [attributedString addAttribute:NSForegroundColorAttributeName 555 [attributedString addAttribute:NSForegroundColorAttributeName
533 value:BaseTextColor() 556 value:BaseTextColor(inDarkMode)
534 range:as_entire_string]; 557 range:as_entire_string];
535 558
536 if (!grey_out_url) { 559 if (!grey_out_url) {
537 [attributedString addAttribute:NSForegroundColorAttributeName 560 [attributedString addAttribute:NSForegroundColorAttributeName
538 value:HostTextColor() 561 value:HostTextColor(inDarkMode)
539 range:ComponentToNSRange(host)]; 562 range:ComponentToNSRange(host)];
540 } 563 }
541 } 564 }
542 565
543 // TODO(shess): GTK has this as a member var, figure out why. 566 // TODO(shess): GTK has this as a member var, figure out why.
544 // [Could it be to not change if no change? If so, I'm guessing 567 // [Could it be to not change if no change? If so, I'm guessing
545 // AppKit may already handle that.] 568 // AppKit may already handle that.]
546 const security_state::SecurityStateModel::SecurityLevel security_level = 569 const security_state::SecurityStateModel::SecurityLevel security_level =
547 controller()->GetToolbarModel()->GetSecurityLevel(false); 570 controller()->GetToolbarModel()->GetSecurityLevel(false);
548 571
549 // Emphasize the scheme for security UI display purposes (if necessary). 572 // Emphasize the scheme for security UI display purposes (if necessary).
550 if (!model()->user_input_in_progress() && model()->CurrentTextIsURL() && 573 if (!model()->user_input_in_progress() && model()->CurrentTextIsURL() &&
551 scheme.is_nonempty() && 574 scheme.is_nonempty() &&
552 (security_level != security_state::SecurityStateModel::NONE)) { 575 (security_level != security_state::SecurityStateModel::NONE)) {
553 NSColor* color; 576 NSColor* color;
554 if (security_level == security_state::SecurityStateModel::EV_SECURE || 577 if (security_level == security_state::SecurityStateModel::EV_SECURE ||
555 security_level == security_state::SecurityStateModel::SECURE) { 578 security_level == security_state::SecurityStateModel::SECURE) {
556 color = SecureSchemeColor(); 579 color = SecureSchemeColor(inDarkMode);
557 } else if (security_level == 580 } else if (security_level ==
558 security_state::SecurityStateModel::SECURITY_ERROR) { 581 security_state::SecurityStateModel::SECURITY_ERROR) {
559 color = SecurityErrorSchemeColor(); 582 color = SecurityErrorSchemeColor(inDarkMode);
560 // Add a strikethrough through the scheme. 583 // Add a strikethrough through the scheme.
561 [attributedString addAttribute:NSStrikethroughStyleAttributeName 584 [attributedString addAttribute:NSStrikethroughStyleAttributeName
562 value:[NSNumber numberWithInt:NSUnderlineStyleSingle] 585 value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
563 range:ComponentToNSRange(scheme)]; 586 range:ComponentToNSRange(scheme)];
564 } else if (security_level == 587 } else if (security_level ==
565 security_state::SecurityStateModel::SECURITY_WARNING) { 588 security_state::SecurityStateModel::SECURITY_WARNING) {
566 color = BaseTextColor(); 589 color = SecurityWarningSchemeColor(inDarkMode);
567 } else { 590 } else {
568 NOTREACHED(); 591 NOTREACHED();
569 color = BaseTextColor(); 592 color = BaseTextColor(inDarkMode);
570 } 593 }
571 [attributedString addAttribute:NSForegroundColorAttributeName 594 [attributedString addAttribute:NSForegroundColorAttributeName
572 value:color 595 value:color
573 range:ComponentToNSRange(scheme)]; 596 range:ComponentToNSRange(scheme)];
597 } else if (as_length) {
598 [attributedString addAttribute:NSForegroundColorAttributeName
599 value:BaseTextColor(inDarkMode)
600 range:as_entire_string];
574 } 601 }
575 } 602 }
576 603
577 void OmniboxViewMac::OnTemporaryTextMaybeChanged( 604 void OmniboxViewMac::OnTemporaryTextMaybeChanged(
578 const base::string16& display_text, 605 const base::string16& display_text,
579 bool save_original_selection, 606 bool save_original_selection,
580 bool notify_text_changed) { 607 bool notify_text_changed) {
581 if (save_original_selection) 608 if (save_original_selection)
582 saved_temporary_selection_ = GetSelectedRange(); 609 saved_temporary_selection_ = GetSelectedRange();
583 610
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 1101
1075 NSUInteger OmniboxViewMac::GetTextLength() const { 1102 NSUInteger OmniboxViewMac::GetTextLength() const {
1076 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] : 1103 return [field_ currentEditor] ? [[[field_ currentEditor] string] length] :
1077 [[field_ stringValue] length]; 1104 [[field_ stringValue] length];
1078 } 1105 }
1079 1106
1080 bool OmniboxViewMac::IsCaretAtEnd() const { 1107 bool OmniboxViewMac::IsCaretAtEnd() const {
1081 const NSRange selection = GetSelectedRange(); 1108 const NSRange selection = GetSelectedRange();
1082 return NSMaxRange(selection) == GetTextLength(); 1109 return NSMaxRange(selection) == GetTextLength();
1083 } 1110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698