| 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 [matrix_ setObserver:NULL]; | 68 [matrix_ setObserver:NULL]; |
| 69 } | 69 } |
| 70 | 70 |
| 71 bool OmniboxPopupViewMac::IsOpen() const { | 71 bool OmniboxPopupViewMac::IsOpen() const { |
| 72 return popup_ != nil; | 72 return popup_ != nil; |
| 73 } | 73 } |
| 74 | 74 |
| 75 void OmniboxPopupViewMac::UpdatePopupAppearance() { | 75 void OmniboxPopupViewMac::UpdatePopupAppearance() { |
| 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 rows = result.size(); |
| 79 const size_t rows = result.size() - start_match; | |
| 80 if (rows == 0) { | 79 if (rows == 0) { |
| 81 [[popup_ parentWindow] removeChildWindow:popup_]; | 80 [[popup_ parentWindow] removeChildWindow:popup_]; |
| 82 [popup_ orderOut:nil]; | 81 [popup_ orderOut:nil]; |
| 83 | 82 |
| 84 // Break references to |this| because the popup may not be | 83 // Break references to |this| because the popup may not be |
| 85 // deallocated immediately. | 84 // deallocated immediately. |
| 86 [matrix_ setDelegate:nil]; | 85 [matrix_ setDelegate:nil]; |
| 87 matrix_.reset(); | 86 matrix_.reset(); |
| 88 | 87 |
| 89 popup_.reset(nil); | 88 popup_.reset(nil); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 100 const CGFloat matrix_width = NSWidth([field_ bounds]); | 99 const CGFloat matrix_width = NSWidth([field_ bounds]); |
| 101 DCHECK_GT(matrix_width, 0.0); | 100 DCHECK_GT(matrix_width, 0.0); |
| 102 | 101 |
| 103 // Load the results into the popup's matrix. | 102 // Load the results into the popup's matrix. |
| 104 DCHECK_GT(rows, 0U); | 103 DCHECK_GT(rows, 0U); |
| 105 [matrix_ renewRows:rows columns:1]; | 104 [matrix_ renewRows:rows columns:1]; |
| 106 CGFloat max_match_contents_width = 0.0f; | 105 CGFloat max_match_contents_width = 0.0f; |
| 107 CGFloat contents_offset = -1.0f; | 106 CGFloat contents_offset = -1.0f; |
| 108 for (size_t ii = 0; ii < rows; ++ii) { | 107 for (size_t ii = 0; ii < rows; ++ii) { |
| 109 OmniboxPopupCell* cell = [matrix_ cellAtRow:ii column:0]; | 108 OmniboxPopupCell* cell = [matrix_ cellAtRow:ii column:0]; |
| 110 const AutocompleteMatch& match = GetResult().match_at(ii + start_match); | 109 const AutocompleteMatch& match = GetResult().match_at(ii); |
| 111 [cell setImage:ImageForMatch(match)]; | 110 [cell setImage:ImageForMatch(match)]; |
| 112 [cell setMatch:match]; | 111 [cell setMatch:match]; |
| 113 // Only set the image on the one cell with match.answer. | 112 // Only set the image on the one cell with match.answer. |
| 114 if (match.answer && !model_->answer_bitmap().isNull()) { | 113 if (match.answer && !model_->answer_bitmap().isNull()) { |
| 115 NSImage* image = | 114 NSImage* image = |
| 116 gfx::Image::CreateFrom1xBitmap(model_->answer_bitmap()).CopyNSImage(); | 115 gfx::Image::CreateFrom1xBitmap(model_->answer_bitmap()).CopyNSImage(); |
| 117 [cell setAnswerImage:image]; | 116 [cell setAnswerImage:image]; |
| 118 } | 117 } |
| 119 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { | 118 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { |
| 120 max_match_contents_width = std::max(max_match_contents_width, | 119 max_match_contents_width = std::max(max_match_contents_width, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | 153 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 155 NSRect monitor_frame = [screen frame]; | 154 NSRect monitor_frame = [screen frame]; |
| 156 gfx::Rect bounds(NSRectToCGRect(target_popup_frame_)); | 155 gfx::Rect bounds(NSRectToCGRect(target_popup_frame_)); |
| 157 bounds.set_y(monitor_frame.size.height - bounds.y() - bounds.height()); | 156 bounds.set_y(monitor_frame.size.height - bounds.y() - bounds.height()); |
| 158 return bounds; | 157 return bounds; |
| 159 } | 158 } |
| 160 | 159 |
| 161 // This is only called by model in SetSelectedLine() after updating | 160 // This is only called by model in SetSelectedLine() after updating |
| 162 // everything. Popup should already be visible. | 161 // everything. Popup should already be visible. |
| 163 void OmniboxPopupViewMac::PaintUpdatesNow() { | 162 void OmniboxPopupViewMac::PaintUpdatesNow() { |
| 164 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; | 163 [matrix_ selectCellAtRow:model_->selected_line() column:0]; |
| 165 if (start_match > model_->selected_line()) { | |
| 166 [matrix_ deselectAllCells]; | |
| 167 } else { | |
| 168 [matrix_ selectCellAtRow:model_->selected_line() - start_match column:0]; | |
| 169 } | |
| 170 | |
| 171 } | 164 } |
| 172 | 165 |
| 173 void OmniboxPopupViewMac::OnMatrixRowSelected(OmniboxPopupMatrix* matrix, | 166 void OmniboxPopupViewMac::OnMatrixRowSelected(OmniboxPopupMatrix* matrix, |
| 174 size_t row) { | 167 size_t row) { |
| 175 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; | 168 model_->SetSelectedLine(row, false, false); |
| 176 model_->SetSelectedLine(row + start_match, false, false); | |
| 177 } | 169 } |
| 178 | 170 |
| 179 void OmniboxPopupViewMac::OnMatrixRowClicked(OmniboxPopupMatrix* matrix, | 171 void OmniboxPopupViewMac::OnMatrixRowClicked(OmniboxPopupMatrix* matrix, |
| 180 size_t row) { | 172 size_t row) { |
| 181 OpenURLForRow(row, | 173 OpenURLForRow(row, |
| 182 ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent])); | 174 ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent])); |
| 183 } | 175 } |
| 184 | 176 |
| 185 void OmniboxPopupViewMac::OnMatrixRowMiddleClicked(OmniboxPopupMatrix* matrix, | 177 void OmniboxPopupViewMac::OnMatrixRowMiddleClicked(OmniboxPopupMatrix* matrix, |
| 186 size_t row) { | 178 size_t row) { |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 if (!image.IsEmpty()) | 321 if (!image.IsEmpty()) |
| 330 return image.AsNSImage(); | 322 return image.AsNSImage(); |
| 331 | 323 |
| 332 const int resource_id = model_->IsStarredMatch(match) ? | 324 const int resource_id = model_->IsStarredMatch(match) ? |
| 333 IDR_OMNIBOX_STAR : AutocompleteMatch::TypeToIcon(match.type); | 325 IDR_OMNIBOX_STAR : AutocompleteMatch::TypeToIcon(match.type); |
| 334 return OmniboxViewMac::ImageForResource(resource_id); | 326 return OmniboxViewMac::ImageForResource(resource_id); |
| 335 } | 327 } |
| 336 | 328 |
| 337 void OmniboxPopupViewMac::OpenURLForRow(size_t row, | 329 void OmniboxPopupViewMac::OpenURLForRow(size_t row, |
| 338 WindowOpenDisposition disposition) { | 330 WindowOpenDisposition disposition) { |
| 339 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; | |
| 340 row += start_match; | |
| 341 DCHECK_LT(row, GetResult().size()); | 331 DCHECK_LT(row, GetResult().size()); |
| 342 omnibox_view_->OpenMatch(GetResult().match_at(row), disposition, GURL(), | 332 omnibox_view_->OpenMatch(GetResult().match_at(row), disposition, GURL(), |
| 343 base::string16(), row); | 333 base::string16(), row); |
| 344 } | 334 } |
| OLD | NEW |