| 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; |
| 25 | 26 |
| 26 } | 27 } |
| 27 | 28 |
| 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 |
| 28 @implementation AutocompleteTextField | 98 @implementation AutocompleteTextField |
| 29 | 99 |
| 30 @synthesize observer = observer_; | 100 @synthesize observer = observer_; |
| 31 | 101 |
| 32 + (Class)cellClass { | 102 + (Class)cellClass { |
| 33 return [AutocompleteTextFieldCell class]; | 103 return [AutocompleteTextFieldCell class]; |
| 34 } | 104 } |
| 35 | 105 |
| 106 + (NSColor*)shadowColor { |
| 107 return [NSColor colorWithGenericGamma22White:0 alpha:0.14]; |
| 108 } |
| 109 |
| 36 - (void)dealloc { | 110 - (void)dealloc { |
| 37 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 111 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 112 [shadowView_ removeFromSuperview]; |
| 38 [super dealloc]; | 113 [super dealloc]; |
| 39 } | 114 } |
| 40 | 115 |
| 41 - (void)awakeFromNib { | 116 - (void)awakeFromNib { |
| 42 DCHECK([[self cell] isKindOfClass:[AutocompleteTextFieldCell class]]); | 117 DCHECK([[self cell] isKindOfClass:[AutocompleteTextFieldCell class]]); |
| 43 [[self cell] setTruncatesLastVisibleLine:YES]; | 118 [[self cell] setTruncatesLastVisibleLine:YES]; |
| 44 [[self cell] setLineBreakMode:NSLineBreakByTruncatingTail]; | 119 [[self cell] setLineBreakMode:NSLineBreakByTruncatingTail]; |
| 45 currentToolTips_.reset([[NSMutableArray alloc] init]); | 120 currentToolTips_.reset([[NSMutableArray alloc] init]); |
| 46 resizeAnimation_.reset([[NSViewAnimation alloc] init]); | 121 resizeAnimation_.reset([[NSViewAnimation alloc] init]); |
| 47 [resizeAnimation_ setDuration:kAnimationDuration]; | 122 [resizeAnimation_ setDuration:kAnimationDuration]; |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 } | 454 } |
| 380 | 455 |
| 381 - (void)updateColorsToMatchTheme { | 456 - (void)updateColorsToMatchTheme { |
| 382 if (![[self window] inIncognitoMode]) { | 457 if (![[self window] inIncognitoMode]) { |
| 383 return; | 458 return; |
| 384 } | 459 } |
| 385 | 460 |
| 386 // 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 |
| 387 // a custom theme. | 462 // a custom theme. |
| 388 bool inDarkMode = [[self window] inIncognitoModeWithSystemTheme]; | 463 bool inDarkMode = [[self window] inIncognitoModeWithSystemTheme]; |
| 389 const CGFloat kDarkModeGray = 97 / 255.; | |
| 390 [self setBackgroundColor: | 464 [self setBackgroundColor: |
| 391 inDarkMode ? [NSColor colorWithGenericGamma22White:kDarkModeGray | 465 inDarkMode ? [NSColor colorWithGenericGamma22White:115 / 255. alpha:1] |
| 392 alpha:1] | |
| 393 : [NSColor whiteColor]]; | 466 : [NSColor whiteColor]]; |
| 394 [self setTextColor:OmniboxViewMac::BaseTextColor(inDarkMode)]; | 467 [self setTextColor:OmniboxViewMac::BaseTextColor(inDarkMode)]; |
| 395 } | 468 } |
| 396 | 469 |
| 397 - (void)viewDidMoveToWindow { | 470 - (void)viewDidMoveToWindow { |
| 398 if (![self window]) { | 471 if (![self window]) { |
| 472 [shadowView_ removeFromSuperview]; |
| 473 shadowView_.reset(); |
| 399 return; | 474 return; |
| 400 } | 475 } |
| 401 | 476 |
| 402 // Allow the ToolbarController to take action upon the | 477 // Allow the ToolbarController to take action upon the |
| 403 // AutocompleteTextField being added to the window. | 478 // AutocompleteTextField being added to the window. |
| 404 if (ui::MaterialDesignController::IsModeMaterial()) { | 479 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 405 BrowserWindowController* browserWindowController = | 480 BrowserWindowController* browserWindowController = |
| 406 [BrowserWindowController browserWindowControllerForView:self]; | 481 [BrowserWindowController browserWindowControllerForView:self]; |
| 407 [[browserWindowController toolbarController] locationBarWasAddedToWindow]; | 482 [[browserWindowController toolbarController] locationBarWasAddedToWindow]; |
| 408 | 483 |
| 484 // Add a 1px shadow below the autocomplete textfield. |
| 485 shadowView_.reset( |
| 486 [[AutocompleteTextFieldShadowView alloc] initWithTextField:self]); |
| 487 [[self superview] addSubview:shadowView_]; |
| 488 |
| 409 [self updateColorsToMatchTheme]; | 489 [self updateColorsToMatchTheme]; |
| 410 } | 490 } |
| 411 | 491 |
| 412 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; | 492 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; |
| 413 [nc addObserver:self | 493 [nc addObserver:self |
| 414 selector:@selector(windowDidResignKey:) | 494 selector:@selector(windowDidResignKey:) |
| 415 name:NSWindowDidResignKeyNotification | 495 name:NSWindowDidResignKeyNotification |
| 416 object:[self window]]; | 496 object:[self window]]; |
| 417 [nc addObserver:self | 497 [nc addObserver:self |
| 418 selector:@selector(windowDidResize:) | 498 selector:@selector(windowDidResize:) |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 NSMinY(frame), | 665 NSMinY(frame), |
| 586 suggestWidth, | 666 suggestWidth, |
| 587 NSHeight(frame)); | 667 NSHeight(frame)); |
| 588 | 668 |
| 589 gfx::ScopedNSGraphicsContextSaveGState saveGState; | 669 gfx::ScopedNSGraphicsContextSaveGState saveGState; |
| 590 NSRectClip(suggestRect); | 670 NSRectClip(suggestRect); |
| 591 [cell drawInteriorWithFrame:frame inView:controlView]; | 671 [cell drawInteriorWithFrame:frame inView:controlView]; |
| 592 } | 672 } |
| 593 | 673 |
| 594 } // namespace autocomplete_text_field | 674 } // namespace autocomplete_text_field |
| OLD | NEW |