| Index: ui/views/selection_controller.cc
|
| diff --git a/ui/views/selection_controller.cc b/ui/views/selection_controller.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6f6365f0b4f5537ecade1c408772ba197dceacaf
|
| --- /dev/null
|
| +++ b/ui/views/selection_controller.cc
|
| @@ -0,0 +1,184 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "ui/views/selection_controller.h"
|
| +
|
| +#include <algorithm>
|
| +
|
| +#include "ui/events/event.h"
|
| +#include "ui/gfx/render_text.h"
|
| +#include "ui/views/metrics.h"
|
| +#include "ui/views/selection_controller_host.h"
|
| +#include "ui/views/style/platform_style.h"
|
| +#include "ui/views/view.h"
|
| +
|
| +namespace views {
|
| +
|
| +SelectionController::SelectionController(SelectionControllerHost* host)
|
| + : aggregated_clicks_(0), host_(host) {
|
| + DCHECK(host);
|
| +}
|
| +
|
| +bool SelectionController::OnMousePressed(const ui::MouseEvent& event,
|
| + bool handled) {
|
| + TrackMouseClicks(event);
|
| +
|
| + if (!GetRenderText() || handled)
|
| + return handled;
|
| +
|
| + if (event.IsOnlyLeftMouseButton()) {
|
| + host_->OnBeforeMouseAction();
|
| + bool selection_changed = false;
|
| + host_->SetTextBeingDragged(false);
|
| + switch (aggregated_clicks_) {
|
| + case 0:
|
| + // If the click location is within existing selection, it may be a
|
| + // potential drag and drop.
|
| + if (GetRenderText()->IsPointInSelection(event.location())) {
|
| + host_->SetTextBeingDragged(true);
|
| + } else {
|
| + host_->MoveCursorTo(event.location(), event.IsShiftDown());
|
| + selection_changed = true;
|
| + }
|
| + break;
|
| + case 1:
|
| + // Select the word at the click location on a double click.
|
| + host_->SelectWordAt(event.location());
|
| + selection_changed = true;
|
| + double_click_word_ = GetRenderText()->selection();
|
| + break;
|
| + case 2:
|
| + // Select all the text on a triple click.
|
| + host_->SelectAll(false);
|
| + selection_changed = true;
|
| + break;
|
| + default:
|
| + NOTREACHED();
|
| + }
|
| + host_->OnAfterMouseAction(false, selection_changed);
|
| + }
|
| +
|
| +// On Linux, middle click should update or paste the selection clipboard.
|
| +#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
|
| + if (event.IsOnlyMiddleMouseButton()) {
|
| + host_->OnBeforeMouseAction();
|
| + bool text_changed = false;
|
| + bool selection_changed = false;
|
| + if (GetRenderText()->IsPointInSelection(event.location())) {
|
| + host_->ClearSelection();
|
| + host_->UpdateSelectionClipboard();
|
| + selection_changed = true;
|
| + } else if (!host_->IsReadOnly()) {
|
| + host_->MoveCursorTo(event.location(), false);
|
| + host_->PasteSelectionClipboard();
|
| + selection_changed = text_changed = true;
|
| + }
|
| + host_->OnAfterMouseAction(text_changed, selection_changed);
|
| + }
|
| +#endif
|
| +
|
| + return true;
|
| +}
|
| +
|
| +bool SelectionController::OnMouseDragged(const ui::MouseEvent& event) {
|
| + // If |drag_selection_timer_| is running, |last_drag_location_| will be used
|
| + // to update the selection.
|
| + last_drag_location_ = event.location();
|
| +
|
| + if (!GetRenderText())
|
| + return false;
|
| +
|
| + // Don't adjust the cursor on a potential drag and drop.
|
| + if (host_->HasTextBeingDragged() || !event.IsOnlyLeftMouseButton())
|
| + return true;
|
| +
|
| + // A timer is used to continuously scroll while selecting beyond side edges.
|
| + const int x = event.location().x();
|
| + if ((x >= 0 && x <= host_->GetViewWidth()) ||
|
| + host_->GetDragSelectionDelay() == 0) {
|
| + drag_selection_timer_.Stop();
|
| + SelectThroughLastDragLocation();
|
| + } else if (!drag_selection_timer_.IsRunning()) {
|
| + // Select through the edge of the visible text, then start the scroll timer.
|
| + last_drag_location_.set_x(std::min(std::max(0, x), host_->GetViewWidth()));
|
| + SelectThroughLastDragLocation();
|
| +
|
| + // Todo what happens when this object is destroyed before timer is invoked
|
| + // but it's scheduled?
|
| + drag_selection_timer_.Start(
|
| + FROM_HERE,
|
| + base::TimeDelta::FromMilliseconds(host_->GetDragSelectionDelay()), this,
|
| + &SelectionController::SelectThroughLastDragLocation);
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +void SelectionController::OnMouseReleased(const ui::MouseEvent& event) {
|
| + host_->OnBeforeMouseAction();
|
| + drag_selection_timer_.Stop();
|
| +
|
| + bool selection_changed = false;
|
| + // Cancel suspected drag initiations, the user was clicking in the selection.
|
| + if (host_->HasTextBeingDragged()) {
|
| + selection_changed = true;
|
| + host_->MoveCursorTo(event.location(), false);
|
| + }
|
| + host_->SetTextBeingDragged(false);
|
| + host_->UpdateSelectionClipboard();
|
| + host_->OnAfterMouseAction(false, selection_changed);
|
| +}
|
| +
|
| +void SelectionController::TrackMouseClicks(const ui::MouseEvent& event) {
|
| + if (event.IsOnlyLeftMouseButton()) {
|
| + base::TimeDelta time_delta = event.time_stamp() - last_click_time_;
|
| + if (!last_click_time_.is_null() &&
|
| + time_delta.InMilliseconds() <= GetDoubleClickInterval() &&
|
| + !View::ExceededDragThreshold(event.location() - last_click_location_)) {
|
| + // Upon clicking after a triple click, the count should go back to
|
| + // double click and alternate between double and triple. This assignment
|
| + // maps 0 to 1, 1 to 2, 2 to 1.
|
| + aggregated_clicks_ = (aggregated_clicks_ % 2) + 1;
|
| + } else {
|
| + aggregated_clicks_ = 0;
|
| + }
|
| + last_click_time_ = event.time_stamp();
|
| + last_click_location_ = event.location();
|
| + }
|
| +}
|
| +
|
| +gfx::RenderText* SelectionController::GetRenderText() {
|
| + return host_->GetRenderTextForSelection();
|
| +}
|
| +
|
| +void SelectionController::SelectThroughLastDragLocation() {
|
| + host_->OnBeforeMouseAction();
|
| +
|
| + // Todo(karandeepb): See if this can be handled at the RenderText level.
|
| + const bool drags_to_end = PlatformStyle::kTextfieldDragVerticallyDragsToEnd;
|
| + if (drags_to_end && last_drag_location_.y() < 0)
|
| + host_->SelectTillEdge(gfx::CURSOR_LEFT);
|
| + else if (drags_to_end && last_drag_location_.y() > host_->GetViewHeight())
|
| + host_->SelectTillEdge(gfx::CURSOR_RIGHT);
|
| + else
|
| + host_->MoveCursorTo(last_drag_location_, true);
|
| +
|
| + if (aggregated_clicks_ == 1) {
|
| + host_->SelectWord();
|
| +
|
| + // Expand the selection so the initially selected word remains selected.
|
| + gfx::Range selection = GetRenderText()->selection();
|
| + const size_t min =
|
| + std::min(selection.GetMin(), double_click_word_.GetMin());
|
| + const size_t max =
|
| + std::max(selection.GetMax(), double_click_word_.GetMax());
|
| + const bool reversed = selection.is_reversed();
|
| + selection.set_start(reversed ? max : min);
|
| + selection.set_end(reversed ? min : max);
|
| + host_->SelectRange(selection);
|
| + }
|
| + host_->OnAfterMouseAction(false, true);
|
| +}
|
| +
|
| +} // namespace views
|
|
|