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

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

Issue 2413223003: Views:: Make Labels support text selection. (Closed)
Patch Set: -- 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 "base/optional.h"
11 #include "ui/gfx/render_text.h" 12 #include "ui/gfx/render_text.h"
13 #include "ui/views/selection_controller_delegate.h"
12 #include "ui/views/view.h" 14 #include "ui/views/view.h"
13 15
14 namespace views { 16 namespace views {
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);
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 IsSelectable() const;
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);
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 // If a subclass stops supporting text selection, it should call
msw 2016/10/28 06:30:26 Since Label [subclass] users can just call SetSele
karandeepb 2016/11/01 11:06:25 Yeah that is why this is made virtual and is overr
194 // SetSelectable(false).
195 virtual bool IsSelectionSupported() const;
196
156 // View: 197 // View:
157 void OnBoundsChanged(const gfx::Rect& previous_bounds) override; 198 void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
158 void VisibilityChanged(View* starting_from, bool is_visible) override; 199 void VisibilityChanged(View* starting_from, bool is_visible) override;
159 void OnPaint(gfx::Canvas* canvas) override; 200 void OnPaint(gfx::Canvas* canvas) override;
160 void OnDeviceScaleFactorChanged(float device_scale_factor) override; 201 void OnDeviceScaleFactorChanged(float device_scale_factor) override;
161 void OnNativeThemeChanged(const ui::NativeTheme* theme) override; 202 void OnNativeThemeChanged(const ui::NativeTheme* theme) override;
203 gfx::NativeCursor GetCursor(const ui::MouseEvent& event) override;
204 void OnFocus() override;
205 void OnBlur() override;
206 bool OnMousePressed(const ui::MouseEvent& event) override;
207 bool OnMouseDragged(const ui::MouseEvent& event) override;
208 void OnMouseReleased(const ui::MouseEvent& event) override;
209 void OnMouseCaptureLost() override;
162 210
163 private: 211 private:
164 FRIEND_TEST_ALL_PREFIXES(LabelTest, ResetRenderTextData); 212 FRIEND_TEST_ALL_PREFIXES(LabelTest, ResetRenderTextData);
165 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultilineSupportedRenderText); 213 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultilineSupportedRenderText);
166 FRIEND_TEST_ALL_PREFIXES(LabelTest, TextChangeWithoutLayout); 214 FRIEND_TEST_ALL_PREFIXES(LabelTest, TextChangeWithoutLayout);
167 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest, FocusBounds); 215 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest, FocusBounds);
168 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest, EmptyLabel); 216 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest, EmptyLabel);
169 217
218 // SelectionControllerDelegate overrides:
219 gfx::RenderText* GetRenderTextForSelectionController() override;
220 bool IsReadOnly() const override;
221 bool SupportsDrag() const override;
222 bool HasTextBeingDragged() const override;
223 void SetTextBeingDragged(bool value) override;
224 int GetViewHeight() const override;
225 int GetViewWidth() const override;
226 int GetDragSelectionDelay() const override;
227 void OnBeforePointerAction() override;
228 void OnAfterPointerAction(bool text_changed, bool selection_changed) override;
229 bool PasteSelectionClipboard() override;
230 void UpdateSelectionClipboard() override;
231
232 const gfx::RenderText* GetRenderTextForSelectionController() const;
233
170 void Init(const base::string16& text, const gfx::FontList& font_list); 234 void Init(const base::string16& text, const gfx::FontList& font_list);
171 235
172 void ResetLayout(); 236 void ResetLayout();
173 237
174 // Set up |lines_| to actually be painted. 238 // Set up |lines_| to actually be painted.
175 void MaybeBuildRenderTextLines(); 239 void MaybeBuildRenderTextLines() const;
176 240
177 gfx::Rect GetFocusBounds(); 241 gfx::Rect GetFocusBounds() const;
178 242
179 // Get the text broken into lines as needed to fit the given |width|. 243 // Get the text broken into lines as needed to fit the given |width|.
180 std::vector<base::string16> GetLinesForWidth(int width) const; 244 std::vector<base::string16> GetLinesForWidth(int width) const;
181 245
182 // Get the text size for the current layout. 246 // Get the text size for the current layout.
183 gfx::Size GetTextSize() const; 247 gfx::Size GetTextSize() const;
184 248
185 // Updates |actual_{enabled,disabled}_color_| from requested colors. 249 // Updates |actual_{enabled,disabled}_color_| from requested colors.
186 void RecalculateColors(); 250 void RecalculateColors();
187 251
188 // Applies |actual_{enabled,disabled}_color_| to |lines_|. 252 // Applies |actual_{enabled,disabled}_color_| to |lines_|.
189 void ApplyTextColors(); 253 void ApplyTextColors() const;
190 254
191 // Updates any colors that have not been explicitly set from the theme. 255 // Updates any colors that have not been explicitly set from the theme.
192 void UpdateColorsFromTheme(const ui::NativeTheme* theme); 256 void UpdateColorsFromTheme(const ui::NativeTheme* theme);
193 257
194 bool ShouldShowDefaultTooltip() const; 258 bool ShouldShowDefaultTooltip() const;
195 259
260 // Empties |lines_| and updates |last_selection_range_|.
261 void ClearRenderTextLines() const;
262
196 // An un-elided and single-line RenderText object used for preferred sizing. 263 // An un-elided and single-line RenderText object used for preferred sizing.
197 std::unique_ptr<gfx::RenderText> render_text_; 264 std::unique_ptr<gfx::RenderText> render_text_;
198 265
199 // The RenderText instances used to display elided and multi-line text. 266 // The RenderText instances used to display elided and multi-line text.
200 std::vector<std::unique_ptr<gfx::RenderText>> lines_; 267 mutable std::vector<std::unique_ptr<gfx::RenderText>> lines_;
268
269 // Persists the current selection range between the calls to
270 // ClearRenderTextLines() and MaybeBuildRenderTextLines().
271 mutable base::Optional<gfx::Range> last_selection_range_;
msw 2016/10/28 06:30:26 Nice explanatory comment. Some questions/comments
karandeepb 2016/11/01 11:06:25 That would cause the selection to erase every time
201 272
202 SkColor requested_enabled_color_ = SK_ColorRED; 273 SkColor requested_enabled_color_ = SK_ColorRED;
203 SkColor actual_enabled_color_ = SK_ColorRED; 274 SkColor actual_enabled_color_ = SK_ColorRED;
204 SkColor requested_disabled_color_ = SK_ColorRED; 275 SkColor requested_disabled_color_ = SK_ColorRED;
205 SkColor actual_disabled_color_ = SK_ColorRED; 276 SkColor actual_disabled_color_ = SK_ColorRED;
206 SkColor background_color_ = SK_ColorRED; 277 SkColor background_color_ = SK_ColorRED;
278 SkColor requested_selection_text_color_ = SK_ColorRED;
279 SkColor actual_selection_text_color_ = SK_ColorRED;
280 SkColor selection_background_color_ = SK_ColorRED;
207 281
208 // Set to true once the corresponding setter is invoked. 282 // Set to true once the corresponding setter is invoked.
209 bool enabled_color_set_; 283 bool enabled_color_set_;
210 bool disabled_color_set_; 284 bool disabled_color_set_;
211 bool background_color_set_; 285 bool background_color_set_;
286 bool selection_text_color_set_;
287 bool selection_background_color_set_;
212 288
213 gfx::ElideBehavior elide_behavior_; 289 gfx::ElideBehavior elide_behavior_;
214 290
215 bool subpixel_rendering_enabled_; 291 bool subpixel_rendering_enabled_;
216 bool auto_color_readability_; 292 bool auto_color_readability_;
217 // TODO(mukai): remove |multi_line_| when all RenderText can render multiline. 293 // TODO(mukai): remove |multi_line_| when all RenderText can render multiline.
218 bool multi_line_; 294 bool multi_line_;
219 base::string16 tooltip_text_; 295 base::string16 tooltip_text_;
220 bool handles_tooltips_; 296 bool handles_tooltips_;
221 // Whether to collapse the label when it's not visible. 297 // Whether to collapse the label when it's not visible.
222 bool collapse_when_hidden_; 298 bool collapse_when_hidden_;
223 int fixed_width_; 299 int fixed_width_;
224 int max_width_; 300 int max_width_;
225 301
226 // TODO(ckocagil): Remove is_first_paint_text_ before crbug.com/441028 is 302 // TODO(ckocagil): Remove is_first_paint_text_ before crbug.com/441028 is
227 // closed. 303 // closed.
228 bool is_first_paint_text_; 304 bool is_first_paint_text_;
229 305
306 std::unique_ptr<SelectionController> selection_controller_;
307
230 DISALLOW_COPY_AND_ASSIGN(Label); 308 DISALLOW_COPY_AND_ASSIGN(Label);
231 }; 309 };
232 310
233 } // namespace views 311 } // namespace views
234 312
235 #endif // UI_VIEWS_CONTROLS_LABEL_H_ 313 #endif // UI_VIEWS_CONTROLS_LABEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698