| OLD | NEW |
| 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 | 25 |
| 27 } | 26 } |
| 28 | 27 |
| 29 // A view that draws a 1px shadow line beneath the autocomplete textfield. | |
| 30 @interface AutocompleteTextFieldShadowView : NSView { | |
| 31 @private | |
| 32 AutocompleteTextField* textField_; // Weak. Owns this. | |
| 33 } | |
| 34 // This is the designated initializer for AutocompleteTextFieldShadowView. | |
| 35 - (instancetype)initWithTextField:(AutocompleteTextField*)aTextField; | |
| 36 @end | |
| 37 | |
| 38 @interface AutocompleteTextFieldShadowView(Private) | |
| 39 // Adjusts the shadow view's position whenever its AutocompleteTextField changes | |
| 40 // its frame. | |
| 41 - (void)adjustFrame; | |
| 42 @end | |
| 43 | |
| 44 @implementation AutocompleteTextFieldShadowView | |
| 45 | |
| 46 - (instancetype)initWithTextField:(AutocompleteTextField*)aTextField { | |
| 47 if ((self = [self initWithFrame:NSZeroRect])) { | |
| 48 textField_ = aTextField; | |
| 49 [[NSNotificationCenter defaultCenter] | |
| 50 addObserver:self | |
| 51 selector:@selector(adjustFrame) | |
| 52 name:NSViewFrameDidChangeNotification | |
| 53 object:textField_]; | |
| 54 } | |
| 55 return self; | |
| 56 } | |
| 57 | |
| 58 - (void)dealloc { | |
| 59 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 60 [super dealloc]; | |
| 61 } | |
| 62 | |
| 63 - (void)adjustFrame { | |
| 64 if (![self window]) { | |
| 65 return; | |
| 66 } | |
| 67 // Make the shadow view 1pt tall and slightly inset from the edges of the | |
| 68 // autocomplete textfield. | |
| 69 NSRect frame = [textField_ frame]; | |
| 70 frame.origin.x += kShadowInset; | |
| 71 frame.size.width -= kShadowInset * 2; | |
| 72 frame.origin.y -= 1; | |
| 73 frame.size.height = 1; | |
| 74 [self setFrame:frame]; | |
| 75 } | |
| 76 | |
| 77 - (void)viewDidMoveToWindow { | |
| 78 [self adjustFrame]; | |
| 79 } | |
| 80 | |
| 81 - (void)drawRect:(NSRect)rect { | |
| 82 // Don't draw anything on a Retina display because on Retina there's room | |
| 83 // for the shadow just beneath the autocomplete textfield path stroke. Why | |
| 84 // even add this view? If the user drags the Incognito window between Retina | |
| 85 // and non-Retina screens there would have to be logic to add and remove the | |
| 86 // view. It's easier just to always add it for Incognito mode and draw | |
| 87 // nothing into it. | |
| 88 if (![[self window] inIncognitoModeWithSystemTheme] || | |
| 89 [self cr_lineWidth] < 1) { | |
| 90 return; | |
| 91 } | |
| 92 [[AutocompleteTextField shadowColor] set]; | |
| 93 NSRectFillUsingOperation(rect, NSCompositeSourceOver); | |
| 94 } | |
| 95 | |
| 96 @end | |
| 97 | |
| 98 @implementation AutocompleteTextField | 28 @implementation AutocompleteTextField |
| 99 | 29 |
| 100 @synthesize observer = observer_; | 30 @synthesize observer = observer_; |
| 101 | 31 |
| 102 + (Class)cellClass { | 32 + (Class)cellClass { |
| 103 return [AutocompleteTextFieldCell class]; | 33 return [AutocompleteTextFieldCell class]; |
| 104 } | 34 } |
| 105 | 35 |
| 106 + (NSColor*)shadowColor { | |
| 107 return [NSColor colorWithGenericGamma22White:0 alpha:0.14]; | |
| 108 } | |
| 109 | |
| 110 - (void)dealloc { | 36 - (void)dealloc { |
| 111 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 37 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 112 [shadowView_ removeFromSuperview]; | |
| 113 [super dealloc]; | 38 [super dealloc]; |
| 114 } | 39 } |
| 115 | 40 |
| 116 - (void)awakeFromNib { | 41 - (void)awakeFromNib { |
| 117 DCHECK([[self cell] isKindOfClass:[AutocompleteTextFieldCell class]]); | 42 DCHECK([[self cell] isKindOfClass:[AutocompleteTextFieldCell class]]); |
| 118 [[self cell] setTruncatesLastVisibleLine:YES]; | 43 [[self cell] setTruncatesLastVisibleLine:YES]; |
| 119 [[self cell] setLineBreakMode:NSLineBreakByTruncatingTail]; | 44 [[self cell] setLineBreakMode:NSLineBreakByTruncatingTail]; |
| 120 currentToolTips_.reset([[NSMutableArray alloc] init]); | 45 currentToolTips_.reset([[NSMutableArray alloc] init]); |
| 121 resizeAnimation_.reset([[NSViewAnimation alloc] init]); | 46 resizeAnimation_.reset([[NSViewAnimation alloc] init]); |
| 122 [resizeAnimation_ setDuration:kAnimationDuration]; | 47 [resizeAnimation_ setDuration:kAnimationDuration]; |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 } | 379 } |
| 455 | 380 |
| 456 - (void)updateColorsToMatchTheme { | 381 - (void)updateColorsToMatchTheme { |
| 457 if (![[self window] inIncognitoMode]) { | 382 if (![[self window] inIncognitoMode]) { |
| 458 return; | 383 return; |
| 459 } | 384 } |
| 460 | 385 |
| 461 // Invert the textfield's colors when Material Design and Incognito and not | 386 // Invert the textfield's colors when Material Design and Incognito and not |
| 462 // a custom theme. | 387 // a custom theme. |
| 463 bool inDarkMode = [[self window] inIncognitoModeWithSystemTheme]; | 388 bool inDarkMode = [[self window] inIncognitoModeWithSystemTheme]; |
| 389 const CGFloat kDarkModeGray = 97 / 255.; |
| 464 [self setBackgroundColor: | 390 [self setBackgroundColor: |
| 465 inDarkMode ? [NSColor colorWithGenericGamma22White:115 / 255. alpha:1] | 391 inDarkMode ? [NSColor colorWithGenericGamma22White:kDarkModeGray |
| 392 alpha:1] |
| 466 : [NSColor whiteColor]]; | 393 : [NSColor whiteColor]]; |
| 467 [self setTextColor:OmniboxViewMac::BaseTextColor(inDarkMode)]; | 394 [self setTextColor:OmniboxViewMac::BaseTextColor(inDarkMode)]; |
| 468 } | 395 } |
| 469 | 396 |
| 470 - (void)viewDidMoveToWindow { | 397 - (void)viewDidMoveToWindow { |
| 471 if (![self window]) { | 398 if (![self window]) { |
| 472 [shadowView_ removeFromSuperview]; | |
| 473 shadowView_.reset(); | |
| 474 return; | 399 return; |
| 475 } | 400 } |
| 476 | 401 |
| 477 // Allow the ToolbarController to take action upon the | 402 // Allow the ToolbarController to take action upon the |
| 478 // AutocompleteTextField being added to the window. | 403 // AutocompleteTextField being added to the window. |
| 479 if (ui::MaterialDesignController::IsModeMaterial()) { | 404 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 480 BrowserWindowController* browserWindowController = | 405 BrowserWindowController* browserWindowController = |
| 481 [BrowserWindowController browserWindowControllerForView:self]; | 406 [BrowserWindowController browserWindowControllerForView:self]; |
| 482 [[browserWindowController toolbarController] locationBarWasAddedToWindow]; | 407 [[browserWindowController toolbarController] locationBarWasAddedToWindow]; |
| 483 | 408 |
| 484 // Add a 1px shadow below the autocomplete textfield. | |
| 485 shadowView_.reset( | |
| 486 [[AutocompleteTextFieldShadowView alloc] initWithTextField:self]); | |
| 487 [[self superview] addSubview:shadowView_]; | |
| 488 | |
| 489 [self updateColorsToMatchTheme]; | 409 [self updateColorsToMatchTheme]; |
| 490 } | 410 } |
| 491 | 411 |
| 492 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; | 412 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; |
| 493 [nc addObserver:self | 413 [nc addObserver:self |
| 494 selector:@selector(windowDidResignKey:) | 414 selector:@selector(windowDidResignKey:) |
| 495 name:NSWindowDidResignKeyNotification | 415 name:NSWindowDidResignKeyNotification |
| 496 object:[self window]]; | 416 object:[self window]]; |
| 497 [nc addObserver:self | 417 [nc addObserver:self |
| 498 selector:@selector(windowDidResize:) | 418 selector:@selector(windowDidResize:) |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 NSMinY(frame), | 585 NSMinY(frame), |
| 666 suggestWidth, | 586 suggestWidth, |
| 667 NSHeight(frame)); | 587 NSHeight(frame)); |
| 668 | 588 |
| 669 gfx::ScopedNSGraphicsContextSaveGState saveGState; | 589 gfx::ScopedNSGraphicsContextSaveGState saveGState; |
| 670 NSRectClip(suggestRect); | 590 NSRectClip(suggestRect); |
| 671 [cell drawInteriorWithFrame:frame inView:controlView]; | 591 [cell drawInteriorWithFrame:frame inView:controlView]; |
| 672 } | 592 } |
| 673 | 593 |
| 674 } // namespace autocomplete_text_field | 594 } // namespace autocomplete_text_field |
| OLD | NEW |