| 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" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 155 |
| 156 *remaining_frame = frame; | 156 *remaining_frame = frame; |
| 157 return left_count; | 157 return left_count; |
| 158 } | 158 } |
| 159 | 159 |
| 160 } // namespace | 160 } // namespace |
| 161 | 161 |
| 162 @implementation AutocompleteTextFieldCell | 162 @implementation AutocompleteTextFieldCell |
| 163 | 163 |
| 164 @synthesize isPopupMode = isPopupMode_; | 164 @synthesize isPopupMode = isPopupMode_; |
| 165 @synthesize singlePixelLineWidth = singlePixelLineWidth_; |
| 165 | 166 |
| 166 - (CGFloat)topTextFrameOffset { | 167 - (CGFloat)topTextFrameOffset { |
| 167 return 3.0; | 168 return 3.0; |
| 168 } | 169 } |
| 169 | 170 |
| 170 - (CGFloat)bottomTextFrameOffset { | 171 - (CGFloat)bottomTextFrameOffset { |
| 171 return 3.0; | 172 return 3.0; |
| 172 } | 173 } |
| 173 | 174 |
| 174 - (CGFloat)cornerRadius { | 175 - (CGFloat)cornerRadius { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 | 237 |
| 237 // Overriden to account for the decorations. | 238 // Overriden to account for the decorations. |
| 238 - (NSRect)textFrameForFrame:(NSRect)cellFrame { | 239 - (NSRect)textFrameForFrame:(NSRect)cellFrame { |
| 239 // Get the frame adjusted for decorations. | 240 // Get the frame adjusted for decorations. |
| 240 std::vector<LocationBarDecoration*> decorations; | 241 std::vector<LocationBarDecoration*> decorations; |
| 241 std::vector<NSRect> decorationFrames; | 242 std::vector<NSRect> decorationFrames; |
| 242 NSRect textFrame = [super textFrameForFrame:cellFrame]; | 243 NSRect textFrame = [super textFrameForFrame:cellFrame]; |
| 243 CalculatePositionsInFrame(textFrame, leftDecorations_, rightDecorations_, | 244 CalculatePositionsInFrame(textFrame, leftDecorations_, rightDecorations_, |
| 244 &decorations, &decorationFrames, &textFrame); | 245 &decorations, &decorationFrames, &textFrame); |
| 245 | 246 |
| 247 // The text needs to be slightly higher than its default position to match the |
| 248 // Material Design spec. It turns out this adjustment is equal to the single |
| 249 // pixel line width (so 1 on non-Retina, 0.5 on Retina). Make this adjustment |
| 250 // after computing decoration positions because the decorations are already |
| 251 // correctly positioned. |
| 252 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 253 textFrame.origin.y -= singlePixelLineWidth_; |
| 254 } |
| 255 |
| 246 // NOTE: This function must closely match the logic in | 256 // NOTE: This function must closely match the logic in |
| 247 // |-drawInteriorWithFrame:inView:|. | 257 // |-drawInteriorWithFrame:inView:|. |
| 248 | 258 |
| 249 return textFrame; | 259 return textFrame; |
| 250 } | 260 } |
| 251 | 261 |
| 252 - (NSRect)textCursorFrameForFrame:(NSRect)cellFrame { | 262 - (NSRect)textCursorFrameForFrame:(NSRect)cellFrame { |
| 253 std::vector<LocationBarDecoration*> decorations; | 263 std::vector<LocationBarDecoration*> decorations; |
| 254 std::vector<NSRect> decorationFrames; | 264 std::vector<NSRect> decorationFrames; |
| 255 NSRect textFrame; | 265 NSRect textFrame; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 283 } else { | 293 } else { |
| 284 maxX = NSMaxX(decorationFrames[index]) + kDecorationHorizontalPad; | 294 maxX = NSMaxX(decorationFrames[index]) + kDecorationHorizontalPad; |
| 285 } | 295 } |
| 286 } | 296 } |
| 287 | 297 |
| 288 // I-beam cursor covers left-most to right-most. | 298 // I-beam cursor covers left-most to right-most. |
| 289 return NSMakeRect(minX, NSMinY(textFrame), maxX - minX, NSHeight(textFrame)); | 299 return NSMakeRect(minX, NSMinY(textFrame), maxX - minX, NSHeight(textFrame)); |
| 290 } | 300 } |
| 291 | 301 |
| 292 - (void)drawWithFrame:(NSRect)frame inView:(NSView*)controlView { | 302 - (void)drawWithFrame:(NSRect)frame inView:(NSView*)controlView { |
| 293 // Background color. | |
| 294 const CGFloat lineWidth = [controlView cr_lineWidth]; | |
| 295 BOOL isModeMaterial = ui::MaterialDesignController::IsModeMaterial(); | 303 BOOL isModeMaterial = ui::MaterialDesignController::IsModeMaterial(); |
| 296 BOOL inDarkMode = [[controlView window] inIncognitoModeWithSystemTheme]; | 304 BOOL inDarkMode = [[controlView window] inIncognitoModeWithSystemTheme]; |
| 297 BOOL showingFirstResponder = [self showsFirstResponder]; | 305 BOOL showingFirstResponder = [self showsFirstResponder]; |
| 298 // Adjust the inset by 1/2 the line width to get a crisp line (screen pixels | 306 // Adjust the inset by 1/2 the line width to get a crisp line (screen pixels |
| 299 // lay between cooridnate space lines). | 307 // lay between cooridnate space lines). |
| 300 CGFloat insetSize = 1 - lineWidth / 2.; | 308 CGFloat insetSize = 1 - singlePixelLineWidth_ / 2.; |
| 301 if (isModeMaterial && showingFirstResponder && !inDarkMode) { | 309 if (isModeMaterial && showingFirstResponder && !inDarkMode) { |
| 302 insetSize++; | 310 insetSize++; |
| 303 } else if (!isModeMaterial) { | 311 } else if (!isModeMaterial) { |
| 304 insetSize = lineWidth == 0.5 ? 1.5 : 2.0; | 312 insetSize = singlePixelLineWidth_ == 0.5 ? 1.5 : 2.0; |
| 305 } | 313 } |
| 306 | 314 |
| 307 // Compute the border's bezier path. | 315 // Compute the border's bezier path. |
| 308 NSRect pathRect = NSInsetRect(frame, insetSize, insetSize); | 316 NSRect pathRect = NSInsetRect(frame, insetSize, insetSize); |
| 309 // In dark mode, make room for a shadow beneath the bottom edge. | 317 // In dark mode, make room for a shadow beneath the bottom edge. |
| 310 if (inDarkMode && isModeMaterial) { | 318 if (inDarkMode && isModeMaterial) { |
| 311 pathRect.size.height -= lineWidth; | 319 pathRect.size.height -= singlePixelLineWidth_; |
| 312 } | 320 } |
| 313 NSBezierPath* path = | 321 NSBezierPath* path = |
| 314 [NSBezierPath bezierPathWithRoundedRect:pathRect | 322 [NSBezierPath bezierPathWithRoundedRect:pathRect |
| 315 xRadius:kCornerRadius | 323 xRadius:kCornerRadius |
| 316 yRadius:kCornerRadius]; | 324 yRadius:kCornerRadius]; |
| 317 if (isModeMaterial) { | 325 if (isModeMaterial) { |
| 318 [path setLineWidth:showingFirstResponder ? lineWidth * 2 : lineWidth]; | 326 [path setLineWidth:showingFirstResponder ? singlePixelLineWidth_ * 2 |
| 327 : singlePixelLineWidth_]; |
| 319 } | 328 } |
| 320 | 329 |
| 321 // Fill the background. | 330 // Fill the background. |
| 322 [[self backgroundColor] set]; | 331 [[self backgroundColor] set]; |
| 323 if (isPopupMode_) { | 332 if (isPopupMode_) { |
| 324 NSRectFillUsingOperation(NSInsetRect(frame, 1, 1), NSCompositeSourceOver); | 333 NSRectFillUsingOperation(NSInsetRect(frame, 1, 1), NSCompositeSourceOver); |
| 325 } else { | 334 } else { |
| 326 [path fill]; | 335 [path fill]; |
| 327 } | 336 } |
| 328 | 337 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 353 [[NSColor colorWithCalibratedWhite:71 / 255. alpha:1] set]; | 362 [[NSColor colorWithCalibratedWhite:71 / 255. alpha:1] set]; |
| 354 [NSBezierPath clipRect:NSMakeRect(NSMinX(frame), NSMinY(frame), | 363 [NSBezierPath clipRect:NSMakeRect(NSMinX(frame), NSMinY(frame), |
| 355 NSWidth(frame), 3)]; | 364 NSWidth(frame), 3)]; |
| 356 [path stroke]; | 365 [path stroke]; |
| 357 } | 366 } |
| 358 | 367 |
| 359 // Draw a highlight beneath the top edge, and a shadow beneath the bottom | 368 // Draw a highlight beneath the top edge, and a shadow beneath the bottom |
| 360 // edge. | 369 // edge. |
| 361 { | 370 { |
| 362 gfx::ScopedNSGraphicsContextSaveGState saveState; | 371 gfx::ScopedNSGraphicsContextSaveGState saveState; |
| 363 [NSBezierPath setDefaultLineWidth:lineWidth]; | 372 [NSBezierPath setDefaultLineWidth:singlePixelLineWidth_]; |
| 364 | 373 |
| 365 [[NSColor colorWithCalibratedWhite:120 / 255. alpha:1] set]; | 374 [[NSColor colorWithCalibratedWhite:120 / 255. alpha:1] set]; |
| 366 NSPoint origin = NSMakePoint(NSMinX(pathRect) + 3, | 375 NSPoint origin = NSMakePoint(NSMinX(pathRect) + 3, |
| 367 NSMinY(pathRect) + lineWidth); | 376 NSMinY(pathRect) + singlePixelLineWidth_); |
| 368 NSPoint destination = NSMakePoint(NSMaxX(pathRect) - 3, | 377 NSPoint destination = |
| 369 NSMinY(pathRect) + lineWidth); | 378 NSMakePoint(NSMaxX(pathRect) - 3, |
| 379 NSMinY(pathRect) + singlePixelLineWidth_); |
| 370 [NSBezierPath strokeLineFromPoint:origin | 380 [NSBezierPath strokeLineFromPoint:origin |
| 371 toPoint:destination]; | 381 toPoint:destination]; |
| 372 | 382 |
| 373 origin.y = destination.y = NSMaxY(pathRect) + lineWidth; | 383 origin.y = destination.y = NSMaxY(pathRect) + singlePixelLineWidth_; |
| 374 [[NSColor colorWithCalibratedWhite:69 / 255. alpha:1] set]; | 384 [[NSColor colorWithCalibratedWhite:69 / 255. alpha:1] set]; |
| 375 [NSBezierPath strokeLineFromPoint:origin | 385 [NSBezierPath strokeLineFromPoint:origin |
| 376 toPoint:destination]; | 386 toPoint:destination]; |
| 377 } | 387 } |
| 378 } | 388 } |
| 379 } else { | 389 } else { |
| 380 ui::DrawNinePartImage(frame, | 390 ui::DrawNinePartImage(frame, |
| 381 isPopupMode_ ? kPopupBorderImageIds | 391 isPopupMode_ ? kPopupBorderImageIds |
| 382 : kNormalBorderImageIds, | 392 : kNormalBorderImageIds, |
| 383 NSCompositeSourceOver, | 393 NSCompositeSourceOver, |
| 384 1.0, | 394 1.0, |
| 385 true); | 395 true); |
| 386 } | 396 } |
| 387 | 397 |
| 388 // Draw the interior contents. We do this after drawing the border as some | 398 // Draw the interior contents. We do this after drawing the border as some |
| 389 // of the interior controls draw over it. | 399 // of the interior controls draw over it. |
| 390 [self drawInteriorWithFrame:frame inView:controlView]; | 400 [self drawInteriorWithFrame:frame inView:controlView]; |
| 391 | 401 |
| 392 // Draw the focus ring. | 402 // Draw the focus ring. |
| 393 if (showingFirstResponder) { | 403 if (showingFirstResponder) { |
| 394 if (!isModeMaterial) { | 404 if (!isModeMaterial) { |
| 395 NSRect focusRingRect = NSInsetRect(frame, lineWidth, lineWidth); | 405 NSRect focusRingRect = |
| 406 NSInsetRect(frame, singlePixelLineWidth_, singlePixelLineWidth_); |
| 396 path = [NSBezierPath bezierPathWithRoundedRect:focusRingRect | 407 path = [NSBezierPath bezierPathWithRoundedRect:focusRingRect |
| 397 xRadius:kCornerRadius | 408 xRadius:kCornerRadius |
| 398 yRadius:kCornerRadius]; | 409 yRadius:kCornerRadius]; |
| 399 [path setLineWidth:lineWidth * 2.0]; | 410 [path setLineWidth:singlePixelLineWidth_ * 2.0]; |
| 400 } | 411 } |
| 401 | 412 |
| 402 CGFloat alphaComponent = 0.5 / lineWidth; | 413 CGFloat alphaComponent = 0.5 / singlePixelLineWidth_; |
| 403 if (isModeMaterial && inDarkMode) { | 414 if (isModeMaterial && inDarkMode) { |
| 404 alphaComponent = 1; | 415 alphaComponent = 1; |
| 405 } | 416 } |
| 406 [[[NSColor keyboardFocusIndicatorColor] | 417 [[[NSColor keyboardFocusIndicatorColor] |
| 407 colorWithAlphaComponent:alphaComponent] set]; | 418 colorWithAlphaComponent:alphaComponent] set]; |
| 408 [path stroke]; | 419 [path stroke]; |
| 409 } | 420 } |
| 410 } | 421 } |
| 411 | 422 |
| 412 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 423 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 753 | 764 |
| 754 - (void)handleFocusEvent:(NSEvent*)event | 765 - (void)handleFocusEvent:(NSEvent*)event |
| 755 ofView:(AutocompleteTextField*)controlView { | 766 ofView:(AutocompleteTextField*)controlView { |
| 756 if ([controlView observer]) { | 767 if ([controlView observer]) { |
| 757 const bool controlDown = ([event modifierFlags] & NSControlKeyMask) != 0; | 768 const bool controlDown = ([event modifierFlags] & NSControlKeyMask) != 0; |
| 758 [controlView observer]->OnSetFocus(controlDown); | 769 [controlView observer]->OnSetFocus(controlDown); |
| 759 } | 770 } |
| 760 } | 771 } |
| 761 | 772 |
| 762 @end | 773 @end |
| OLD | NEW |