Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "ui/views/selection_controller.h" | 5 #include "ui/views/selection_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ui/events/event.h" | 9 #include "ui/events/event.h" |
| 10 #include "ui/gfx/render_text.h" | 10 #include "ui/gfx/render_text.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 delegate_->SetTextBeingDragged(true); | 49 delegate_->SetTextBeingDragged(true); |
| 50 } else { | 50 } else { |
| 51 delegate_->OnBeforePointerAction(); | 51 delegate_->OnBeforePointerAction(); |
| 52 const bool selection_changed = | 52 const bool selection_changed = |
| 53 render_text->MoveCursorTo(event.location(), event.IsShiftDown()); | 53 render_text->MoveCursorTo(event.location(), event.IsShiftDown()); |
| 54 delegate_->OnAfterPointerAction(false, selection_changed); | 54 delegate_->OnAfterPointerAction(false, selection_changed); |
| 55 } | 55 } |
| 56 break; | 56 break; |
| 57 case 1: | 57 case 1: |
| 58 // Select the word at the click location on a double click. | 58 // Select the word at the click location on a double click. |
| 59 delegate_->OnBeforePointerAction(); | 59 SelectWord(event.location()); |
| 60 render_text->MoveCursorTo(event.location(), false); | |
| 61 render_text->SelectWord(); | |
| 62 delegate_->OnAfterPointerAction(false, true); | |
| 63 double_click_word_ = render_text->selection(); | 60 double_click_word_ = render_text->selection(); |
| 64 break; | 61 break; |
| 65 case 2: | 62 case 2: |
| 66 // Select all the text on a triple click. | 63 // Select all the text on a triple click. |
| 67 delegate_->OnBeforePointerAction(); | 64 delegate_->OnBeforePointerAction(); |
| 68 render_text->SelectAll(false); | 65 render_text->SelectAll(false); |
| 69 delegate_->OnAfterPointerAction(false, true); | 66 delegate_->OnAfterPointerAction(false, true); |
| 70 break; | 67 break; |
| 71 default: | 68 default: |
| 72 NOTREACHED(); | 69 NOTREACHED(); |
| 73 } | 70 } |
| 74 } | 71 } |
| 75 | 72 |
| 73 // TODO(crbug.com/676296): Right clicking an unfocused text view should select | |
| 74 // all its text on Mac. | |
| 75 const bool select_word_on_right_click = | |
| 76 event.IsOnlyRightMouseButton() && | |
| 77 PlatformStyle::kSelectWordOnRightClick && | |
| 78 !render_text->IsPointInSelection(event.location()); | |
| 79 if (select_word_on_right_click) | |
| 80 SelectWord(event.location()); | |
| 81 | |
| 76 if (handles_selection_clipboard_ && event.IsOnlyMiddleMouseButton()) { | 82 if (handles_selection_clipboard_ && event.IsOnlyMiddleMouseButton()) { |
| 77 if (render_text->IsPointInSelection(event.location())) { | 83 if (render_text->IsPointInSelection(event.location())) { |
| 78 delegate_->OnBeforePointerAction(); | 84 delegate_->OnBeforePointerAction(); |
| 79 render_text->ClearSelection(); | 85 render_text->ClearSelection(); |
| 80 delegate_->UpdateSelectionClipboard(); | 86 delegate_->UpdateSelectionClipboard(); |
| 81 delegate_->OnAfterPointerAction(false, true); | 87 delegate_->OnAfterPointerAction(false, true); |
| 82 } else if (!delegate_->IsReadOnly()) { | 88 } else if (!delegate_->IsReadOnly()) { |
| 83 delegate_->OnBeforePointerAction(); | 89 delegate_->OnBeforePointerAction(); |
| 84 const bool selection_changed = | 90 const bool selection_changed = |
| 85 render_text->MoveCursorTo(event.location(), false); | 91 render_text->MoveCursorTo(event.location(), false); |
| 86 const bool text_changed = delegate_->PasteSelectionClipboard(); | 92 const bool text_changed = delegate_->PasteSelectionClipboard(); |
| 87 delegate_->OnAfterPointerAction(text_changed, | 93 delegate_->OnAfterPointerAction(text_changed, |
| 88 selection_changed | text_changed); | 94 selection_changed | text_changed); |
| 89 } | 95 } |
| 90 } | 96 } |
| 91 | 97 |
| 92 return true; | 98 return true; |
| 93 } | 99 } |
| 94 | 100 |
| 101 void SelectionController::SelectWord(const gfx::Point& point) { | |
|
tapted
2016/12/22 05:34:46
nit: reorder to match header (between TrackMouseCl
karandeepb
2016/12/22 08:51:25
Done.
| |
| 102 gfx::RenderText* render_text = GetRenderText(); | |
| 103 DCHECK(render_text); | |
| 104 delegate_->OnBeforePointerAction(); | |
| 105 render_text->MoveCursorTo(point, false); | |
| 106 render_text->SelectWord(); | |
| 107 delegate_->OnAfterPointerAction(false, true); | |
| 108 } | |
| 109 | |
| 95 bool SelectionController::OnMouseDragged(const ui::MouseEvent& event) { | 110 bool SelectionController::OnMouseDragged(const ui::MouseEvent& event) { |
| 96 DCHECK(GetRenderText()); | 111 DCHECK(GetRenderText()); |
| 97 // If |drag_selection_timer_| is running, |last_drag_location_| will be used | 112 // If |drag_selection_timer_| is running, |last_drag_location_| will be used |
| 98 // to update the selection. | 113 // to update the selection. |
| 99 last_drag_location_ = event.location(); | 114 last_drag_location_ = event.location(); |
| 100 | 115 |
| 101 // Don't adjust the cursor on a potential drag and drop. | 116 // Don't adjust the cursor on a potential drag and drop. |
| 102 if (delegate_->HasTextBeingDragged() || !event.IsOnlyLeftMouseButton()) | 117 if (delegate_->HasTextBeingDragged() || !event.IsOnlyLeftMouseButton()) |
| 103 return true; | 118 return true; |
| 104 | 119 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 std::max(selection.GetMax(), double_click_word_.GetMax()); | 219 std::max(selection.GetMax(), double_click_word_.GetMax()); |
| 205 const bool reversed = selection.is_reversed(); | 220 const bool reversed = selection.is_reversed(); |
| 206 selection.set_start(reversed ? max : min); | 221 selection.set_start(reversed ? max : min); |
| 207 selection.set_end(reversed ? min : max); | 222 selection.set_end(reversed ? min : max); |
| 208 render_text->SelectRange(selection); | 223 render_text->SelectRange(selection); |
| 209 } | 224 } |
| 210 delegate_->OnAfterPointerAction(false, true); | 225 delegate_->OnAfterPointerAction(false, true); |
| 211 } | 226 } |
| 212 | 227 |
| 213 } // namespace views | 228 } // namespace views |
| OLD | NEW |