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

Side by Side Diff: chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.mm

Issue 1905163002: [Mac][Material Design] Rework how location bar shadow is drawn. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix crasher with installing a theme. Created 4 years, 7 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" 5 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #import "base/mac/mac_util.h" 8 #import "base/mac/mac_util.h"
9 #import "base/mac/sdk_forward_declarations.h" 9 #import "base/mac/sdk_forward_declarations.h"
10 #include "chrome/browser/themes/theme_service.h" 10 #include "chrome/browser/themes/theme_service.h"
11 #import "chrome/browser/ui/cocoa/browser_window_controller.h" 11 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
12 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" 12 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
13 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h" 13 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_editor.h"
14 #import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h" 14 #import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h"
15 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" 15 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
16 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" 16 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
17 #import "chrome/browser/ui/cocoa/url_drop_target.h" 17 #import "chrome/browser/ui/cocoa/url_drop_target.h"
18 #import "chrome/browser/ui/cocoa/view_id_util.h" 18 #import "chrome/browser/ui/cocoa/view_id_util.h"
19 #import "ui/base/cocoa/nsview_additions.h" 19 #import "ui/base/cocoa/nsview_additions.h"
20 #include "ui/base/material_design/material_design_controller.h" 20 #include "ui/base/material_design/material_design_controller.h"
21 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" 21 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
22 22
23 namespace { 23 namespace {
24 const CGFloat kAnimationDuration = 0.2; 24 const CGFloat kAnimationDuration = 0.2;
25 const CGFloat kShadowInset = 3;
26 const CGFloat kShadowColor = 69 / 255.;
27
25 } 28 }
26 29
30 // A view that draws a 1px shadow line beneath the autocomplete textfield.
31 @interface AutocompleteTextFieldShadowView : NSView {
32 @private
33 AutocompleteTextField* textField_; // Weak. Owns this.
34 }
35 // This is the designated initializer for AutocompleteTextFieldShadowView.
36 - (instancetype)initWithTextField:(AutocompleteTextField*)aTextField;
37 @end
38
39 @interface AutocompleteTextFieldShadowView(Private)
40 // Adjusts the shadow view's position whenever its AutocompleteTextField changes
41 // its frame.
42 - (void)adjustFrame;
43 @end
44
45 @implementation AutocompleteTextFieldShadowView
46
47 - (instancetype)initWithTextField:(AutocompleteTextField*)aTextField {
48 if ((self = [self initWithFrame:NSZeroRect])) {
49 textField_ = aTextField;
50 [[NSNotificationCenter defaultCenter]
51 addObserver:self
52 selector:@selector(adjustFrame)
53 name:NSViewFrameDidChangeNotification
54 object:textField_];
55 }
56 return self;
57 }
58
59 - (void)dealloc {
60 [[NSNotificationCenter defaultCenter] removeObserver:self];
61 [super dealloc];
62 }
63
64 - (void)adjustFrame {
65 if (![self window]) {
66 return;
67 }
68 // Make the shadow view 1pt tall and slightly inset from the edges of the
69 // autocomplete textfield.
70 NSRect frame = [textField_ frame];
71 frame.origin.x += kShadowInset;
72 frame.size.width -= kShadowInset * 2;
73 frame.origin.y -= 1;
74 frame.size.height = 1;
75 [self setFrame:frame];
76 }
77
78 - (void)viewDidMoveToWindow {
79 [self adjustFrame];
80 }
81
82 - (void)drawRect:(NSRect)rect {
83 // Don't draw anything on a Retina display because on Retina there's room
84 // for the shadow just beneath the autocomplete textfield path stroke. Why
85 // even add this view? If the user drags the Incognito window between Retina
86 // and non-Retina screens there would have to be logic to add and remove the
87 // view. It's easier just to always add it for Incognito mode and draw
88 // nothing into it.
89 if (![[self window] inIncognitoMode] || [self cr_lineWidth] < 1) {
90 return;
91 }
92 [[AutocompleteTextField shadowColor] set];
93 NSRectFill(rect);
94 }
95
96 @end
97
27 @implementation AutocompleteTextField 98 @implementation AutocompleteTextField
28 99
29 @synthesize observer = observer_; 100 @synthesize observer = observer_;
30 101
31 + (Class)cellClass { 102 + (Class)cellClass {
32 return [AutocompleteTextFieldCell class]; 103 return [AutocompleteTextFieldCell class];
33 } 104 }
34 105
106 + (NSColor*)shadowColor {
107 return [NSColor colorWithGenericGamma22White:kShadowColor alpha:1];
108 }
109
35 - (void)dealloc { 110 - (void)dealloc {
36 [[NSNotificationCenter defaultCenter] removeObserver:self]; 111 [[NSNotificationCenter defaultCenter] removeObserver:self];
112 [shadowView_ removeFromSuperview];
37 [super dealloc]; 113 [super dealloc];
38 } 114 }
39 115
40 - (void)awakeFromNib { 116 - (void)awakeFromNib {
41 DCHECK([[self cell] isKindOfClass:[AutocompleteTextFieldCell class]]); 117 DCHECK([[self cell] isKindOfClass:[AutocompleteTextFieldCell class]]);
42 [[self cell] setTruncatesLastVisibleLine:YES]; 118 [[self cell] setTruncatesLastVisibleLine:YES];
43 [[self cell] setLineBreakMode:NSLineBreakByTruncatingTail]; 119 [[self cell] setLineBreakMode:NSLineBreakByTruncatingTail];
44 currentToolTips_.reset([[NSMutableArray alloc] init]); 120 currentToolTips_.reset([[NSMutableArray alloc] init]);
45 resizeAnimation_.reset([[NSViewAnimation alloc] init]); 121 resizeAnimation_.reset([[NSViewAnimation alloc] init]);
46 [resizeAnimation_ setDuration:kAnimationDuration]; 122 [resizeAnimation_ setDuration:kAnimationDuration];
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 455
380 - (void)updateColorsToMatchTheme { 456 - (void)updateColorsToMatchTheme {
381 if (![[self window] inIncognitoMode]) { 457 if (![[self window] inIncognitoMode]) {
382 return; 458 return;
383 } 459 }
384 460
385 // Invert the textfield's colors when Material Design and Incognito and not 461 // Invert the textfield's colors when Material Design and Incognito and not
386 // a custom theme. 462 // a custom theme.
387 bool inDarkMode = [[self window] inIncognitoModeWithSystemTheme]; 463 bool inDarkMode = [[self window] inIncognitoModeWithSystemTheme];
388 [self setBackgroundColor: 464 [self setBackgroundColor:
389 inDarkMode ? [NSColor colorWithCalibratedWhite:115 / 255. alpha:1] 465 inDarkMode ? [NSColor colorWithGenericGamma22White:115 / 255. alpha:1]
390 : [NSColor whiteColor]]; 466 : [NSColor whiteColor]];
391 [self setTextColor:OmniboxViewMac::BaseTextColor(inDarkMode)]; 467 [self setTextColor:OmniboxViewMac::BaseTextColor(inDarkMode)];
392 } 468 }
393 469
394 - (void)viewDidMoveToWindow { 470 - (void)viewDidMoveToWindow {
395 if (![self window]) { 471 if (![self window]) {
472 [shadowView_ removeFromSuperview];
473 shadowView_.reset();
396 return; 474 return;
397 } 475 }
398 476
399 // Allow the ToolbarController to take action upon the 477 // Allow the ToolbarController to take action upon the
400 // AutocompleteTextField being added to the window. 478 // AutocompleteTextField being added to the window.
401 if (ui::MaterialDesignController::IsModeMaterial()) { 479 if (ui::MaterialDesignController::IsModeMaterial()) {
402 BrowserWindowController* browserWindowController = 480 BrowserWindowController* browserWindowController =
403 [BrowserWindowController browserWindowControllerForView:self]; 481 [BrowserWindowController browserWindowControllerForView:self];
404 [[browserWindowController toolbarController] locationBarWasAddedToWindow]; 482 [[browserWindowController toolbarController] locationBarWasAddedToWindow];
405 483
484 // Add a 1px shadow below the autocomplete textfield.
485 shadowView_.reset(
486 [[AutocompleteTextFieldShadowView alloc] initWithTextField:self]);
487 [[self superview] addSubview:shadowView_];
488
406 [self updateColorsToMatchTheme]; 489 [self updateColorsToMatchTheme];
407 } 490 }
408 491
409 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 492 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
410 [nc addObserver:self 493 [nc addObserver:self
411 selector:@selector(windowDidResignKey:) 494 selector:@selector(windowDidResignKey:)
412 name:NSWindowDidResignKeyNotification 495 name:NSWindowDidResignKeyNotification
413 object:[self window]]; 496 object:[self window]];
414 [nc addObserver:self 497 [nc addObserver:self
415 selector:@selector(windowDidResize:) 498 selector:@selector(windowDidResize:)
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 NSMinY(frame), 665 NSMinY(frame),
583 suggestWidth, 666 suggestWidth,
584 NSHeight(frame)); 667 NSHeight(frame));
585 668
586 gfx::ScopedNSGraphicsContextSaveGState saveGState; 669 gfx::ScopedNSGraphicsContextSaveGState saveGState;
587 NSRectClip(suggestRect); 670 NSRectClip(suggestRect);
588 [cell drawInteriorWithFrame:frame inView:controlView]; 671 [cell drawInteriorWithFrame:frame inView:controlView];
589 } 672 }
590 673
591 } // namespace autocomplete_text_field 674 } // namespace autocomplete_text_field
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698