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

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

Issue 2408623002: Views: Extract text selection code from Textfield. (Closed)
Patch Set: Rebase. 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 void OnBeforeSelectionUpdated() override;
361 void OnAfterSelectionUpdated() override;
362 void PasteSelectionClipboard(const ui::MouseEvent& event) override;
363 void UpdateSelectionClipboard() override;
364
348 // Handles a request to change the value of this text field from software 365 // Handles a request to change the value of this text field from software
349 // using an accessibility API (typically automation software, screen readers 366 // using an accessibility API (typically automation software, screen readers
350 // don't normally use this). Sets the value and clears the selection. 367 // don't normally use this). Sets the value and clears the selection.
351 void AccessibilitySetValue(const base::string16& new_value); 368 void AccessibilitySetValue(const base::string16& new_value);
352 369
353 // Updates the painted background color. 370 // Updates the painted background color.
354 void UpdateBackgroundColor(); 371 void UpdateBackgroundColor();
355 372
356 // Updates the border per the state of |invalid_|. 373 // Updates the border per the state of |invalid_|.
357 void UpdateBorder(); 374 void UpdateBorder();
358 375
359 // Does necessary updates when the text and/or cursor position changes. 376 // Does necessary updates when the text and/or cursor position changes. NO-OP
msw 2016/10/18 23:15:05 nit: put NO-OP on the next line.
karandeepb 2016/10/20 04:12:57 Obsolete.
377 // if |performing_mouse_action_| is true to ensure updates are only made when
378 // the mouse action completes i.e. on the call to OnAfterMouseAction.
360 void UpdateAfterChange(bool text_changed, bool cursor_changed); 379 void UpdateAfterChange(bool text_changed, bool cursor_changed);
361 380
362 // A callback function to periodically update the cursor state. 381 // A callback function to periodically update the cursor state.
363 void UpdateCursor(); 382 void UpdateCursor();
364 383
365 // Repaint the cursor. 384 // Repaint the cursor.
366 void RepaintCursor(); 385 void RepaintCursor();
367 386
368 void PaintTextAndCursor(gfx::Canvas* canvas); 387 void PaintTextAndCursor(gfx::Canvas* canvas);
369 388
370 // Helper function to call MoveCursorTo on the TextfieldModel. 389 // Helper function to call MoveCursorTo on the TextfieldModel.
371 void MoveCursorTo(const gfx::Point& point, bool select); 390 void MoveCursorTo(const gfx::Point& point, bool select);
372 391
373 // Helper function to update the selection on a mouse drag.
374 void SelectThroughLastDragLocation();
375
376 // Convenience method to notify the InputMethod and TouchSelectionController. 392 // Convenience method to notify the InputMethod and TouchSelectionController.
377 void OnCaretBoundsChanged(); 393 void OnCaretBoundsChanged();
378 394
379 // Convenience method to call TextfieldController::OnBeforeUserAction(); 395 // Convenience method to call TextfieldController::OnBeforeUserAction();
380 void OnBeforeUserAction(); 396 void OnBeforeUserAction();
381 397
382 // Convenience method to call TextfieldController::OnAfterUserAction(); 398 // Convenience method to call TextfieldController::OnAfterUserAction();
383 void OnAfterUserAction(); 399 void OnAfterUserAction();
384 400
385 // Calls |model_->Cut()| and notifies TextfieldController on success. 401 // Calls |model_->Cut()| and notifies TextfieldController on success.
386 bool Cut(); 402 bool Cut();
387 403
388 // Calls |model_->Copy()| and notifies TextfieldController on success. 404 // Calls |model_->Copy()| and notifies TextfieldController on success.
389 bool Copy(); 405 bool Copy();
390 406
391 // Calls |model_->Paste()| and calls TextfieldController::ContentsChanged() 407 // Calls |model_->Paste()| and calls TextfieldController::ContentsChanged()
392 // explicitly if paste succeeded. 408 // explicitly if paste succeeded.
393 bool Paste(); 409 bool Paste();
394 410
395 // Utility function to prepare the context menu. 411 // Utility function to prepare the context menu.
396 void UpdateContextMenu(); 412 void UpdateContextMenu();
397 413
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. 414 // Returns true if the current text input type allows access by the IME.
402 bool ImeEditingAllowed() const; 415 bool ImeEditingAllowed() const;
403 416
404 // Reveals the password character at |index| for a set duration. 417 // Reveals the password character at |index| for a set duration.
405 // If |index| is -1, the existing revealed character will be reset. 418 // If |index| is -1, the existing revealed character will be reset.
406 void RevealPasswordChar(int index); 419 void RevealPasswordChar(int index);
407 420
408 void CreateTouchSelectionControllerAndNotifyIt(); 421 void CreateTouchSelectionControllerAndNotifyIt();
409 422
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. 423 // Called when editing a textfield fails because the textfield is readonly.
418 void OnEditFailed(); 424 void OnEditFailed();
419 425
420 // Returns true if an insertion cursor should be visible (a vertical bar, 426 // Returns true if an insertion cursor should be visible (a vertical bar,
421 // placed at the point new text will be inserted). 427 // placed at the point new text will be inserted).
422 bool ShouldShowCursor() const; 428 bool ShouldShowCursor() const;
423 429
424 // Returns true if an insertion cursor should be visible and blinking. 430 // Returns true if an insertion cursor should be visible and blinking.
425 bool ShouldBlinkCursor() const; 431 bool ShouldBlinkCursor() const;
426 432
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 // The input flags of this text field. 490 // The input flags of this text field.
485 int text_input_flags_; 491 int text_input_flags_;
486 492
487 // The timer to reveal the last typed password character. 493 // The timer to reveal the last typed password character.
488 base::OneShotTimer password_reveal_timer_; 494 base::OneShotTimer password_reveal_timer_;
489 495
490 // Tracks whether a user action is being performed; i.e. OnBeforeUserAction() 496 // Tracks whether a user action is being performed; i.e. OnBeforeUserAction()
491 // has been called, but OnAfterUserAction() has not yet been called. 497 // has been called, but OnAfterUserAction() has not yet been called.
492 bool performing_user_action_; 498 bool performing_user_action_;
493 499
500 // Tracks whether a mouse action is being performed i.e. OnBeforeMouseAction()
501 // has been called, but OnAfterMouseAction() has not yet been called.
502 bool performing_mouse_action_;
503
494 // True if InputMethod::CancelComposition() should not be called. 504 // True if InputMethod::CancelComposition() should not be called.
495 bool skip_input_method_cancel_composition_; 505 bool skip_input_method_cancel_composition_;
496 506
497 // Insertion cursor repaint timer and visibility. 507 // Insertion cursor repaint timer and visibility.
498 base::RepeatingTimer cursor_blink_timer_; 508 base::RepeatingTimer cursor_blink_timer_;
499 509
500 // The drop cursor is a visual cue for where dragged text will be dropped. 510 // The drop cursor is a visual cue for where dragged text will be dropped.
501 bool drop_cursor_visible_; 511 bool drop_cursor_visible_;
502 gfx::SelectionModel drop_cursor_position_; 512 gfx::SelectionModel drop_cursor_position_;
503 513
504 // Is the user potentially dragging and dropping from this view? 514 // Is the user potentially dragging and dropping from this view?
505 bool initiating_drag_; 515 bool initiating_drag_;
506 516
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> 517 std::unique_ptr<ui::TouchEditingControllerDeprecated>
518 touch_selection_controller_; 518 touch_selection_controller_;
519 519
520 std::unique_ptr<SelectionController> selection_controller_;
521
520 // Used to track touch drag starting location and offset to enable touch 522 // Used to track touch drag starting location and offset to enable touch
521 // scrolling. 523 // scrolling.
522 gfx::Point drag_start_location_; 524 gfx::Point drag_start_location_;
523 int drag_start_display_offset_; 525 int drag_start_display_offset_;
524 526
525 // Tracks if touch editing handles are hidden because user has started 527 // Tracks if touch editing handles are hidden because user has started
526 // scrolling. If |true|, handles are shown after scrolling ends. 528 // scrolling. If |true|, handles are shown after scrolling ends.
527 bool touch_handles_hidden_due_to_scroll_; 529 bool touch_handles_hidden_due_to_scroll_;
528 530
529 // True if this textfield should use a focus ring to indicate focus. 531 // True if this textfield should use a focus ring to indicate focus.
530 bool use_focus_ring_; 532 bool use_focus_ring_;
531 533
532 // Context menu related members. 534 // Context menu related members.
533 std::unique_ptr<ui::SimpleMenuModel> context_menu_contents_; 535 std::unique_ptr<ui::SimpleMenuModel> context_menu_contents_;
534 std::unique_ptr<views::MenuRunner> context_menu_runner_; 536 std::unique_ptr<views::MenuRunner> context_menu_runner_;
535 537
536 // Used to bind callback functions to this object. 538 // Used to bind callback functions to this object.
537 base::WeakPtrFactory<Textfield> weak_ptr_factory_; 539 base::WeakPtrFactory<Textfield> weak_ptr_factory_;
538 540
539 DISALLOW_COPY_AND_ASSIGN(Textfield); 541 DISALLOW_COPY_AND_ASSIGN(Textfield);
540 }; 542 };
541 543
542 } // namespace views 544 } // namespace views
543 545
544 #endif // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_ 546 #endif // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698