1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "chrome/browser/ui/cocoa/autofill/autofill_section_view.h"
6
7#import "chrome/browser/ui/chrome_style.h"
8#include "skia/ext/skia_utils_mac.h"
9
10namespace {
11
12// Slight shading for mouse hover.
13SkColor kShadingColor = 0x07000000;  // SkColorSetARGB(7, 0, 0, 0);
14
15}  // namespace
16
17@implementation AutofillSectionView
18
19@synthesize clickTarget = clickTarget_;
20@synthesize shouldHighlightOnHover = shouldHighlightOnHover_;
21@synthesize isHighlighted = isHighlighted_;
22
23- (void)updateHoverState {
24  NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream];
25  mouseLoc = [self convertPoint:mouseLoc fromView:nil];
26  [self setIsHighlighted:NSPointInRect(mouseLoc, [self bounds])];
27}
28
29- (void)mouseEvent:(NSEvent*)event {
30  if ([event type] == NSMouseExited)
31    [self setIsHighlighted:NO];
32  else if ([event type] == NSMouseEntered)
33    [self setIsHighlighted:YES];
34  else if ([event type] == NSLeftMouseDown)
35    [clickTarget_ performClick:clickTarget_];
36}
37
38- (void)drawRect:(NSRect)dirtyRect {
39  if (shouldHighlightOnHover_ && isHighlighted_) {
40    [[self hoverColor] set];
41    NSRectFill([self bounds]);
42  }
43}
44
45- (NSColor*)hoverColor {
46  // Shading color is specified as a alpha component color, so premultiply.
47  NSColor* shadingColor = gfx::SkColorToCalibratedNSColor(kShadingColor);
48  NSColor* blendedColor = [[[self window] backgroundColor]
49      blendedColorWithFraction:[shadingColor alphaComponent]
50                       ofColor:shadingColor];
51  return [blendedColor colorWithAlphaComponent:1.0];
52}
53
54- (void)setShouldHighlightOnHover:(BOOL)shouldHighlight {
55  if (shouldHighlight == shouldHighlightOnHover_)
56    return;
57  shouldHighlightOnHover_ = shouldHighlight;
58  [self setNeedsDisplay:YES];
59}
60
61- (void)setIsHighlighted:(BOOL)isHighlighted {
62  isHighlighted_ = isHighlighted;
63  if (shouldHighlightOnHover_)
64    [self setNeedsDisplay:YES];
65}
66
67@end
68