OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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*)separatorColor { | |
25 return [NSColor colorWithCalibratedWhite:220 / 255.0 alpha:1]; | |
26 } | |
27 | |
28 - (NSColor*)highlightColor { | |
29 return [NSColor selectedControlColor]; | |
30 } | |
31 | |
32 - (NSColor*)nameColor { | |
33 return [NSColor blackColor]; | |
34 } | |
35 | |
36 - (NSColor*)warningColor { | |
37 return [NSColor grayColor]; | |
38 } | |
39 | |
40 - (NSColor*)subtextColor { | |
41 return [NSColor grayColor]; | |
42 } | |
43 | |
44 #pragma mark - | |
45 #pragma mark Public methods | |
46 | |
47 - (id)initWithAutofillPopupViewDelegate: | |
48 (autofill::AutofillPopupViewDelegate*)delegate frame:(NSRect)frame { | |
49 self = [super initWithFrame:frame]; | |
50 if (self) | |
51 delegate_ = delegate; | |
52 | |
53 return self; | |
54 } | |
55 | |
56 - (void)controllerDestroyed { | |
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)drawBackgroundAndBorderInRect:(NSRect)dirtyRect { | |
Ilya Sherman
2014/05/09 21:51:28
nit: Doesn't look like dirtyRect is used.
Patrick Dubroy
2014/05/12 14:13:39
I just removed the parameter altogether, since the
| |
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 [self controllerDestroyed]; | |
165 | |
166 // Remove the child window before closing, otherwise it can mess up | |
167 // display ordering. | |
168 NSWindow* window = [self window]; | |
169 [[window parentWindow] removeChildWindow:window]; | |
170 [window close]; | |
171 } | |
172 | |
173 @end | |
OLD | NEW |