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

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

Issue 2422993002: views::Label: Implement context menu, keyboard shortcuts for copy/select all. (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
« no previous file with comments | « ui/views/cocoa/bridged_content_view.mm ('k') | ui/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) 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/base/models/simple_menu_model.h"
11 #include "ui/gfx/render_text.h" 12 #include "ui/gfx/render_text.h"
13 #include "ui/views/context_menu_controller.h"
12 #include "ui/views/selection_controller_delegate.h" 14 #include "ui/views/selection_controller_delegate.h"
13 #include "ui/views/view.h" 15 #include "ui/views/view.h"
14 16
15 namespace views { 17 namespace views {
16 class LabelSelectionTest; 18 class LabelSelectionTest;
19 class MenuRunner;
17 class SelectionController; 20 class SelectionController;
18 21
19 // A view subclass that can display a string. 22 // A view subclass that can display a string.
20 class VIEWS_EXPORT Label : public View, public SelectionControllerDelegate { 23 class VIEWS_EXPORT Label : public View,
24 public ContextMenuController,
25 public SelectionControllerDelegate,
26 public ui::SimpleMenuModel::Delegate {
21 public: 27 public:
22 // Internal class name. 28 // Internal class name.
23 static const char kViewClassName[]; 29 static const char kViewClassName[];
24 30
25 // The padding for the focus border when rendering focused text. 31 // The padding for the focus border when rendering focused text.
26 static const int kFocusBorderPadding; 32 static const int kFocusBorderPadding;
27 33
28 Label(); 34 Label();
29 explicit Label(const base::string16& text); 35 explicit Label(const base::string16& text);
30 Label(const base::string16& text, const gfx::FontList& font_list); 36 Label(const base::string16& text, const gfx::FontList& font_list);
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 // Sets whether the preferred size is empty when the label is not visible. 144 // Sets whether the preferred size is empty when the label is not visible.
139 void set_collapse_when_hidden(bool value) { collapse_when_hidden_ = value; } 145 void set_collapse_when_hidden(bool value) { collapse_when_hidden_ = value; }
140 146
141 // Get the text as displayed to the user, respecting the obscured flag. 147 // Get the text as displayed to the user, respecting the obscured flag.
142 base::string16 GetDisplayTextForTesting(); 148 base::string16 GetDisplayTextForTesting();
143 149
144 // Returns true if the label is selectable. Default is false. 150 // Returns true if the label is selectable. Default is false.
145 bool selectable() const { return !!selection_controller_; } 151 bool selectable() const { return !!selection_controller_; }
146 152
147 // Sets whether the label is selectable. False is returned if the call fails, 153 // 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. 154 // i.e. when selection is not supported but |selectable| is true. For example,
155 // obscured labels do not support text selection.
149 bool SetSelectable(bool selectable); 156 bool SetSelectable(bool selectable);
150 157
151 // Returns true if the label has a selection. 158 // Returns true if the label has a selection.
152 bool HasSelection() const; 159 bool HasSelection() const;
153 160
154 // Selects the entire text. NO-OP if the label is not selectable. 161 // Selects the entire text. NO-OP if the label is not selectable.
155 void SelectAll(); 162 void SelectAll();
156 163
157 // Clears any active selection. 164 // Clears any active selection.
158 void ClearSelection(); 165 void ClearSelection();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 void OnPaint(gfx::Canvas* canvas) override; 208 void OnPaint(gfx::Canvas* canvas) override;
202 void OnDeviceScaleFactorChanged(float device_scale_factor) override; 209 void OnDeviceScaleFactorChanged(float device_scale_factor) override;
203 void OnNativeThemeChanged(const ui::NativeTheme* theme) override; 210 void OnNativeThemeChanged(const ui::NativeTheme* theme) override;
204 gfx::NativeCursor GetCursor(const ui::MouseEvent& event) override; 211 gfx::NativeCursor GetCursor(const ui::MouseEvent& event) override;
205 void OnFocus() override; 212 void OnFocus() override;
206 void OnBlur() override; 213 void OnBlur() override;
207 bool OnMousePressed(const ui::MouseEvent& event) override; 214 bool OnMousePressed(const ui::MouseEvent& event) override;
208 bool OnMouseDragged(const ui::MouseEvent& event) override; 215 bool OnMouseDragged(const ui::MouseEvent& event) override;
209 void OnMouseReleased(const ui::MouseEvent& event) override; 216 void OnMouseReleased(const ui::MouseEvent& event) override;
210 void OnMouseCaptureLost() override; 217 void OnMouseCaptureLost() override;
218 bool OnKeyPressed(const ui::KeyEvent& event) override;
219 bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
220 bool CanHandleAccelerators() const override;
211 221
212 private: 222 private:
213 FRIEND_TEST_ALL_PREFIXES(LabelTest, ResetRenderTextData); 223 FRIEND_TEST_ALL_PREFIXES(LabelTest, ResetRenderTextData);
214 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultilineSupportedRenderText); 224 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultilineSupportedRenderText);
215 FRIEND_TEST_ALL_PREFIXES(LabelTest, TextChangeWithoutLayout); 225 FRIEND_TEST_ALL_PREFIXES(LabelTest, TextChangeWithoutLayout);
216 FRIEND_TEST_ALL_PREFIXES(LabelTest, EmptyLabel); 226 FRIEND_TEST_ALL_PREFIXES(LabelTest, EmptyLabel);
217 FRIEND_TEST_ALL_PREFIXES(LabelTest, FocusBounds); 227 FRIEND_TEST_ALL_PREFIXES(LabelTest, FocusBounds);
218 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultiLineSizingWithElide); 228 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultiLineSizingWithElide);
219 friend class LabelSelectionTest; 229 friend class LabelSelectionTest;
220 230
231 // ContextMenuController overrides:
232 void ShowContextMenuForView(View* source,
233 const gfx::Point& point,
234 ui::MenuSourceType source_type) override;
235
221 // SelectionControllerDelegate overrides: 236 // SelectionControllerDelegate overrides:
222 gfx::RenderText* GetRenderTextForSelectionController() override; 237 gfx::RenderText* GetRenderTextForSelectionController() override;
223 bool IsReadOnly() const override; 238 bool IsReadOnly() const override;
224 bool SupportsDrag() const override; 239 bool SupportsDrag() const override;
225 bool HasTextBeingDragged() const override; 240 bool HasTextBeingDragged() const override;
226 void SetTextBeingDragged(bool value) override; 241 void SetTextBeingDragged(bool value) override;
227 int GetViewHeight() const override; 242 int GetViewHeight() const override;
228 int GetViewWidth() const override; 243 int GetViewWidth() const override;
229 int GetDragSelectionDelay() const override; 244 int GetDragSelectionDelay() const override;
230 void OnBeforePointerAction() override; 245 void OnBeforePointerAction() override;
231 void OnAfterPointerAction(bool text_changed, bool selection_changed) override; 246 void OnAfterPointerAction(bool text_changed, bool selection_changed) override;
232 bool PasteSelectionClipboard() override; 247 bool PasteSelectionClipboard() override;
233 void UpdateSelectionClipboard() override; 248 void UpdateSelectionClipboard() override;
234 249
250 // ui::SimpleMenuModel::Delegate overrides:
251 bool IsCommandIdChecked(int command_id) const override;
252 bool IsCommandIdEnabled(int command_id) const override;
253 void ExecuteCommand(int command_id, int event_flags) override;
254 bool GetAcceleratorForCommandId(int command_id,
255 ui::Accelerator* accelerator) const override;
256
235 const gfx::RenderText* GetRenderTextForSelectionController() const; 257 const gfx::RenderText* GetRenderTextForSelectionController() const;
236 258
237 void Init(const base::string16& text, const gfx::FontList& font_list); 259 void Init(const base::string16& text, const gfx::FontList& font_list);
238 260
239 void ResetLayout(); 261 void ResetLayout();
240 262
241 // Set up |lines_| to actually be painted. 263 // Set up |lines_| to actually be painted.
242 void MaybeBuildRenderTextLines() const; 264 void MaybeBuildRenderTextLines() const;
243 265
244 gfx::Rect GetFocusBounds() const; 266 gfx::Rect GetFocusBounds() const;
(...skipping 11 matching lines...) Expand all
256 void ApplyTextColors() const; 278 void ApplyTextColors() const;
257 279
258 // Updates any colors that have not been explicitly set from the theme. 280 // Updates any colors that have not been explicitly set from the theme.
259 void UpdateColorsFromTheme(const ui::NativeTheme* theme); 281 void UpdateColorsFromTheme(const ui::NativeTheme* theme);
260 282
261 bool ShouldShowDefaultTooltip() const; 283 bool ShouldShowDefaultTooltip() const;
262 284
263 // Empties |lines_| and updates |stored_selection_range_|. 285 // Empties |lines_| and updates |stored_selection_range_|.
264 void ClearRenderTextLines() const; 286 void ClearRenderTextLines() const;
265 287
288 // Returns the currently selected text.
289 base::string16 GetSelectedText() const;
290
291 // Updates the clipboard with the currently selected text.
292 void CopyToClipboard();
293
294 // Builds |context_menu_contents_|.
295 void BuildContextMenuContents();
296
266 // An un-elided and single-line RenderText object used for preferred sizing. 297 // An un-elided and single-line RenderText object used for preferred sizing.
267 std::unique_ptr<gfx::RenderText> render_text_; 298 std::unique_ptr<gfx::RenderText> render_text_;
268 299
269 // The RenderText instances used to display elided and multi-line text. 300 // The RenderText instances used to display elided and multi-line text.
270 mutable std::vector<std::unique_ptr<gfx::RenderText>> lines_; 301 mutable std::vector<std::unique_ptr<gfx::RenderText>> lines_;
271 302
272 // Persists the current selection range between the calls to 303 // Persists the current selection range between the calls to
273 // ClearRenderTextLines() and MaybeBuildRenderTextLines(). Holds an 304 // ClearRenderTextLines() and MaybeBuildRenderTextLines(). Holds an
274 // InvalidRange when not in use. 305 // InvalidRange when not in use.
275 mutable gfx::Range stored_selection_range_; 306 mutable gfx::Range stored_selection_range_;
(...skipping 26 matching lines...) Expand all
302 bool collapse_when_hidden_; 333 bool collapse_when_hidden_;
303 int fixed_width_; 334 int fixed_width_;
304 int max_width_; 335 int max_width_;
305 336
306 // TODO(ckocagil): Remove is_first_paint_text_ before crbug.com/441028 is 337 // TODO(ckocagil): Remove is_first_paint_text_ before crbug.com/441028 is
307 // closed. 338 // closed.
308 bool is_first_paint_text_; 339 bool is_first_paint_text_;
309 340
310 std::unique_ptr<SelectionController> selection_controller_; 341 std::unique_ptr<SelectionController> selection_controller_;
311 342
343 // Context menu related members.
344 ui::SimpleMenuModel context_menu_contents_;
345 std::unique_ptr<views::MenuRunner> context_menu_runner_;
346
312 DISALLOW_COPY_AND_ASSIGN(Label); 347 DISALLOW_COPY_AND_ASSIGN(Label);
313 }; 348 };
314 349
315 } // namespace views 350 } // namespace views
316 351
317 #endif // UI_VIEWS_CONTROLS_LABEL_H_ 352 #endif // UI_VIEWS_CONTROLS_LABEL_H_
OLDNEW
« no previous file with comments | « ui/views/cocoa/bridged_content_view.mm ('k') | ui/views/controls/label.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698