| 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" |
| 11 #include "ui/views/metrics.h" | 11 #include "ui/views/metrics.h" |
| 12 #include "ui/views/selection_controller_delegate.h" | 12 #include "ui/views/selection_controller_delegate.h" |
| 13 #include "ui/views/style/platform_style.h" | 13 #include "ui/views/style/platform_style.h" |
| 14 #include "ui/views/view.h" | 14 #include "ui/views/view.h" |
| 15 | 15 |
| 16 namespace views { | 16 namespace views { |
| 17 | 17 |
| 18 SelectionController::SelectionController(SelectionControllerDelegate* delegate) | 18 SelectionController::SelectionController(SelectionControllerDelegate* delegate) |
| 19 : aggregated_clicks_(0), | 19 : aggregated_clicks_(0), |
| 20 delegate_(delegate), | 20 delegate_(delegate), |
| 21 handles_selection_clipboard_(false) { | 21 handles_selection_clipboard_(false) { |
| 22 // On Linux, update the selection clipboard on a text selection. | 22 // On Linux, update the selection clipboard on a text selection. |
| 23 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 23 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 24 set_handles_selection_clipboard(true); | 24 set_handles_selection_clipboard(true); |
| 25 #endif | 25 #endif |
| 26 | 26 |
| 27 DCHECK(delegate); | 27 DCHECK(delegate); |
| 28 } | 28 } |
| 29 | 29 |
| 30 bool SelectionController::OnMousePressed(const ui::MouseEvent& event, | 30 bool SelectionController::OnMousePressed( |
| 31 bool handled) { | 31 const ui::MouseEvent& event, |
| 32 bool handled, |
| 33 InitialFocusStateOnMousePress initial_focus_state) { |
| 32 gfx::RenderText* render_text = GetRenderText(); | 34 gfx::RenderText* render_text = GetRenderText(); |
| 33 DCHECK(render_text); | 35 DCHECK(render_text); |
| 34 | 36 |
| 35 TrackMouseClicks(event); | 37 TrackMouseClicks(event); |
| 36 if (handled) | 38 if (handled) |
| 37 return true; | 39 return true; |
| 38 | 40 |
| 39 if (event.IsOnlyLeftMouseButton()) { | 41 if (event.IsOnlyLeftMouseButton()) { |
| 40 if (delegate_->SupportsDrag()) | 42 if (delegate_->SupportsDrag()) |
| 41 delegate_->SetTextBeingDragged(false); | 43 delegate_->SetTextBeingDragged(false); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 54 delegate_->OnAfterPointerAction(false, selection_changed); | 56 delegate_->OnAfterPointerAction(false, selection_changed); |
| 55 } | 57 } |
| 56 break; | 58 break; |
| 57 case 1: | 59 case 1: |
| 58 // Select the word at the click location on a double click. | 60 // Select the word at the click location on a double click. |
| 59 SelectWord(event.location()); | 61 SelectWord(event.location()); |
| 60 double_click_word_ = render_text->selection(); | 62 double_click_word_ = render_text->selection(); |
| 61 break; | 63 break; |
| 62 case 2: | 64 case 2: |
| 63 // Select all the text on a triple click. | 65 // Select all the text on a triple click. |
| 64 delegate_->OnBeforePointerAction(); | 66 SelectAll(); |
| 65 render_text->SelectAll(false); | |
| 66 delegate_->OnAfterPointerAction(false, true); | |
| 67 break; | 67 break; |
| 68 default: | 68 default: |
| 69 NOTREACHED(); | 69 NOTREACHED(); |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 | 72 |
| 73 // TODO(crbug.com/676296): Right clicking an unfocused text view should select | 73 if (event.IsOnlyRightMouseButton()) { |
| 74 // all its text on Mac. | 74 if (PlatformStyle::kSelectAllOnRightClickWhenUnfocused && |
| 75 const bool select_word_on_right_click = | 75 initial_focus_state == InitialFocusStateOnMousePress::UNFOCUSED) { |
| 76 event.IsOnlyRightMouseButton() && | 76 SelectAll(); |
| 77 PlatformStyle::kSelectWordOnRightClick && | 77 } else if (PlatformStyle::kSelectWordOnRightClick && |
| 78 !render_text->IsPointInSelection(event.location()); | 78 !render_text->IsPointInSelection(event.location())) { |
| 79 if (select_word_on_right_click) | 79 SelectWord(event.location()); |
| 80 SelectWord(event.location()); | 80 } |
| 81 } |
| 81 | 82 |
| 82 if (handles_selection_clipboard_ && event.IsOnlyMiddleMouseButton()) { | 83 if (handles_selection_clipboard_ && event.IsOnlyMiddleMouseButton()) { |
| 83 if (render_text->IsPointInSelection(event.location())) { | 84 if (render_text->IsPointInSelection(event.location())) { |
| 84 delegate_->OnBeforePointerAction(); | 85 delegate_->OnBeforePointerAction(); |
| 85 render_text->ClearSelection(); | 86 render_text->ClearSelection(); |
| 86 delegate_->UpdateSelectionClipboard(); | 87 delegate_->UpdateSelectionClipboard(); |
| 87 delegate_->OnAfterPointerAction(false, true); | 88 delegate_->OnAfterPointerAction(false, true); |
| 88 } else if (!delegate_->IsReadOnly()) { | 89 } else if (!delegate_->IsReadOnly()) { |
| 89 delegate_->OnBeforePointerAction(); | 90 delegate_->OnBeforePointerAction(); |
| 90 const bool selection_changed = | 91 const bool selection_changed = |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 | 180 |
| 180 void SelectionController::SelectWord(const gfx::Point& point) { | 181 void SelectionController::SelectWord(const gfx::Point& point) { |
| 181 gfx::RenderText* render_text = GetRenderText(); | 182 gfx::RenderText* render_text = GetRenderText(); |
| 182 DCHECK(render_text); | 183 DCHECK(render_text); |
| 183 delegate_->OnBeforePointerAction(); | 184 delegate_->OnBeforePointerAction(); |
| 184 render_text->MoveCursorTo(point, false); | 185 render_text->MoveCursorTo(point, false); |
| 185 render_text->SelectWord(); | 186 render_text->SelectWord(); |
| 186 delegate_->OnAfterPointerAction(false, true); | 187 delegate_->OnAfterPointerAction(false, true); |
| 187 } | 188 } |
| 188 | 189 |
| 190 void SelectionController::SelectAll() { |
| 191 gfx::RenderText* render_text = GetRenderText(); |
| 192 DCHECK(render_text); |
| 193 delegate_->OnBeforePointerAction(); |
| 194 render_text->SelectAll(false); |
| 195 delegate_->OnAfterPointerAction(false, true); |
| 196 } |
| 197 |
| 189 gfx::RenderText* SelectionController::GetRenderText() { | 198 gfx::RenderText* SelectionController::GetRenderText() { |
| 190 return delegate_->GetRenderTextForSelectionController(); | 199 return delegate_->GetRenderTextForSelectionController(); |
| 191 } | 200 } |
| 192 | 201 |
| 193 void SelectionController::SelectThroughLastDragLocation() { | 202 void SelectionController::SelectThroughLastDragLocation() { |
| 194 gfx::RenderText* render_text = GetRenderText(); | 203 gfx::RenderText* render_text = GetRenderText(); |
| 195 DCHECK(render_text); | 204 DCHECK(render_text); |
| 196 | 205 |
| 197 delegate_->OnBeforePointerAction(); | 206 delegate_->OnBeforePointerAction(); |
| 198 | 207 |
| 199 render_text->MoveCursorTo(last_drag_location_, true); | 208 render_text->MoveCursorTo(last_drag_location_, true); |
| 200 | 209 |
| 201 if (aggregated_clicks_ == 1) { | 210 if (aggregated_clicks_ == 1) { |
| 202 render_text->SelectWord(); | 211 render_text->SelectWord(); |
| 203 // Expand the selection so the initially selected word remains selected. | 212 // Expand the selection so the initially selected word remains selected. |
| 204 gfx::Range selection = render_text->selection(); | 213 gfx::Range selection = render_text->selection(); |
| 205 const size_t min = | 214 const size_t min = |
| 206 std::min(selection.GetMin(), double_click_word_.GetMin()); | 215 std::min(selection.GetMin(), double_click_word_.GetMin()); |
| 207 const size_t max = | 216 const size_t max = |
| 208 std::max(selection.GetMax(), double_click_word_.GetMax()); | 217 std::max(selection.GetMax(), double_click_word_.GetMax()); |
| 209 const bool reversed = selection.is_reversed(); | 218 const bool reversed = selection.is_reversed(); |
| 210 selection.set_start(reversed ? max : min); | 219 selection.set_start(reversed ? max : min); |
| 211 selection.set_end(reversed ? min : max); | 220 selection.set_end(reversed ? min : max); |
| 212 render_text->SelectRange(selection); | 221 render_text->SelectRange(selection); |
| 213 } | 222 } |
| 214 delegate_->OnAfterPointerAction(false, true); | 223 delegate_->OnAfterPointerAction(false, true); |
| 215 } | 224 } |
| 216 | 225 |
| 217 } // namespace views | 226 } // namespace views |
| OLD | NEW |