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

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 for Retina screens. 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 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;
25 } 26 }
26 27
28 // A view that draws a 1px shadow line beneath the autocomplete textfield.
29 @interface AutocompleteTextFieldShadowView : NSView {
tapted 2016/04/22 01:11:46 nit: curlies not needed
shrike 2016/04/26 19:19:30 Done.
30 }
31 @end
32
33 @implementation AutocompleteTextFieldShadowView
34
35 - (void)drawRect:(NSRect)rect {
36 // Don't draw anything on a Retina display because on Retina there's room
37 // for the shadow just beneath the autocomplete textfield path stroke. Why
38 // even add this view? If the user drags the Incognito window between Retina
39 // and non-Retina screens there would have to be logic to add and remove the
40 // view. It's easier just to always add it for Incognito mode and draw
41 // nothing into it.
42 if ([self cr_lineWidth] < 1) {
tapted 2016/04/22 01:11:46 optional nit: curlies not needed (but I'd keep a b
shrike 2016/04/26 19:19:30 Acknowledged.
43 return;
44 }
45 [[NSColor colorWithCalibratedWhite:69 / 255. alpha:1] set];
tapted 2016/04/22 01:11:46 hmmmm this should be `colorWithGenericGamma22White
shrike 2016/04/22 02:09:00 I'm confused, because the link you sent says 2.2 i
tapted 2016/04/22 04:41:34 I don't think it's clear what gamma you get with `
46 NSRectFill(rect);
47 }
48
49 @end
50
27 @implementation AutocompleteTextField 51 @implementation AutocompleteTextField
28 52
29 @synthesize observer = observer_; 53 @synthesize observer = observer_;
30 54
31 + (Class)cellClass { 55 + (Class)cellClass {
32 return [AutocompleteTextFieldCell class]; 56 return [AutocompleteTextFieldCell class];
33 } 57 }
34 58
35 - (void)dealloc { 59 - (void)dealloc {
36 [[NSNotificationCenter defaultCenter] removeObserver:self]; 60 [[NSNotificationCenter defaultCenter] removeObserver:self];
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 234
211 // Overridden so that cursor and tooltip rects can be updated. 235 // Overridden so that cursor and tooltip rects can be updated.
212 - (void)setFrame:(NSRect)frameRect { 236 - (void)setFrame:(NSRect)frameRect {
213 [super setFrame:frameRect]; 237 [super setFrame:frameRect];
214 if (observer_) { 238 if (observer_) {
215 observer_->OnFrameChanged(); 239 observer_->OnFrameChanged();
216 } 240 }
217 [self updateMouseTracking]; 241 [self updateMouseTracking];
218 } 242 }
219 243
244 // Overridden to adjust the shadow view whenever the textfield's origin changes.
tapted 2016/04/22 01:11:46 I think the typical solution for this under c/b/ui
shrike 2016/04/22 02:09:00 OK. I can't remember off the top of my head if it'
tapted 2016/04/22 04:41:34 Would it work to pass in `self` into new initializ
245 - (void)setFrameOrigin:(NSPoint)frameOrigin {
246 [super setFrameOrigin:frameOrigin];
247 [self adjustShadowViewFrame];
248 }
249
250 // Overridden to adjust the shadow view whenever the textfield's size changes.
251 - (void)setFrameSize:(NSSize)frameSize {
252 [super setFrameSize:frameSize];
253 [self adjustShadowViewFrame];
254 }
255
220 - (void)setAttributedStringValue:(NSAttributedString*)aString { 256 - (void)setAttributedStringValue:(NSAttributedString*)aString {
221 AutocompleteTextFieldEditor* editor = 257 AutocompleteTextFieldEditor* editor =
222 static_cast<AutocompleteTextFieldEditor*>([self currentEditor]); 258 static_cast<AutocompleteTextFieldEditor*>([self currentEditor]);
223 259
224 if (!editor) { 260 if (!editor) {
225 [super setAttributedStringValue:aString]; 261 [super setAttributedStringValue:aString];
226 } else { 262 } else {
227 // The type of the field editor must be AutocompleteTextFieldEditor, 263 // The type of the field editor must be AutocompleteTextFieldEditor,
228 // otherwise things won't work. 264 // otherwise things won't work.
229 DCHECK([editor isKindOfClass:[AutocompleteTextFieldEditor class]]); 265 DCHECK([editor isKindOfClass:[AutocompleteTextFieldEditor class]]);
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 } 406 }
371 407
372 - (void)windowDidChangeScreen { 408 - (void)windowDidChangeScreen {
373 // Inform the AutocompleteTextFieldCell's of the coordinate system line 409 // Inform the AutocompleteTextFieldCell's of the coordinate system line
374 // width needed to draw a single-pixel line. This value changes as we move 410 // width needed to draw a single-pixel line. This value changes as we move
375 // between Retina and non-Retina displays. 411 // between Retina and non-Retina displays.
376 [[self cell] setSinglePixelLineWidth:[self cr_lineWidth]]; 412 [[self cell] setSinglePixelLineWidth:[self cr_lineWidth]];
377 [self setNeedsDisplay]; 413 [self setNeedsDisplay];
378 } 414 }
379 415
416 - (void)adjustShadowViewFrame {
417 // Make the shadow view 1pt tall and slightly inset from the edges of the
418 // autocomplete textfield.
419 NSRect shadowViewFrame = [self frame];
420 shadowViewFrame.origin.x += kShadowInset;
421 shadowViewFrame.size.width -= kShadowInset * 2;
422 shadowViewFrame.origin.y -= 1;
423 shadowViewFrame.size.height = 1;
424 [shadowView_ setFrame:shadowViewFrame];
425 }
426
380 - (void)updateColorsToMatchTheme { 427 - (void)updateColorsToMatchTheme {
381 if (![[self window] inIncognitoMode]) { 428 if (![[self window] inIncognitoMode]) {
429 [shadowView_ removeFromSuperview];
382 return; 430 return;
383 } 431 }
384 432
433 // Add 1px shadow below the autocomplete textfield.
434 if (!shadowView_.get()) {
435 shadowView_.reset(
436 [[AutocompleteTextFieldShadowView alloc] initWithFrame:NSZeroRect]);
437 }
438 [self adjustShadowViewFrame];
439 [[self superview] addSubview:shadowView_];
440
385 // Invert the textfield's colors when Material Design and Incognito and not 441 // Invert the textfield's colors when Material Design and Incognito and not
386 // a custom theme. 442 // a custom theme.
387 bool inDarkMode = [[self window] inIncognitoModeWithSystemTheme]; 443 bool inDarkMode = [[self window] inIncognitoModeWithSystemTheme];
388 [self setBackgroundColor: 444 [self setBackgroundColor:
389 inDarkMode ? [NSColor colorWithCalibratedWhite:115 / 255. alpha:1] 445 inDarkMode ? [NSColor colorWithCalibratedWhite:115 / 255. alpha:1]
390 : [NSColor whiteColor]]; 446 : [NSColor whiteColor]];
391 [self setTextColor:OmniboxViewMac::BaseTextColor(inDarkMode)]; 447 [self setTextColor:OmniboxViewMac::BaseTextColor(inDarkMode)];
392 } 448 }
393 449
394 - (void)viewDidMoveToWindow { 450 - (void)viewDidMoveToWindow {
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 NSMinY(frame), 638 NSMinY(frame),
583 suggestWidth, 639 suggestWidth,
584 NSHeight(frame)); 640 NSHeight(frame));
585 641
586 gfx::ScopedNSGraphicsContextSaveGState saveGState; 642 gfx::ScopedNSGraphicsContextSaveGState saveGState;
587 NSRectClip(suggestRect); 643 NSRectClip(suggestRect);
588 [cell drawInteriorWithFrame:frame inView:controlView]; 644 [cell drawInteriorWithFrame:frame inView:controlView];
589 } 645 }
590 646
591 } // namespace autocomplete_text_field 647 } // namespace autocomplete_text_field
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698