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

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

Issue 2408623002: Views: Extract text selection code from Textfield. (Closed)
Patch Set: Created 4 years, 2 months 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_TEXTFIELD_TEXTFIELD_H_ 5 #ifndef UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_
6 #define UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_ 6 #define UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 11 matching lines...) Expand all
22 #include "ui/base/models/simple_menu_model.h" 22 #include "ui/base/models/simple_menu_model.h"
23 #include "ui/base/touch/touch_editing_controller.h" 23 #include "ui/base/touch/touch_editing_controller.h"
24 #include "ui/events/keycodes/keyboard_codes.h" 24 #include "ui/events/keycodes/keyboard_codes.h"
25 #include "ui/gfx/font_list.h" 25 #include "ui/gfx/font_list.h"
26 #include "ui/gfx/range/range.h" 26 #include "ui/gfx/range/range.h"
27 #include "ui/gfx/selection_model.h" 27 #include "ui/gfx/selection_model.h"
28 #include "ui/gfx/text_constants.h" 28 #include "ui/gfx/text_constants.h"
29 #include "ui/views/context_menu_controller.h" 29 #include "ui/views/context_menu_controller.h"
30 #include "ui/views/controls/textfield/textfield_model.h" 30 #include "ui/views/controls/textfield/textfield_model.h"
31 #include "ui/views/drag_controller.h" 31 #include "ui/views/drag_controller.h"
32 #include "ui/views/selection_controller_delegate.h"
32 #include "ui/views/view.h" 33 #include "ui/views/view.h"
33 #include "ui/views/word_lookup_client.h" 34 #include "ui/views/word_lookup_client.h"
34 35
35 namespace views { 36 namespace views {
36 37
37 class MenuRunner; 38 class MenuRunner;
38 class Painter; 39 class Painter;
40 class SelectionController;
39 class TextfieldController; 41 class TextfieldController;
40 42
41 // A views/skia textfield implementation. No platform-specific code is used. 43 // A views/skia textfield implementation. No platform-specific code is used.
42 class VIEWS_EXPORT Textfield : public View, 44 class VIEWS_EXPORT Textfield : public View,
43 public TextfieldModel::Delegate, 45 public TextfieldModel::Delegate,
44 public ContextMenuController, 46 public ContextMenuController,
45 public DragController, 47 public DragController,
46 public WordLookupClient, 48 public WordLookupClient,
49 public SelectionControllerDelegate,
47 public ui::TouchEditable, 50 public ui::TouchEditable,
48 public ui::TextInputClient { 51 public ui::TextInputClient {
49 public: 52 public:
50 // The textfield's class name. 53 // The textfield's class name.
51 static const char kViewClassName[]; 54 static const char kViewClassName[];
52 55
53 // The preferred size of the padding to be used around textfield text. 56 // The preferred size of the padding to be used around textfield text.
54 static const int kTextPadding; 57 static const int kTextPadding;
55 58
56 // Returns the text cursor blink time in milliseconds, or 0 for no blinking. 59 // Returns the text cursor blink time in milliseconds, or 0 for no blinking.
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 // Empty and invalid ranges are ignored. 206 // Empty and invalid ranges are ignored.
204 void SetStyle(gfx::TextStyle style, bool value); 207 void SetStyle(gfx::TextStyle style, bool value);
205 void ApplyStyle(gfx::TextStyle style, bool value, const gfx::Range& range); 208 void ApplyStyle(gfx::TextStyle style, bool value, const gfx::Range& range);
206 209
207 // Clears Edit history. 210 // Clears Edit history.
208 void ClearEditHistory(); 211 void ClearEditHistory();
209 212
210 // Set the accessible name of the text field. 213 // Set the accessible name of the text field.
211 void SetAccessibleName(const base::string16& name); 214 void SetAccessibleName(const base::string16& name);
212 215
213 // Returns whether there is a drag operation originating from the textfield.
214 bool HasTextBeingDragged();
215
216 // View overrides: 216 // View overrides:
217 gfx::Insets GetInsets() const override; 217 gfx::Insets GetInsets() const override;
218 int GetBaseline() const override; 218 int GetBaseline() const override;
219 gfx::Size GetPreferredSize() const override; 219 gfx::Size GetPreferredSize() const override;
220 const char* GetClassName() const override; 220 const char* GetClassName() const override;
221 void SetBorder(std::unique_ptr<Border> b) override; 221 void SetBorder(std::unique_ptr<Border> b) override;
222 gfx::NativeCursor GetCursor(const ui::MouseEvent& event) override; 222 gfx::NativeCursor GetCursor(const ui::MouseEvent& event) override;
223 bool OnMousePressed(const ui::MouseEvent& event) override; 223 bool OnMousePressed(const ui::MouseEvent& event) override;
224 bool OnMouseDragged(const ui::MouseEvent& event) override; 224 bool OnMouseDragged(const ui::MouseEvent& event) override;
225 void OnMouseReleased(const ui::MouseEvent& event) override; 225 void OnMouseReleased(const ui::MouseEvent& event) override;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 int GetDragOperationsForView(View* sender, const gfx::Point& p) override; 263 int GetDragOperationsForView(View* sender, const gfx::Point& p) override;
264 bool CanStartDragForView(View* sender, 264 bool CanStartDragForView(View* sender,
265 const gfx::Point& press_pt, 265 const gfx::Point& press_pt,
266 const gfx::Point& p) override; 266 const gfx::Point& p) override;
267 267
268 // WordLookupClient overrides: 268 // WordLookupClient overrides:
269 bool GetDecoratedWordAtPoint(const gfx::Point& point, 269 bool GetDecoratedWordAtPoint(const gfx::Point& point,
270 gfx::DecoratedText* decorated_word, 270 gfx::DecoratedText* decorated_word,
271 gfx::Point* baseline_point) override; 271 gfx::Point* baseline_point) override;
272 272
273 // SelectionControllerDelegate overrides:
274 bool HasTextBeingDragged() const override;
275
273 // ui::TouchEditable overrides: 276 // ui::TouchEditable overrides:
274 void SelectRect(const gfx::Point& start, const gfx::Point& end) override; 277 void SelectRect(const gfx::Point& start, const gfx::Point& end) override;
275 void MoveCaretTo(const gfx::Point& point) override; 278 void MoveCaretTo(const gfx::Point& point) override;
276 void GetSelectionEndPoints(gfx::SelectionBound* anchor, 279 void GetSelectionEndPoints(gfx::SelectionBound* anchor,
277 gfx::SelectionBound* focus) override; 280 gfx::SelectionBound* focus) override;
278 gfx::Rect GetBounds() override; 281 gfx::Rect GetBounds() override;
279 gfx::NativeView GetNativeView() const override; 282 gfx::NativeView GetNativeView() const override;
280 void ConvertPointToScreen(gfx::Point* point) override; 283 void ConvertPointToScreen(gfx::Point* point) override;
281 void ConvertPointFromScreen(gfx::Point* point) override; 284 void ConvertPointFromScreen(gfx::Point* point) override;
282 bool DrawsHandles() override; 285 bool DrawsHandles() override;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 bool IsTextEditCommandEnabled(ui::TextEditCommand command) const override; 323 bool IsTextEditCommandEnabled(ui::TextEditCommand command) const override;
321 void SetTextEditCommandForNextKeyEvent(ui::TextEditCommand command) override; 324 void SetTextEditCommandForNextKeyEvent(ui::TextEditCommand command) override;
322 325
323 protected: 326 protected:
324 // Inserts or appends a character in response to an IME operation. 327 // Inserts or appends a character in response to an IME operation.
325 virtual void DoInsertChar(base::char16 ch); 328 virtual void DoInsertChar(base::char16 ch);
326 329
327 // Returns the TextfieldModel's text/cursor/selection rendering model. 330 // Returns the TextfieldModel's text/cursor/selection rendering model.
328 gfx::RenderText* GetRenderText() const; 331 gfx::RenderText* GetRenderText() const;
329 332
330 gfx::Point last_click_location() const { return last_click_location_; } 333 gfx::Point GetLastClickLocation() const;
331 334
332 // Get the text from the selection clipboard. 335 // Get the text from the selection clipboard.
333 virtual base::string16 GetSelectionClipboardText() const; 336 virtual base::string16 GetSelectionClipboardText() const;
334 337
335 // Executes the given |command|. 338 // Executes the given |command|.
336 virtual void ExecuteTextEditCommand(ui::TextEditCommand command); 339 virtual void ExecuteTextEditCommand(ui::TextEditCommand command);
337 340
338 private: 341 private:
339 friend class TextfieldTestApi; 342 friend class TextfieldTestApi;
340 343
341 // View overrides: 344 // View overrides:
342 // Declared final since overriding by subclasses would interfere with the 345 // Declared final since overriding by subclasses would interfere with the
343 // accounting related to the scheduled text edit command. Subclasses should 346 // accounting related to the scheduled text edit command. Subclasses should
344 // use TextfieldController::HandleKeyEvent, to intercept the key event. 347 // use TextfieldController::HandleKeyEvent, to intercept the key event.
345 bool OnKeyPressed(const ui::KeyEvent& event) final; 348 bool OnKeyPressed(const ui::KeyEvent& event) final;
346 bool OnKeyReleased(const ui::KeyEvent& event) final; 349 bool OnKeyReleased(const ui::KeyEvent& event) final;
347 350
351 // SelectionControllerDelegate overrides:
352 gfx::RenderText* GetRenderTextForSelectionController() override;
353 bool IsReadOnly() const override;
354 void SetTextBeingDragged(bool value) override;
355 int GetViewHeight() const override;
356 int GetViewWidth() const override;
357 int GetDragSelectionDelay() const override;
358 void OnBeforeMouseAction() override;
359 void OnAfterMouseAction(bool text_changed, bool selection_changed) override;
360 bool PasteSelectionClipboard() override;
361 void UpdateSelectionClipboard() override;
362
348 // Handles a request to change the value of this text field from software 363 // Handles a request to change the value of this text field from software
349 // using an accessibility API (typically automation software, screen readers 364 // using an accessibility API (typically automation software, screen readers
350 // don't normally use this). Sets the value and clears the selection. 365 // don't normally use this). Sets the value and clears the selection.
351 void AccessibilitySetValue(const base::string16& new_value); 366 void AccessibilitySetValue(const base::string16& new_value);
352 367
353 // Updates the painted background color. 368 // Updates the painted background color.
354 void UpdateBackgroundColor(); 369 void UpdateBackgroundColor();
355 370
356 // Updates the border per the state of |invalid_|. 371 // Updates the border per the state of |invalid_|.
357 void UpdateBorder(); 372 void UpdateBorder();
358 373
359 // Does necessary updates when the text and/or cursor position changes. 374 // Does necessary updates when the text and/or cursor position changes.
360 void UpdateAfterChange(bool text_changed, bool cursor_changed); 375 void UpdateAfterChange(bool text_changed, bool cursor_changed);
361 376
362 // A callback function to periodically update the cursor state. 377 // A callback function to periodically update the cursor state.
363 void UpdateCursor(); 378 void UpdateCursor();
364 379
365 // Repaint the cursor. 380 // Repaint the cursor.
366 void RepaintCursor(); 381 void RepaintCursor();
367 382
368 void PaintTextAndCursor(gfx::Canvas* canvas); 383 void PaintTextAndCursor(gfx::Canvas* canvas);
369 384
370 // Helper function to call MoveCursorTo on the TextfieldModel. 385 // Helper function to call MoveCursorTo on the TextfieldModel.
371 void MoveCursorTo(const gfx::Point& point, bool select); 386 void MoveCursorTo(const gfx::Point& point, bool select);
372 387
373 // Helper function to update the selection on a mouse drag.
374 void SelectThroughLastDragLocation();
375
376 // Convenience method to notify the InputMethod and TouchSelectionController. 388 // Convenience method to notify the InputMethod and TouchSelectionController.
377 void OnCaretBoundsChanged(); 389 void OnCaretBoundsChanged();
378 390
379 // Convenience method to call TextfieldController::OnBeforeUserAction(); 391 // Convenience method to call TextfieldController::OnBeforeUserAction();
380 void OnBeforeUserAction(); 392 void OnBeforeUserAction();
381 393
382 // Convenience method to call TextfieldController::OnAfterUserAction(); 394 // Convenience method to call TextfieldController::OnAfterUserAction();
383 void OnAfterUserAction(); 395 void OnAfterUserAction();
384 396
385 // Calls |model_->Cut()| and notifies TextfieldController on success. 397 // Calls |model_->Cut()| and notifies TextfieldController on success.
386 bool Cut(); 398 bool Cut();
387 399
388 // Calls |model_->Copy()| and notifies TextfieldController on success. 400 // Calls |model_->Copy()| and notifies TextfieldController on success.
389 bool Copy(); 401 bool Copy();
390 402
391 // Calls |model_->Paste()| and calls TextfieldController::ContentsChanged() 403 // Calls |model_->Paste()| and calls TextfieldController::ContentsChanged()
392 // explicitly if paste succeeded. 404 // explicitly if paste succeeded.
393 bool Paste(); 405 bool Paste();
394 406
395 // Utility function to prepare the context menu. 407 // Utility function to prepare the context menu.
396 void UpdateContextMenu(); 408 void UpdateContextMenu();
397 409
398 // Tracks the mouse clicks for single/double/triple clicks.
399 void TrackMouseClicks(const ui::MouseEvent& event);
400
401 // Returns true if the current text input type allows access by the IME. 410 // Returns true if the current text input type allows access by the IME.
402 bool ImeEditingAllowed() const; 411 bool ImeEditingAllowed() const;
403 412
404 // Reveals the password character at |index| for a set duration. 413 // Reveals the password character at |index| for a set duration.
405 // If |index| is -1, the existing revealed character will be reset. 414 // If |index| is -1, the existing revealed character will be reset.
406 void RevealPasswordChar(int index); 415 void RevealPasswordChar(int index);
407 416
408 void CreateTouchSelectionControllerAndNotifyIt(); 417 void CreateTouchSelectionControllerAndNotifyIt();
409 418
410 // Updates the selection clipboard to any non-empty text selection for a non-
411 // password textfield.
412 void UpdateSelectionClipboard() const;
413
414 // Pastes the selection clipboard for the specified mouse event.
415 void PasteSelectionClipboard(const ui::MouseEvent& event);
416
417 // Called when editing a textfield fails because the textfield is readonly. 419 // Called when editing a textfield fails because the textfield is readonly.
418 void OnEditFailed(); 420 void OnEditFailed();
419 421
420 // Returns true if an insertion cursor should be visible (a vertical bar, 422 // Returns true if an insertion cursor should be visible (a vertical bar,
421 // placed at the point new text will be inserted). 423 // placed at the point new text will be inserted).
422 bool ShouldShowCursor() const; 424 bool ShouldShowCursor() const;
423 425
424 // Returns true if an insertion cursor should be visible and blinking. 426 // Returns true if an insertion cursor should be visible and blinking.
425 bool ShouldBlinkCursor() const; 427 bool ShouldBlinkCursor() const;
426 428
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 482
481 // The input type of this text field. 483 // The input type of this text field.
482 ui::TextInputType text_input_type_; 484 ui::TextInputType text_input_type_;
483 485
484 // The input flags of this text field. 486 // The input flags of this text field.
485 int text_input_flags_; 487 int text_input_flags_;
486 488
487 // The timer to reveal the last typed password character. 489 // The timer to reveal the last typed password character.
488 base::OneShotTimer password_reveal_timer_; 490 base::OneShotTimer password_reveal_timer_;
489 491
490 // Tracks whether a user action is being performed; i.e. OnBeforeUserAction() 492 // Tracks whether a user action is being performed which may change the
491 // has been called, but OnAfterUserAction() has not yet been called. 493 // textfield; i.e. OnBeforeUserAction() has been called, but
494 // OnAfterUserAction() has not yet been called.
492 bool performing_user_action_; 495 bool performing_user_action_;
493 496
494 // True if InputMethod::CancelComposition() should not be called. 497 // True if InputMethod::CancelComposition() should not be called.
495 bool skip_input_method_cancel_composition_; 498 bool skip_input_method_cancel_composition_;
496 499
497 // Insertion cursor repaint timer and visibility. 500 // Insertion cursor repaint timer and visibility.
498 base::RepeatingTimer cursor_blink_timer_; 501 base::RepeatingTimer cursor_blink_timer_;
499 502
500 // The drop cursor is a visual cue for where dragged text will be dropped. 503 // The drop cursor is a visual cue for where dragged text will be dropped.
501 bool drop_cursor_visible_; 504 bool drop_cursor_visible_;
502 gfx::SelectionModel drop_cursor_position_; 505 gfx::SelectionModel drop_cursor_position_;
503 506
504 // Is the user potentially dragging and dropping from this view? 507 // Is the user potentially dragging and dropping from this view?
505 bool initiating_drag_; 508 bool initiating_drag_;
506 509
507 // A timer and point used to modify the selection when dragging.
508 base::RepeatingTimer drag_selection_timer_;
509 gfx::Point last_drag_location_;
510
511 // State variables used to track double and triple clicks.
512 size_t aggregated_clicks_;
513 base::TimeTicks last_click_time_;
514 gfx::Point last_click_location_;
515 gfx::Range double_click_word_;
516
517 std::unique_ptr<ui::TouchEditingControllerDeprecated> 510 std::unique_ptr<ui::TouchEditingControllerDeprecated>
518 touch_selection_controller_; 511 touch_selection_controller_;
519 512
513 std::unique_ptr<SelectionController> selection_controller_;
msw 2016/10/21 02:14:16 nit: make this a member object without using uniqu
karandeepb 2016/10/21 05:04:32 Done.
514
520 // Used to track touch drag starting location and offset to enable touch 515 // Used to track touch drag starting location and offset to enable touch
521 // scrolling. 516 // scrolling.
522 gfx::Point drag_start_location_; 517 gfx::Point drag_start_location_;
523 int drag_start_display_offset_; 518 int drag_start_display_offset_;
524 519
525 // Tracks if touch editing handles are hidden because user has started 520 // Tracks if touch editing handles are hidden because user has started
526 // scrolling. If |true|, handles are shown after scrolling ends. 521 // scrolling. If |true|, handles are shown after scrolling ends.
527 bool touch_handles_hidden_due_to_scroll_; 522 bool touch_handles_hidden_due_to_scroll_;
528 523
529 // True if this textfield should use a focus ring to indicate focus. 524 // True if this textfield should use a focus ring to indicate focus.
530 bool use_focus_ring_; 525 bool use_focus_ring_;
531 526
532 // Context menu related members. 527 // Context menu related members.
533 std::unique_ptr<ui::SimpleMenuModel> context_menu_contents_; 528 std::unique_ptr<ui::SimpleMenuModel> context_menu_contents_;
534 std::unique_ptr<views::MenuRunner> context_menu_runner_; 529 std::unique_ptr<views::MenuRunner> context_menu_runner_;
535 530
536 // Used to bind callback functions to this object. 531 // Used to bind callback functions to this object.
537 base::WeakPtrFactory<Textfield> weak_ptr_factory_; 532 base::WeakPtrFactory<Textfield> weak_ptr_factory_;
538 533
539 DISALLOW_COPY_AND_ASSIGN(Textfield); 534 DISALLOW_COPY_AND_ASSIGN(Textfield);
540 }; 535 };
541 536
542 } // namespace views 537 } // namespace views
543 538
544 #endif // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_ 539 #endif // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698