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

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

Issue 2413223003: Views:: Make Labels support text selection. (Closed)
Patch Set: Address comments. Created 4 years, 1 month 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
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 "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/gtest_prod_util.h" 9 #include "base/gtest_prod_util.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "ui/gfx/render_text.h" 11 #include "ui/gfx/render_text.h"
12 #include "ui/views/selection_controller_delegate.h"
12 #include "ui/views/view.h" 13 #include "ui/views/view.h"
13 14
14 namespace views { 15 namespace views {
16 class LabelTest;
17 class SelectionController;
15 18
16 // A view subclass that can display a string. 19 // A view subclass that can display a string.
17 class VIEWS_EXPORT Label : public View { 20 class VIEWS_EXPORT Label : public View, public SelectionControllerDelegate {
18 public: 21 public:
19 // Internal class name. 22 // Internal class name.
20 static const char kViewClassName[]; 23 static const char kViewClassName[];
21 24
22 // The padding for the focus border when rendering focused text. 25 // The padding for the focus border when rendering focused text.
23 static const int kFocusBorderPadding; 26 static const int kFocusBorderPadding;
24 27
25 Label(); 28 Label();
26 explicit Label(const base::string16& text); 29 explicit Label(const base::string16& text);
27 Label(const base::string16& text, const gfx::FontList& font_list); 30 Label(const base::string16& text, const gfx::FontList& font_list);
(...skipping 16 matching lines...) Expand all
44 // ensure that the foreground colors are readable over the background color. 47 // ensure that the foreground colors are readable over the background color.
45 void SetAutoColorReadabilityEnabled(bool enabled); 48 void SetAutoColorReadabilityEnabled(bool enabled);
46 49
47 // Sets the color. This will automatically force the color to be readable 50 // Sets the color. This will automatically force the color to be readable
48 // over the current background color, if auto color readability is enabled. 51 // over the current background color, if auto color readability is enabled.
49 virtual void SetEnabledColor(SkColor color); 52 virtual void SetEnabledColor(SkColor color);
50 void SetDisabledColor(SkColor color); 53 void SetDisabledColor(SkColor color);
51 54
52 SkColor enabled_color() const { return actual_enabled_color_; } 55 SkColor enabled_color() const { return actual_enabled_color_; }
53 56
54 // Sets the background color. This won't be explicitly drawn, but the label 57 // Sets the background color. This won't be explicitly drawn, but the label
55 // will force the text color to be readable over it. 58 // will force the text color to be readable over it.
56 void SetBackgroundColor(SkColor color); 59 void SetBackgroundColor(SkColor color);
57 SkColor background_color() const { return background_color_; } 60 SkColor background_color() const { return background_color_; }
58 61
62 // Sets the selection text color. This will automatically force the color to
63 // be readable over the selection background color, if auto color readability
64 // is enabled. Initialized with system default.
65 void SetSelectionTextColor(SkColor color);
sky 2016/11/03 03:23:20 Is setting the selection colors really needed? AFA
karandeepb 2016/11/03 10:38:56 Yeah for textfields there are no usages of SetSele
66 SkColor selection_text_color() const { return actual_selection_text_color_; }
67
68 // Sets the selection background color. Initialized with system default.
69 void SetSelectionBackgroundColor(SkColor color);
70 SkColor selection_background_color() const {
71 return selection_background_color_;
72 }
73
59 // Set drop shadows underneath the text. 74 // Set drop shadows underneath the text.
60 void SetShadows(const gfx::ShadowValues& shadows); 75 void SetShadows(const gfx::ShadowValues& shadows);
61 const gfx::ShadowValues& shadows() const { return render_text_->shadows(); } 76 const gfx::ShadowValues& shadows() const { return render_text_->shadows(); }
62 77
63 // Sets whether subpixel rendering is used; the default is true, but this 78 // Sets whether subpixel rendering is used; the default is true, but this
64 // feature also requires an opaque background color. 79 // feature also requires an opaque background color.
65 // TODO(mukai): rename this as SetSubpixelRenderingSuppressed() to keep the 80 // TODO(mukai): rename this as SetSubpixelRenderingSuppressed() to keep the
66 // consistency with RenderText field name. 81 // consistency with RenderText field name.
67 void SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled); 82 void SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled);
68 83
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 134
120 // Like SizeToFit, but uses a smaller width if possible. 135 // Like SizeToFit, but uses a smaller width if possible.
121 void SetMaximumWidth(int max_width); 136 void SetMaximumWidth(int max_width);
122 137
123 // Sets whether the preferred size is empty when the label is not visible. 138 // Sets whether the preferred size is empty when the label is not visible.
124 void set_collapse_when_hidden(bool value) { collapse_when_hidden_ = value; } 139 void set_collapse_when_hidden(bool value) { collapse_when_hidden_ = value; }
125 140
126 // Get the text as displayed to the user, respecting the obscured flag. 141 // Get the text as displayed to the user, respecting the obscured flag.
127 base::string16 GetDisplayTextForTesting(); 142 base::string16 GetDisplayTextForTesting();
128 143
144 // Returns true if the label is selectable. Default is false.
145 bool selectable() const { return !!selection_controller_; }
146
147 // Sets whether the label is selectable. False is returned if the call fails,
148 // i.e. when selection is not supported but |selectable| is true.
149 bool SetSelectable(bool selectable);
sky 2016/11/03 03:23:20 Couple of questions: Is the plan to make selection
karandeepb 2016/11/03 10:38:56 Cases where selection is not supported- 1) MacView
150
151 // Returns true if the label has a selection.
152 bool HasSelection() const;
153
154 // Selects the entire text. NO-OP if the label is not selectable.
155 void SelectAll();
156
157 // Clears any active selection.
158 void ClearSelection();
159
160 // Selects the given text range. NO-OP if the label is not selectable or the
161 // |range| endpoints don't lie on grapheme boundaries.
162 void SelectRange(const gfx::Range& range);
163
129 // View: 164 // View:
130 gfx::Insets GetInsets() const override; 165 gfx::Insets GetInsets() const override;
131 int GetBaseline() const override; 166 int GetBaseline() const override;
132 gfx::Size GetPreferredSize() const override; 167 gfx::Size GetPreferredSize() const override;
133 gfx::Size GetMinimumSize() const override; 168 gfx::Size GetMinimumSize() const override;
134 int GetHeightForWidth(int w) const override; 169 int GetHeightForWidth(int w) const override;
135 void Layout() override; 170 void Layout() override;
136 const char* GetClassName() const override; 171 const char* GetClassName() const override;
137 View* GetTooltipHandlerForPoint(const gfx::Point& point) override; 172 View* GetTooltipHandlerForPoint(const gfx::Point& point) override;
138 bool CanProcessEventsWithinSubtree() const override; 173 bool CanProcessEventsWithinSubtree() const override;
139 void GetAccessibleState(ui::AXViewState* state) override; 174 void GetAccessibleState(ui::AXViewState* state) override;
140 bool GetTooltipText(const gfx::Point& p, 175 bool GetTooltipText(const gfx::Point& p,
141 base::string16* tooltip) const override; 176 base::string16* tooltip) const override;
142 void OnEnabledChanged() override; 177 void OnEnabledChanged() override;
143 178
144 protected: 179 protected:
145 // Create a single RenderText instance to actually be painted. 180 // Create a single RenderText instance to actually be painted.
146 virtual std::unique_ptr<gfx::RenderText> CreateRenderText( 181 virtual std::unique_ptr<gfx::RenderText> CreateRenderText(
147 const base::string16& text, 182 const base::string16& text,
148 gfx::HorizontalAlignment alignment, 183 gfx::HorizontalAlignment alignment,
149 gfx::DirectionalityMode directionality, 184 gfx::DirectionalityMode directionality,
150 gfx::ElideBehavior elide_behavior); 185 gfx::ElideBehavior elide_behavior) const;
151 186
152 void PaintText(gfx::Canvas* canvas); 187 void PaintText(gfx::Canvas* canvas);
153 188
154 SkColor disabled_color() const { return actual_disabled_color_; } 189 SkColor disabled_color() const { return actual_disabled_color_; }
155 190
191 // Returns true if the label can be made selectable. For example, links do not
192 // support text selection.
193 // Subclasses should override this function in case they want to selectively
194 // support text selection. If a subclass stops supporting text selection, it
195 // should call SetSelectable(false).
196 virtual bool IsSelectionSupported() const;
197
156 // View: 198 // View:
157 void OnBoundsChanged(const gfx::Rect& previous_bounds) override; 199 void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
158 void VisibilityChanged(View* starting_from, bool is_visible) override; 200 void VisibilityChanged(View* starting_from, bool is_visible) override;
159 void OnPaint(gfx::Canvas* canvas) override; 201 void OnPaint(gfx::Canvas* canvas) override;
160 void OnDeviceScaleFactorChanged(float device_scale_factor) override; 202 void OnDeviceScaleFactorChanged(float device_scale_factor) override;
161 void OnNativeThemeChanged(const ui::NativeTheme* theme) override; 203 void OnNativeThemeChanged(const ui::NativeTheme* theme) override;
204 gfx::NativeCursor GetCursor(const ui::MouseEvent& event) override;
205 void OnFocus() override;
206 void OnBlur() override;
207 bool OnMousePressed(const ui::MouseEvent& event) override;
208 bool OnMouseDragged(const ui::MouseEvent& event) override;
209 void OnMouseReleased(const ui::MouseEvent& event) override;
210 void OnMouseCaptureLost() override;
162 211
163 private: 212 private:
164 FRIEND_TEST_ALL_PREFIXES(LabelTest, ResetRenderTextData); 213 FRIEND_TEST_ALL_PREFIXES(LabelTest, ResetRenderTextData);
165 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultilineSupportedRenderText); 214 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultilineSupportedRenderText);
166 FRIEND_TEST_ALL_PREFIXES(LabelTest, TextChangeWithoutLayout); 215 FRIEND_TEST_ALL_PREFIXES(LabelTest, TextChangeWithoutLayout);
167 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest, FocusBounds); 216 FRIEND_TEST_ALL_PREFIXES(LabelTest, EmptyLabel);
168 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest, EmptyLabel); 217 FRIEND_TEST_ALL_PREFIXES(LabelTest, FocusBounds);
218 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultiLineSizingWithElide);
219 friend class LabelTest;
220
221 // SelectionControllerDelegate overrides:
222 gfx::RenderText* GetRenderTextForSelectionController() override;
223 bool IsReadOnly() const override;
224 bool SupportsDrag() const override;
225 bool HasTextBeingDragged() const override;
226 void SetTextBeingDragged(bool value) override;
227 int GetViewHeight() const override;
228 int GetViewWidth() const override;
229 int GetDragSelectionDelay() const override;
230 void OnBeforePointerAction() override;
231 void OnAfterPointerAction(bool text_changed, bool selection_changed) override;
232 bool PasteSelectionClipboard() override;
233 void UpdateSelectionClipboard() override;
234
235 const gfx::RenderText* GetRenderTextForSelectionController() const;
169 236
170 void Init(const base::string16& text, const gfx::FontList& font_list); 237 void Init(const base::string16& text, const gfx::FontList& font_list);
171 238
172 void ResetLayout(); 239 void ResetLayout();
173 240
174 // Set up |lines_| to actually be painted. 241 // Set up |lines_| to actually be painted.
175 void MaybeBuildRenderTextLines(); 242 void MaybeBuildRenderTextLines() const;
176 243
177 gfx::Rect GetFocusBounds(); 244 gfx::Rect GetFocusBounds() const;
178 245
179 // Get the text broken into lines as needed to fit the given |width|. 246 // Get the text broken into lines as needed to fit the given |width|.
180 std::vector<base::string16> GetLinesForWidth(int width) const; 247 std::vector<base::string16> GetLinesForWidth(int width) const;
181 248
182 // Get the text size for the current layout. 249 // Get the text size for the current layout.
183 gfx::Size GetTextSize() const; 250 gfx::Size GetTextSize() const;
184 251
185 // Updates |actual_{enabled,disabled}_color_| from requested colors. 252 // Updates |actual_{enabled,disabled}_color_| from requested colors.
186 void RecalculateColors(); 253 void RecalculateColors();
187 254
188 // Applies |actual_{enabled,disabled}_color_| to |lines_|. 255 // Applies |actual_{enabled,disabled}_color_| to |lines_|.
189 void ApplyTextColors(); 256 void ApplyTextColors() const;
190 257
191 // Updates any colors that have not been explicitly set from the theme. 258 // Updates any colors that have not been explicitly set from the theme.
192 void UpdateColorsFromTheme(const ui::NativeTheme* theme); 259 void UpdateColorsFromTheme(const ui::NativeTheme* theme);
193 260
194 bool ShouldShowDefaultTooltip() const; 261 bool ShouldShowDefaultTooltip() const;
195 262
263 // Empties |lines_| and updates |stored_selection_range_|.
264 void ClearRenderTextLines() const;
265
196 // An un-elided and single-line RenderText object used for preferred sizing. 266 // An un-elided and single-line RenderText object used for preferred sizing.
197 std::unique_ptr<gfx::RenderText> render_text_; 267 std::unique_ptr<gfx::RenderText> render_text_;
198 268
199 // The RenderText instances used to display elided and multi-line text. 269 // The RenderText instances used to display elided and multi-line text.
200 std::vector<std::unique_ptr<gfx::RenderText>> lines_; 270 mutable std::vector<std::unique_ptr<gfx::RenderText>> lines_;
271
272 // Persists the current selection range between the calls to
273 // ClearRenderTextLines() and MaybeBuildRenderTextLines(). Holds an
274 // InvalidRange when not in use.
275 mutable gfx::Range stored_selection_range_;
201 276
202 SkColor requested_enabled_color_ = SK_ColorRED; 277 SkColor requested_enabled_color_ = SK_ColorRED;
203 SkColor actual_enabled_color_ = SK_ColorRED; 278 SkColor actual_enabled_color_ = SK_ColorRED;
204 SkColor requested_disabled_color_ = SK_ColorRED; 279 SkColor requested_disabled_color_ = SK_ColorRED;
205 SkColor actual_disabled_color_ = SK_ColorRED; 280 SkColor actual_disabled_color_ = SK_ColorRED;
206 SkColor background_color_ = SK_ColorRED; 281 SkColor background_color_ = SK_ColorRED;
282 SkColor requested_selection_text_color_ = SK_ColorRED;
283 SkColor actual_selection_text_color_ = SK_ColorRED;
284 SkColor selection_background_color_ = SK_ColorRED;
207 285
208 // Set to true once the corresponding setter is invoked. 286 // Set to true once the corresponding setter is invoked.
209 bool enabled_color_set_; 287 bool enabled_color_set_;
210 bool disabled_color_set_; 288 bool disabled_color_set_;
211 bool background_color_set_; 289 bool background_color_set_;
290 bool selection_text_color_set_;
291 bool selection_background_color_set_;
212 292
213 gfx::ElideBehavior elide_behavior_; 293 gfx::ElideBehavior elide_behavior_;
214 294
215 bool subpixel_rendering_enabled_; 295 bool subpixel_rendering_enabled_;
216 bool auto_color_readability_; 296 bool auto_color_readability_;
217 // TODO(mukai): remove |multi_line_| when all RenderText can render multiline. 297 // TODO(mukai): remove |multi_line_| when all RenderText can render multiline.
218 bool multi_line_; 298 bool multi_line_;
219 base::string16 tooltip_text_; 299 base::string16 tooltip_text_;
220 bool handles_tooltips_; 300 bool handles_tooltips_;
221 // Whether to collapse the label when it's not visible. 301 // Whether to collapse the label when it's not visible.
222 bool collapse_when_hidden_; 302 bool collapse_when_hidden_;
223 int fixed_width_; 303 int fixed_width_;
224 int max_width_; 304 int max_width_;
225 305
226 // TODO(ckocagil): Remove is_first_paint_text_ before crbug.com/441028 is 306 // TODO(ckocagil): Remove is_first_paint_text_ before crbug.com/441028 is
227 // closed. 307 // closed.
228 bool is_first_paint_text_; 308 bool is_first_paint_text_;
229 309
310 std::unique_ptr<SelectionController> selection_controller_;
311
230 DISALLOW_COPY_AND_ASSIGN(Label); 312 DISALLOW_COPY_AND_ASSIGN(Label);
231 }; 313 };
232 314
233 } // namespace views 315 } // namespace views
234 316
235 #endif // UI_VIEWS_CONTROLS_LABEL_H_ 317 #endif // UI_VIEWS_CONTROLS_LABEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698