Chromium Code Reviews| 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)drawSeparatorWithBounds:(NSRect)bounds { | |
| 57 [[self separatorColor] set]; | |
| 58 [NSBezierPath fillRect:bounds]; | |
| 59 } | |
| 60 | |
| 61 // A slight optimization for drawing: | |
| 62 // https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Cocoa ViewsGuide/Optimizing/Optimizing.html | |
| 63 - (BOOL)isOpaque { | |
| 64 return YES; | |
| 65 } | |
| 66 | |
| 67 - (BOOL)isFlipped { | |
| 68 // Flipped so that it's easier to share controller logic with other OSes. | |
| 69 return YES; | |
| 70 } | |
| 71 | |
| 72 - (void)drawBackgroundAndBorder { | |
| 73 // The inset is needed since the border is centered on the |path|. | |
| 74 // TODO(isherman): We should consider using asset-based drawing for the | |
| 75 // border, creating simple bitmaps for the view's border and background, and | |
| 76 // drawing them using NSDrawNinePartImage(). | |
| 77 CGFloat inset = autofill::kPopupBorderThickness / 2.0; | |
| 78 NSRect borderRect = NSInsetRect([self bounds], inset, inset); | |
| 79 NSBezierPath* path = [NSBezierPath bezierPathWithRect:borderRect]; | |
| 80 [[self backgroundColor] setFill]; | |
| 81 [path fill]; | |
| 82 [path setLineWidth:autofill::kPopupBorderThickness]; | |
| 83 [[self borderColor] setStroke]; | |
| 84 [path stroke]; | |
| 85 } | |
| 86 | |
| 87 - (void)mouseUp:(NSEvent*)theEvent { | |
| 88 // If the view is in the process of being destroyed, abort. | |
| 89 if (!delegate_) | |
| 90 return; | |
|
Ilya Sherman
2014/05/12 21:46:10
This code is currently unreachable. I think that'
Patrick Dubroy
2014/05/13 12:18:05
Ok, I've added back controllerDestroyed.
| |
| 91 | |
| 92 NSPoint location = [self convertPoint:[theEvent locationInWindow] | |
| 93 fromView:nil]; | |
| 94 | |
| 95 if (NSPointInRect(location, [self bounds])) { | |
| 96 delegate_->SetSelectionAtPoint(gfx::Point(NSPointToCGPoint(location))); | |
| 97 delegate_->AcceptSelectedLine(); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 - (void)mouseMoved:(NSEvent*)theEvent { | |
| 102 // If the view is in the process of being destroyed, abort. | |
| 103 if (!delegate_) | |
| 104 return; | |
| 105 | |
| 106 NSPoint location = [self convertPoint:[theEvent locationInWindow] | |
| 107 fromView:nil]; | |
| 108 | |
| 109 delegate_->SetSelectionAtPoint(gfx::Point(NSPointToCGPoint(location))); | |
| 110 } | |
| 111 | |
| 112 - (void)mouseDragged:(NSEvent*)theEvent { | |
| 113 [self mouseMoved:theEvent]; | |
| 114 } | |
| 115 | |
| 116 - (void)mouseExited:(NSEvent*)theEvent { | |
| 117 // If the view is in the process of being destroyed, abort. | |
| 118 if (!delegate_) | |
| 119 return; | |
| 120 | |
| 121 delegate_->SelectionCleared(); | |
| 122 } | |
| 123 | |
| 124 #pragma mark - | |
| 125 #pragma mark Messages from AutofillPopupViewBridge: | |
| 126 | |
| 127 - (void)updateBoundsAndRedrawPopup { | |
| 128 NSRect frame = NSRectFromCGRect(delegate_->popup_bounds().ToCGRect()); | |
| 129 | |
| 130 // Flip coordinates back into Cocoa-land. The controller's platform-neutral | |
| 131 // coordinate space places the origin at the top-left of the first screen, | |
| 132 // whereas Cocoa's coordinate space expects the origin to be at the | |
| 133 // bottom-left of this same screen. | |
| 134 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | |
| 135 frame.origin.y = NSMaxY([screen frame]) - NSMaxY(frame); | |
| 136 | |
| 137 // TODO(isherman): The view should support scrolling if the popup gets too | |
| 138 // big to fit on the screen. | |
| 139 [[self window] setFrame:frame display:YES]; | |
| 140 [self setNeedsDisplay:YES]; | |
| 141 } | |
| 142 | |
| 143 - (void)showPopup { | |
| 144 NSWindow* window = | |
| 145 [[NSWindow alloc] initWithContentRect:ui::kWindowSizeDeterminedLater | |
| 146 styleMask:NSBorderlessWindowMask | |
| 147 backing:NSBackingStoreBuffered | |
| 148 defer:YES]; | |
| 149 [window setContentView:self]; | |
| 150 | |
| 151 // Telling Cocoa that the window is opaque enables some drawing optimizations. | |
| 152 [window setOpaque:YES]; | |
| 153 | |
| 154 [self updateBoundsAndRedrawPopup]; | |
| 155 [[delegate_->container_view() window] addChildWindow:window | |
| 156 ordered:NSWindowAbove]; | |
| 157 } | |
| 158 | |
| 159 - (void)hidePopup { | |
| 160 // Remove the child window before closing, otherwise it can mess up | |
| 161 // display ordering. | |
| 162 NSWindow* window = [self window]; | |
| 163 [[window parentWindow] removeChildWindow:window]; | |
| 164 [window close]; | |
| 165 } | |
| 166 | |
| 167 @end | |
| OLD | NEW |