Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: ui/views/controls/label.cc

Issue 111023004: Add GetMinimumSize() for Labels and ensure it's zero for empty Links. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "ui/views/controls/label.h" 5 #include "ui/views/controls/label.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/i18n/rtl.h" 12 #include "base/i18n/rtl.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/strings/string_split.h" 14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "ui/base/accessibility/accessible_view_state.h" 17 #include "ui/base/accessibility/accessible_view_state.h"
18 #include "ui/base/resource/resource_bundle.h" 18 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/gfx/canvas.h" 19 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/color_utils.h" 20 #include "ui/gfx/color_utils.h"
21 #include "ui/gfx/insets.h" 21 #include "ui/gfx/insets.h"
22 #include "ui/gfx/shadow_value.h" 22 #include "ui/gfx/shadow_value.h"
23 #include "ui/gfx/text_elider.h" 23 #include "ui/gfx/text_elider.h"
24 #include "ui/gfx/text_utils.h" 24 #include "ui/gfx/text_utils.h"
25 #include "ui/native_theme/native_theme.h" 25 #include "ui/native_theme/native_theme.h"
26 #include "ui/views/background.h" 26 #include "ui/views/background.h"
27 27
28 namespace { 28 namespace {
29 29
30 // The padding for the focus border when rendering focused text.
31 const int kFocusBorderPadding = 1;
32 const int kCachedSizeLimit = 10; 30 const int kCachedSizeLimit = 10;
33 31
34 gfx::FontList GetDefaultFontList() { 32 gfx::FontList GetDefaultFontList() {
35 return ui::ResourceBundle::GetSharedInstance().GetFontList( 33 return ui::ResourceBundle::GetSharedInstance().GetFontList(
36 ui::ResourceBundle::BaseFont); 34 ui::ResourceBundle::BaseFont);
37 } 35 }
38 36
39 } // namespace 37 } // namespace
40 38
41 namespace views { 39 namespace views {
42 40
43 // static 41 // static
44 const char Label::kViewClassName[] = "Label"; 42 const char Label::kViewClassName[] = "Label";
43 const int Label::kFocusBorderPadding = 1;
45 44
46 Label::Label() { 45 Label::Label() {
47 Init(string16(), GetDefaultFontList()); 46 Init(string16(), GetDefaultFontList());
48 } 47 }
49 48
50 Label::Label(const string16& text) { 49 Label::Label(const string16& text) {
51 Init(text, GetDefaultFontList()); 50 Init(text, GetDefaultFontList());
52 } 51 }
53 52
54 Label::Label(const string16& text, const gfx::FontList& font_list) { 53 Label::Label(const string16& text, const gfx::FontList& font_list) {
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 193
195 label_width += GetInsets().width(); 194 label_width += GetInsets().width();
196 195
197 if (max_width > 0) 196 if (max_width > 0)
198 label_width = std::min(label_width, max_width); 197 label_width = std::min(label_width, max_width);
199 198
200 SetBounds(x(), y(), label_width, 0); 199 SetBounds(x(), y(), label_width, 0);
201 SizeToPreferredSize(); 200 SizeToPreferredSize();
202 } 201 }
203 202
204 void Label::SetHasFocusBorder(bool has_focus_border) {
205 has_focus_border_ = has_focus_border;
206 if (is_multi_line_) {
207 ResetCachedSize();
208 PreferredSizeChanged();
209 }
210 }
211
212 gfx::Insets Label::GetInsets() const { 203 gfx::Insets Label::GetInsets() const {
213 gfx::Insets insets = View::GetInsets(); 204 gfx::Insets insets = View::GetInsets();
214 if (focusable() || has_focus_border_) { 205 if (IsFocusable()) {
sky 2013/12/10 17:18:00 Why do you want IsFocusable() here rather than foc
215 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding, 206 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding,
216 kFocusBorderPadding, kFocusBorderPadding); 207 kFocusBorderPadding, kFocusBorderPadding);
217 } 208 }
218 return insets; 209 return insets;
219 } 210 }
220 211
221 int Label::GetBaseline() const { 212 int Label::GetBaseline() const {
222 return GetInsets().top() + font_list_.GetBaseline(); 213 return GetInsets().top() + font_list_.GetBaseline();
223 } 214 }
224 215
225 gfx::Size Label::GetPreferredSize() { 216 gfx::Size Label::GetPreferredSize() {
226 // Return a size of (0, 0) if the label is not visible and if the 217 // Return a size of (0, 0) if the label is not visible and if the
227 // collapse_when_hidden_ flag is set. 218 // collapse_when_hidden_ flag is set.
228 // TODO(munjal): This logic probably belongs to the View class. But for now, 219 // TODO(munjal): This logic probably belongs to the View class. But for now,
229 // put it here since putting it in View class means all inheriting classes 220 // put it here since putting it in View class means all inheriting classes
230 // need ot respect the collapse_when_hidden_ flag. 221 // need ot respect the collapse_when_hidden_ flag.
231 if (!visible() && collapse_when_hidden_) 222 if (!visible() && collapse_when_hidden_)
232 return gfx::Size(); 223 return gfx::Size();
233 224
234 gfx::Size prefsize(GetTextSize()); 225 gfx::Size size(GetTextSize());
235 gfx::Insets insets = GetInsets(); 226 gfx::Insets insets = GetInsets();
236 prefsize.Enlarge(insets.width(), insets.height()); 227 size.Enlarge(insets.width(), insets.height());
237 return prefsize; 228 return size;
229 }
230
231 gfx::Size Label::GetMinimumSize() {
232 gfx::Size text_size(GetTextSize());
233 if ((!visible() && collapse_when_hidden_) || text_size.IsEmpty())
234 return gfx::Size();
235
236 gfx::Size size(font_list_.GetStringWidth(string16(gfx::kEllipsisUTF16)),
237 font_list_.GetHeight());
238 size.SetToMin(text_size); // The actual text may be shorter than an ellipsis.
239 gfx::Insets insets = GetInsets();
240 size.Enlarge(insets.width(), insets.height());
241 return size;
238 } 242 }
239 243
240 int Label::GetHeightForWidth(int w) { 244 int Label::GetHeightForWidth(int w) {
241 if (!is_multi_line_) 245 if (!is_multi_line_)
242 return View::GetHeightForWidth(w); 246 return View::GetHeightForWidth(w);
243 247
244 w = std::max(0, w - GetInsets().width()); 248 w = std::max(0, w - GetInsets().width());
245 249
246 for (size_t i = 0; i < cached_heights_.size(); ++i) { 250 for (size_t i = 0; i < cached_heights_.size(); ++i) {
247 const gfx::Size& s = cached_heights_[i]; 251 const gfx::Size& s = cached_heights_[i];
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; 378 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false;
375 auto_color_readability_ = true; 379 auto_color_readability_ = true;
376 UpdateColorsFromTheme(ui::NativeTheme::instance()); 380 UpdateColorsFromTheme(ui::NativeTheme::instance());
377 horizontal_alignment_ = gfx::ALIGN_CENTER; 381 horizontal_alignment_ = gfx::ALIGN_CENTER;
378 line_height_ = 0; 382 line_height_ = 0;
379 is_multi_line_ = false; 383 is_multi_line_ = false;
380 allow_character_break_ = false; 384 allow_character_break_ = false;
381 elide_behavior_ = ELIDE_AT_END; 385 elide_behavior_ = ELIDE_AT_END;
382 collapse_when_hidden_ = false; 386 collapse_when_hidden_ = false;
383 directionality_mode_ = USE_UI_DIRECTIONALITY; 387 directionality_mode_ = USE_UI_DIRECTIONALITY;
384 has_focus_border_ = false;
385 enabled_shadow_color_ = 0; 388 enabled_shadow_color_ = 0;
386 disabled_shadow_color_ = 0; 389 disabled_shadow_color_ = 0;
387 shadow_offset_.SetPoint(1, 1); 390 shadow_offset_.SetPoint(1, 1);
388 has_shadow_ = false; 391 has_shadow_ = false;
389 cached_heights_.resize(kCachedSizeLimit); 392 cached_heights_.resize(kCachedSizeLimit);
390 ResetCachedSize(); 393 ResetCachedSize();
391 394
392 SetText(text); 395 SetText(text);
393 } 396 }
394 397
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 for (int i = 0; i < kCachedSizeLimit; ++i) 537 for (int i = 0; i < kCachedSizeLimit; ++i)
535 cached_heights_[i] = gfx::Size(); 538 cached_heights_[i] = gfx::Size();
536 } 539 }
537 540
538 bool Label::ShouldShowDefaultTooltip() const { 541 bool Label::ShouldShowDefaultTooltip() const {
539 return !is_multi_line_ && 542 return !is_multi_line_ &&
540 gfx::GetStringWidth(text_, font_list_) > GetAvailableRect().width(); 543 gfx::GetStringWidth(text_, font_list_) > GetAvailableRect().width();
541 } 544 }
542 545
543 } // namespace views 546 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698