summaryrefslogtreecommitdiffstats
path: root/src/core/touch_selection_controller_client_qt.cpp
blob: befee6aaf4fba648f8f254a179c5e6a38cb91025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "render_widget_host_view_qt.h"
#include "touch_handle_drawable_qt.h"
#include "touch_handle_drawable_client.h"
#include "touch_selection_controller_client_qt.h"
#include "touch_selection_menu_controller.h"
#include "type_conversion.h"
#include "web_contents_adapter.h"
#include "web_contents_adapter_client.h"

#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "ui/gfx/geometry/size_conversions.h"

#include <QClipboard>
#include <QGuiApplication>

namespace QtWebEngineCore {

TouchSelectionControllerClientQt::TouchSelectionControllerClientQt(RenderWidgetHostViewQt *rwhv)
    : m_rwhv(rwhv)
    , m_menuController(new TouchSelectionMenuController(this))
    , m_menuShowing(false)
    , m_menuRequested(false)
    , m_touchDown(false)
    , m_scrollInProgress(false)
    , m_handleDragInProgress(false)
{
    Q_ASSERT(rwhv);
}

TouchSelectionControllerClientQt::~TouchSelectionControllerClientQt()
{
}

bool TouchSelectionControllerClientQt::handleContextMenu(const content::ContextMenuParams& params)
{
    if ((params.source_type == ui::MENU_SOURCE_LONG_PRESS ||
         params.source_type == ui::MENU_SOURCE_LONG_TAP) &&
        params.is_editable && params.selection_text.empty()) {
        m_menuRequested = true;
        updateMenu();
        return true;
    }

    const bool from_touch = params.source_type == ui::MENU_SOURCE_LONG_PRESS ||
                            params.source_type == ui::MENU_SOURCE_LONG_TAP ||
                            params.source_type == ui::MENU_SOURCE_TOUCH;
    if (from_touch && !params.selection_text.empty())
        return true;

    GetTouchSelectionController()->HideAndDisallowShowingAutomatically();
    return false;
}

void TouchSelectionControllerClientQt::onTouchDown()
{
    m_touchDown = true;
    updateMenu();
}

void TouchSelectionControllerClientQt::onTouchUp()
{
    m_touchDown = false;
    updateMenu();
}

void TouchSelectionControllerClientQt::onScrollBegin()
{
    m_scrollInProgress = true;
    GetTouchSelectionController()->SetTemporarilyHidden(true);
    updateMenu();
}

void TouchSelectionControllerClientQt::onScrollEnd()
{
    m_scrollInProgress = false;
    GetTouchSelectionController()->SetTemporarilyHidden(false);
    updateMenu();
}

bool TouchSelectionControllerClientQt::IsCommandIdEnabled(int command_id) const
{
    bool editable = m_rwhv->getTextInputType() != ui::TEXT_INPUT_TYPE_NONE;
    bool readable = m_rwhv->getTextInputType() != ui::TEXT_INPUT_TYPE_PASSWORD;
    bool hasSelection = !m_rwhv->GetSelectedText().empty();

    switch (command_id) {
    case TouchSelectionMenuController::Cut:
        return editable && readable && hasSelection;
    case TouchSelectionMenuController::Copy:
        return readable && hasSelection;
    case TouchSelectionMenuController::Paste:
        return editable && !QGuiApplication::clipboard()->text().isEmpty();
    default:
        return false;
    }
}

void TouchSelectionControllerClientQt::ExecuteCommand(int command_id, int event_flags)
{
    Q_UNUSED(event_flags);
    GetTouchSelectionController()->HideAndDisallowShowingAutomatically();

    WebContentsAdapterClient *adapterClient = m_rwhv->adapterClient();
    Q_ASSERT(adapterClient);
    WebContentsAdapter *adapter = adapterClient->webContentsAdapter();
    Q_ASSERT(adapter);

    switch (command_id) {
    case TouchSelectionMenuController::Cut:
        adapter->cut();
        break;
    case TouchSelectionMenuController::Copy:
        adapter->copy();
        break;
    case TouchSelectionMenuController::Paste:
        adapter->paste();
        break;
    default:
        NOTREACHED();
        break;
    }
}

void TouchSelectionControllerClientQt::RunContextMenu()
{
    gfx::RectF anchorRect = GetTouchSelectionController()->GetRectBetweenBounds();
    gfx::PointF anchorPoint = gfx::PointF(anchorRect.CenterPoint().x(), anchorRect.y());

    content::RenderWidgetHostImpl *host = m_rwhv->host();
    host->ShowContextMenuAtPoint(gfx::ToRoundedPoint(anchorPoint),
                                 ui::MENU_SOURCE_TOUCH_EDIT_MENU);

    // Hide selection handles after getting rect-between-bounds from touch
    // selection controller; otherwise, rect would be empty and the above
    // calculations would be invalid.
    GetTouchSelectionController()->HideAndDisallowShowingAutomatically();
}

void TouchSelectionControllerClientQt::DidStopFlinging()
{
    onScrollEnd();
}

void TouchSelectionControllerClientQt::UpdateClientSelectionBounds(const gfx::SelectionBound& start,
                                                                   const gfx::SelectionBound& end)
{
    UpdateClientSelectionBounds(start, end, this, this);
}

void TouchSelectionControllerClientQt::UpdateClientSelectionBounds(const gfx::SelectionBound& start,
                                                                   const gfx::SelectionBound& end,
                                                                   ui::TouchSelectionControllerClient* client,
                                                                   ui::TouchSelectionMenuClient* menu_client)
{
    Q_UNUSED(client);
    Q_UNUSED(menu_client);

    GetTouchSelectionController()->OnSelectionBoundsChanged(start, end);
}

void TouchSelectionControllerClientQt::InvalidateClient(ui::TouchSelectionControllerClient* client)
{
    Q_UNUSED(client);
}

ui::TouchSelectionController* TouchSelectionControllerClientQt::GetTouchSelectionController()
{
    return m_rwhv->getTouchSelectionController();
}

void TouchSelectionControllerClientQt::AddObserver(Observer* observer)
{
    Q_UNUSED(observer);
}

void TouchSelectionControllerClientQt::RemoveObserver(Observer* observer)
{
    Q_UNUSED(observer);
}

bool TouchSelectionControllerClientQt::SupportsAnimation() const
{
    return false;
}

void TouchSelectionControllerClientQt::SetNeedsAnimate()
{
    NOTREACHED();
}

void TouchSelectionControllerClientQt::MoveCaret(const gfx::PointF& position)
{
    auto *frameWidgetInputHandler = m_rwhv->getFrameWidgetInputHandler();
    if (!frameWidgetInputHandler)
        return;

    frameWidgetInputHandler->MoveCaret(gfx::ToRoundedPoint(position));
}

void TouchSelectionControllerClientQt::MoveRangeSelectionExtent(const gfx::PointF& extent)
{
    auto *frameWidgetInputHandler = m_rwhv->getFrameWidgetInputHandler();
    if (!frameWidgetInputHandler)
        return;

    frameWidgetInputHandler->MoveRangeSelectionExtent(gfx::ToRoundedPoint(extent));
}

void TouchSelectionControllerClientQt::SelectBetweenCoordinates(const gfx::PointF& base, const gfx::PointF& extent)
{
    auto *frameWidgetInputHandler = m_rwhv->getFrameWidgetInputHandler();
    if (!frameWidgetInputHandler)
        return;

    frameWidgetInputHandler->SelectRange(gfx::ToRoundedPoint(base), gfx::ToRoundedPoint(extent));
}

void TouchSelectionControllerClientQt::OnSelectionEvent(ui::SelectionEventType event)
{
    switch (event) {
    case ui::SELECTION_HANDLES_SHOWN:
        m_menuRequested = true;
        break;
    case ui::INSERTION_HANDLE_SHOWN:
        break;
    case ui::SELECTION_HANDLES_CLEARED:
    case ui::INSERTION_HANDLE_CLEARED:
        m_menuRequested = false;
        break;
    case ui::SELECTION_HANDLE_DRAG_STARTED:
    case ui::INSERTION_HANDLE_DRAG_STARTED:
        m_handleDragInProgress = true;
        break;
    case ui::SELECTION_HANDLE_DRAG_STOPPED:
    case ui::INSERTION_HANDLE_DRAG_STOPPED:
        m_handleDragInProgress = false;
        break;
    case ui::SELECTION_HANDLES_MOVED:
    case ui::INSERTION_HANDLE_MOVED:
        break;
    case ui::INSERTION_HANDLE_TAPPED:
        m_menuRequested = !m_menuRequested;
        break;
    }

    updateMenu();
}

void TouchSelectionControllerClientQt::OnDragUpdate(const ui::TouchSelectionDraggable::Type type, const gfx::PointF& position)
{
    Q_UNUSED(type);
    Q_UNUSED(position);
}

std::unique_ptr<ui::TouchHandleDrawable> TouchSelectionControllerClientQt::CreateDrawable()
{
    Q_ASSERT(m_rwhv);
    Q_ASSERT(m_rwhv->adapterClient());
    QMap<int, QImage> images;
    for (int orientation = 0; orientation < static_cast<int>(ui::TouchHandleOrientation::UNDEFINED);
         ++orientation) {
        gfx::Image *image = TouchHandleDrawableQt::GetHandleImage(
                static_cast<ui::TouchHandleOrientation>(orientation));
        images.insert(orientation, toQImage(image->AsBitmap()));
    }
    auto delegate = m_rwhv->adapterClient()->createTouchHandleDelegate(images);
    return std::unique_ptr<ui::TouchHandleDrawable>(new TouchHandleDrawableQt(delegate));
}

void TouchSelectionControllerClientQt::DidScroll()
{
}

void TouchSelectionControllerClientQt::resetControls()
{
    if (m_menuShowing) {
        hideMenu();
        GetTouchSelectionController()->HideAndDisallowShowingAutomatically();
    }
}

void TouchSelectionControllerClientQt::showMenu()
{
    gfx::RectF rect = GetTouchSelectionController()->GetRectBetweenBounds();
    gfx::PointF origin = rect.origin();
    gfx::PointF bottom_right = rect.bottom_right();

    gfx::Vector2dF diagonal = bottom_right - origin;
    gfx::SizeF size(diagonal.x(), diagonal.y());
    gfx::RectF anchor_rect(origin, size);

    // Calculate maximum handle image size;
    gfx::SizeF max_handle_size = GetTouchSelectionController()->GetStartHandleRect().size();
    max_handle_size.SetToMax(GetTouchSelectionController()->GetEndHandleRect().size());

    WebContentsAdapterClient *adapterClient = m_rwhv->adapterClient();
    Q_ASSERT(adapterClient);
    adapterClient->showTouchSelectionMenu(m_menuController.data(),
                                          QRect(toQt(gfx::ToEnclosingRect(anchor_rect))),
                                          QSize(toQt(gfx::ToRoundedSize(max_handle_size))));
    m_menuShowing = true;
}

void TouchSelectionControllerClientQt::hideMenu()
{
    WebContentsAdapterClient *adapterClient = m_rwhv->adapterClient();
    Q_ASSERT(adapterClient);
    adapterClient->hideTouchSelectionMenu();
    m_menuShowing = false;
}

void TouchSelectionControllerClientQt::updateMenu()
{
    if (m_menuShowing)
        hideMenu();

    if (m_menuRequested && !m_touchDown &&
            !m_scrollInProgress && !m_handleDragInProgress) {
        showMenu();
    }
}

} // namespace QtWebEngineCore