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/omnibox/omnibox_popup_model.h" | 5 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 } | 44 } |
45 | 45 |
46 // static | 46 // static |
47 void OmniboxPopupModel::ComputeMatchMaxWidths(int contents_width, | 47 void OmniboxPopupModel::ComputeMatchMaxWidths(int contents_width, |
48 int separator_width, | 48 int separator_width, |
49 int description_width, | 49 int description_width, |
50 int available_width, | 50 int available_width, |
51 bool allow_shrinking_contents, | 51 bool allow_shrinking_contents, |
52 int* contents_max_width, | 52 int* contents_max_width, |
53 int* description_max_width) { | 53 int* description_max_width) { |
54 if (available_width <= 0) { | 54 available_width = std::max(available_width, 0); |
55 *contents_max_width = 0; | 55 *contents_max_width = std::min(contents_width, available_width); |
56 *description_max_width = 0; | |
57 return; | |
58 } | |
59 | |
60 *contents_max_width = contents_width; | |
61 *description_max_width = description_width; | 56 *description_max_width = description_width; |
62 | 57 |
63 // If the description is empty, the contents can get the full width. | 58 // If the description is empty, the contents can get the full width. |
64 if (!description_width) | 59 if (!description_width) |
65 return; | 60 return; |
66 | 61 |
67 // If we want to display the description, we need to reserve enough space for | 62 // If we want to display the description, we need to reserve enough space for |
68 // the separator. | 63 // the separator. |
69 available_width -= separator_width; | 64 available_width -= separator_width; |
| 65 if (available_width < 0) { |
| 66 *description_max_width = 0; |
| 67 return; |
| 68 } |
70 | 69 |
71 if (contents_width + description_width > available_width) { | 70 if (contents_width + description_width > available_width) { |
72 if (allow_shrinking_contents) { | 71 if (allow_shrinking_contents) { |
73 // Try to split the available space fairly between contents and | 72 // Try to split the available space fairly between contents and |
74 // description (if one wants less than half, give it all it wants and | 73 // description (if one wants less than half, give it all it wants and |
75 // give the other the remaining space; otherwise, give each half). | 74 // give the other the remaining space; otherwise, give each half). |
76 // However, if this makes the contents too narrow to show a significant | 75 // However, if this makes the contents too narrow to show a significant |
77 // amount of information, give the contents more space. | 76 // amount of information, give the contents more space. |
78 *contents_max_width = std::max( | 77 *contents_max_width = std::max( |
79 (available_width + 1) / 2, available_width - description_width); | 78 (available_width + 1) / 2, available_width - description_width); |
80 | 79 |
81 const int kMinimumContentsWidth = 300; | 80 const int kMinimumContentsWidth = 300; |
82 *contents_max_width = std::min( | 81 *contents_max_width = std::min(std::min( |
83 std::max(*contents_max_width, kMinimumContentsWidth), contents_width); | 82 std::max(*contents_max_width, kMinimumContentsWidth), contents_width), |
| 83 available_width); |
84 } | 84 } |
85 | 85 |
86 // Give the description the remaining space, unless this makes it too small | 86 // Give the description the remaining space, unless this makes it too small |
87 // to display anything meaningful, in which case just hide the description | 87 // to display anything meaningful, in which case just hide the description |
88 // and let the contents take up the whole width. | 88 // and let the contents take up the whole width. |
89 *description_max_width = available_width - *contents_max_width; | 89 *description_max_width = |
| 90 std::min(description_width, available_width - *contents_max_width); |
90 const int kMinimumDescriptionWidth = 75; | 91 const int kMinimumDescriptionWidth = 75; |
91 if (*description_max_width < | 92 if (*description_max_width < |
92 std::min(description_width, kMinimumDescriptionWidth)) { | 93 std::min(description_width, kMinimumDescriptionWidth)) { |
93 *description_max_width = 0; | 94 *description_max_width = 0; |
94 *contents_max_width = contents_width; | 95 // Since we're not going to display the description, the contents can have |
| 96 // the space we reserved for the separator. |
| 97 available_width += separator_width; |
| 98 *contents_max_width = std::min(contents_width, available_width); |
95 } | 99 } |
96 } | 100 } |
97 } | 101 } |
98 | 102 |
99 bool OmniboxPopupModel::IsOpen() const { | 103 bool OmniboxPopupModel::IsOpen() const { |
100 return view_->IsOpen(); | 104 return view_->IsOpen(); |
101 } | 105 } |
102 | 106 |
103 void OmniboxPopupModel::SetHoveredLine(size_t line) { | 107 void OmniboxPopupModel::SetHoveredLine(size_t line) { |
104 const bool is_disabling = (line == kNoMatch); | 108 const bool is_disabling = (line == kNoMatch); |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 } | 294 } |
291 } | 295 } |
292 | 296 |
293 void OmniboxPopupModel::AddObserver(OmniboxPopupModelObserver* observer) { | 297 void OmniboxPopupModel::AddObserver(OmniboxPopupModelObserver* observer) { |
294 observers_.AddObserver(observer); | 298 observers_.AddObserver(observer); |
295 } | 299 } |
296 | 300 |
297 void OmniboxPopupModel::RemoveObserver(OmniboxPopupModelObserver* observer) { | 301 void OmniboxPopupModel::RemoveObserver(OmniboxPopupModelObserver* observer) { |
298 observers_.RemoveObserver(observer); | 302 observers_.RemoveObserver(observer); |
299 } | 303 } |
OLD | NEW |