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

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: Address comments. 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 AutocompleteTextField* textField_; // weak
tapted 2016/04/27 00:55:32 nit: @private before this? Also, I'd go for "// We
shrike 2016/04/27 17:00:21 Done.
33 }
34 @end
35
36 @implementation AutocompleteTextFieldShadowView
37
38 - (instancetype)initWithTextField:(AutocompleteTextField*)aTextField {
tapted 2016/04/27 00:55:32 this should have a prototype in @interface to say
shrike 2016/04/27 17:00:22 Done.
39 self = [self initWithFrame:NSZeroRect];
tapted 2016/04/27 00:55:32 if ((self = ..))
shrike 2016/04/27 17:00:22 Done. I was never in the habit of doing this check
tapted 2016/04/28 00:41:53 Interesting question -I haven't thought about it m
40 textField_ = aTextField;
41 [[NSNotificationCenter defaultCenter]
42 addObserver:self
43 selector:@selector(adjustFrame)
44 name:NSViewFrameDidChangeNotification
45 object:textField_];
46 return self;
47 }
48
49 - (void)dealloc {
50 [[NSNotificationCenter defaultCenter] removeObserver:self];
51 [super dealloc];
52 }
53
54 - (void)adjustFrame {
55 // Make the shadow view 1pt tall and slightly inset from the edges of the
56 // autocomplete textfield.
57 NSRect frame = [textField_ frame];
58 frame.origin.x += kShadowInset;
59 frame.size.width -= kShadowInset * 2;
60 frame.origin.y -= 1;
61 frame.size.height = 1;
62 [self setFrame:frame];
63 }
64
65 - (void)viewDidMoveToWindow {
66 [self adjustFrame];
tapted 2016/04/27 00:55:32 if ([self window]) ? perhaps in adjustFrame inste
shrike 2016/04/27 17:00:21 Done. [shadowView_ removeFromSuperview] was added
67 }
68
69 - (void)drawRect:(NSRect)rect {
70 // Don't draw anything on a Retina display because on Retina there's room
71 // for the shadow just beneath the autocomplete textfield path stroke. Why
72 // even add this view? If the user drags the Incognito window between Retina
73 // and non-Retina screens there would have to be logic to add and remove the
74 // view. It's easier just to always add it for Incognito mode and draw
75 // nothing into it.
76 if ([self cr_lineWidth] < 1) {
77 return;
78 }
79 [[AutocompleteTextField shadowColor] set];
80 NSRectFill(rect);
81 }
82
83 @end
84
27 @implementation AutocompleteTextField 85 @implementation AutocompleteTextField
28 86
29 @synthesize observer = observer_; 87 @synthesize observer = observer_;
30 88
31 + (Class)cellClass { 89 + (Class)cellClass {
32 return [AutocompleteTextFieldCell class]; 90 return [AutocompleteTextFieldCell class];
33 } 91 }
34 92
93 + (NSColor*)shadowColor {
94 return [NSColor colorWithGenericGamma22White:kShadowColor alpha:1];
95 }
96
35 - (void)dealloc { 97 - (void)dealloc {
36 [[NSNotificationCenter defaultCenter] removeObserver:self]; 98 [[NSNotificationCenter defaultCenter] removeObserver:self];
99 [shadowView_ removeFromSuperview];
37 [super dealloc]; 100 [super dealloc];
38 } 101 }
39 102
40 - (void)awakeFromNib { 103 - (void)awakeFromNib {
41 DCHECK([[self cell] isKindOfClass:[AutocompleteTextFieldCell class]]); 104 DCHECK([[self cell] isKindOfClass:[AutocompleteTextFieldCell class]]);
42 [[self cell] setTruncatesLastVisibleLine:YES]; 105 [[self cell] setTruncatesLastVisibleLine:YES];
43 [[self cell] setLineBreakMode:NSLineBreakByTruncatingTail]; 106 [[self cell] setLineBreakMode:NSLineBreakByTruncatingTail];
44 currentToolTips_.reset([[NSMutableArray alloc] init]); 107 currentToolTips_.reset([[NSMutableArray alloc] init]);
45 resizeAnimation_.reset([[NSViewAnimation alloc] init]); 108 resizeAnimation_.reset([[NSViewAnimation alloc] init]);
46 [resizeAnimation_ setDuration:kAnimationDuration]; 109 [resizeAnimation_ setDuration:kAnimationDuration];
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 - (void)windowDidChangeScreen { 435 - (void)windowDidChangeScreen {
373 // Inform the AutocompleteTextFieldCell's of the coordinate system line 436 // Inform the AutocompleteTextFieldCell's of the coordinate system line
374 // width needed to draw a single-pixel line. This value changes as we move 437 // width needed to draw a single-pixel line. This value changes as we move
375 // between Retina and non-Retina displays. 438 // between Retina and non-Retina displays.
376 [[self cell] setSinglePixelLineWidth:[self cr_lineWidth]]; 439 [[self cell] setSinglePixelLineWidth:[self cr_lineWidth]];
377 [self setNeedsDisplay]; 440 [self setNeedsDisplay];
378 } 441 }
379 442
380 - (void)updateColorsToMatchTheme { 443 - (void)updateColorsToMatchTheme {
381 if (![[self window] inIncognitoMode]) { 444 if (![[self window] inIncognitoMode]) {
445 [shadowView_ removeFromSuperview];
382 return; 446 return;
383 } 447 }
384 448
449 // Add 1px shadow below the autocomplete textfield.
450 if (!shadowView_.get()) {
451 shadowView_.reset(
452 [[AutocompleteTextFieldShadowView alloc] initWithTextField:self]);
453 }
454 [[self superview] addSubview:shadowView_];
455
385 // Invert the textfield's colors when Material Design and Incognito and not 456 // Invert the textfield's colors when Material Design and Incognito and not
386 // a custom theme. 457 // a custom theme.
387 bool inDarkMode = [[self window] inIncognitoModeWithSystemTheme]; 458 bool inDarkMode = [[self window] inIncognitoModeWithSystemTheme];
388 [self setBackgroundColor: 459 [self setBackgroundColor:
389 inDarkMode ? [NSColor colorWithCalibratedWhite:115 / 255. alpha:1] 460 inDarkMode ? [NSColor colorWithGenericGamma22White:115 / 255. alpha:1]
390 : [NSColor whiteColor]]; 461 : [NSColor colorWithGenericGamma22White:1 alpha:1]];
tapted 2016/04/27 00:55:32 hm, I've always assumed "white" \implies{} "max co
shrike 2016/04/27 17:00:22 OK. I wasn't sure about whiteColor. I'm still not
391 [self setTextColor:OmniboxViewMac::BaseTextColor(inDarkMode)]; 462 [self setTextColor:OmniboxViewMac::BaseTextColor(inDarkMode)];
392 } 463 }
393 464
394 - (void)viewDidMoveToWindow { 465 - (void)viewDidMoveToWindow {
395 if (![self window]) { 466 if (![self window]) {
396 return; 467 return;
397 } 468 }
398 469
399 // Allow the ToolbarController to take action upon the 470 // Allow the ToolbarController to take action upon the
400 // AutocompleteTextField being added to the window. 471 // AutocompleteTextField being added to the window.
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 NSMinY(frame), 653 NSMinY(frame),
583 suggestWidth, 654 suggestWidth,
584 NSHeight(frame)); 655 NSHeight(frame));
585 656
586 gfx::ScopedNSGraphicsContextSaveGState saveGState; 657 gfx::ScopedNSGraphicsContextSaveGState saveGState;
587 NSRectClip(suggestRect); 658 NSRectClip(suggestRect);
588 [cell drawInteriorWithFrame:frame inView:controlView]; 659 [cell drawInteriorWithFrame:frame inView:controlView];
589 } 660 }
590 661
591 } // namespace autocomplete_text_field 662 } // namespace autocomplete_text_field
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698