1 // Copyright 2014 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.
5 #import "chrome/browser/ui/cocoa/autofill/autofill_popup_base_view_cocoa.h"
7 #include "chrome/browser/ui/autofill/autofill_popup_view_delegate.h"
8 #include "chrome/browser/ui/autofill/popup_constants.h"
9 #include "ui/base/cocoa/window_size_constants.h"
11 @implementation AutofillPopupBaseViewCocoa
16 - (NSColor*)backgroundColor {
17 return [NSColor whiteColor];
20 - (NSColor*)borderColor {
21 return [NSColor colorForControlTint:[NSColor currentControlTint]];
24 - (NSColor*)highlightColor {
25 return [NSColor selectedControlColor];
28 - (NSColor*)nameColor {
29 return [NSColor blackColor];
32 - (NSColor*)separatorColor {
33 return [NSColor colorWithCalibratedWhite:220 / 255.0 alpha:1];
36 - (NSColor*)subtextColor {
37 return [NSColor grayColor];
40 - (NSColor*)warningColor {
41 return [NSColor grayColor];
45 #pragma mark Public methods
47 - (id)initWithDelegate:(autofill::AutofillPopupViewDelegate*)delegate
49 self = [super initWithFrame:frame];
56 - (void)delegateDestroyed {
60 - (void)drawSeparatorWithBounds:(NSRect)bounds {
61 [[self separatorColor] set];
62 [NSBezierPath fillRect:bounds];
65 // A slight optimization for drawing:
66 // https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaViewsGuide/Optimizing/Optimizing.html
72 // Flipped so that it's easier to share controller logic with other OSes.
76 - (void)drawBackgroundAndBorder {
77 // The inset is needed since the border is centered on the |path|.
78 // TODO(isherman): We should consider using asset-based drawing for the
79 // border, creating simple bitmaps for the view's border and background, and
80 // drawing them using NSDrawNinePartImage().
81 CGFloat inset = autofill::kPopupBorderThickness / 2.0;
82 NSRect borderRect = NSInsetRect([self bounds], inset, inset);
83 NSBezierPath* path = [NSBezierPath bezierPathWithRect:borderRect];
84 [[self backgroundColor] setFill];
86 [path setLineWidth:autofill::kPopupBorderThickness];
87 [[self borderColor] setStroke];
91 - (void)mouseUp:(NSEvent*)theEvent {
92 // If the view is in the process of being destroyed, abort.
96 // Only accept single-click.
97 if ([theEvent clickCount] > 1)
100 NSPoint location = [self convertPoint:[theEvent locationInWindow]
103 if (NSPointInRect(location, [self bounds])) {
104 delegate_->SetSelectionAtPoint(gfx::Point(NSPointToCGPoint(location)));
105 delegate_->AcceptSelectedLine();
109 - (void)mouseMoved:(NSEvent*)theEvent {
110 // If the view is in the process of being destroyed, abort.
114 NSPoint location = [self convertPoint:[theEvent locationInWindow]
117 delegate_->SetSelectionAtPoint(gfx::Point(NSPointToCGPoint(location)));
120 - (void)mouseDragged:(NSEvent*)theEvent {
121 [self mouseMoved:theEvent];
124 - (void)mouseExited:(NSEvent*)theEvent {
125 // If the view is in the process of being destroyed, abort.
129 delegate_->SelectionCleared();
133 #pragma mark Messages from AutofillPopupViewBridge:
135 - (void)updateBoundsAndRedrawPopup {
136 NSRect frame = NSRectFromCGRect(delegate_->popup_bounds().ToCGRect());
138 // Flip coordinates back into Cocoa-land. The controller's platform-neutral
139 // coordinate space places the origin at the top-left of the first screen,
140 // whereas Cocoa's coordinate space expects the origin to be at the
141 // bottom-left of this same screen.
142 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
143 frame.origin.y = NSMaxY([screen frame]) - NSMaxY(frame);
145 // TODO(isherman): The view should support scrolling if the popup gets too
146 // big to fit on the screen.
147 [[self window] setFrame:frame display:YES];
148 [self setNeedsDisplay:YES];
153 [[NSWindow alloc] initWithContentRect:ui::kWindowSizeDeterminedLater
154 styleMask:NSBorderlessWindowMask
155 backing:NSBackingStoreBuffered
157 [window setContentView:self];
159 // Telling Cocoa that the window is opaque enables some drawing optimizations.
160 [window setOpaque:YES];
162 [self updateBoundsAndRedrawPopup];
163 [[delegate_->container_view() window] addChildWindow:window
164 ordered:NSWindowAbove];
168 // Remove the child window before closing, otherwise it can mess up
170 NSWindow* window = [self window];
171 [[window parentWindow] removeChildWindow:window];