Chromium Code Reviews| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 DCHECK([NSThread isMainThread]); | 76 DCHECK([NSThread isMainThread]); |
| 77 const AutocompleteResult& result = GetResult(); | 77 const AutocompleteResult& result = GetResult(); |
| 78 const size_t start_match = result.ShouldHideTopMatch() ? 1 : 0; | 78 const size_t start_match = result.ShouldHideTopMatch() ? 1 : 0; |
| 79 const size_t rows = result.size() - start_match; | 79 const size_t rows = result.size() - start_match; |
| 80 if (rows == 0) { | 80 if (rows == 0) { |
| 81 [[popup_ parentWindow] removeChildWindow:popup_]; | 81 [[popup_ parentWindow] removeChildWindow:popup_]; |
| 82 [popup_ orderOut:nil]; | 82 [popup_ orderOut:nil]; |
| 83 | 83 |
| 84 // Break references to |this| because the popup may not be | 84 // Break references to |this| because the popup may not be |
| 85 // deallocated immediately. | 85 // deallocated immediately. |
| 86 [matrix_ setDelegate:nil]; | 86 [matrix_ setObserver:NULL]; |
| 87 matrix_.reset(); | 87 matrix_.reset(); |
| 88 | 88 |
| 89 popup_.reset(nil); | 89 popup_.reset(nil); |
| 90 | 90 |
| 91 target_popup_frame_ = NSZeroRect; | 91 target_popup_frame_ = NSZeroRect; |
| 92 | 92 |
| 93 return; | 93 return; |
| 94 } | 94 } |
| 95 | 95 |
| 96 CreatePopupIfNeeded(); | 96 CreatePopupIfNeeded(); |
| 97 | 97 |
| 98 // Calculate the width of the matrix based on backing out the popup's border | |
| 99 // from the width of the field. | |
| 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. | 98 // Load the results into the popup's matrix. |
| 104 DCHECK_GT(rows, 0U); | 99 DCHECK_GT(rows, 0U); |
| 105 [matrix_ renewRows:rows columns:1]; | 100 NSMutableArray* array = [NSMutableArray array]; |
|
Scott Hess - ex-Googler
2015/05/15 00:09:38
I'd also do scoped_nsobject<> of [[NSMutableArray
dschuyler
2015/05/15 21:52:04
Done.
| |
| 101 CGFloat popupHeight = 0; | |
| 102 NSRect cellRect = NSZeroRect; | |
| 103 cellRect.size.width = NSWidth([field_ bounds]); | |
| 106 CGFloat max_match_contents_width = 0.0f; | 104 CGFloat max_match_contents_width = 0.0f; |
| 107 CGFloat contents_offset = -1.0f; | 105 CGFloat contents_offset = -1.0f; |
| 108 for (size_t ii = 0; ii < rows; ++ii) { | 106 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); | 107 const AutocompleteMatch& match = GetResult().match_at(ii + start_match); |
| 111 [cell setImage:ImageForMatch(match)]; | 108 base::scoped_nsobject<OmniboxPopupCellData> cellData( |
| 112 [cell setMatch:match]; | 109 [[OmniboxPopupCellData alloc] initWithMatch:match]); |
| 110 cellRect.origin.y = popupHeight; | |
| 111 NSImage* image = ImageForMatch(match); | |
| 112 cellRect.size.height = [image size].height + kCellHeightAdjust; | |
| 113 [cellData setRect:cellRect]; | |
| 114 [cellData setImage:image]; | |
|
Scott Hess - ex-Googler
2015/05/15 00:09:38
Should these be individual setters, or should the
groby-ooo-7-16
2015/05/15 16:53:56
I'd prefer -initWithMatch:image - because that way
Scott Hess - ex-Googler
2015/05/15 17:28:59
I don't think it can get to zero setters, because
dschuyler
2015/05/15 21:52:04
Done.
| |
| 115 [array addObject:cellData]; | |
| 116 popupHeight += cellRect.size.height; | |
| 113 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { | 117 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { |
| 114 max_match_contents_width = std::max(max_match_contents_width, | 118 max_match_contents_width = |
| 115 [cell getMatchContentsWidth]); | 119 std::max(max_match_contents_width, [cellData getMatchContentsWidth]); |
| 116 if (contents_offset < 0.0f) { | 120 if (contents_offset < 0.0f) { |
| 117 contents_offset = [OmniboxPopupCell computeContentsOffset:match]; | 121 contents_offset = [OmniboxPopupCell computeContentsOffset:match]; |
|
Scott Hess - ex-Googler
2015/05/15 17:28:59
Does this helper still belong in the cell class, o
dschuyler
2015/05/15 21:52:04
It sounds like this search suggest tail feature is
Scott Hess - ex-Googler
2015/05/15 22:02:08
In that case I can definitely see not changing fro
dschuyler
2015/05/18 23:38:44
I've contacted Anuj about the fact that I'm making
| |
| 118 } | 122 } |
| 119 [cell setContentsOffset:contents_offset]; | 123 [cellData setContentsOffset:contents_offset]; |
| 120 } | 124 } |
| 121 } | 125 } |
| 122 | 126 |
| 123 for (size_t ii = 0; ii < rows; ++ii) { | 127 for (size_t ii = 0; ii < rows; ++ii) { |
| 124 OmniboxPopupCell* cell = [matrix_ cellAtRow:ii column:0]; | 128 OmniboxPopupCellData* cellData = [array objectAtIndex:ii]; |
| 125 [cell setMaxMatchContentsWidth:max_match_contents_width]; | 129 [cellData setMaxMatchContentsWidth:max_match_contents_width]; |
| 126 } | 130 } |
| 127 | 131 |
| 128 // Set the cell size to fit a line of text in the cell's font. All | 132 [matrix_ setDataArray:array]; |
| 129 // cells should use the same font and each should layout in one | 133 [matrix_ reloadData]; |
| 130 // line, so they should all be about the same height. | 134 popupHeight += [matrix_ intercellSpacing].height * (rows - 1); |
| 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 | 135 |
| 136 // Update the selection before placing (and displaying) the window. | 136 // Update the selection before placing (and displaying) the window. |
| 137 PaintUpdatesNow(); | 137 PaintUpdatesNow(); |
| 138 | 138 |
| 139 // Calculate the matrix size manually rather than using -sizeToCells | 139 // Calculate the matrix size manually rather than using -sizeToCells |
| 140 // because actually resizing the matrix messed up the popup size | 140 // because actually resizing the matrix messed up the popup size |
| 141 // animation. | 141 // animation. |
| 142 DCHECK_EQ([matrix_ intercellSpacing].height, 0.0); | 142 DCHECK_EQ([matrix_ intercellSpacing].height, 0.0); |
| 143 PositionPopup(rows * cell_height); | 143 PositionPopup(popupHeight); |
| 144 } | 144 } |
| 145 | 145 |
| 146 gfx::Rect OmniboxPopupViewMac::GetTargetBounds() { | 146 gfx::Rect OmniboxPopupViewMac::GetTargetBounds() { |
| 147 // Flip the coordinate system before returning. | 147 // Flip the coordinate system before returning. |
| 148 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | 148 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 149 NSRect monitor_frame = [screen frame]; | 149 NSRect monitor_frame = [screen frame]; |
| 150 gfx::Rect bounds(NSRectToCGRect(target_popup_frame_)); | 150 gfx::Rect bounds(NSRectToCGRect(target_popup_frame_)); |
| 151 bounds.set_y(monitor_frame.size.height - bounds.y() - bounds.height()); | 151 bounds.set_y(monitor_frame.size.height - bounds.y() - bounds.height()); |
| 152 return bounds; | 152 return bounds; |
| 153 } | 153 } |
| 154 | 154 |
| 155 // This is only called by model in SetSelectedLine() after updating | 155 // This is only called by model in SetSelectedLine() after updating |
| 156 // everything. Popup should already be visible. | 156 // everything. Popup should already be visible. |
| 157 void OmniboxPopupViewMac::PaintUpdatesNow() { | 157 void OmniboxPopupViewMac::PaintUpdatesNow() { |
| 158 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; | 158 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; |
| 159 if (start_match > model_->selected_line()) { | 159 NSIndexSet* indexSet = nil; |
| 160 [matrix_ deselectAllCells]; | 160 if (start_match <= model_->selected_line()) { |
| 161 indexSet = | |
| 162 [NSIndexSet indexSetWithIndex:model_->selected_line() - start_match]; | |
| 161 } else { | 163 } else { |
| 162 [matrix_ selectCellAtRow:model_->selected_line() - start_match column:0]; | 164 indexSet = [NSIndexSet indexSet]; |
| 163 } | 165 } |
|
Scott Hess - ex-Googler
2015/05/15 00:09:38
Nevermind my earlier (or later?) comment, this is
dschuyler
2015/05/15 21:52:04
Acknowledged.
| |
| 164 | 166 [matrix_ selectRowIndexes:indexSet byExtendingSelection:NO]; |
| 165 } | 167 } |
| 166 | 168 |
| 167 void OmniboxPopupViewMac::OnMatrixRowSelected(OmniboxPopupMatrix* matrix, | 169 void OmniboxPopupViewMac::OnMatrixRowSelected(OmniboxPopupMatrix* matrix, |
| 168 size_t row) { | 170 size_t row) { |
| 169 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; | 171 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; |
| 170 model_->SetSelectedLine(row + start_match, false, false); | 172 model_->SetSelectedLine(row + start_match, false, false); |
| 171 } | 173 } |
| 172 | 174 |
| 173 void OmniboxPopupViewMac::OnMatrixRowClicked(OmniboxPopupMatrix* matrix, | 175 void OmniboxPopupViewMac::OnMatrixRowClicked(OmniboxPopupMatrix* matrix, |
| 174 size_t row) { | 176 size_t row) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 NSRect popup_frame = anchor_rect_base; | 237 NSRect popup_frame = anchor_rect_base; |
| 236 // Size to fit the matrix and shift down by the size. | 238 // Size to fit the matrix and shift down by the size. |
| 237 popup_frame.size.height = matrixHeight + kPopupPaddingVertical * 2.0; | 239 popup_frame.size.height = matrixHeight + kPopupPaddingVertical * 2.0; |
| 238 popup_frame.size.height += [OmniboxPopupTopSeparatorView preferredHeight]; | 240 popup_frame.size.height += [OmniboxPopupTopSeparatorView preferredHeight]; |
| 239 popup_frame.size.height += [OmniboxPopupBottomSeparatorView preferredHeight]; | 241 popup_frame.size.height += [OmniboxPopupBottomSeparatorView preferredHeight]; |
| 240 popup_frame.origin.y -= NSHeight(popup_frame); | 242 popup_frame.origin.y -= NSHeight(popup_frame); |
| 241 // Shift to screen coordinates. | 243 // Shift to screen coordinates. |
| 242 popup_frame.origin = | 244 popup_frame.origin = |
| 243 [[controller window] convertBaseToScreen:popup_frame.origin]; | 245 [[controller window] convertBaseToScreen:popup_frame.origin]; |
| 244 | 246 |
| 245 // Do nothing if the popup is already animating to the given |frame|. | |
| 246 if (NSEqualRects(popup_frame, target_popup_frame_)) | |
| 247 return; | |
| 248 | |
| 249 // Top separator. | 247 // Top separator. |
| 250 NSRect top_separator_frame = NSZeroRect; | 248 NSRect top_separator_frame = NSZeroRect; |
| 251 top_separator_frame.size.width = NSWidth(popup_frame); | 249 top_separator_frame.size.width = NSWidth(popup_frame); |
| 252 top_separator_frame.size.height = | 250 top_separator_frame.size.height = |
| 253 [OmniboxPopupTopSeparatorView preferredHeight]; | 251 [OmniboxPopupTopSeparatorView preferredHeight]; |
| 254 [top_separator_view_ setFrame:top_separator_frame]; | 252 [top_separator_view_ setFrame:top_separator_frame]; |
| 255 | 253 |
| 256 // Bottom separator. | 254 // Bottom separator. |
| 257 NSRect bottom_separator_frame = NSZeroRect; | 255 NSRect bottom_separator_frame = NSZeroRect; |
| 258 bottom_separator_frame.size.width = NSWidth(popup_frame); | 256 bottom_separator_frame.size.width = NSWidth(popup_frame); |
| 259 bottom_separator_frame.size.height = | 257 bottom_separator_frame.size.height = |
| 260 [OmniboxPopupBottomSeparatorView preferredHeight]; | 258 [OmniboxPopupBottomSeparatorView preferredHeight]; |
| 261 bottom_separator_frame.origin.y = | 259 bottom_separator_frame.origin.y = |
| 262 NSHeight(popup_frame) - NSHeight(bottom_separator_frame); | 260 NSHeight(popup_frame) - NSHeight(bottom_separator_frame); |
| 263 [bottom_separator_view_ setFrame:bottom_separator_frame]; | 261 [bottom_separator_view_ setFrame:bottom_separator_frame]; |
| 264 | 262 |
| 265 // Background view. | 263 // Background view. |
| 266 NSRect background_rect = NSZeroRect; | 264 NSRect background_rect = NSZeroRect; |
| 267 background_rect.size.width = NSWidth(popup_frame); | 265 background_rect.size.width = NSWidth(popup_frame); |
| 268 background_rect.size.height = NSHeight(popup_frame) - | 266 background_rect.size.height = NSHeight(popup_frame) - |
| 269 NSHeight(top_separator_frame) - NSHeight(bottom_separator_frame); | 267 NSHeight(top_separator_frame) - NSHeight(bottom_separator_frame); |
| 270 background_rect.origin.y = NSMaxY(top_separator_frame); | 268 background_rect.origin.y = NSMaxY(top_separator_frame); |
| 271 [background_view_ setFrame:background_rect]; | 269 [background_view_ setFrame:background_rect]; |
| 272 | 270 |
| 271 // Calculate the width of the table based on backing out the popup's border | |
| 272 // from the width of the field. | |
| 273 const CGFloat tableWidth = NSWidth([field_ bounds]); | |
| 274 DCHECK_GT(tableWidth, 0.0); | |
| 275 | |
| 273 // Matrix. | 276 // Matrix. |
| 274 NSPoint field_origin_base = | 277 NSPoint field_origin_base = |
| 275 [field_ convertPoint:[field_ bounds].origin toView:nil]; | 278 [field_ convertPoint:[field_ bounds].origin toView:nil]; |
| 276 NSRect matrix_frame = NSZeroRect; | 279 NSRect matrix_frame = NSZeroRect; |
| 277 matrix_frame.origin.x = field_origin_base.x - NSMinX(anchor_rect_base); | 280 matrix_frame.origin.x = field_origin_base.x - NSMinX(anchor_rect_base); |
| 278 matrix_frame.origin.y = kPopupPaddingVertical; | 281 matrix_frame.origin.y = kPopupPaddingVertical; |
| 279 matrix_frame.size.width = [matrix_ cellSize].width; | 282 matrix_frame.size.width = tableWidth; |
| 280 matrix_frame.size.height = matrixHeight; | 283 matrix_frame.size.height = matrixHeight; |
| 281 [matrix_ setFrame:matrix_frame]; | 284 [matrix_ setFrame:matrix_frame]; |
| 282 | 285 |
| 283 NSRect current_poup_frame = [popup_ frame]; | 286 NSRect current_poup_frame = [popup_ frame]; |
| 284 target_popup_frame_ = popup_frame; | 287 target_popup_frame_ = popup_frame; |
| 285 | 288 |
| 286 // Animate the frame change if the only change is that the height got smaller. | 289 // Animate the frame change if the only change is that the height got smaller. |
| 287 // Otherwise, resize immediately. | 290 // Otherwise, resize immediately. |
| 288 bool animate = (NSHeight(popup_frame) < NSHeight(current_poup_frame) && | 291 bool animate = (NSHeight(popup_frame) < NSHeight(current_poup_frame) && |
| 289 NSWidth(popup_frame) == NSWidth(current_poup_frame)); | 292 NSWidth(popup_frame) == NSWidth(current_poup_frame)); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 329 } | 332 } |
| 330 | 333 |
| 331 void OmniboxPopupViewMac::OpenURLForRow(size_t row, | 334 void OmniboxPopupViewMac::OpenURLForRow(size_t row, |
| 332 WindowOpenDisposition disposition) { | 335 WindowOpenDisposition disposition) { |
| 333 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; | 336 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; |
| 334 row += start_match; | 337 row += start_match; |
| 335 DCHECK_LT(row, GetResult().size()); | 338 DCHECK_LT(row, GetResult().size()); |
| 336 omnibox_view_->OpenMatch(GetResult().match_at(row), disposition, GURL(), | 339 omnibox_view_->OpenMatch(GetResult().match_at(row), disposition, GURL(), |
| 337 base::string16(), row); | 340 base::string16(), row); |
| 338 } | 341 } |
| OLD | NEW |