Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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); | |
| 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 | |
| 164 // Returns the selected text. | |
| 165 base::string16 GetSelectedText() const; | |
|
msw
2016/11/01 23:42:22
nit: inline this; it's only called once by Label,
karandeepb
2016/11/02 06:29:37
Done.
| |
| 166 | |
| 129 // View: | 167 // View: |
| 130 gfx::Insets GetInsets() const override; | 168 gfx::Insets GetInsets() const override; |
| 131 int GetBaseline() const override; | 169 int GetBaseline() const override; |
| 132 gfx::Size GetPreferredSize() const override; | 170 gfx::Size GetPreferredSize() const override; |
| 133 gfx::Size GetMinimumSize() const override; | 171 gfx::Size GetMinimumSize() const override; |
| 134 int GetHeightForWidth(int w) const override; | 172 int GetHeightForWidth(int w) const override; |
| 135 void Layout() override; | 173 void Layout() override; |
| 136 const char* GetClassName() const override; | 174 const char* GetClassName() const override; |
| 137 View* GetTooltipHandlerForPoint(const gfx::Point& point) override; | 175 View* GetTooltipHandlerForPoint(const gfx::Point& point) override; |
| 138 bool CanProcessEventsWithinSubtree() const override; | 176 bool CanProcessEventsWithinSubtree() const override; |
| 139 void GetAccessibleState(ui::AXViewState* state) override; | 177 void GetAccessibleState(ui::AXViewState* state) override; |
| 140 bool GetTooltipText(const gfx::Point& p, | 178 bool GetTooltipText(const gfx::Point& p, |
| 141 base::string16* tooltip) const override; | 179 base::string16* tooltip) const override; |
| 142 void OnEnabledChanged() override; | 180 void OnEnabledChanged() override; |
| 143 | 181 |
| 144 protected: | 182 protected: |
| 145 // Create a single RenderText instance to actually be painted. | 183 // Create a single RenderText instance to actually be painted. |
| 146 virtual std::unique_ptr<gfx::RenderText> CreateRenderText( | 184 virtual std::unique_ptr<gfx::RenderText> CreateRenderText( |
| 147 const base::string16& text, | 185 const base::string16& text, |
| 148 gfx::HorizontalAlignment alignment, | 186 gfx::HorizontalAlignment alignment, |
| 149 gfx::DirectionalityMode directionality, | 187 gfx::DirectionalityMode directionality, |
| 150 gfx::ElideBehavior elide_behavior); | 188 gfx::ElideBehavior elide_behavior) const; |
| 151 | 189 |
| 152 void PaintText(gfx::Canvas* canvas); | 190 void PaintText(gfx::Canvas* canvas); |
| 153 | 191 |
| 154 SkColor disabled_color() const { return actual_disabled_color_; } | 192 SkColor disabled_color() const { return actual_disabled_color_; } |
| 155 | 193 |
| 194 // Returns true if the label can be made selectable. For example, links do not | |
| 195 // support text selection. | |
| 196 // Subclasses should override this function in case they want to selectively | |
| 197 // support text selection. If a subclass stops supporting text selection, it | |
| 198 // should call SetSelectable(false). | |
| 199 virtual bool IsSelectionSupported() const; | |
| 200 | |
| 156 // View: | 201 // View: |
| 157 void OnBoundsChanged(const gfx::Rect& previous_bounds) override; | 202 void OnBoundsChanged(const gfx::Rect& previous_bounds) override; |
| 158 void VisibilityChanged(View* starting_from, bool is_visible) override; | 203 void VisibilityChanged(View* starting_from, bool is_visible) override; |
| 159 void OnPaint(gfx::Canvas* canvas) override; | 204 void OnPaint(gfx::Canvas* canvas) override; |
| 160 void OnDeviceScaleFactorChanged(float device_scale_factor) override; | 205 void OnDeviceScaleFactorChanged(float device_scale_factor) override; |
| 161 void OnNativeThemeChanged(const ui::NativeTheme* theme) override; | 206 void OnNativeThemeChanged(const ui::NativeTheme* theme) override; |
| 207 gfx::NativeCursor GetCursor(const ui::MouseEvent& event) override; | |
| 208 void OnFocus() override; | |
| 209 void OnBlur() override; | |
| 210 bool OnMousePressed(const ui::MouseEvent& event) override; | |
| 211 bool OnMouseDragged(const ui::MouseEvent& event) override; | |
| 212 void OnMouseReleased(const ui::MouseEvent& event) override; | |
| 213 void OnMouseCaptureLost() override; | |
| 162 | 214 |
| 163 private: | 215 private: |
| 164 FRIEND_TEST_ALL_PREFIXES(LabelTest, ResetRenderTextData); | 216 FRIEND_TEST_ALL_PREFIXES(LabelTest, ResetRenderTextData); |
| 165 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultilineSupportedRenderText); | 217 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultilineSupportedRenderText); |
| 166 FRIEND_TEST_ALL_PREFIXES(LabelTest, TextChangeWithoutLayout); | 218 FRIEND_TEST_ALL_PREFIXES(LabelTest, TextChangeWithoutLayout); |
| 167 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest, FocusBounds); | 219 FRIEND_TEST_ALL_PREFIXES(LabelTest, EmptyLabel); |
| 168 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest, EmptyLabel); | 220 FRIEND_TEST_ALL_PREFIXES(LabelTest, FocusBounds); |
| 221 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultiLineSizingWithElide); | |
| 222 friend class LabelTest; | |
| 223 | |
| 224 // SelectionControllerDelegate overrides: | |
| 225 gfx::RenderText* GetRenderTextForSelectionController() override; | |
| 226 bool IsReadOnly() const override; | |
| 227 bool SupportsDrag() const override; | |
| 228 bool HasTextBeingDragged() const override; | |
| 229 void SetTextBeingDragged(bool value) override; | |
| 230 int GetViewHeight() const override; | |
| 231 int GetViewWidth() const override; | |
| 232 int GetDragSelectionDelay() const override; | |
| 233 void OnBeforePointerAction() override; | |
| 234 void OnAfterPointerAction(bool text_changed, bool selection_changed) override; | |
| 235 bool PasteSelectionClipboard() override; | |
| 236 void UpdateSelectionClipboard() override; | |
| 237 | |
| 238 const gfx::RenderText* GetRenderTextForSelectionController() const; | |
| 169 | 239 |
| 170 void Init(const base::string16& text, const gfx::FontList& font_list); | 240 void Init(const base::string16& text, const gfx::FontList& font_list); |
| 171 | 241 |
| 172 void ResetLayout(); | 242 void ResetLayout(); |
| 173 | 243 |
| 174 // Set up |lines_| to actually be painted. | 244 // Set up |lines_| to actually be painted. |
| 175 void MaybeBuildRenderTextLines(); | 245 void MaybeBuildRenderTextLines() const; |
| 176 | 246 |
| 177 gfx::Rect GetFocusBounds(); | 247 gfx::Rect GetFocusBounds() const; |
| 178 | 248 |
| 179 // Get the text broken into lines as needed to fit the given |width|. | 249 // Get the text broken into lines as needed to fit the given |width|. |
| 180 std::vector<base::string16> GetLinesForWidth(int width) const; | 250 std::vector<base::string16> GetLinesForWidth(int width) const; |
| 181 | 251 |
| 182 // Get the text size for the current layout. | 252 // Get the text size for the current layout. |
| 183 gfx::Size GetTextSize() const; | 253 gfx::Size GetTextSize() const; |
| 184 | 254 |
| 185 // Updates |actual_{enabled,disabled}_color_| from requested colors. | 255 // Updates |actual_{enabled,disabled}_color_| from requested colors. |
| 186 void RecalculateColors(); | 256 void RecalculateColors(); |
| 187 | 257 |
| 188 // Applies |actual_{enabled,disabled}_color_| to |lines_|. | 258 // Applies |actual_{enabled,disabled}_color_| to |lines_|. |
| 189 void ApplyTextColors(); | 259 void ApplyTextColors() const; |
| 190 | 260 |
| 191 // Updates any colors that have not been explicitly set from the theme. | 261 // Updates any colors that have not been explicitly set from the theme. |
| 192 void UpdateColorsFromTheme(const ui::NativeTheme* theme); | 262 void UpdateColorsFromTheme(const ui::NativeTheme* theme); |
| 193 | 263 |
| 194 bool ShouldShowDefaultTooltip() const; | 264 bool ShouldShowDefaultTooltip() const; |
| 195 | 265 |
| 266 // Empties |lines_| and updates |stored_selection_range_|. | |
| 267 void ClearRenderTextLines() const; | |
| 268 | |
| 196 // An un-elided and single-line RenderText object used for preferred sizing. | 269 // An un-elided and single-line RenderText object used for preferred sizing. |
| 197 std::unique_ptr<gfx::RenderText> render_text_; | 270 std::unique_ptr<gfx::RenderText> render_text_; |
| 198 | 271 |
| 199 // The RenderText instances used to display elided and multi-line text. | 272 // The RenderText instances used to display elided and multi-line text. |
| 200 std::vector<std::unique_ptr<gfx::RenderText>> lines_; | 273 mutable std::vector<std::unique_ptr<gfx::RenderText>> lines_; |
| 274 | |
| 275 // Persists the current selection range between the calls to | |
| 276 // ClearRenderTextLines() and MaybeBuildRenderTextLines(). Holds an | |
| 277 // InvalidRange when not in use. | |
| 278 mutable gfx::Range stored_selection_range_; | |
| 201 | 279 |
| 202 SkColor requested_enabled_color_ = SK_ColorRED; | 280 SkColor requested_enabled_color_ = SK_ColorRED; |
| 203 SkColor actual_enabled_color_ = SK_ColorRED; | 281 SkColor actual_enabled_color_ = SK_ColorRED; |
| 204 SkColor requested_disabled_color_ = SK_ColorRED; | 282 SkColor requested_disabled_color_ = SK_ColorRED; |
| 205 SkColor actual_disabled_color_ = SK_ColorRED; | 283 SkColor actual_disabled_color_ = SK_ColorRED; |
| 206 SkColor background_color_ = SK_ColorRED; | 284 SkColor background_color_ = SK_ColorRED; |
| 285 SkColor requested_selection_text_color_ = SK_ColorRED; | |
| 286 SkColor actual_selection_text_color_ = SK_ColorRED; | |
| 287 SkColor selection_background_color_ = SK_ColorRED; | |
| 207 | 288 |
| 208 // Set to true once the corresponding setter is invoked. | 289 // Set to true once the corresponding setter is invoked. |
| 209 bool enabled_color_set_; | 290 bool enabled_color_set_; |
| 210 bool disabled_color_set_; | 291 bool disabled_color_set_; |
| 211 bool background_color_set_; | 292 bool background_color_set_; |
| 293 bool selection_text_color_set_; | |
| 294 bool selection_background_color_set_; | |
| 212 | 295 |
| 213 gfx::ElideBehavior elide_behavior_; | 296 gfx::ElideBehavior elide_behavior_; |
| 214 | 297 |
| 215 bool subpixel_rendering_enabled_; | 298 bool subpixel_rendering_enabled_; |
| 216 bool auto_color_readability_; | 299 bool auto_color_readability_; |
| 217 // TODO(mukai): remove |multi_line_| when all RenderText can render multiline. | 300 // TODO(mukai): remove |multi_line_| when all RenderText can render multiline. |
| 218 bool multi_line_; | 301 bool multi_line_; |
| 219 base::string16 tooltip_text_; | 302 base::string16 tooltip_text_; |
| 220 bool handles_tooltips_; | 303 bool handles_tooltips_; |
| 221 // Whether to collapse the label when it's not visible. | 304 // Whether to collapse the label when it's not visible. |
| 222 bool collapse_when_hidden_; | 305 bool collapse_when_hidden_; |
| 223 int fixed_width_; | 306 int fixed_width_; |
| 224 int max_width_; | 307 int max_width_; |
| 225 | 308 |
| 226 // TODO(ckocagil): Remove is_first_paint_text_ before crbug.com/441028 is | 309 // TODO(ckocagil): Remove is_first_paint_text_ before crbug.com/441028 is |
| 227 // closed. | 310 // closed. |
| 228 bool is_first_paint_text_; | 311 bool is_first_paint_text_; |
| 229 | 312 |
| 313 std::unique_ptr<SelectionController> selection_controller_; | |
| 314 | |
| 230 DISALLOW_COPY_AND_ASSIGN(Label); | 315 DISALLOW_COPY_AND_ASSIGN(Label); |
| 231 }; | 316 }; |
| 232 | 317 |
| 233 } // namespace views | 318 } // namespace views |
| 234 | 319 |
| 235 #endif // UI_VIEWS_CONTROLS_LABEL_H_ | 320 #endif // UI_VIEWS_CONTROLS_LABEL_H_ |
| OLD | NEW |