|
| 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. |
| 4 | + |
| 5 | +#import "content/nw/src/browser/autofill_popup_base_view_cocoa.h" |
| 6 | + |
| 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" |
| 10 | + |
| 11 | +@implementation AutofillPopupBaseViewCocoa |
| 12 | + |
| 13 | +#pragma mark - |
| 14 | +#pragma mark Colors |
| 15 | + |
| 16 | +- (NSColor*)backgroundColor { |
| 17 | + return [NSColor whiteColor]; |
| 18 | +} |
| 19 | + |
| 20 | +- (NSColor*)borderColor { |
| 21 | + return [NSColor colorForControlTint:[NSColor currentControlTint]]; |
| 22 | +} |
| 23 | + |
| 24 | +- (NSColor*)highlightColor { |
| 25 | + return [NSColor selectedControlColor]; |
| 26 | +} |
| 27 | + |
| 28 | +- (NSColor*)nameColor { |
| 29 | + return [NSColor blackColor]; |
| 30 | +} |
| 31 | + |
| 32 | +- (NSColor*)separatorColor { |
| 33 | + return [NSColor colorWithCalibratedWhite:220 / 255.0 alpha:1]; |
| 34 | +} |
| 35 | + |
| 36 | +- (NSColor*)subtextColor { |
| 37 | + return [NSColor grayColor]; |
| 38 | +} |
| 39 | + |
| 40 | +- (NSColor*)warningColor { |
| 41 | + return [NSColor grayColor]; |
| 42 | +} |
| 43 | + |
| 44 | +#pragma mark - |
| 45 | +#pragma mark Public methods |
| 46 | + |
| 47 | +- (id)initWithDelegate:(autofill::AutofillPopupViewDelegate*)delegate |
| 48 | + frame:(NSRect)frame { |
| 49 | + self = [super initWithFrame:frame]; |
| 50 | + if (self) |
| 51 | + delegate_ = delegate; |
| 52 | + |
| 53 | + return self; |
| 54 | +} |
| 55 | + |
| 56 | +- (void)delegateDestroyed { |
| 57 | + delegate_ = NULL; |
| 58 | +} |
| 59 | + |
| 60 | +- (void)drawSeparatorWithBounds:(NSRect)bounds { |
| 61 | + [[self separatorColor] set]; |
| 62 | + [NSBezierPath fillRect:bounds]; |
| 63 | +} |
| 64 | + |
| 65 | +// A slight optimization for drawing: |
| 66 | +// https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaViewsGuide/Optimizing/Optimizing.html |
| 67 | +- (BOOL)isOpaque { |
| 68 | + return YES; |
| 69 | +} |
| 70 | + |
| 71 | +- (BOOL)isFlipped { |
| 72 | + // Flipped so that it's easier to share controller logic with other OSes. |
| 73 | + return YES; |
| 74 | +} |
| 75 | + |
| 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]; |
| 85 | + [path fill]; |
| 86 | + [path setLineWidth:autofill::kPopupBorderThickness]; |
| 87 | + [[self borderColor] setStroke]; |
| 88 | + [path stroke]; |
| 89 | +} |
| 90 | + |
| 91 | +- (void)mouseUp:(NSEvent*)theEvent { |
| 92 | + // If the view is in the process of being destroyed, abort. |
| 93 | + if (!delegate_) |
| 94 | + return; |
| 95 | + |
| 96 | + // Only accept single-click. |
| 97 | + if ([theEvent clickCount] > 1) |
| 98 | + return; |
| 99 | + |
| 100 | + NSPoint location = [self convertPoint:[theEvent locationInWindow] |
| 101 | + fromView:nil]; |
| 102 | + |
| 103 | + if (NSPointInRect(location, [self bounds])) { |
| 104 | + delegate_->SetSelectionAtPoint(gfx::Point(NSPointToCGPoint(location))); |
| 105 | + delegate_->AcceptSelectedLine(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +- (void)mouseMoved:(NSEvent*)theEvent { |
| 110 | + // If the view is in the process of being destroyed, abort. |
| 111 | + if (!delegate_) |
| 112 | + return; |
| 113 | + |
| 114 | + NSPoint location = [self convertPoint:[theEvent locationInWindow] |
| 115 | + fromView:nil]; |
| 116 | + |
| 117 | + delegate_->SetSelectionAtPoint(gfx::Point(NSPointToCGPoint(location))); |
| 118 | +} |
| 119 | + |
| 120 | +- (void)mouseDragged:(NSEvent*)theEvent { |
| 121 | + [self mouseMoved:theEvent]; |
| 122 | +} |
| 123 | + |
| 124 | +- (void)mouseExited:(NSEvent*)theEvent { |
| 125 | + // If the view is in the process of being destroyed, abort. |
| 126 | + if (!delegate_) |
| 127 | + return; |
| 128 | + |
| 129 | + delegate_->SelectionCleared(); |
| 130 | +} |
| 131 | + |
| 132 | +#pragma mark - |
| 133 | +#pragma mark Messages from AutofillPopupViewBridge: |
| 134 | + |
| 135 | +- (void)updateBoundsAndRedrawPopup { |
| 136 | + NSRect frame = NSRectFromCGRect(delegate_->popup_bounds().ToCGRect()); |
| 137 | + |
| 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); |
| 144 | + |
| 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]; |
| 149 | +} |
| 150 | + |
| 151 | +- (void)showPopup { |
| 152 | + NSWindow* window = |
| 153 | + [[NSWindow alloc] initWithContentRect:ui::kWindowSizeDeterminedLater |
| 154 | + styleMask:NSBorderlessWindowMask |
| 155 | + backing:NSBackingStoreBuffered |
| 156 | + defer:YES]; |
| 157 | + [window setContentView:self]; |
| 158 | + |
| 159 | + // Telling Cocoa that the window is opaque enables some drawing optimizations. |
| 160 | + [window setOpaque:YES]; |
| 161 | + |
| 162 | + [self updateBoundsAndRedrawPopup]; |
| 163 | + [[delegate_->container_view() window] addChildWindow:window |
| 164 | + ordered:NSWindowAbove]; |
| 165 | +} |
| 166 | + |
| 167 | +- (void)hidePopup { |
| 168 | + // Remove the child window before closing, otherwise it can mess up |
| 169 | + // display ordering. |
| 170 | + NSWindow* window = [self window]; |
| 171 | + [[window parentWindow] removeChildWindow:window]; |
| 172 | + [window close]; |
| 173 | +} |
| 174 | + |
| 175 | +@end |
0 commit comments