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