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

Unified 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, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.mm
diff --git a/chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.mm b/chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.mm
index 36b9a024ad561c5de3a1eac6b1a9db6e8777a9e6..cd230fc9f45d901149c0c3a87dcee815c2d25903 100644
--- a/chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.mm
+++ b/chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.mm
@@ -22,8 +22,79 @@
namespace {
const CGFloat kAnimationDuration = 0.2;
+const CGFloat kShadowInset = 3;
+const CGFloat kShadowColor = 69 / 255.;
+
}
+// A view that draws a 1px shadow line beneath the autocomplete textfield.
+@interface AutocompleteTextFieldShadowView : NSView {
+ @private
+ AutocompleteTextField* textField_; // Weak. Owns this.
+}
+// This is the designated initializer for AutocompleteTextFieldShadowView.
+- (instancetype)initWithTextField:(AutocompleteTextField*)aTextField;
+@end
+
+@interface AutocompleteTextFieldShadowView(Private)
+// Adjusts the shadow view's position whenever its AutocompleteTextField changes
+// its frame.
+- (void)adjustFrame;
+@end
+
+@implementation AutocompleteTextFieldShadowView
+
+- (instancetype)initWithTextField:(AutocompleteTextField*)aTextField {
+ if ((self = [self initWithFrame:NSZeroRect])) {
+ textField_ = aTextField;
+ [[NSNotificationCenter defaultCenter]
+ addObserver:self
+ selector:@selector(adjustFrame)
+ name:NSViewFrameDidChangeNotification
+ object:textField_];
+ }
+ return self;
+}
+
+- (void)dealloc {
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
+ [super dealloc];
+}
+
+- (void)adjustFrame {
+ if (![self window]) {
+ return;
+ }
+ // Make the shadow view 1pt tall and slightly inset from the edges of the
+ // autocomplete textfield.
+ NSRect frame = [textField_ frame];
+ frame.origin.x += kShadowInset;
+ frame.size.width -= kShadowInset * 2;
+ frame.origin.y -= 1;
+ frame.size.height = 1;
+ [self setFrame:frame];
+}
+
+- (void)viewDidMoveToWindow {
+ [self adjustFrame];
+}
+
+- (void)drawRect:(NSRect)rect {
+ // Don't draw anything on a Retina display because on Retina there's room
+ // for the shadow just beneath the autocomplete textfield path stroke. Why
+ // even add this view? If the user drags the Incognito window between Retina
+ // and non-Retina screens there would have to be logic to add and remove the
+ // view. It's easier just to always add it for Incognito mode and draw
+ // nothing into it.
+ if (![[self window] inIncognitoMode] || [self cr_lineWidth] < 1) {
+ return;
+ }
+ [[AutocompleteTextField shadowColor] set];
+ NSRectFill(rect);
+}
+
+@end
+
@implementation AutocompleteTextField
@synthesize observer = observer_;
@@ -32,8 +103,13 @@ const CGFloat kAnimationDuration = 0.2;
return [AutocompleteTextFieldCell class];
}
++ (NSColor*)shadowColor {
+ return [NSColor colorWithGenericGamma22White:kShadowColor alpha:1];
+}
+
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
+ [shadowView_ removeFromSuperview];
[super dealloc];
}
@@ -386,13 +462,15 @@ const CGFloat kAnimationDuration = 0.2;
// a custom theme.
bool inDarkMode = [[self window] inIncognitoModeWithSystemTheme];
[self setBackgroundColor:
- inDarkMode ? [NSColor colorWithCalibratedWhite:115 / 255. alpha:1]
+ inDarkMode ? [NSColor colorWithGenericGamma22White:115 / 255. alpha:1]
: [NSColor whiteColor]];
[self setTextColor:OmniboxViewMac::BaseTextColor(inDarkMode)];
}
- (void)viewDidMoveToWindow {
if (![self window]) {
+ [shadowView_ removeFromSuperview];
+ shadowView_.reset();
return;
}
@@ -403,6 +481,11 @@ const CGFloat kAnimationDuration = 0.2;
[BrowserWindowController browserWindowControllerForView:self];
[[browserWindowController toolbarController] locationBarWasAddedToWindow];
+ // Add a 1px shadow below the autocomplete textfield.
+ shadowView_.reset(
+ [[AutocompleteTextFieldShadowView alloc] initWithTextField:self]);
+ [[self superview] addSubview:shadowView_];
+
[self updateColorsToMatchTheme];
}

Powered by Google App Engine
This is Rietveld 408576698