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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 gfx::Font result_font(OmniboxViewMac::GetFieldFont()); | 116 gfx::Font result_font(OmniboxViewMac::GetFieldFont()); |
117 | 117 |
118 // Calculate the width of the matrix based on backing out the popup's border | 118 // Calculate the width of the matrix based on backing out the popup's border |
119 // from the width of the field. | 119 // from the width of the field. |
120 const CGFloat matrix_width = NSWidth([field_ bounds]); | 120 const CGFloat matrix_width = NSWidth([field_ bounds]); |
121 DCHECK_GT(matrix_width, 0.0); | 121 DCHECK_GT(matrix_width, 0.0); |
122 | 122 |
123 // Load the results into the popup's matrix. | 123 // Load the results into the popup's matrix. |
124 DCHECK_GT(rows, 0U); | 124 DCHECK_GT(rows, 0U); |
125 [matrix_ renewRows:rows columns:1]; | 125 [matrix_ renewRows:rows columns:1]; |
| 126 |
| 127 CGFloat max_match_width = 0; |
| 128 CGFloat max_required_width = 0; |
| 129 CGFloat ignored_prefix_width = 0; |
| 130 CGFloat ellipsis_prefix_width = 0; |
126 for (size_t ii = 0; ii < rows; ++ii) { | 131 for (size_t ii = 0; ii < rows; ++ii) { |
127 OmniboxPopupCell* cell = [matrix_ cellAtRow:ii column:0]; | 132 OmniboxPopupCell* cell = [matrix_ cellAtRow:ii column:0]; |
128 const AutocompleteMatch& match = GetResult().match_at(ii + start_match); | 133 const AutocompleteMatch& match = GetResult().match_at(ii + start_match); |
129 [cell setImage:ImageForMatch(match)]; | 134 [cell setImage:ImageForMatch(match)]; |
130 [cell setAttributedTitle:MatchText(match, result_font, matrix_width)]; | 135 NSAttributedString* match_text = |
| 136 MatchText(match, result_font, matrix_width); |
| 137 |
| 138 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_INFINITE) { |
| 139 // Use ignored_prefix from the fill_into_edit to compute the |
| 140 // required_width and max_required_width. |
| 141 size_t input_start = |
| 142 (match.transition == content::PAGE_TRANSITION_KEYWORD) ? |
| 143 match.keyword.length() + 1 : 0; |
| 144 string16 ignored_prefix = match.fill_into_edit.substr(input_start, |
| 145 match.fill_into_edit.rfind(match.contents) - input_start); |
| 146 NSMutableAttributedString* ignored_prefix_as = DecorateMatchedString( |
| 147 ignored_prefix, ACMatchClassifications(), ContentTextColor(), |
| 148 DimContentTextColor(), result_font); |
| 149 if (ignored_prefix_width == 0) |
| 150 ignored_prefix_width = [ignored_prefix_as size].width; |
| 151 [ignored_prefix_as appendAttributedString:match_text]; |
| 152 CGFloat required_width = [ignored_prefix_as size].width; |
| 153 if (required_width > max_required_width) |
| 154 max_required_width = required_width; |
| 155 |
| 156 // Use ellipsis prefix to compute the match_width and max_match_width. |
| 157 NSMutableAttributedString* ellipsis_prefix = DecorateMatchedString( |
| 158 base::string16(gfx::kEllipsisUTF16).append(UTF8ToUTF16(" ")), |
| 159 ACMatchClassifications(), ContentTextColor(), DimContentTextColor(), |
| 160 result_font); |
| 161 if (ellipsis_prefix_width == 0) |
| 162 ellipsis_prefix_width = [ellipsis_prefix size].width; |
| 163 [ellipsis_prefix appendAttributedString:match_text]; |
| 164 CGFloat match_width = [ellipsis_prefix size].width; |
| 165 if (match_width > max_match_width) |
| 166 max_match_width = match_width; |
| 167 match_text = ellipsis_prefix; |
| 168 } |
| 169 [cell setAttributedTitle:match_text]; |
| 170 } |
| 171 |
| 172 const float text_width = matrix_width - kTextXOffset; |
| 173 CGFloat infinite_suggest_offset = (max_required_width < text_width) ? |
| 174 std::max(ignored_prefix_width - ellipsis_prefix_width, 0.0f) : |
| 175 std::max(text_width - max_match_width, 0.0f); |
| 176 |
| 177 for (size_t ii = 0; ii < rows; ++ii) { |
| 178 const AutocompleteMatch& match = GetResult().match_at(ii + start_match); |
| 179 OmniboxPopupCell* cell = [matrix_ cellAtRow:ii column:0]; |
| 180 CGFloat additional_offset = |
| 181 (match.type != AutocompleteMatchType::SEARCH_SUGGEST_INFINITE) ? |
| 182 0 : infinite_suggest_offset; |
| 183 [cell setAdditionalOffset:additional_offset]; |
131 } | 184 } |
132 | 185 |
133 // Set the cell size to fit a line of text in the cell's font. All | 186 // Set the cell size to fit a line of text in the cell's font. All |
134 // cells should use the same font and each should layout in one | 187 // cells should use the same font and each should layout in one |
135 // line, so they should all be about the same height. | 188 // line, so they should all be about the same height. |
136 const NSSize cell_size = [[matrix_ cellAtRow:0 column:0] cellSize]; | 189 const NSSize cell_size = [[matrix_ cellAtRow:0 column:0] cellSize]; |
137 DCHECK_GT(cell_size.height, 0.0); | 190 DCHECK_GT(cell_size.height, 0.0); |
138 const CGFloat cell_height = cell_size.height + kCellHeightAdjust; | 191 const CGFloat cell_height = cell_size.height + kCellHeightAdjust; |
139 [matrix_ setCellSize:NSMakeSize(matrix_width, cell_height)]; | 192 [matrix_ setCellSize:NSMakeSize(matrix_width, cell_height)]; |
140 | 193 |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 return OmniboxViewMac::ImageForResource(resource_id); | 545 return OmniboxViewMac::ImageForResource(resource_id); |
493 } | 546 } |
494 | 547 |
495 void OmniboxPopupViewMac::OpenURLForRow(size_t row, | 548 void OmniboxPopupViewMac::OpenURLForRow(size_t row, |
496 WindowOpenDisposition disposition) { | 549 WindowOpenDisposition disposition) { |
497 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; | 550 size_t start_match = model_->result().ShouldHideTopMatch() ? 1 : 0; |
498 row += start_match; | 551 row += start_match; |
499 DCHECK_LT(row, GetResult().size()); | 552 DCHECK_LT(row, GetResult().size()); |
500 omnibox_view_->OpenMatch(GetResult().match_at(row), disposition, GURL(), row); | 553 omnibox_view_->OpenMatch(GetResult().match_at(row), disposition, GURL(), row); |
501 } | 554 } |
OLD | NEW |