OLD | NEW |
(Empty) | |
| 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 "chrome/browser/ui/cocoa/autofill/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/Cocoa
ViewsGuide/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 NSPoint location = [self convertPoint:[theEvent locationInWindow] |
| 97 fromView:nil]; |
| 98 |
| 99 if (NSPointInRect(location, [self bounds])) { |
| 100 delegate_->SetSelectionAtPoint(gfx::Point(NSPointToCGPoint(location))); |
| 101 delegate_->AcceptSelectedLine(); |
| 102 } |
| 103 } |
| 104 |
| 105 - (void)mouseMoved:(NSEvent*)theEvent { |
| 106 // If the view is in the process of being destroyed, abort. |
| 107 if (!delegate_) |
| 108 return; |
| 109 |
| 110 NSPoint location = [self convertPoint:[theEvent locationInWindow] |
| 111 fromView:nil]; |
| 112 |
| 113 delegate_->SetSelectionAtPoint(gfx::Point(NSPointToCGPoint(location))); |
| 114 } |
| 115 |
| 116 - (void)mouseDragged:(NSEvent*)theEvent { |
| 117 [self mouseMoved:theEvent]; |
| 118 } |
| 119 |
| 120 - (void)mouseExited:(NSEvent*)theEvent { |
| 121 // If the view is in the process of being destroyed, abort. |
| 122 if (!delegate_) |
| 123 return; |
| 124 |
| 125 delegate_->SelectionCleared(); |
| 126 } |
| 127 |
| 128 #pragma mark - |
| 129 #pragma mark Messages from AutofillPopupViewBridge: |
| 130 |
| 131 - (void)updateBoundsAndRedrawPopup { |
| 132 NSRect frame = NSRectFromCGRect(delegate_->popup_bounds().ToCGRect()); |
| 133 |
| 134 // Flip coordinates back into Cocoa-land. The controller's platform-neutral |
| 135 // coordinate space places the origin at the top-left of the first screen, |
| 136 // whereas Cocoa's coordinate space expects the origin to be at the |
| 137 // bottom-left of this same screen. |
| 138 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 139 frame.origin.y = NSMaxY([screen frame]) - NSMaxY(frame); |
| 140 |
| 141 // TODO(isherman): The view should support scrolling if the popup gets too |
| 142 // big to fit on the screen. |
| 143 [[self window] setFrame:frame display:YES]; |
| 144 [self setNeedsDisplay:YES]; |
| 145 } |
| 146 |
| 147 - (void)showPopup { |
| 148 NSWindow* window = |
| 149 [[NSWindow alloc] initWithContentRect:ui::kWindowSizeDeterminedLater |
| 150 styleMask:NSBorderlessWindowMask |
| 151 backing:NSBackingStoreBuffered |
| 152 defer:YES]; |
| 153 [window setContentView:self]; |
| 154 |
| 155 // Telling Cocoa that the window is opaque enables some drawing optimizations. |
| 156 [window setOpaque:YES]; |
| 157 |
| 158 [self updateBoundsAndRedrawPopup]; |
| 159 [[delegate_->container_view() window] addChildWindow:window |
| 160 ordered:NSWindowAbove]; |
| 161 } |
| 162 |
| 163 - (void)hidePopup { |
| 164 // Remove the child window before closing, otherwise it can mess up |
| 165 // display ordering. |
| 166 NSWindow* window = [self window]; |
| 167 [[window parentWindow] removeChildWindow:window]; |
| 168 [window close]; |
| 169 } |
| 170 |
| 171 @end |
OLD | NEW |