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

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

Powered by Google App Engine
This is Rietveld 408576698