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

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

Issue 8221027: Make views::Label and views::Link auto-color themselves to be readable over their background colo... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 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 | « chrome/browser/ui/views/wrench_menu.cc ('k') | 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 VIEWS_CONTROLS_LABEL_H_ 5 #ifndef VIEWS_CONTROLS_LABEL_H_
6 #define VIEWS_CONTROLS_LABEL_H_ 6 #define VIEWS_CONTROLS_LABEL_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 10
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 68
69 // Return the label text. 69 // Return the label text.
70 const string16 GetText() const; 70 const string16 GetText() const;
71 71
72 // Set URL Value - text_ is set to spec(). 72 // Set URL Value - text_ is set to spec().
73 void SetURL(const GURL& url); 73 void SetURL(const GURL& url);
74 74
75 // Return the label URL. 75 // Return the label URL.
76 const GURL GetURL() const; 76 const GURL GetURL() const;
77 77
78 // Set the color 78 // Enables or disables auto-color-readability (enabled by default). If this
79 virtual void SetColor(const SkColor& color); 79 // is enabled, then calls to set any foreground or background color will
80 // trigger an automatic mapper that uses color_utils::GetReadableColor() to
81 // ensure that the foreground colors are readable over the background color.
82 void SetAutoColorReadabilityEnabled(bool enabled);
80 83
81 // Return a reference to the currently used color. 84 // Set the color. This will automatically force the color to be readable
82 virtual SkColor GetColor() const; 85 // over the current background color.
86 virtual void SetEnabledColor(const SkColor& color);
87 void SetDisabledColor(const SkColor& color);
83 88
84 // If you'll be displaying the label over some non-system background color, 89 SkColor enabled_color() const { return actual_enabled_color_; }
85 // call this with the relevant color and the label will auto-set its color to 90
86 // be readable. 91 // Set the background color. This won't be explicitly drawn, but the label
87 virtual void MakeReadableOverBackgroundColor(const SkColor& background_color); 92 // will force the text color to be readable over it.
93 void SetBackgroundColor(const SkColor& color);
88 94
89 // Set horizontal alignment. If the locale is RTL, and the RTL alignment 95 // Set horizontal alignment. If the locale is RTL, and the RTL alignment
90 // setting is set as USE_UI_ALIGNMENT, the alignment is flipped around. 96 // setting is set as USE_UI_ALIGNMENT, the alignment is flipped around.
91 // 97 //
92 // Caveat: for labels originating from a web page, the RTL alignment mode 98 // Caveat: for labels originating from a web page, the RTL alignment mode
93 // should be reset to AUTO_DETECT_ALIGNMENT before the horizontal alignment 99 // should be reset to AUTO_DETECT_ALIGNMENT before the horizontal alignment
94 // is set. Otherwise, the label's alignment specified as a parameter will be 100 // is set. Otherwise, the label's alignment specified as a parameter will be
95 // flipped in RTL locales. Please see the comments in SetRTLAlignmentMode for 101 // flipped in RTL locales. Please see the comments in SetRTLAlignmentMode for
96 // more information. 102 // more information.
97 void SetHorizontalAlignment(Alignment alignment); 103 void SetHorizontalAlignment(Alignment alignment);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 167
162 // Overridden from View: 168 // Overridden from View:
163 virtual gfx::Insets GetInsets() const; 169 virtual gfx::Insets GetInsets() const;
164 virtual int GetBaseline() const OVERRIDE; 170 virtual int GetBaseline() const OVERRIDE;
165 // Overridden to compute the size required to display this label. 171 // Overridden to compute the size required to display this label.
166 virtual gfx::Size GetPreferredSize() OVERRIDE; 172 virtual gfx::Size GetPreferredSize() OVERRIDE;
167 // Return the height necessary to display this label with the provided width. 173 // Return the height necessary to display this label with the provided width.
168 // This method is used to layout multi-line labels. It is equivalent to 174 // This method is used to layout multi-line labels. It is equivalent to
169 // GetPreferredSize().height() if the receiver is not multi-line. 175 // GetPreferredSize().height() if the receiver is not multi-line.
170 virtual int GetHeightForWidth(int w); 176 virtual int GetHeightForWidth(int w);
171 // Sets the enabled state. Setting the enabled state resets the color.
172 virtual void OnEnabledChanged() OVERRIDE;
173 virtual std::string GetClassName() const OVERRIDE; 177 virtual std::string GetClassName() const OVERRIDE;
174 virtual bool HitTest(const gfx::Point& l) const OVERRIDE; 178 virtual bool HitTest(const gfx::Point& l) const OVERRIDE;
175 // Mouse enter/exit are overridden to render mouse over background color. 179 // Mouse enter/exit are overridden to render mouse over background color.
176 // These invoke SetContainsMouse as necessary. 180 // These invoke SetContainsMouse as necessary.
177 virtual void OnMouseMoved(const MouseEvent& event) OVERRIDE; 181 virtual void OnMouseMoved(const MouseEvent& event) OVERRIDE;
178 virtual void OnMouseEntered(const MouseEvent& event) OVERRIDE; 182 virtual void OnMouseEntered(const MouseEvent& event) OVERRIDE;
179 virtual void OnMouseExited(const MouseEvent& event) OVERRIDE; 183 virtual void OnMouseExited(const MouseEvent& event) OVERRIDE;
180 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 184 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
181 // Gets the tooltip text for labels that are wider than their bounds, except 185 // Gets the tooltip text for labels that are wider than their bounds, except
182 // when the label is multiline, in which case it just returns false (no 186 // when the label is multiline, in which case it just returns false (no
183 // tooltip). If a custom tooltip has been specified with SetTooltipText() 187 // tooltip). If a custom tooltip has been specified with SetTooltipText()
184 // it is returned instead. 188 // it is returned instead.
185 virtual bool GetTooltipText(const gfx::Point& p, string16* tooltip) OVERRIDE; 189 virtual bool GetTooltipText(const gfx::Point& p, string16* tooltip) OVERRIDE;
186 190
187 protected: 191 protected:
188 // Called by Paint to paint the text. Override this to change how 192 // Called by Paint to paint the text. Override this to change how
189 // text is painted. 193 // text is painted.
190 virtual void PaintText(gfx::Canvas* canvas, 194 virtual void PaintText(gfx::Canvas* canvas,
191 const string16& text, 195 const string16& text,
192 const gfx::Rect& text_bounds, 196 const gfx::Rect& text_bounds,
193 int flags); 197 int flags);
194 198
195 void invalidate_text_size() { text_size_valid_ = false; } 199 void invalidate_text_size() { text_size_valid_ = false; }
196 200
197 virtual gfx::Size GetTextSize() const; 201 virtual gfx::Size GetTextSize() const;
198 202
203 SkColor disabled_color() const { return actual_disabled_color_; }
204
199 // Overridden from View: 205 // Overridden from View:
200 // Overridden to dirty our text bounds if we're multi-line. 206 // Overridden to dirty our text bounds if we're multi-line.
201 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; 207 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
202 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 208 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
203 // If the mouse is over the label, and a mouse over background has been 209 // If the mouse is over the label, and a mouse over background has been
204 // specified, its used. Otherwise super's implementation is invoked. 210 // specified, its used. Otherwise super's implementation is invoked.
205 virtual void OnPaintBackground(gfx::Canvas* canvas) OVERRIDE; 211 virtual void OnPaintBackground(gfx::Canvas* canvas) OVERRIDE;
206 212
207 private: 213 private:
208 // These tests call CalculateDrawStringParams in order to verify the 214 // These tests call CalculateDrawStringParams in order to verify the
209 // calculations done for drawing text. 215 // calculations done for drawing text.
210 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineString); 216 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineString);
211 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineString); 217 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineString);
212 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineStringInRTL); 218 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineStringInRTL);
213 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineStringInRTL); 219 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineStringInRTL);
214 220
215 static gfx::Font GetDefaultFont(); 221 static gfx::Font GetDefaultFont();
216 222
217 void Init(const string16& text, const gfx::Font& font); 223 void Init(const string16& text, const gfx::Font& font);
218 224
225 void RecalculateColors();
226
219 // If the mouse is over the text, SetContainsMouse(true) is invoked, otherwise 227 // If the mouse is over the text, SetContainsMouse(true) is invoked, otherwise
220 // SetContainsMouse(false) is invoked. 228 // SetContainsMouse(false) is invoked.
221 void UpdateContainsMouse(const MouseEvent& event); 229 void UpdateContainsMouse(const MouseEvent& event);
222 230
223 // Updates whether the mouse is contained in the Label. If the new value 231 // Updates whether the mouse is contained in the Label. If the new value
224 // differs from the current value, and a mouse over background is specified, 232 // differs from the current value, and a mouse over background is specified,
225 // SchedulePaint is invoked. 233 // SchedulePaint is invoked.
226 void SetContainsMouse(bool contains_mouse); 234 void SetContainsMouse(bool contains_mouse);
227 235
228 // Returns where the text is drawn, in the receivers coordinate system. 236 // Returns where the text is drawn, in the receivers coordinate system.
229 gfx::Rect GetTextBounds() const; 237 gfx::Rect GetTextBounds() const;
230 238
231 int ComputeMultiLineFlags() const; 239 int ComputeMultiLineFlags() const;
232 240
233 gfx::Rect GetAvailableRect() const; 241 gfx::Rect GetAvailableRect() const;
234 242
235 // Returns parameters to be used for the DrawString call. 243 // Returns parameters to be used for the DrawString call.
236 void CalculateDrawStringParams(string16* paint_text, 244 void CalculateDrawStringParams(string16* paint_text,
237 gfx::Rect* text_bounds, 245 gfx::Rect* text_bounds,
238 int* flags) const; 246 int* flags) const;
239 247
240 string16 text_; 248 string16 text_;
241 GURL url_; 249 GURL url_;
242 gfx::Font font_; 250 gfx::Font font_;
243 SkColor color_; 251 SkColor requested_enabled_color_;
252 SkColor actual_enabled_color_;
253 SkColor requested_disabled_color_;
254 SkColor actual_disabled_color_;
255 SkColor background_color_;
256 bool auto_color_readability_;
244 mutable gfx::Size text_size_; 257 mutable gfx::Size text_size_;
245 mutable bool text_size_valid_; 258 mutable bool text_size_valid_;
246 bool is_multi_line_; 259 bool is_multi_line_;
247 bool allow_character_break_; 260 bool allow_character_break_;
248 bool elide_in_middle_; 261 bool elide_in_middle_;
249 bool url_set_; 262 bool url_set_;
250 Alignment horiz_alignment_; 263 Alignment horiz_alignment_;
251 string16 tooltip_text_; 264 string16 tooltip_text_;
252 // Whether the mouse is over this label. 265 // Whether the mouse is over this label.
253 bool contains_mouse_; 266 bool contains_mouse_;
(...skipping 11 matching lines...) Expand all
265 // allows this view to reserve space for a focus border that it otherwise 278 // allows this view to reserve space for a focus border that it otherwise
266 // might not have because it is not itself focusable. 279 // might not have because it is not itself focusable.
267 bool has_focus_border_; 280 bool has_focus_border_;
268 281
269 DISALLOW_COPY_AND_ASSIGN(Label); 282 DISALLOW_COPY_AND_ASSIGN(Label);
270 }; 283 };
271 284
272 } // namespace views 285 } // namespace views
273 286
274 #endif // VIEWS_CONTROLS_LABEL_H_ 287 #endif // VIEWS_CONTROLS_LABEL_H_
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/wrench_menu.cc ('k') | views/controls/label.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698