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

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

Issue 413823002: Cache gfx::RenderText instances in views::Label. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months 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
« no previous file with comments | « ui/views/controls/button/label_button.cc ('k') | ui/views/controls/label.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #ifndef UI_VIEWS_CONTROLS_LABEL_H_ 5 #ifndef UI_VIEWS_CONTROLS_LABEL_H_
6 #define UI_VIEWS_CONTROLS_LABEL_H_ 6 #define UI_VIEWS_CONTROLS_LABEL_H_
7 7
8 #include <string>
9 #include <vector>
10
11 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h" 9 #include "base/gtest_prod_util.h"
13 #include "base/strings/string16.h" 10 #include "base/memory/scoped_vector.h"
14 #include "third_party/skia/include/core/SkColor.h" 11 #include "ui/gfx/render_text.h"
15 #include "ui/gfx/font_list.h"
16 #include "ui/gfx/shadow_value.h"
17 #include "ui/gfx/text_constants.h"
18 #include "ui/views/view.h" 12 #include "ui/views/view.h"
19 13
20 namespace views { 14 namespace views {
21 15
22 // A view subclass that can display a string. 16 // A view subclass that can display a string.
23 class VIEWS_EXPORT Label : public View { 17 class VIEWS_EXPORT Label : public View {
24 public: 18 public:
25 // Internal class name. 19 // Internal class name.
26 static const char kViewClassName[]; 20 static const char kViewClassName[];
27 21
28 // The padding for the focus border when rendering focused text. 22 // The padding for the focus border when rendering focused text.
29 static const int kFocusBorderPadding; 23 static const int kFocusBorderPadding;
30 24
31 Label(); 25 Label();
32 explicit Label(const base::string16& text); 26 explicit Label(const base::string16& text);
33 Label(const base::string16& text, const gfx::FontList& font_list); 27 Label(const base::string16& text, const gfx::FontList& font_list);
34 virtual ~Label(); 28 virtual ~Label();
35 29
36 // Gets or sets the fonts used by this label. 30 // Gets or sets the fonts used by this label.
37 const gfx::FontList& font_list() const { return font_list_; } 31 const gfx::FontList& font_list() const { return render_text_->font_list(); }
38 virtual void SetFontList(const gfx::FontList& font_list); 32 virtual void SetFontList(const gfx::FontList& font_list);
39 33
40 // Get or set the label text. 34 // Get or set the label text.
41 const base::string16& text() const { return text_; } 35 const base::string16& text() const { return render_text_->text(); }
42 virtual void SetText(const base::string16& text); 36 virtual void SetText(const base::string16& text);
43 37
44 // Enables or disables auto-color-readability (enabled by default). If this 38 // Enables or disables auto-color-readability (enabled by default). If this
45 // is enabled, then calls to set any foreground or background color will 39 // is enabled, then calls to set any foreground or background color will
46 // trigger an automatic mapper that uses color_utils::GetReadableColor() to 40 // trigger an automatic mapper that uses color_utils::GetReadableColor() to
47 // ensure that the foreground colors are readable over the background color. 41 // ensure that the foreground colors are readable over the background color.
48 void SetAutoColorReadabilityEnabled(bool enabled); 42 void SetAutoColorReadabilityEnabled(bool enabled);
49 43
50 // Sets the color. This will automatically force the color to be readable 44 // Sets the color. This will automatically force the color to be readable
51 // over the current background color, if auto color readability is enabled. 45 // over the current background color, if auto color readability is enabled.
52 virtual void SetEnabledColor(SkColor color); 46 virtual void SetEnabledColor(SkColor color);
53 void SetDisabledColor(SkColor color); 47 void SetDisabledColor(SkColor color);
54 48
55 SkColor enabled_color() const { return actual_enabled_color_; } 49 SkColor enabled_color() const { return actual_enabled_color_; }
56 50
57 // Sets the background color. This won't be explicitly drawn, but the label 51 // Sets the background color. This won't be explicitly drawn, but the label
58 // will force the text color to be readable over it. 52 // will force the text color to be readable over it.
59 void SetBackgroundColor(SkColor color); 53 void SetBackgroundColor(SkColor color);
60 SkColor background_color() const { return background_color_; } 54 SkColor background_color() const { return background_color_; }
61 55
62 // Set drop shadows underneath the text. 56 // Set drop shadows underneath the text.
63 void SetShadows(const gfx::ShadowValues& shadows); 57 void SetShadows(const gfx::ShadowValues& shadows);
64 const gfx::ShadowValues& shadows() const { return shadows_; } 58 const gfx::ShadowValues& shadows() const { return render_text_->shadows(); }
65 59
66 // Sets whether subpixel rendering is used; the default is true, but this 60 // Sets whether subpixel rendering is used; the default is true, but this
67 // feature also requires an opaque background color. 61 // feature also requires an opaque background color.
68 void SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled); 62 void SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled);
69 63
70 // Sets the horizontal alignment; the argument value is mirrored in RTL UI. 64 // Sets the horizontal alignment; the argument value is mirrored in RTL UI.
71 void SetHorizontalAlignment(gfx::HorizontalAlignment alignment); 65 void SetHorizontalAlignment(gfx::HorizontalAlignment alignment);
72 gfx::HorizontalAlignment GetHorizontalAlignment() const; 66 gfx::HorizontalAlignment horizontal_alignment() const {
67 return render_text_->horizontal_alignment();
68 }
73 69
74 // Get or set the distance in pixels between baselines of multi-line text. 70 // Get or set the distance in pixels between baselines of multi-line text.
75 // Default is 0, indicating the distance between lines should be the standard 71 // Default is 0, indicating the distance between lines should be the standard
76 // one for the label's text, font list, and platform. 72 // one for the label's text, font list, and platform.
77 int line_height() const { return line_height_; } 73 int line_height() const { return line_height_; }
78 void SetLineHeight(int height); 74 void SetLineHeight(int height);
79 75
80 // Get or set if the label text can wrap on multiple lines; default is false. 76 // Get or set if the label text can wrap on multiple lines; default is false.
81 bool multi_line() const { return multi_line_; } 77 bool multi_line() const { return multi_line_; }
82 void SetMultiLine(bool multi_line); 78 void SetMultiLine(bool multi_line);
83 79
84 // Get or set if the label text should be obscured before rendering (e.g. 80 // Get or set if the label text should be obscured before rendering (e.g.
85 // should "Password!" display as "*********"); default is false. 81 // should "Password!" display as "*********"); default is false.
86 bool obscured() const { return obscured_; } 82 bool obscured() const { return render_text_->obscured(); }
87 void SetObscured(bool obscured); 83 void SetObscured(bool obscured);
88 84
89 // Sets whether multi-line text can wrap mid-word; the default is false. 85 // Sets whether multi-line text can wrap mid-word; the default is false.
90 void SetAllowCharacterBreak(bool allow_character_break); 86 void SetAllowCharacterBreak(bool allow_character_break);
91 87
92 // Sets the eliding or fading behavior, applied as necessary. The default is 88 // Sets the eliding or fading behavior, applied as necessary. The default is
93 // to elide at the end. Eliding is not well supported for multi-line labels. 89 // to elide at the end. Eliding is not well-supported for multi-line labels.
94 void SetElideBehavior(gfx::ElideBehavior elide_behavior); 90 void SetElideBehavior(gfx::ElideBehavior elide_behavior);
91 gfx::ElideBehavior elide_behavior() const { return elide_behavior_; }
95 92
96 // Sets the tooltip text. Default behavior for a label (single-line) is to 93 // Sets the tooltip text. Default behavior for a label (single-line) is to
97 // show the full text if it is wider than its bounds. Calling this overrides 94 // show the full text if it is wider than its bounds. Calling this overrides
98 // the default behavior and lets you set a custom tooltip. To revert to 95 // the default behavior and lets you set a custom tooltip. To revert to
99 // default behavior, call this with an empty string. 96 // default behavior, call this with an empty string.
100 void SetTooltipText(const base::string16& tooltip_text); 97 void SetTooltipText(const base::string16& tooltip_text);
101 98
102 // Resizes the label so its width is set to the width of the longest line and 99 // Resizes the label so its width is set to the width of the longest line and
103 // its height deduced accordingly. 100 // its height deduced accordingly.
104 // This is only intended for multi-line labels and is useful when the label's 101 // This is only intended for multi-line labels and is useful when the label's
105 // text contains several lines separated with \n. 102 // text contains several lines separated with \n.
106 // |max_width| is the maximum width that will be used (longer lines will be 103 // |max_width| is the maximum width that will be used (longer lines will be
107 // wrapped). If 0, no maximum width is enforced. 104 // wrapped). If 0, no maximum width is enforced.
108 void SizeToFit(int max_width); 105 void SizeToFit(int max_width);
109 106
110 // Sets whether the preferred size is empty when the label is not visible. 107 // Sets whether the preferred size is empty when the label is not visible.
111 void set_collapse_when_hidden(bool value) { collapse_when_hidden_ = value; } 108 void set_collapse_when_hidden(bool value) { collapse_when_hidden_ = value; }
112 109
113 // Get the text as displayed to the user, respecting the obscured flag. 110 // Get the text as displayed to the user, respecting the obscured flag.
114 const base::string16& GetLayoutTextForTesting() const; 111 const base::string16& GetLayoutTextForTesting() const;
115 112
116 // View: 113 // View:
117 virtual gfx::Insets GetInsets() const OVERRIDE; 114 virtual gfx::Insets GetInsets() const OVERRIDE;
118 virtual int GetBaseline() const OVERRIDE; 115 virtual int GetBaseline() const OVERRIDE;
119 virtual gfx::Size GetPreferredSize() const OVERRIDE; 116 virtual gfx::Size GetPreferredSize() const OVERRIDE;
120 virtual gfx::Size GetMinimumSize() const OVERRIDE; 117 virtual gfx::Size GetMinimumSize() const OVERRIDE;
121 virtual int GetHeightForWidth(int w) const OVERRIDE; 118 virtual int GetHeightForWidth(int w) const OVERRIDE;
119 virtual void Layout() OVERRIDE;
122 virtual const char* GetClassName() const OVERRIDE; 120 virtual const char* GetClassName() const OVERRIDE;
123 virtual View* GetTooltipHandlerForPoint(const gfx::Point& point) OVERRIDE; 121 virtual View* GetTooltipHandlerForPoint(const gfx::Point& point) OVERRIDE;
124 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE; 122 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE;
125 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; 123 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
126 virtual bool GetTooltipText(const gfx::Point& p, 124 virtual bool GetTooltipText(const gfx::Point& p,
127 base::string16* tooltip) const OVERRIDE; 125 base::string16* tooltip) const OVERRIDE;
126 virtual void OnEnabledChanged() OVERRIDE;
128 127
129 protected: 128 protected:
130 // Called by Paint to paint the text. 129 void PaintText(gfx::Canvas* canvas);
131 void PaintText(gfx::Canvas* canvas,
132 const base::string16& text,
133 const gfx::Rect& text_bounds,
134 int flags);
135
136 virtual gfx::Size GetTextSize() const;
137 130
138 SkColor disabled_color() const { return actual_disabled_color_; } 131 SkColor disabled_color() const { return actual_disabled_color_; }
139 132
140 // View: 133 // View:
141 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; 134 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
142 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 135 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
143 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE; 136 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
144 137
145 private: 138 private:
146 // These tests call CalculateDrawStringParams in order to verify the
147 // calculations done for drawing text.
148 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineString);
149 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineString);
150 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineStringInRTL);
151 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineStringInRTL);
152 FRIEND_TEST_ALL_PREFIXES(LabelTest, DirectionalityFromText);
153 FRIEND_TEST_ALL_PREFIXES(LabelTest, DisableSubpixelRendering);
154
155 // Sets both |text_| and |layout_text_| to appropriate values, taking
156 // the label's 'obscured' status into account.
157 void SetTextInternal(const base::string16& text);
158
159 void Init(const base::string16& text, const gfx::FontList& font_list); 139 void Init(const base::string16& text, const gfx::FontList& font_list);
160 140
141 void ResetLayout();
142
143 // Get the text broken into lines as needed to fit the given |width|.
144 std::vector<base::string16> GetLinesForWidth(int width) const;
145
146 // Get the natural text size, unelided and only wrapped on newlines.
147 gfx::Size GetTextSize() const;
148
161 void RecalculateColors(); 149 void RecalculateColors();
162 150
163 // Returns where the text is drawn, in the receivers coordinate system.
164 gfx::Rect GetTextBounds() const;
165
166 int ComputeDrawStringFlags() const;
167
168 gfx::Rect GetAvailableRect() const;
169
170 // Returns parameters to be used for the DrawString call.
171 void CalculateDrawStringParams(base::string16* paint_text,
172 gfx::Rect* text_bounds,
173 int* flags) const;
174
175 // Updates any colors that have not been explicitly set from the theme. 151 // Updates any colors that have not been explicitly set from the theme.
176 void UpdateColorsFromTheme(const ui::NativeTheme* theme); 152 void UpdateColorsFromTheme(const ui::NativeTheme* theme);
177 153
178 // Resets |cached_heights_| and |cached_heights_cursor_| and mark
179 // |text_size_valid_| as false.
180 void ResetCachedSize();
181
182 bool ShouldShowDefaultTooltip() const; 154 bool ShouldShowDefaultTooltip() const;
183 155
184 base::string16 text_; 156 // An un-elided and single-line RenderText object used for preferred sizing.
185 base::string16 layout_text_; 157 scoped_ptr<gfx::RenderText> render_text_;
186 gfx::FontList font_list_; 158
159 // The RenderText instances used to display elided and multi-line text.
160 ScopedVector<gfx::RenderText> lines_;
161
162 gfx::ElideBehavior elide_behavior_;
163
187 SkColor requested_enabled_color_; 164 SkColor requested_enabled_color_;
188 SkColor actual_enabled_color_; 165 SkColor actual_enabled_color_;
189 SkColor requested_disabled_color_; 166 SkColor requested_disabled_color_;
190 SkColor actual_disabled_color_; 167 SkColor actual_disabled_color_;
191 SkColor background_color_; 168 SkColor background_color_;
192 169
193 // Set to true once the corresponding setter is invoked. 170 // Set to true once the corresponding setter is invoked.
194 bool enabled_color_set_; 171 bool enabled_color_set_;
195 bool disabled_color_set_; 172 bool disabled_color_set_;
196 bool background_color_set_; 173 bool background_color_set_;
197 174
198 bool subpixel_rendering_enabled_; 175 bool subpixel_rendering_enabled_;
199 bool auto_color_readability_; 176 bool auto_color_readability_;
200 mutable gfx::Size text_size_; 177 base::string16 tooltip_text_;
201 mutable bool text_size_valid_; 178 bool collapse_when_hidden_;
179 bool allow_character_break_;
180 bool multi_line_;
202 int line_height_; 181 int line_height_;
203 bool multi_line_; 182 int max_width_;
204 bool obscured_;
205 bool allow_character_break_;
206 gfx::ElideBehavior elide_behavior_;
207 gfx::HorizontalAlignment horizontal_alignment_;
208 base::string16 tooltip_text_;
209 // Whether to collapse the label when it's not visible.
210 bool collapse_when_hidden_;
211 gfx::ShadowValues shadows_;
212
213 // The cached heights to avoid recalculation in GetHeightForWidth().
214 mutable std::vector<gfx::Size> cached_heights_;
215 mutable int cached_heights_cursor_;
216 183
217 DISALLOW_COPY_AND_ASSIGN(Label); 184 DISALLOW_COPY_AND_ASSIGN(Label);
218 }; 185 };
219 186
220 } // namespace views 187 } // namespace views
221 188
222 #endif // UI_VIEWS_CONTROLS_LABEL_H_ 189 #endif // UI_VIEWS_CONTROLS_LABEL_H_
OLDNEW
« no previous file with comments | « ui/views/controls/button/label_button.cc ('k') | ui/views/controls/label.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698