OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" | 5 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
22 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSAnimation+Duration.h " | 22 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSAnimation+Duration.h " |
23 #import "ui/base/cocoa/cocoa_base_utils.h" | 23 #import "ui/base/cocoa/cocoa_base_utils.h" |
24 #import "ui/base/cocoa/flipped_view.h" | 24 #import "ui/base/cocoa/flipped_view.h" |
25 #include "ui/base/cocoa/window_size_constants.h" | 25 #include "ui/base/cocoa/window_size_constants.h" |
26 #include "ui/gfx/geometry/rect.h" | 26 #include "ui/gfx/geometry/rect.h" |
27 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 27 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
28 #include "ui/gfx/text_elider.h" | 28 #include "ui/gfx/text_elider.h" |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 // How much to adjust the cell sizing up from the default determined | |
33 // by the font. | |
34 const CGFloat kCellHeightAdjust = 6.0; | |
35 | |
36 // Padding between matrix and the top and bottom of the popup window. | 32 // Padding between matrix and the top and bottom of the popup window. |
37 const CGFloat kPopupPaddingVertical = 5.0; | 33 const CGFloat kPopupPaddingVertical = 5.0; |
38 | 34 |
39 // Animation duration when animating the popup window smaller. | 35 // Animation duration when animating the popup window smaller. |
40 const NSTimeInterval kShrinkAnimationDuration = 0.1; | 36 const NSTimeInterval kShrinkAnimationDuration = 0.1; |
41 | 37 |
42 // Background colors for different states of the popup elements. | 38 // Background colors for different states of the popup elements. |
43 NSColor* BackgroundColor() { | 39 NSColor* BackgroundColor() { |
44 return [NSColor controlBackgroundColor]; | 40 return [NSColor controlBackgroundColor]; |
45 } | 41 } |
(...skipping 30 matching lines...) Expand all Loading... | |
76 DCHECK([NSThread isMainThread]); | 72 DCHECK([NSThread isMainThread]); |
77 const AutocompleteResult& result = GetResult(); | 73 const AutocompleteResult& result = GetResult(); |
78 const size_t start_match = result.ShouldHideTopMatch() ? 1 : 0; | 74 const size_t start_match = result.ShouldHideTopMatch() ? 1 : 0; |
79 const size_t rows = result.size() - start_match; | 75 const size_t rows = result.size() - start_match; |
80 if (rows == 0) { | 76 if (rows == 0) { |
81 [[popup_ parentWindow] removeChildWindow:popup_]; | 77 [[popup_ parentWindow] removeChildWindow:popup_]; |
82 [popup_ orderOut:nil]; | 78 [popup_ orderOut:nil]; |
83 | 79 |
84 // Break references to |this| because the popup may not be | 80 // Break references to |this| because the popup may not be |
85 // deallocated immediately. | 81 // deallocated immediately. |
86 [matrix_ setDelegate:nil]; | 82 [matrix_ setObserver:NULL]; |
87 matrix_.reset(); | 83 matrix_.reset(); |
88 | 84 |
89 popup_.reset(nil); | 85 popup_.reset(nil); |
90 | 86 |
91 target_popup_frame_ = NSZeroRect; | 87 target_popup_frame_ = NSZeroRect; |
92 | 88 |
93 return; | 89 return; |
94 } | 90 } |
95 | 91 |
96 CreatePopupIfNeeded(); | 92 CreatePopupIfNeeded(); |
97 | 93 [matrix_ setController:[[OmniboxPopupTableController alloc] |
98 // Calculate the width of the matrix based on backing out the popup's border | 94 initWithMatchResults:result |
99 // from the width of the field. | 95 popupView:*this]]; |
Scott Hess - ex-Googler
2015/05/21 20:40:27
I don't think anyone has the owning reference to O
dschuyler
2015/05/26 18:40:21
Done.
| |
100 const CGFloat matrix_width = NSWidth([field_ bounds]); | |
101 DCHECK_GT(matrix_width, 0.0); | |
102 | |
103 // Load the results into the popup's matrix. | |
104 DCHECK_GT(rows, 0U); | |
105 [matrix_ renewRows:rows columns:1]; | |
106 CGFloat max_match_contents_width = 0.0f; | |
107 CGFloat contents_offset = -1.0f; | |
108 for (size_t ii = 0; ii < rows; ++ii) { | |
109 OmniboxPopupCell* cell = [matrix_ cellAtRow:ii column:0]; | |
110 const AutocompleteMatch& match = GetResult().match_at(ii + start_match); | |
111 [cell setImage:ImageForMatch(match)]; | |
112 [cell setMatch:match]; | |
113 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { | |
114 max_match_contents_width = std::max(max_match_contents_width, | |
115 [cell getMatchContentsWidth]); | |
116 if (contents_offset < 0.0f) { | |
117 contents_offset = [OmniboxPopupCell computeContentsOffset:match]; | |
118 } | |
119 [cell setContentsOffset:contents_offset]; | |
120 } | |
121 } | |
122 | |
123 for (size_t ii = 0; ii < rows; ++ii) { | |
124 OmniboxPopupCell* cell = [matrix_ cellAtRow:ii column:0]; | |
125 [cell setMaxMatchContentsWidth:max_match_contents_width]; | |
126 } | |
127 | |
128 // Set the cell size to fit a line of text in the cell's font. All | |
129 // cells should use the same font and each should layout in one | |
130 // line, so they should all be about the same height. | |
131 const NSSize cell_size = [[matrix_ cellAtRow:0 column:0] cellSize]; | |
132 DCHECK_GT(cell_size.height, 0.0); | |
133 const CGFloat cell_height = cell_size.height + kCellHeightAdjust; | |
134 [matrix_ setCellSize:NSMakeSize(matrix_width, cell_height)]; | |
135 | 96 |
136 // Update the selection before placing (and displaying) the window. | 97 // Update the selection before placing (and displaying) the window. |
137 PaintUpdatesNow(); | 98 PaintUpdatesNow(); |
138 | 99 |
139 // Calculate the matrix size manually rather than using -sizeToCells | 100 // Calculate the matrix size manually rather than using -sizeToCells |
140 // because actually resizing the matrix messed up the popup size | 101 // because actually resizing the matrix messed up the popup size |
141 // animation. | 102 // animation. |
142 DCHECK_EQ([matrix_ intercellSpacing].height, 0.0); | 103 DCHECK_EQ([matrix_ intercellSpacing].height, 0.0); |
143 PositionPopup(rows * cell_height); | 104 PositionPopup(NSHeight([matrix_ frame])); |
144 } | 105 } |
145 | 106 |
146 gfx::Rect OmniboxPopupViewMac::GetTargetBounds() { | 107 gfx::Rect OmniboxPopupViewMac::GetTargetBounds() { |
147 // Flip the coordinate system before returning. | 108 // Flip the coordinate system before returning. |
148 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | 109 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
149 NSRect monitor_frame = [screen frame]; | 110 NSRect monitor_frame = [screen frame]; |
150 gfx::Rect bounds(NSRectToCGRect(target_popup_frame_)); | 111 gfx::Rect bounds(NSRectToCGRect(target_popup_frame_)); |
151 bounds.set_y(monitor_frame.size.height - bounds.y() - bounds.height()); | 112 bounds.set_y(monitor_frame.size.height - bounds.y() - bounds.height()); |
152 return bounds; | 113 return bounds; |
153 } | 114 } |
154 | 115 |
155 // This is only called by model in SetSelectedLine() after updating | 116 // This is only called by model in SetSelectedLine() after updating |
156 // everything. Popup should already be visible. | 117 // everything. Popup should already be visible. |
157 void OmniboxPopupViewMac::PaintUpdatesNow() { | 118 void OmniboxPopupViewMac::PaintUpdatesNow() { |
158 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; | 119 [matrix_ selectRowIndex:model_->selected_line()]; |
159 if (start_match > model_->selected_line()) { | |
160 [matrix_ deselectAllCells]; | |
161 } else { | |
162 [matrix_ selectCellAtRow:model_->selected_line() - start_match column:0]; | |
163 } | |
164 | |
165 } | 120 } |
166 | 121 |
167 void OmniboxPopupViewMac::OnMatrixRowSelected(OmniboxPopupMatrix* matrix, | 122 void OmniboxPopupViewMac::OnMatrixRowSelected(OmniboxPopupMatrix* matrix, |
168 size_t row) { | 123 size_t row) { |
169 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; | 124 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; |
170 model_->SetSelectedLine(row + start_match, false, false); | 125 model_->SetSelectedLine(row + start_match, false, false); |
171 } | 126 } |
172 | 127 |
173 void OmniboxPopupViewMac::OnMatrixRowClicked(OmniboxPopupMatrix* matrix, | 128 void OmniboxPopupViewMac::OnMatrixRowClicked(OmniboxPopupMatrix* matrix, |
174 size_t row) { | 129 size_t row) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 NSRect popup_frame = anchor_rect_base; | 190 NSRect popup_frame = anchor_rect_base; |
236 // Size to fit the matrix and shift down by the size. | 191 // Size to fit the matrix and shift down by the size. |
237 popup_frame.size.height = matrixHeight + kPopupPaddingVertical * 2.0; | 192 popup_frame.size.height = matrixHeight + kPopupPaddingVertical * 2.0; |
238 popup_frame.size.height += [OmniboxPopupTopSeparatorView preferredHeight]; | 193 popup_frame.size.height += [OmniboxPopupTopSeparatorView preferredHeight]; |
239 popup_frame.size.height += [OmniboxPopupBottomSeparatorView preferredHeight]; | 194 popup_frame.size.height += [OmniboxPopupBottomSeparatorView preferredHeight]; |
240 popup_frame.origin.y -= NSHeight(popup_frame); | 195 popup_frame.origin.y -= NSHeight(popup_frame); |
241 // Shift to screen coordinates. | 196 // Shift to screen coordinates. |
242 popup_frame.origin = | 197 popup_frame.origin = |
243 [[controller window] convertBaseToScreen:popup_frame.origin]; | 198 [[controller window] convertBaseToScreen:popup_frame.origin]; |
244 | 199 |
245 // Do nothing if the popup is already animating to the given |frame|. | |
246 if (NSEqualRects(popup_frame, target_popup_frame_)) | |
247 return; | |
Scott Hess - ex-Googler
2015/05/21 20:40:27
Did I already mention that this looks like somethi
dschuyler
2015/05/26 18:40:21
Acknowledged.
| |
248 | |
249 // Top separator. | 200 // Top separator. |
250 NSRect top_separator_frame = NSZeroRect; | 201 NSRect top_separator_frame = NSZeroRect; |
251 top_separator_frame.size.width = NSWidth(popup_frame); | 202 top_separator_frame.size.width = NSWidth(popup_frame); |
252 top_separator_frame.size.height = | 203 top_separator_frame.size.height = |
253 [OmniboxPopupTopSeparatorView preferredHeight]; | 204 [OmniboxPopupTopSeparatorView preferredHeight]; |
254 [top_separator_view_ setFrame:top_separator_frame]; | 205 [top_separator_view_ setFrame:top_separator_frame]; |
255 | 206 |
256 // Bottom separator. | 207 // Bottom separator. |
257 NSRect bottom_separator_frame = NSZeroRect; | 208 NSRect bottom_separator_frame = NSZeroRect; |
258 bottom_separator_frame.size.width = NSWidth(popup_frame); | 209 bottom_separator_frame.size.width = NSWidth(popup_frame); |
259 bottom_separator_frame.size.height = | 210 bottom_separator_frame.size.height = |
260 [OmniboxPopupBottomSeparatorView preferredHeight]; | 211 [OmniboxPopupBottomSeparatorView preferredHeight]; |
261 bottom_separator_frame.origin.y = | 212 bottom_separator_frame.origin.y = |
262 NSHeight(popup_frame) - NSHeight(bottom_separator_frame); | 213 NSHeight(popup_frame) - NSHeight(bottom_separator_frame); |
263 [bottom_separator_view_ setFrame:bottom_separator_frame]; | 214 [bottom_separator_view_ setFrame:bottom_separator_frame]; |
264 | 215 |
265 // Background view. | 216 // Background view. |
266 NSRect background_rect = NSZeroRect; | 217 NSRect background_rect = NSZeroRect; |
267 background_rect.size.width = NSWidth(popup_frame); | 218 background_rect.size.width = NSWidth(popup_frame); |
268 background_rect.size.height = NSHeight(popup_frame) - | 219 background_rect.size.height = NSHeight(popup_frame) - |
269 NSHeight(top_separator_frame) - NSHeight(bottom_separator_frame); | 220 NSHeight(top_separator_frame) - NSHeight(bottom_separator_frame); |
270 background_rect.origin.y = NSMaxY(top_separator_frame); | 221 background_rect.origin.y = NSMaxY(top_separator_frame); |
271 [background_view_ setFrame:background_rect]; | 222 [background_view_ setFrame:background_rect]; |
272 | 223 |
224 // Calculate the width of the table based on backing out the popup's border | |
225 // from the width of the field. | |
226 const CGFloat tableWidth = NSWidth([field_ bounds]); | |
227 DCHECK_GT(tableWidth, 0.0); | |
228 | |
273 // Matrix. | 229 // Matrix. |
274 NSPoint field_origin_base = | 230 NSPoint field_origin_base = |
275 [field_ convertPoint:[field_ bounds].origin toView:nil]; | 231 [field_ convertPoint:[field_ bounds].origin toView:nil]; |
276 NSRect matrix_frame = NSZeroRect; | 232 NSRect matrix_frame = NSZeroRect; |
277 matrix_frame.origin.x = field_origin_base.x - NSMinX(anchor_rect_base); | 233 matrix_frame.origin.x = field_origin_base.x - NSMinX(anchor_rect_base); |
278 matrix_frame.origin.y = kPopupPaddingVertical; | 234 matrix_frame.origin.y = kPopupPaddingVertical; |
279 matrix_frame.size.width = [matrix_ cellSize].width; | 235 matrix_frame.size.width = tableWidth; |
280 matrix_frame.size.height = matrixHeight; | 236 matrix_frame.size.height = matrixHeight; |
281 [matrix_ setFrame:matrix_frame]; | 237 [matrix_ setFrame:matrix_frame]; |
282 | 238 |
283 NSRect current_poup_frame = [popup_ frame]; | 239 NSRect current_poup_frame = [popup_ frame]; |
284 target_popup_frame_ = popup_frame; | 240 target_popup_frame_ = popup_frame; |
285 | 241 |
286 // Animate the frame change if the only change is that the height got smaller. | 242 // Animate the frame change if the only change is that the height got smaller. |
287 // Otherwise, resize immediately. | 243 // Otherwise, resize immediately. |
288 bool animate = (NSHeight(popup_frame) < NSHeight(current_poup_frame) && | 244 bool animate = (NSHeight(popup_frame) < NSHeight(current_poup_frame) && |
289 NSWidth(popup_frame) == NSWidth(current_poup_frame)); | 245 NSWidth(popup_frame) == NSWidth(current_poup_frame)); |
(...skipping 21 matching lines...) Expand all Loading... | |
311 if (!animate) { | 267 if (!animate) { |
312 // Restore the original animations dictionary. This does not reinstate any | 268 // Restore the original animations dictionary. This does not reinstate any |
313 // previously running animations. | 269 // previously running animations. |
314 [popup_ setAnimations:savedAnimations]; | 270 [popup_ setAnimations:savedAnimations]; |
315 } | 271 } |
316 | 272 |
317 if (![popup_ isVisible]) | 273 if (![popup_ isVisible]) |
318 [[field_ window] addChildWindow:popup_ ordered:NSWindowAbove]; | 274 [[field_ window] addChildWindow:popup_ ordered:NSWindowAbove]; |
319 } | 275 } |
320 | 276 |
321 NSImage* OmniboxPopupViewMac::ImageForMatch(const AutocompleteMatch& match) { | 277 NSImage* OmniboxPopupViewMac::ImageForMatch( |
278 const AutocompleteMatch& match) const { | |
Scott Hess - ex-Googler
2015/05/21 20:40:27
I was wondering earlier if this could just be pull
dschuyler
2015/05/26 18:40:21
I wondered about the same. In the scope of 'this
groby-ooo-7-16
2015/05/26 21:23:22
I think we've heaped on enough change :) Follow-up
dschuyler
2015/05/28 20:34:22
The bug is 492896
Done.
| |
322 gfx::Image image = model_->GetIconIfExtensionMatch(match); | 279 gfx::Image image = model_->GetIconIfExtensionMatch(match); |
323 if (!image.IsEmpty()) | 280 if (!image.IsEmpty()) |
324 return image.AsNSImage(); | 281 return image.AsNSImage(); |
325 | 282 |
326 const int resource_id = model_->IsStarredMatch(match) ? | 283 const int resource_id = model_->IsStarredMatch(match) ? |
327 IDR_OMNIBOX_STAR : AutocompleteMatch::TypeToIcon(match.type); | 284 IDR_OMNIBOX_STAR : AutocompleteMatch::TypeToIcon(match.type); |
328 return OmniboxViewMac::ImageForResource(resource_id); | 285 return OmniboxViewMac::ImageForResource(resource_id); |
329 } | 286 } |
330 | 287 |
331 void OmniboxPopupViewMac::OpenURLForRow(size_t row, | 288 void OmniboxPopupViewMac::OpenURLForRow(size_t row, |
332 WindowOpenDisposition disposition) { | 289 WindowOpenDisposition disposition) { |
333 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; | 290 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; |
334 row += start_match; | 291 row += start_match; |
335 DCHECK_LT(row, GetResult().size()); | 292 DCHECK_LT(row, GetResult().size()); |
336 omnibox_view_->OpenMatch(GetResult().match_at(row), disposition, GURL(), | 293 omnibox_view_->OpenMatch(GetResult().match_at(row), disposition, GURL(), |
337 base::string16(), row); | 294 base::string16(), row); |
338 } | 295 } |
OLD | NEW |