| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_cell.h" | 5 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
| 11 #include "base/mac/mac_logging.h" | 11 #include "base/mac/mac_logging.h" |
| 12 #include "chrome/browser/search/search.h" | 12 #include "chrome/browser/search/search.h" |
| 13 #include "chrome/browser/themes/theme_service.h" |
| 13 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" | 14 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" |
| 14 #import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h" | 15 #import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h" |
| 16 #import "chrome/browser/ui/cocoa/themed_window.h" |
| 15 #import "extensions/common/feature_switch.h" | 17 #import "extensions/common/feature_switch.h" |
| 16 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
| 17 #import "third_party/mozilla/NSPasteboard+Utils.h" | 19 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 18 #import "ui/base/cocoa/appkit_utils.h" | 20 #import "ui/base/cocoa/appkit_utils.h" |
| 19 #import "ui/base/cocoa/nsview_additions.h" | 21 #import "ui/base/cocoa/nsview_additions.h" |
| 20 #include "ui/base/cocoa/scoped_cg_context_smooth_fonts.h" | 22 #include "ui/base/cocoa/scoped_cg_context_smooth_fonts.h" |
| 23 #include "ui/base/material_design/material_design_controller.h" |
| 21 | 24 |
| 22 using extensions::FeatureSwitch; | 25 using extensions::FeatureSwitch; |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 // Matches the clipping radius of |GradientButtonCell|. | 29 // Matches the clipping radius of |GradientButtonCell|. |
| 27 const CGFloat kCornerRadius = 3.0; | 30 const CGFloat kCornerRadius = 3.0; |
| 28 | 31 |
| 29 // How far to inset the left- and right-hand decorations from the field's | 32 // How far to inset the left- and right-hand decorations from the field's |
| 30 // bounds. | 33 // bounds. |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 } | 285 } |
| 283 } | 286 } |
| 284 | 287 |
| 285 // I-beam cursor covers left-most to right-most. | 288 // I-beam cursor covers left-most to right-most. |
| 286 return NSMakeRect(minX, NSMinY(textFrame), maxX - minX, NSHeight(textFrame)); | 289 return NSMakeRect(minX, NSMinY(textFrame), maxX - minX, NSHeight(textFrame)); |
| 287 } | 290 } |
| 288 | 291 |
| 289 - (void)drawWithFrame:(NSRect)frame inView:(NSView*)controlView { | 292 - (void)drawWithFrame:(NSRect)frame inView:(NSView*)controlView { |
| 290 // Background color. | 293 // Background color. |
| 291 const CGFloat lineWidth = [controlView cr_lineWidth]; | 294 const CGFloat lineWidth = [controlView cr_lineWidth]; |
| 295 BOOL isModeMaterial = ui::MaterialDesignController::IsModeMaterial(); |
| 296 BOOL inDarkMode = [[controlView window] inIncognitoModeWithSystemTheme]; |
| 297 BOOL showingFirstResponder = [self showsFirstResponder]; |
| 298 // Adjust the inset by 1/2 the line width to get a crisp line (screen pixels |
| 299 // lay between cooridnate space lines). |
| 300 CGFloat insetSize = 1 - lineWidth / 2.; |
| 301 if (isModeMaterial && showingFirstResponder && !inDarkMode) { |
| 302 insetSize++; |
| 303 } else if (!isModeMaterial) { |
| 304 insetSize = lineWidth == 0.5 ? 1.5 : 2.0; |
| 305 } |
| 306 |
| 307 // Compute the border's bezier path. |
| 308 NSRect pathRect = NSInsetRect(frame, insetSize, insetSize); |
| 309 // In dark mode, make room for a shadow beneath the bottom edge. |
| 310 if (inDarkMode && isModeMaterial) { |
| 311 pathRect.size.height -= lineWidth; |
| 312 } |
| 313 NSBezierPath* path = |
| 314 [NSBezierPath bezierPathWithRoundedRect:pathRect |
| 315 xRadius:kCornerRadius |
| 316 yRadius:kCornerRadius]; |
| 317 if (isModeMaterial) { |
| 318 [path setLineWidth:showingFirstResponder ? lineWidth * 2 : lineWidth]; |
| 319 } |
| 320 |
| 321 // Fill the background. |
| 322 [[self backgroundColor] set]; |
| 292 if (isPopupMode_) { | 323 if (isPopupMode_) { |
| 293 [[self backgroundColor] set]; | |
| 294 NSRectFillUsingOperation(NSInsetRect(frame, 1, 1), NSCompositeSourceOver); | 324 NSRectFillUsingOperation(NSInsetRect(frame, 1, 1), NSCompositeSourceOver); |
| 295 } else { | 325 } else { |
| 296 CGFloat insetSize = lineWidth == 0.5 ? 1.5 : 2.0; | 326 [path fill]; |
| 297 NSRect fillRect = NSInsetRect(frame, insetSize, insetSize); | |
| 298 [[self backgroundColor] set]; | |
| 299 [[NSBezierPath bezierPathWithRoundedRect:fillRect | |
| 300 xRadius:kCornerRadius | |
| 301 yRadius:kCornerRadius] fill]; | |
| 302 } | 327 } |
| 303 | 328 |
| 304 // Border. | 329 // Draw the border. |
| 305 ui::DrawNinePartImage(frame, | 330 if (isModeMaterial) { |
| 306 isPopupMode_ ? kPopupBorderImageIds | 331 if (!inDarkMode) { |
| 307 : kNormalBorderImageIds, | 332 [[NSColor colorWithCalibratedWhite:168 / 255. alpha:1] set]; |
| 308 NSCompositeSourceOver, | 333 [path stroke]; |
| 309 1.0, | 334 } else { |
| 310 true); | 335 // In dark mode the top, middle, and bottom portions of the stroke are |
| 336 // drawn in different colors. |
| 337 { |
| 338 gfx::ScopedNSGraphicsContextSaveGState saveState; |
| 339 [[NSColor colorWithCalibratedWhite:52 / 255. alpha:1] set]; |
| 340 [NSBezierPath clipRect:NSMakeRect(NSMinX(frame), NSMaxY(frame) - 2, |
| 341 NSWidth(frame), 2)]; |
| 342 [path stroke]; |
| 343 } |
| 344 { |
| 345 gfx::ScopedNSGraphicsContextSaveGState saveState; |
| 346 [[NSColor colorWithCalibratedWhite:61 / 255. alpha:1] set]; |
| 347 [NSBezierPath clipRect:NSMakeRect(NSMinX(frame), NSMinY(frame) + 3, |
| 348 NSWidth(frame), NSHeight(frame) - 5)]; |
| 349 [path stroke]; |
| 350 } |
| 351 { |
| 352 gfx::ScopedNSGraphicsContextSaveGState saveState; |
| 353 [[NSColor colorWithCalibratedWhite:71 / 255. alpha:1] set]; |
| 354 [NSBezierPath clipRect:NSMakeRect(NSMinX(frame), NSMinY(frame), |
| 355 NSWidth(frame), 3)]; |
| 356 [path stroke]; |
| 357 } |
| 311 | 358 |
| 312 // Interior contents. Drawn after the border as some of the interior controls | 359 // Draw a highlight beneath the top edge, and a shadow beneath the bottom |
| 313 // draw over the border. | 360 // edge. |
| 361 { |
| 362 gfx::ScopedNSGraphicsContextSaveGState saveState; |
| 363 [NSBezierPath setDefaultLineWidth:lineWidth]; |
| 364 |
| 365 [[NSColor colorWithCalibratedWhite:120 / 255. alpha:1] set]; |
| 366 NSPoint origin = NSMakePoint(NSMinX(pathRect) + 3, |
| 367 NSMinY(pathRect) + lineWidth); |
| 368 NSPoint destination = NSMakePoint(NSMaxX(pathRect) - 3, |
| 369 NSMinY(pathRect) + lineWidth); |
| 370 [NSBezierPath strokeLineFromPoint:origin |
| 371 toPoint:destination]; |
| 372 |
| 373 origin.y = destination.y = NSMaxY(pathRect) + lineWidth; |
| 374 [[NSColor colorWithCalibratedWhite:69 / 255. alpha:1] set]; |
| 375 [NSBezierPath strokeLineFromPoint:origin |
| 376 toPoint:destination]; |
| 377 } |
| 378 } |
| 379 } else { |
| 380 ui::DrawNinePartImage(frame, |
| 381 isPopupMode_ ? kPopupBorderImageIds |
| 382 : kNormalBorderImageIds, |
| 383 NSCompositeSourceOver, |
| 384 1.0, |
| 385 true); |
| 386 } |
| 387 |
| 388 // Draw the interior contents. We do this after drawing the border as some |
| 389 // of the interior controls draw over it. |
| 314 [self drawInteriorWithFrame:frame inView:controlView]; | 390 [self drawInteriorWithFrame:frame inView:controlView]; |
| 315 | 391 |
| 316 // Focus ring. | 392 // Draw the focus ring. |
| 317 if ([self showsFirstResponder]) { | 393 if (showingFirstResponder) { |
| 318 NSRect focusRingRect = NSInsetRect(frame, lineWidth, lineWidth); | 394 if (!isModeMaterial) { |
| 395 NSRect focusRingRect = NSInsetRect(frame, lineWidth, lineWidth); |
| 396 path = [NSBezierPath bezierPathWithRoundedRect:focusRingRect |
| 397 xRadius:kCornerRadius |
| 398 yRadius:kCornerRadius]; |
| 399 [path setLineWidth:lineWidth * 2.0]; |
| 400 } |
| 401 |
| 402 CGFloat alphaComponent = 0.5 / lineWidth; |
| 403 if (isModeMaterial && inDarkMode) { |
| 404 alphaComponent = 1; |
| 405 } |
| 319 [[[NSColor keyboardFocusIndicatorColor] | 406 [[[NSColor keyboardFocusIndicatorColor] |
| 320 colorWithAlphaComponent:0.5 / lineWidth] set]; | 407 colorWithAlphaComponent:alphaComponent] set]; |
| 321 NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:focusRingRect | |
| 322 xRadius:kCornerRadius | |
| 323 yRadius:kCornerRadius]; | |
| 324 [path setLineWidth:lineWidth * 2.0]; | |
| 325 [path stroke]; | 408 [path stroke]; |
| 326 } | 409 } |
| 327 } | 410 } |
| 328 | 411 |
| 329 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 412 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 330 std::vector<LocationBarDecoration*> decorations; | 413 std::vector<LocationBarDecoration*> decorations; |
| 331 std::vector<NSRect> decorationFrames; | 414 std::vector<NSRect> decorationFrames; |
| 332 NSRect workingFrame; | 415 NSRect workingFrame; |
| 333 | 416 |
| 334 CalculatePositionsInFrame(cellFrame, leftDecorations_, rightDecorations_, | 417 CalculatePositionsInFrame(cellFrame, leftDecorations_, rightDecorations_, |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 | 753 |
| 671 - (void)handleFocusEvent:(NSEvent*)event | 754 - (void)handleFocusEvent:(NSEvent*)event |
| 672 ofView:(AutocompleteTextField*)controlView { | 755 ofView:(AutocompleteTextField*)controlView { |
| 673 if ([controlView observer]) { | 756 if ([controlView observer]) { |
| 674 const bool controlDown = ([event modifierFlags] & NSControlKeyMask) != 0; | 757 const bool controlDown = ([event modifierFlags] & NSControlKeyMask) != 0; |
| 675 [controlView observer]->OnSetFocus(controlDown); | 758 [controlView observer]->OnSetFocus(controlDown); |
| 676 } | 759 } |
| 677 } | 760 } |
| 678 | 761 |
| 679 @end | 762 @end |
| OLD | NEW |