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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWebEngine module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "render_widget_host_view_qt.h"
#include "common/qt_messages.h"
#include "browser_accessibility_manager_qt.h"
#include "browser_accessibility_qt.h"
#include "chromium_overrides.h"
#include "delegated_frame_node.h"
#include "qtwebenginecoreglobal_p.h"
#include "render_widget_host_view_qt_delegate.h"
#include "type_conversion.h"
#include "web_contents_adapter.h"
#include "web_contents_adapter_client.h"
#include "web_event_factory.h"
#include "base/command_line.h"
#include "cc/output/direct_renderer.h"
#include "components/viz/service/frame_sinks/frame_sink_manager_impl.h"
#include "content/browser/accessibility/browser_accessibility_state_impl.h"
#include "content/browser/browser_main_loop.h"
#include "content/browser/frame_host/render_frame_host_impl.h"
#include "content/browser/frame_host/frame_tree.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/common/cursors/webcursor.h"
#include "content/common/input_messages.h"
#include "content/public/browser/browser_accessibility_state.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_switches.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/WebKit/public/platform/WebColor.h"
#include "third_party/WebKit/public/platform/WebCursorInfo.h"
#include "third_party/WebKit/public/web/WebCompositionUnderline.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "ui/events/blink/blink_event_util.h"
#include "ui/events/event.h"
#include "ui/events/gesture_detection/gesture_provider_config_helper.h"
#include "ui/events/gesture_detection/motion_event.h"
#include "ui/gfx/geometry/size_conversions.h"
#if defined(USE_AURA)
#include "ui/base/cursor/cursor.h"
#include "ui/base/cursor/cursors_aura.h"
#endif
#include <private/qguiapplication_p.h>
#include <qpa/qplatforminputcontext.h>
#include <qpa/qplatformintegration.h>
#include <QEvent>
#include <QFocusEvent>
#include <QGuiApplication>
#include <QInputMethodEvent>
#include <QLoggingCategory>
#include <QTextFormat>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPixmap>
#include <QScreen>
#include <QStyleHints>
#include <QVariant>
#include <QWheelEvent>
#include <QWindow>
#include <QtGui/qaccessible.h>
namespace QtWebEngineCore {
enum ImStateFlags {
TextInputStateUpdated = 1 << 0,
TextSelectionUpdated = 1 << 1,
TextSelectionBoundsUpdated = 1 << 2,
TextSelectionFlags = TextSelectionUpdated | TextSelectionBoundsUpdated,
AllFlags = TextInputStateUpdated | TextSelectionUpdated | TextSelectionBoundsUpdated
};
static inline ui::LatencyInfo CreateLatencyInfo(const blink::WebInputEvent& event) {
ui::LatencyInfo latency_info;
// The latency number should only be added if the timestamp is valid.
if (event.TimeStampSeconds()) {
const int64_t time_micros = static_cast<int64_t>(
event.TimeStampSeconds() * base::Time::kMicrosecondsPerSecond);
latency_info.AddLatencyNumberWithTimestamp(
ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT,
0,
0,
base::TimeTicks() + base::TimeDelta::FromMicroseconds(time_micros),
1);
}
return latency_info;
}
static inline Qt::InputMethodHints toQtInputMethodHints(ui::TextInputType inputType)
{
switch (inputType) {
case ui::TEXT_INPUT_TYPE_TEXT:
return Qt::ImhPreferLowercase;
case ui::TEXT_INPUT_TYPE_SEARCH:
return Qt::ImhPreferLowercase | Qt::ImhNoAutoUppercase;
case ui::TEXT_INPUT_TYPE_PASSWORD:
return Qt::ImhSensitiveData | Qt::ImhNoPredictiveText | Qt::ImhNoAutoUppercase | Qt::ImhHiddenText;
case ui::TEXT_INPUT_TYPE_EMAIL:
return Qt::ImhEmailCharactersOnly;
case ui::TEXT_INPUT_TYPE_NUMBER:
return Qt::ImhFormattedNumbersOnly;
case ui::TEXT_INPUT_TYPE_TELEPHONE:
return Qt::ImhDialableCharactersOnly;
case ui::TEXT_INPUT_TYPE_URL:
return Qt::ImhUrlCharactersOnly | Qt::ImhNoPredictiveText | Qt::ImhNoAutoUppercase;
case ui::TEXT_INPUT_TYPE_DATE_TIME:
case ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL:
case ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD:
return Qt::ImhDate | Qt::ImhTime;
case ui::TEXT_INPUT_TYPE_DATE:
case ui::TEXT_INPUT_TYPE_MONTH:
case ui::TEXT_INPUT_TYPE_WEEK:
return Qt::ImhDate;
case ui::TEXT_INPUT_TYPE_TIME:
return Qt::ImhTime;
case ui::TEXT_INPUT_TYPE_TEXT_AREA:
case ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE:
return Qt::ImhMultiLine | Qt::ImhPreferLowercase;
default:
return Qt::ImhNone;
}
}
static inline int firstAvailableId(const QMap<int, int> &map)
{
ui::BitSet32 usedIds;
QMap<int, int>::const_iterator end = map.end();
for (QMap<int, int>::const_iterator it = map.begin(); it != end; ++it)
usedIds.mark_bit(it.value());
return usedIds.first_unmarked_bit();
}
static inline ui::GestureProvider::Config QtGestureProviderConfig() {
ui::GestureProvider::Config config = ui::GetGestureProviderConfig(ui::GestureProviderConfigType::CURRENT_PLATFORM);
// Causes an assert in CreateWebGestureEventFromGestureEventData and we don't need them in Qt.
config.gesture_begin_end_types_enabled = false;
config.gesture_detector_config.swipe_enabled = false;
config.gesture_detector_config.two_finger_tap_enabled = false;
return config;
}
static inline bool compareTouchPoints(const QTouchEvent::TouchPoint &lhs, const QTouchEvent::TouchPoint &rhs)
{
// TouchPointPressed < TouchPointMoved < TouchPointReleased
return lhs.state() < rhs.state();
}
static uint32_t s_eventId = 0;
class MotionEventQt : public ui::MotionEvent {
public:
MotionEventQt(const QList<QTouchEvent::TouchPoint> &touchPoints, const base::TimeTicks &eventTime, Action action, const Qt::KeyboardModifiers modifiers, float dpiScale, int index = -1)
: touchPoints(touchPoints)
, eventTime(eventTime)
, action(action)
, eventId(++s_eventId)
, flags(flagsFromModifiers(modifiers))
, index(index)
, dpiScale(dpiScale)
{
// ACTION_DOWN and ACTION_UP must be accesssed through pointer_index 0
Q_ASSERT((action != ACTION_DOWN && action != ACTION_UP) || index == 0);
}
uint32_t GetUniqueEventId() const override { return eventId; }
Action GetAction() const override { return action; }
int GetActionIndex() const override { return index; }
size_t GetPointerCount() const override { return touchPoints.size(); }
int GetPointerId(size_t pointer_index) const override { return touchPoints.at(pointer_index).id(); }
float GetX(size_t pointer_index) const override { return touchPoints.at(pointer_index).pos().x() / dpiScale; }
float GetY(size_t pointer_index) const override { return touchPoints.at(pointer_index).pos().y() / dpiScale; }
float GetRawX(size_t pointer_index) const override { return touchPoints.at(pointer_index).screenPos().x(); }
float GetRawY(size_t pointer_index) const override { return touchPoints.at(pointer_index).screenPos().y(); }
float GetTouchMajor(size_t pointer_index) const override
{
QRectF touchRect = touchPoints.at(pointer_index).rect();
return std::max(touchRect.height(), touchRect.width());
}
float GetTouchMinor(size_t pointer_index) const override
{
QRectF touchRect = touchPoints.at(pointer_index).rect();
return std::min(touchRect.height(), touchRect.width());
}
float GetOrientation(size_t pointer_index) const override
{
return 0;
}
int GetFlags() const override { return flags; }
float GetPressure(size_t pointer_index) const override { return touchPoints.at(pointer_index).pressure(); }
float GetTiltX(size_t pointer_index) const override { return 0; }
float GetTiltY(size_t pointer_index) const override { return 0; }
base::TimeTicks GetEventTime() const override { return eventTime; }
size_t GetHistorySize() const override { return 0; }
base::TimeTicks GetHistoricalEventTime(size_t historical_index) const override { return base::TimeTicks(); }
float GetHistoricalTouchMajor(size_t pointer_index, size_t historical_index) const override { return 0; }
float GetHistoricalX(size_t pointer_index, size_t historical_index) const override { return 0; }
float GetHistoricalY(size_t pointer_index, size_t historical_index) const override { return 0; }
ToolType GetToolType(size_t pointer_index) const override { return ui::MotionEvent::TOOL_TYPE_UNKNOWN; }
int GetButtonState() const override { return 0; }
private:
QList<QTouchEvent::TouchPoint> touchPoints;
base::TimeTicks eventTime;
Action action;
const uint32_t eventId;
int flags;
int index;
float dpiScale;
};
bool isAccessibilityEnabled() {
// On Linux accessibility is disabled by default due to performance issues,
// and can be re-enabled by setting the QTWEBENGINE_ENABLE_LINUX_ACCESSIBILITY environment
// variable. For details, see QTBUG-59922.
#ifdef Q_OS_LINUX
static bool accessibility_enabled
= qEnvironmentVariableIsSet("QTWEBENGINE_ENABLE_LINUX_ACCESSIBILITY");
#else
const bool accessibility_enabled = true;
#endif
return accessibility_enabled;
}
RenderWidgetHostViewQt::RenderWidgetHostViewQt(content::RenderWidgetHost* widget)
: m_host(content::RenderWidgetHostImpl::From(widget))
, m_gestureProvider(QtGestureProviderConfig(), this)
, m_sendMotionActionDown(false)
, m_touchMotionStarted(false)
, m_chromiumCompositorData(new ChromiumCompositorData)
, m_needsDelegatedFrameAck(false)
, m_loadVisuallyCommittedState(NotCommitted)
, m_adapterClient(0)
, m_rendererCompositorFrameSink(0)
, m_imeInProgress(false)
, m_receivedEmptyImeText(false)
, m_initPending(false)
, m_beginFrameSource(nullptr)
, m_needsBeginFrames(false)
, m_addedFrameObserver(false)
, m_backgroundColor(SK_ColorWHITE)
, m_imState(0)
, m_anchorPositionWithinSelection(-1)
, m_cursorPositionWithinSelection(-1)
, m_cursorPosition(0)
, m_emptyPreviousSelection(true)
{
m_host->SetView(this);
#ifndef QT_NO_ACCESSIBILITY
if (isAccessibilityEnabled()) {
QAccessible::installActivationObserver(this);
if (QAccessible::isActive())
content::BrowserAccessibilityStateImpl::GetInstance()->EnableAccessibility();
}
#endif // QT_NO_ACCESSIBILITY
auto* task_runner = base::ThreadTaskRunnerHandle::Get().get();
m_beginFrameSource.reset(new cc::DelayBasedBeginFrameSource(
base::MakeUnique<cc::DelayBasedTimeSource>(task_runner)));
if (GetTextInputManager())
GetTextInputManager()->AddObserver(this);
const QPlatformInputContext *context = QGuiApplicationPrivate::platformIntegration()->inputContext();
m_imeHasHiddenTextCapability = context && context->hasCapability(QPlatformInputContext::HiddenTextCapability);
}
RenderWidgetHostViewQt::~RenderWidgetHostViewQt()
{
QObject::disconnect(m_adapterClientDestroyedConnection);
#ifndef QT_NO_ACCESSIBILITY
QAccessible::removeActivationObserver(this);
#endif // QT_NO_ACCESSIBILITY
if (text_input_manager_)
text_input_manager_->RemoveObserver(this);
}
void RenderWidgetHostViewQt::setDelegate(RenderWidgetHostViewQtDelegate* delegate)
{
m_delegate.reset(delegate);
}
void RenderWidgetHostViewQt::setAdapterClient(WebContentsAdapterClient *adapterClient)
{
Q_ASSERT(!m_adapterClient);
m_adapterClient = adapterClient;
QObject::disconnect(m_adapterClientDestroyedConnection);
m_adapterClientDestroyedConnection = QObject::connect(adapterClient->holdingQObject(),
&QObject::destroyed, [this] {
m_adapterClient = nullptr; });
if (m_initPending)
InitAsChild(0);
}
void RenderWidgetHostViewQt::InitAsChild(gfx::NativeView)
{
if (!m_adapterClient) {
m_initPending = true;
return;
}
m_initPending = false;
m_delegate->initAsChild(m_adapterClient);
}
void RenderWidgetHostViewQt::InitAsPopup(content::RenderWidgetHostView*, const gfx::Rect& rect)
{
m_delegate->initAsPopup(toQt(rect));
}
void RenderWidgetHostViewQt::InitAsFullscreen(content::RenderWidgetHostView*)
{
}
content::RenderWidgetHost* RenderWidgetHostViewQt::GetRenderWidgetHost() const
{
return m_host;
}
void RenderWidgetHostViewQt::SetSize(const gfx::Size& size)
{
int width = size.width();
int height = size.height();
m_delegate->resize(width,height);
}
void RenderWidgetHostViewQt::SetBounds(const gfx::Rect& screenRect)
{
// This is called when webkit has sent us a Move message.
if (IsPopup())
m_delegate->move(toQt(screenRect.origin()));
SetSize(screenRect.size());
}
gfx::Vector2dF RenderWidgetHostViewQt::GetLastScrollOffset() const {
return m_lastScrollOffset;
}
gfx::Size RenderWidgetHostViewQt::GetPhysicalBackingSize() const
{
if (!m_delegate || !m_delegate->window() || !m_delegate->window()->screen())
return gfx::Size();
const QScreen* screen = m_delegate->window()->screen();
gfx::SizeF size = toGfx(m_delegate->screenRect().size());
return gfx::ToCeiledSize(gfx::ScaleSize(size, screen->devicePixelRatio()));
}
gfx::NativeView RenderWidgetHostViewQt::GetNativeView() const
{
// gfx::NativeView is a typedef to a platform specific view
// pointer (HWND, NSView*, GtkWidget*) and other ports use
// this function in the renderer_host layer when setting up
// the view hierarchy and for generating snapshots in tests.
// Since we manage the view hierarchy in Qt its value hasn't
// been meaningful.
return gfx::NativeView();
}
gfx::NativeViewAccessible RenderWidgetHostViewQt::GetNativeViewAccessible()
{
return 0;
}
content::BrowserAccessibilityManager* RenderWidgetHostViewQt::CreateBrowserAccessibilityManager(content::BrowserAccessibilityDelegate* delegate, bool for_root_frame)
{
Q_UNUSED(for_root_frame); // FIXME
#ifndef QT_NO_ACCESSIBILITY
return new content::BrowserAccessibilityManagerQt(
m_adapterClient->accessibilityParentObject(),
content::BrowserAccessibilityManagerQt::GetEmptyDocument(),
delegate);
#else
return 0;
#endif // QT_NO_ACCESSIBILITY
}
// Set focus to the associated View component.
void RenderWidgetHostViewQt::Focus()
{
if (!IsPopup())
m_delegate->setKeyboardFocus();
m_host->Focus();
}
bool RenderWidgetHostViewQt::HasFocus() const
{
return m_delegate->hasKeyboardFocus();
}
bool RenderWidgetHostViewQt::IsSurfaceAvailableForCopy() const
{
return true;
}
void RenderWidgetHostViewQt::Show()
{
m_delegate->show();
}
void RenderWidgetHostViewQt::Hide()
{
m_delegate->hide();
}
bool RenderWidgetHostViewQt::IsShowing()
{
return m_delegate->isVisible();
}
// Retrieve the bounds of the View, in screen coordinates.
gfx::Rect RenderWidgetHostViewQt::GetViewBounds() const
{
QRectF p = m_delegate->contentsRect();
float s = dpiScale();
gfx::Point p1(floor(p.x() / s), floor(p.y() / s));
gfx::Point p2(ceil(p.right() /s), ceil(p.bottom() / s));
return gfx::BoundingRect(p1, p2);
}
SkColor RenderWidgetHostViewQt::background_color() const
{
return m_backgroundColor;
}
void RenderWidgetHostViewQt::SetBackgroundColor(SkColor color)
{
if (m_backgroundColor == color)
return;
m_backgroundColor = color;
// Set the background of the compositor if necessary
m_delegate->setClearColor(toQt(color));
// Set the background of the blink::FrameView
m_host->SetBackgroundOpaque(SkColorGetA(color) == SK_AlphaOPAQUE);
m_host->Send(new RenderViewObserverQt_SetBackgroundColor(m_host->GetRoutingID(), color));
}
// Return value indicates whether the mouse is locked successfully or not.
bool RenderWidgetHostViewQt::LockMouse()
{
mouse_locked_ = true;
m_previousMousePosition = QCursor::pos();
m_delegate->lockMouse();
qApp->setOverrideCursor(Qt::BlankCursor);
return true;
}
void RenderWidgetHostViewQt::UnlockMouse()
{
mouse_locked_ = false;
m_delegate->unlockMouse();
qApp->restoreOverrideCursor();
m_host->LostMouseLock();
}
void RenderWidgetHostViewQt::UpdateCursor(const content::WebCursor &webCursor)
{
content::CursorInfo cursorInfo;
webCursor.GetCursorInfo(&cursorInfo);
Qt::CursorShape shape = Qt::ArrowCursor;
#if defined(USE_AURA)
ui::CursorType auraType = ui::CursorType::kNull;
#endif
switch (cursorInfo.type) {
case blink::WebCursorInfo::kTypePointer:
shape = Qt::ArrowCursor;
break;
case blink::WebCursorInfo::kTypeCross:
shape = Qt::CrossCursor;
break;
case blink::WebCursorInfo::kTypeHand:
shape = Qt::PointingHandCursor;
break;
case blink::WebCursorInfo::kTypeIBeam:
shape = Qt::IBeamCursor;
break;
case blink::WebCursorInfo::kTypeWait:
shape = Qt::WaitCursor;
break;
case blink::WebCursorInfo::kTypeHelp:
shape = Qt::WhatsThisCursor;
break;
case blink::WebCursorInfo::kTypeEastResize:
case blink::WebCursorInfo::kTypeWestResize:
case blink::WebCursorInfo::kTypeEastWestResize:
case blink::WebCursorInfo::kTypeEastPanning:
case blink::WebCursorInfo::kTypeWestPanning:
shape = Qt::SizeHorCursor;
break;
case blink::WebCursorInfo::kTypeNorthResize:
case blink::WebCursorInfo::kTypeSouthResize:
case blink::WebCursorInfo::kTypeNorthSouthResize:
case blink::WebCursorInfo::kTypeNorthPanning:
case blink::WebCursorInfo::kTypeSouthPanning:
shape = Qt::SizeVerCursor;
break;
case blink::WebCursorInfo::kTypeNorthEastResize:
case blink::WebCursorInfo::kTypeSouthWestResize:
case blink::WebCursorInfo::kTypeNorthEastSouthWestResize:
case blink::WebCursorInfo::kTypeNorthEastPanning:
case blink::WebCursorInfo::kTypeSouthWestPanning:
shape = Qt::SizeBDiagCursor;
break;
case blink::WebCursorInfo::kTypeNorthWestResize:
case blink::WebCursorInfo::kTypeSouthEastResize:
case blink::WebCursorInfo::kTypeNorthWestSouthEastResize:
case blink::WebCursorInfo::kTypeNorthWestPanning:
case blink::WebCursorInfo::kTypeSouthEastPanning:
shape = Qt::SizeFDiagCursor;
break;
case blink::WebCursorInfo::kTypeColumnResize:
shape = Qt::SplitHCursor;
break;
case blink::WebCursorInfo::kTypeRowResize:
shape = Qt::SplitVCursor;
break;
case blink::WebCursorInfo::kTypeMiddlePanning:
case blink::WebCursorInfo::kTypeMove:
shape = Qt::SizeAllCursor;
break;
case blink::WebCursorInfo::kTypeProgress:
shape = Qt::BusyCursor;
break;
#if defined(USE_AURA)
case blink::WebCursorInfo::kTypeVerticalText:
auraType = ui::CursorType::kVerticalText;
break;
case blink::WebCursorInfo::kTypeCell:
auraType = ui::CursorType::kCell;
break;
case blink::WebCursorInfo::kTypeContextMenu:
auraType = ui::CursorType::kContextMenu;
break;
case blink::WebCursorInfo::kTypeAlias:
auraType = ui::CursorType::kAlias;
break;
case blink::WebCursorInfo::kTypeCopy:
auraType = ui::CursorType::kCopy;
break;
case blink::WebCursorInfo::kTypeZoomIn:
auraType = ui::CursorType::kZoomIn;
break;
case blink::WebCursorInfo::kTypeZoomOut:
auraType = ui::CursorType::kZoomOut;
break;
#else
case blink::WebCursorInfo::kTypeVerticalText:
case blink::WebCursorInfo::kTypeCell:
case blink::WebCursorInfo::kTypeContextMenu:
case blink::WebCursorInfo::kTypeAlias:
case blink::WebCursorInfo::kTypeCopy:
case blink::WebCursorInfo::kTypeZoomIn:
case blink::WebCursorInfo::kTypeZoomOut:
// FIXME: Support on OS X
break;
#endif
case blink::WebCursorInfo::kTypeNoDrop:
case blink::WebCursorInfo::kTypeNotAllowed:
shape = Qt::ForbiddenCursor;
break;
case blink::WebCursorInfo::kTypeNone:
shape = Qt::BlankCursor;
break;
case blink::WebCursorInfo::kTypeGrab:
shape = Qt::OpenHandCursor;
break;
case blink::WebCursorInfo::kTypeGrabbing:
shape = Qt::ClosedHandCursor;
break;
case blink::WebCursorInfo::kTypeCustom:
if (cursorInfo.custom_image.colorType() == SkColorType::kN32_SkColorType) {
QImage cursor = toQImage(cursorInfo.custom_image, QImage::Format_ARGB32);
m_delegate->updateCursor(QCursor(QPixmap::fromImage(cursor), cursorInfo.hotspot.x(), cursorInfo.hotspot.y()));
return;
}
break;
}
#if defined(USE_AURA)
if (auraType != ui::CursorType::kNull) {
SkBitmap bitmap;
gfx::Point hotspot;
if (ui::GetCursorBitmap(auraType, &bitmap, &hotspot)) {
m_delegate->updateCursor(QCursor(QPixmap::fromImage(toQImage(bitmap)), hotspot.x(), hotspot.y()));
return;
}
}
#endif
m_delegate->updateCursor(QCursor(shape));
}
void RenderWidgetHostViewQt::SetIsLoading(bool)
{
// We use WebContentsDelegateQt::LoadingStateChanged to notify about loading state.
}
void RenderWidgetHostViewQt::ImeCancelComposition()
{
qApp->inputMethod()->reset();
}
void RenderWidgetHostViewQt::ImeCompositionRangeChanged(const gfx::Range&, const std::vector<gfx::Rect>&)
{
// FIXME: not implemented?
QT_NOT_YET_IMPLEMENTED
}
void RenderWidgetHostViewQt::RenderProcessGone(base::TerminationStatus terminationStatus,
int exitCode)
{
m_adapterClient->renderProcessTerminated(
m_adapterClient->renderProcessExitStatus(terminationStatus), exitCode);
Destroy();
}
void RenderWidgetHostViewQt::Destroy()
{
delete this;
}
void RenderWidgetHostViewQt::SetTooltipText(const base::string16 &tooltip_text)
{
m_adapterClient->setToolTip(toQt(tooltip_text));
}
bool RenderWidgetHostViewQt::HasAcceleratedSurface(const gfx::Size&)
{
return false;
}
void RenderWidgetHostViewQt::DidCreateNewRendererCompositorFrameSink(cc::mojom::CompositorFrameSinkClient *frameSink)
{
// Accumulated resources belong to the old RendererCompositorFrameSink and
// should not be returned.
m_resourcesToRelease.clear();
m_rendererCompositorFrameSink = frameSink;
}
void RenderWidgetHostViewQt::SubmitCompositorFrame(const viz::LocalSurfaceId &local_surface_id, cc::CompositorFrame frame)
{
bool scrollOffsetChanged = (m_lastScrollOffset != frame.metadata.root_scroll_offset);
bool contentsSizeChanged = (m_lastContentsSize != frame.metadata.root_layer_size);
m_lastScrollOffset = frame.metadata.root_scroll_offset;
m_lastContentsSize = frame.metadata.root_layer_size;
m_backgroundColor = frame.metadata.root_background_color;
if (m_localSurfaceId != local_surface_id) {
m_localSurfaceId = local_surface_id;
// FIXME: update frame_size and device_scale_factor?
// FIXME: showPrimarySurface()?
}
Q_ASSERT(!m_needsDelegatedFrameAck);
m_needsDelegatedFrameAck = true;
m_chromiumCompositorData->previousFrameData = std::move(m_chromiumCompositorData->frameData);
m_chromiumCompositorData->frameDevicePixelRatio = frame.metadata.device_scale_factor;
m_chromiumCompositorData->frameData = std::move(frame);
// Support experimental.viewport.devicePixelRatio, see GetScreenInfo implementation below.
float dpiScale = this->dpiScale();
if (dpiScale != 0 && dpiScale != 1)
m_chromiumCompositorData->frameDevicePixelRatio /= dpiScale;
m_delegate->update();
if (m_loadVisuallyCommittedState == NotCommitted) {
m_loadVisuallyCommittedState = DidFirstCompositorFrameSwap;
} else if (m_loadVisuallyCommittedState == DidFirstVisuallyNonEmptyPaint) {
m_adapterClient->loadVisuallyCommitted();
m_loadVisuallyCommittedState = NotCommitted;
}
if (scrollOffsetChanged)
m_adapterClient->updateScrollPosition(toQt(m_lastScrollOffset));
if (contentsSizeChanged)
m_adapterClient->updateContentsSize(toQt(m_lastContentsSize));
}
void RenderWidgetHostViewQt::GetScreenInfo(content::ScreenInfo* results)
{
QWindow* window = m_delegate->window();
if (!window)
return;
GetScreenInfoFromNativeWindow(window, results);
// Support experimental.viewport.devicePixelRatio
results->device_scale_factor *= dpiScale();
}
gfx::Rect RenderWidgetHostViewQt::GetBoundsInRootWindow()
{
if (!m_delegate->window())
return gfx::Rect();
QRect r = m_delegate->window()->frameGeometry();
return gfx::Rect(r.x(), r.y(), r.width(), r.height());
}
void RenderWidgetHostViewQt::ClearCompositorFrame()
{
}
void RenderWidgetHostViewQt::OnUpdateTextInputStateCalled(content::TextInputManager *text_input_manager, RenderWidgetHostViewBase *updated_view, bool did_update_state)
{
Q_UNUSED(text_input_manager);
Q_UNUSED(updated_view);
Q_UNUSED(did_update_state);
ui::TextInputType type = getTextInputType();
m_delegate->inputMethodStateChanged(type != ui::TEXT_INPUT_TYPE_NONE, type == ui::TEXT_INPUT_TYPE_PASSWORD);
m_delegate->setInputMethodHints(toQtInputMethodHints(type));
const content::TextInputState *state = text_input_manager_->GetTextInputState();
if (!state)
return;
if (GetSelectedText().empty())
m_cursorPosition = state->selection_start;
m_surroundingText = QString::fromStdString(state->value);
// Remove IME composition text from the surrounding text
if (state->composition_start != -1 && state->composition_end != -1)
m_surroundingText.remove(state->composition_start, state->composition_end - state->composition_start);
if (m_imState & ImStateFlags::TextInputStateUpdated) {
m_imState = ImStateFlags::TextInputStateUpdated;
return;
}
// Ignore selection change triggered by ime composition unless it clears an actual text selection
if (state->composition_start != -1 && m_emptyPreviousSelection) {
m_imState = 0;
return;
}
m_imState |= ImStateFlags::TextInputStateUpdated;
if (m_imState == ImStateFlags::AllFlags)
selectionChanged();
}
void RenderWidgetHostViewQt::OnSelectionBoundsChanged(content::TextInputManager *text_input_manager, RenderWidgetHostViewBase *updated_view)
{
Q_UNUSED(text_input_manager);
Q_UNUSED(updated_view);
m_imState |= ImStateFlags::TextSelectionBoundsUpdated;
if (m_imState == ImStateFlags::AllFlags
|| (m_imState == ImStateFlags::TextSelectionFlags && getTextInputType() == ui::TEXT_INPUT_TYPE_NONE)) {
selectionChanged();
}
}
void RenderWidgetHostViewQt::OnTextSelectionChanged(content::TextInputManager *text_input_manager, RenderWidgetHostViewBase *updated_view)
{
Q_UNUSED(text_input_manager);
Q_UNUSED(updated_view);
const content::TextInputManager::TextSelection *selection = GetTextInputManager()->GetTextSelection(updated_view);
if (!selection)
return;
#if defined(USE_X11)
if (!selection->selected_text().empty() && selection->user_initiated()) {
// Set the CLIPBOARD_TYPE_SELECTION to the ui::Clipboard.
ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_SELECTION);
clipboard_writer.WriteText(selection->selected_text());
}
#endif // defined(USE_X11)
m_imState |= ImStateFlags::TextSelectionUpdated;
if (m_imState == ImStateFlags::AllFlags
|| (m_imState == ImStateFlags::TextSelectionFlags && getTextInputType() == ui::TEXT_INPUT_TYPE_NONE)) {
selectionChanged();
}
}
void RenderWidgetHostViewQt::selectionChanged()
{
// Reset input manager state
m_imState = 0;
// Handle text selection out of an input field
if (getTextInputType() == ui::TEXT_INPUT_TYPE_NONE) {
if (GetSelectedText().empty() && m_emptyPreviousSelection)
return;
// Reset position values to emit selectionChanged signal when clearing text selection
// by clicking into an input field. These values are intended to be used by inputMethodQuery
// so they are not expected to be valid when selection is out of an input field.
m_anchorPositionWithinSelection = -1;
m_cursorPositionWithinSelection = -1;
m_emptyPreviousSelection = GetSelectedText().empty();
m_adapterClient->selectionChanged();
return;
}
if (GetSelectedText().empty()) {
m_anchorPositionWithinSelection = m_cursorPosition;
m_cursorPositionWithinSelection = m_cursorPosition;
if (!m_emptyPreviousSelection) {
m_emptyPreviousSelection = GetSelectedText().empty();
m_adapterClient->selectionChanged();
}
return;
}
const content::TextInputManager::TextSelection *selection = text_input_manager_->GetTextSelection();
if (!selection)
return;
if (!selection->range().IsValid())
return;
int newAnchorPositionWithinSelection = 0;
int newCursorPositionWithinSelection = 0;
if (text_input_manager_->GetSelectionRegion()->anchor.type() == gfx::SelectionBound::RIGHT) {
newAnchorPositionWithinSelection = selection->range().GetMax() - selection->offset();
newCursorPositionWithinSelection = selection->range().GetMin() - selection->offset();
} else {
newAnchorPositionWithinSelection = selection->range().GetMin() - selection->offset();
newCursorPositionWithinSelection = selection->range().GetMax() - selection->offset();
}
if (m_anchorPositionWithinSelection == newAnchorPositionWithinSelection && m_cursorPositionWithinSelection == newCursorPositionWithinSelection)
return;
m_anchorPositionWithinSelection = newAnchorPositionWithinSelection;
m_cursorPositionWithinSelection = newCursorPositionWithinSelection;
if (!selection->selected_text().empty())
m_cursorPosition = newCursorPositionWithinSelection;
m_emptyPreviousSelection = selection->selected_text().empty();
m_adapterClient->selectionChanged();
}
void RenderWidgetHostViewQt::OnGestureEvent(const ui::GestureEventData& gesture)
{
m_host->ForwardGestureEvent(ui::CreateWebGestureEventFromGestureEventData(gesture));
}
QSGNode *RenderWidgetHostViewQt::updatePaintNode(QSGNode *oldNode)
{
DelegatedFrameNode *frameNode = static_cast<DelegatedFrameNode *>(oldNode);
if (!frameNode)
frameNode = new DelegatedFrameNode;
frameNode->commit(m_chromiumCompositorData.data(), &m_resourcesToRelease, m_delegate.get());
// This is possibly called from the Qt render thread, post the ack back to the UI
// to tell the child compositors to release resources and trigger a new frame.
if (m_needsDelegatedFrameAck) {
m_needsDelegatedFrameAck = false;
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
base::Bind(&RenderWidgetHostViewQt::sendDelegatedFrameAck, AsWeakPtr()));
}
return frameNode;
}
void RenderWidgetHostViewQt::notifyResize()
{
m_host->WasResized();
m_host->SendScreenRects();
}
void RenderWidgetHostViewQt::notifyShown()
{
m_host->WasShown(ui::LatencyInfo());
}
void RenderWidgetHostViewQt::notifyHidden()
{
m_host->WasHidden();
}
void RenderWidgetHostViewQt::windowBoundsChanged()
{
m_host->SendScreenRects();
if (m_delegate->window())
m_host->NotifyScreenInfoChanged();
}
void RenderWidgetHostViewQt::windowChanged()
{
if (m_delegate->window())
m_host->NotifyScreenInfoChanged();
}
bool RenderWidgetHostViewQt::forwardEvent(QEvent *event)
{
Q_ASSERT(m_host->GetView());
switch (event->type()) {
case QEvent::MouseButtonPress:
Focus(); // Fall through.
case QEvent::MouseButtonRelease:
case QEvent::MouseMove:
// Skip second MouseMove event when a window is being adopted, so that Chromium
// can properly handle further move events.
if (m_adapterClient->isBeingAdopted())
return false;
handleMouseEvent(static_cast<QMouseEvent*>(event));
break;
case QEvent::KeyPress:
case QEvent::KeyRelease:
handleKeyEvent(static_cast<QKeyEvent*>(event));
break;
case QEvent::Wheel:
handleWheelEvent(static_cast<QWheelEvent*>(event));
break;
case QEvent::TouchBegin:
Focus(); // Fall through.
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
case QEvent::TouchCancel:
handleTouchEvent(static_cast<QTouchEvent*>(event));
break;
case QEvent::TabletPress:
Focus(); // Fall through.
case QEvent::TabletRelease:
case QEvent::TabletMove:
handleTabletEvent(static_cast<QTabletEvent*>(event));
break;
#ifndef QT_NO_GESTURES
case QEvent::NativeGesture:
handleGestureEvent(static_cast<QNativeGestureEvent *>(event));
break;
#endif // QT_NO_GESTURES
case QEvent::HoverMove:
handleHoverEvent(static_cast<QHoverEvent*>(event));
break;
case QEvent::FocusIn:
case QEvent::FocusOut:
handleFocusEvent(static_cast<QFocusEvent*>(event));
break;
case QEvent::InputMethod:
handleInputMethodEvent(static_cast<QInputMethodEvent*>(event));
break;
case QEvent::InputMethodQuery:
handleInputMethodQueryEvent(static_cast<QInputMethodQueryEvent*>(event));
break;
default:
return false;
}
return true;
}
QVariant RenderWidgetHostViewQt::inputMethodQuery(Qt::InputMethodQuery query)
{
switch (query) {
case Qt::ImEnabled: {
ui::TextInputType type = getTextInputType();
bool editorVisible = type != ui::TEXT_INPUT_TYPE_NONE;
// IME manager should disable composition on input fields with ImhHiddenText hint if supported
if (m_imeHasHiddenTextCapability)
return QVariant(editorVisible);
bool passwordInput = type == ui::TEXT_INPUT_TYPE_PASSWORD;
return QVariant(editorVisible && !passwordInput);
}
case Qt::ImFont:
// TODO: Implement this
return QVariant();
case Qt::ImCursorRectangle: {
if (text_input_manager_) {
if (auto *region = text_input_manager_->GetSelectionRegion()) {
gfx::Rect caretRect = gfx::RectBetweenSelectionBounds(region->anchor, region->focus);
if (caretRect.width() == 0)
caretRect.set_width(1); // IME API on Windows expects a width > 0
return toQt(caretRect);
}
}
return QVariant();
}
case Qt::ImCursorPosition:
return m_cursorPosition;
case Qt::ImAnchorPosition:
return GetSelectedText().empty() ? m_cursorPosition : m_anchorPositionWithinSelection;
case Qt::ImSurroundingText:
return m_surroundingText;
case Qt::ImCurrentSelection:
return toQt(GetSelectedText());
case Qt::ImMaximumTextLength:
// TODO: Implement this
return QVariant(); // No limit.
case Qt::ImHints:
return int(toQtInputMethodHints(getTextInputType()));
default:
return QVariant();
}
}
void RenderWidgetHostViewQt::ProcessAckedTouchEvent(const content::TouchEventWithLatencyInfo &touch, content::InputEventAckState ack_result) {
Q_UNUSED(touch);
const bool eventConsumed = ack_result == content::INPUT_EVENT_ACK_STATE_CONSUMED;
m_gestureProvider.OnTouchEventAck(touch.event.unique_touch_event_id, eventConsumed, /*fixme: ?? */false);
}
void RenderWidgetHostViewQt::sendDelegatedFrameAck()
{
m_beginFrameSource->DidFinishFrame(this);
std::vector<cc::ReturnedResource> resources;
m_resourcesToRelease.swap(resources);
if (m_rendererCompositorFrameSink)
m_rendererCompositorFrameSink->DidReceiveCompositorFrameAck(resources);
}
void RenderWidgetHostViewQt::processMotionEvent(const ui::MotionEvent &motionEvent)
{
auto result = m_gestureProvider.OnTouchEvent(motionEvent);
if (!result.succeeded)
return;
blink::WebTouchEvent touchEvent = ui::CreateWebTouchEventFromMotionEvent(motionEvent,
result.moved_beyond_slop_region);
m_host->ForwardTouchEventWithLatencyInfo(touchEvent, CreateLatencyInfo(touchEvent));
}
QList<QTouchEvent::TouchPoint> RenderWidgetHostViewQt::mapTouchPointIds(const QList<QTouchEvent::TouchPoint> &inputPoints)
{
QList<QTouchEvent::TouchPoint> outputPoints = inputPoints;
for (int i = 0; i < outputPoints.size(); ++i) {
QTouchEvent::TouchPoint &point = outputPoints[i];
int qtId = point.id();
QMap<int, int>::const_iterator it = m_touchIdMapping.find(qtId);
if (it == m_touchIdMapping.end())
it = m_touchIdMapping.insert(qtId, firstAvailableId(m_touchIdMapping));
point.setId(it.value());
if (point.state() == Qt::TouchPointReleased)
m_touchIdMapping.remove(qtId);
}
return outputPoints;
}
float RenderWidgetHostViewQt::dpiScale() const
{
return m_adapterClient ? m_adapterClient->dpiScale() : 1.0;
}
bool RenderWidgetHostViewQt::IsPopup() const
{
return popup_type_ != blink::kWebPopupTypeNone;
}
void RenderWidgetHostViewQt::handleMouseEvent(QMouseEvent* event)
{
// Don't forward mouse events synthesized by the system, which are caused by genuine touch
// events. Chromium would then process for e.g. a mouse click handler twice, once due to the
// system synthesized mouse event, and another time due to a touch-to-gesture-to-mouse
// transformation done by Chromium.
if (event->source() == Qt::MouseEventSynthesizedBySystem)
return;
handlePointerEvent<QMouseEvent>(event);
}
void RenderWidgetHostViewQt::handleKeyEvent(QKeyEvent *ev)
{
if (IsMouseLocked() && ev->key() == Qt::Key_Escape && ev->type() == QEvent::KeyRelease)
UnlockMouse();
if (m_receivedEmptyImeText) {
// IME composition was not finished with a valid commit string.
// We're getting the composition result in a key event.
if (ev->key() != 0) {
// The key event is not a result of an IME composition. Cancel IME.
m_host->ImeCancelComposition();
m_receivedEmptyImeText = false;
} else {
if (ev->type() == QEvent::KeyRelease) {
m_receivedEmptyImeText = false;
m_host->ImeSetComposition(toString16(ev->text()),
std::vector<ui::CompositionUnderline>(),
gfx::Range::InvalidRange(),
gfx::Range::InvalidRange().start(),
gfx::Range::InvalidRange().end());
m_host->ImeFinishComposingText(false);
m_imeInProgress = false;
}
return;
}
}
content::NativeWebKeyboardEvent webEvent = WebEventFactory::toWebKeyboardEvent(ev);
bool keyDownTextInsertion = webEvent.GetType() == blink::WebInputEvent::kRawKeyDown && webEvent.text[0];
webEvent.skip_in_browser = keyDownTextInsertion;
m_host->ForwardKeyboardEvent(webEvent);
if (keyDownTextInsertion) {
// Blink won't consume the RawKeyDown, but rather the Char event in this case.
// The RawKeyDown is skipped on the way back (see above).
// The same os_event will be set on both NativeWebKeyboardEvents.
webEvent.skip_in_browser = false;
webEvent.SetType(blink::WebInputEvent::kChar);
m_host->ForwardKeyboardEvent(webEvent);
}
}
void RenderWidgetHostViewQt::handleInputMethodEvent(QInputMethodEvent *ev)
{
// Reset input manager state
m_imState = 0;
if (!m_host)
return;
QString commitString = ev->commitString();
QString preeditString = ev->preeditString();
int cursorPositionInPreeditString = -1;
gfx::Range selectionRange = gfx::Range::InvalidRange();
const QList<QInputMethodEvent::Attribute> &attributes = ev->attributes();
std::vector<ui::CompositionUnderline> underlines;
bool hasSelection = false;
for (const auto &attribute : attributes) {
switch (attribute.type) {
case QInputMethodEvent::TextFormat: {
if (preeditString.isEmpty())
break;
int start = qMin(attribute.start, (attribute.start + attribute.length));
int end = qMax(attribute.start, (attribute.start + attribute.length));
// Blink does not support negative position values. Adjust start and end positions
// to non-negative values.
if (start < 0) {
start = 0;
end = qMax(0, start + end);
}
QTextCharFormat format = qvariant_cast<QTextFormat>(attribute.value).toCharFormat();
QColor underlineColor(0, 0, 0, 0);
if (format.underlineStyle() != QTextCharFormat::NoUnderline)
underlineColor = format.underlineColor();
underlines.push_back(ui::CompositionUnderline(start, end, toSk(underlineColor), /*thick*/ false, SK_ColorTRANSPARENT));
break;
}
case QInputMethodEvent::Cursor:
// Always set the position of the cursor, even if it's marked invisible by Qt, otherwise
// there is no way the user will know which part of the composition string will be
// changed, when performing an IME-specific action (like selecting a different word
// suggestion).
cursorPositionInPreeditString = attribute.start;
break;
case QInputMethodEvent::Selection:
hasSelection = true;
// Cancel IME composition
if (preeditString.isEmpty() && attribute.start + attribute.length == 0) {
selectionRange.set_start(0);
selectionRange.set_end(0);
break;
}
selectionRange.set_start(qMin(attribute.start, (attribute.start + attribute.length)));
selectionRange.set_end(qMax(attribute.start, (attribute.start + attribute.length)));
break;
default:
break;
}
}
if (!selectionRange.IsValid()) {
// We did not receive a valid selection range, hence the range is going to mark the
// cursor position.
int newCursorPosition =
(cursorPositionInPreeditString < 0) ? preeditString.length()
: cursorPositionInPreeditString;
selectionRange.set_start(newCursorPosition);
selectionRange.set_end(newCursorPosition);
}
if (hasSelection) {
content::RenderFrameHostImpl *frameHost = static_cast<content::RenderFrameHostImpl *>(getFocusedFrameHost());
if (frameHost)
frameHost->GetFrameInputHandler()->SetEditableSelectionOffsets(selectionRange.start(), selectionRange.end());
}
int replacementLength = ev->replacementLength();
gfx::Range replacementRange = gfx::Range::InvalidRange();
if (replacementLength > 0)
{
int start = ev->replacementStart();
if (start >= 0)
replacementRange = gfx::Range(start, start + replacementLength);
else if (m_surroundingText.length() + start >= 0) {
start = m_surroundingText.length() + start;
replacementRange = gfx::Range(start, start + replacementLength);
}
}
auto setCompositionString = [&](const QString &compositionString){
m_host->ImeSetComposition(toString16(compositionString),
underlines,
replacementRange,
selectionRange.start(),
selectionRange.end());
};
if (!commitString.isEmpty() || replacementLength > 0) {
setCompositionString(commitString);
m_host->ImeFinishComposingText(false);
// We might get a commit string and a pre-edit string in a single event, which means
// we need to confirm the last composition, and start a new composition.
if (!preeditString.isEmpty()) {
setCompositionString(preeditString);
m_imeInProgress = true;
} else {
m_imeInProgress = false;
}
m_receivedEmptyImeText = commitString.isEmpty();
} else if (!preeditString.isEmpty()) {
setCompositionString(preeditString);
m_imeInProgress = true;
m_receivedEmptyImeText = false;
} else {
// There are so-far two known cases, when an empty QInputMethodEvent is received.
// First one happens when backspace is used to remove the last character in the pre-edit
// string, thus signaling the end of the composition.
// The second one happens (on Windows) when a Korean char gets composed, but instead of
// the event having a commit string, both strings are empty, and the actual char is received
// as a QKeyEvent after the QInputMethodEvent is processed.
// In lieu of the second case, we can't simply cancel the composition on an empty event,
// and then add the Korean char when QKeyEvent is received, because that leads to text
// flickering in the textarea (or any other element).
// Instead we postpone the processing of the empty QInputMethodEvent by posting it
// to the same focused object, and cancelling the composition on the next event loop tick.
if (!m_receivedEmptyImeText && m_imeInProgress && !hasSelection) {
m_receivedEmptyImeText = true;
QInputMethodEvent *eventCopy = new QInputMethodEvent(*ev);
QGuiApplication::postEvent(qApp->focusObject(), eventCopy);
} else {
m_receivedEmptyImeText = false;
if (m_imeInProgress) {
m_imeInProgress = false;
m_host->ImeCancelComposition();
}
}
}
}
void RenderWidgetHostViewQt::handleInputMethodQueryEvent(QInputMethodQueryEvent *ev)
{
Qt::InputMethodQueries queries = ev->queries();
for (uint i = 0; i < 32; ++i) {
Qt::InputMethodQuery query = (Qt::InputMethodQuery)(int)(queries & (1<<i));
if (query) {
QVariant v = inputMethodQuery(query);
ev->setValue(query, v);
}
}
ev->accept();
}
#ifndef QT_NO_ACCESSIBILITY
void RenderWidgetHostViewQt::accessibilityActiveChanged(bool active)
{
if (active)
content::BrowserAccessibilityStateImpl::GetInstance()->EnableAccessibility();
else
content::BrowserAccessibilityStateImpl::GetInstance()->DisableAccessibility();
}
#endif // QT_NO_ACCESSIBILITY
void RenderWidgetHostViewQt::handleWheelEvent(QWheelEvent *ev)
{
m_host->ForwardWheelEvent(WebEventFactory::toWebWheelEvent(ev, dpiScale()));
}
void RenderWidgetHostViewQt::clearPreviousTouchMotionState()
{
m_previousTouchPoints.clear();
m_touchMotionStarted = false;
}
#ifndef QT_NO_GESTURES
void RenderWidgetHostViewQt::handleGestureEvent(QNativeGestureEvent *ev)
{
const Qt::NativeGestureType type = ev->gestureType();
// These are the only supported gestures by Chromium so far.
if (type == Qt::ZoomNativeGesture || type == Qt::SmartZoomNativeGesture) {
m_host->ForwardGestureEvent(WebEventFactory::toWebGestureEvent(
ev,
static_cast<double>(dpiScale())));
}
}
#endif
Q_DECLARE_LOGGING_CATEGORY(QWEBENGINE_TOUCH_HANDLING);
Q_LOGGING_CATEGORY(QWEBENGINE_TOUCH_HANDLING, "qt.webengine.touch");
void RenderWidgetHostViewQt::handleTouchEvent(QTouchEvent *ev)
{
// On macOS instead of handling touch events, we use the OS provided QNativeGestureEvents.
#ifdef Q_OS_MACOS
if (ev->spontaneous()) {
return;
} else {
qCWarning(QWEBENGINE_TOUCH_HANDLING)
<< "Sending simulated touch events to Chromium does not work properly on macOS. "
"Consider using QNativeGestureEvents or QMouseEvents.";
}
#endif
// Chromium expects the touch event timestamps to be comparable to base::TimeTicks::Now().
// Most importantly we also have to preserve the relative time distance between events.
// Calculate a delta between event timestamps and Now() on the first received event, and
// apply this delta to all successive events. This delta is most likely smaller than it
// should by calculating it here but this will hopefully cause less than one frame of delay.
base::TimeTicks eventTimestamp = base::TimeTicks() + base::TimeDelta::FromMilliseconds(ev->timestamp());
if (m_eventsToNowDelta == base::TimeDelta())
m_eventsToNowDelta = base::TimeTicks::Now() - eventTimestamp;
eventTimestamp += m_eventsToNowDelta;
QList<QTouchEvent::TouchPoint> touchPoints = mapTouchPointIds(ev->touchPoints());
switch (ev->type()) {
case QEvent::TouchBegin:
m_sendMotionActionDown = true;
m_touchMotionStarted = true;
break;
case QEvent::TouchUpdate:
m_touchMotionStarted = true;
break;
case QEvent::TouchCancel:
{
// Don't process a TouchCancel event if no motion was started beforehand, or if there are
// no touch points in the current event or in the previously processed event.
if (!m_touchMotionStarted || (touchPoints.isEmpty() && m_previousTouchPoints.isEmpty())) {
clearPreviousTouchMotionState();
return;
}
// Use last saved touch points for the cancel event, to get rid of a QList assert,
// because Chromium expects a MotionEvent::ACTION_CANCEL instance to contain at least
// one touch point, whereas a QTouchCancel may not contain any touch points at all.
if (touchPoints.isEmpty())
touchPoints = m_previousTouchPoints;
clearPreviousTouchMotionState();
MotionEventQt cancelEvent(touchPoints, eventTimestamp, ui::MotionEvent::ACTION_CANCEL,
ev->modifiers(), dpiScale());
processMotionEvent(cancelEvent);
return;
}
case QEvent::TouchEnd:
clearPreviousTouchMotionState();
break;
default:
break;
}
if (m_imeInProgress && ev->type() == QEvent::TouchBegin) {
m_imeInProgress = false;
// Tell input method to commit the pre-edit string entered so far, and finish the
// composition operation.
#ifdef Q_OS_WIN
// Yes the function name is counter-intuitive, but commit isn't actually implemented
// by the Windows QPA, and reset does exactly what is necessary in this case.
qApp->inputMethod()->reset();
#else
qApp->inputMethod()->commit();
#endif
}
// Make sure that ACTION_POINTER_DOWN is delivered before ACTION_MOVE,
// and ACTION_MOVE before ACTION_POINTER_UP.
std::sort(touchPoints.begin(), touchPoints.end(), compareTouchPoints);
m_previousTouchPoints = touchPoints;
for (int i = 0; i < touchPoints.size(); ++i) {
ui::MotionEvent::Action action;
switch (touchPoints[i].state()) {
case Qt::TouchPointPressed:
if (m_sendMotionActionDown) {
action = ui::MotionEvent::ACTION_DOWN;
m_sendMotionActionDown = false;
} else {
action = ui::MotionEvent::ACTION_POINTER_DOWN;
}
break;
case Qt::TouchPointMoved:
action = ui::MotionEvent::ACTION_MOVE;
break;
case Qt::TouchPointReleased:
action = touchPoints.size() > 1 ? ui::MotionEvent::ACTION_POINTER_UP :
ui::MotionEvent::ACTION_UP;
break;
default:
// Ignore Qt::TouchPointStationary
continue;
}
MotionEventQt motionEvent(touchPoints, eventTimestamp, action, ev->modifiers(), dpiScale(),
i);
processMotionEvent(motionEvent);
}
}
void RenderWidgetHostViewQt::handleTabletEvent(QTabletEvent *event)
{
handlePointerEvent<QTabletEvent>(event);
}
template<class T>
void RenderWidgetHostViewQt::handlePointerEvent(T *event)
{
// Currently WebMouseEvent is a subclass of WebPointerProperties, so basically
// tablet events are mouse events with extra properties.
blink::WebMouseEvent webEvent = WebEventFactory::toWebMouseEvent(event, dpiScale());
if ((webEvent.GetType() == blink::WebInputEvent::kMouseDown || webEvent.GetType() == blink::WebInputEvent::kMouseUp)
&& webEvent.button == blink::WebMouseEvent::Button::kNoButton) {
// Blink can only handle the 3 main mouse-buttons and may assert when processing mouse-down for no button.
return;
}
if (webEvent.GetType() == blink::WebInputEvent::kMouseDown) {
if (event->button() != m_clickHelper.lastPressButton
|| (event->timestamp() - m_clickHelper.lastPressTimestamp > static_cast<ulong>(qGuiApp->styleHints()->mouseDoubleClickInterval()))
|| (event->pos() - m_clickHelper.lastPressPosition).manhattanLength() > qGuiApp->styleHints()->startDragDistance())
m_clickHelper.clickCounter = 0;
m_clickHelper.lastPressTimestamp = event->timestamp();
webEvent.click_count = ++m_clickHelper.clickCounter;
m_clickHelper.lastPressButton = event->button();
m_clickHelper.lastPressPosition = QPointF(event->pos()).toPoint();
}
webEvent.movement_x = event->globalX() - m_previousMousePosition.x();
webEvent.movement_y = event->globalY() - m_previousMousePosition.y();
if (IsMouseLocked())
QCursor::setPos(m_previousMousePosition);
else
m_previousMousePosition = event->globalPos();
if (m_imeInProgress && webEvent.GetType() == blink::WebInputEvent::kMouseDown) {
m_imeInProgress = false;
// Tell input method to commit the pre-edit string entered so far, and finish the
// composition operation.
#ifdef Q_OS_WIN
// Yes the function name is counter-intuitive, but commit isn't actually implemented
// by the Windows QPA, and reset does exactly what is necessary in this case.
qApp->inputMethod()->reset();
#else
qApp->inputMethod()->commit();
#endif
}
m_host->ForwardMouseEvent(webEvent);
}
void RenderWidgetHostViewQt::handleHoverEvent(QHoverEvent *ev)
{
m_host->ForwardMouseEvent(WebEventFactory::toWebMouseEvent(ev, dpiScale()));
}
void RenderWidgetHostViewQt::handleFocusEvent(QFocusEvent *ev)
{
if (ev->gotFocus()) {
m_host->GotFocus();
m_host->SetActive(true);
content::RenderViewHostImpl *viewHost = content::RenderViewHostImpl::From(m_host);
Q_ASSERT(viewHost);
if (ev->reason() == Qt::TabFocusReason)
viewHost->SetInitialFocus(false);
else if (ev->reason() == Qt::BacktabFocusReason)
viewHost->SetInitialFocus(true);
ev->accept();
} else if (ev->lostFocus()) {
m_host->SetActive(false);
m_host->Blur();
ev->accept();
}
}
void RenderWidgetHostViewQt::SetNeedsBeginFrames(bool needs_begin_frames)
{
m_needsBeginFrames = needs_begin_frames;
updateNeedsBeginFramesInternal();
}
void RenderWidgetHostViewQt::updateNeedsBeginFramesInternal()
{
if (!m_beginFrameSource)
return;
if (m_addedFrameObserver == m_needsBeginFrames)
return;
if (m_needsBeginFrames)
m_beginFrameSource->AddObserver(this);
else
m_beginFrameSource->RemoveObserver(this);
m_addedFrameObserver = m_needsBeginFrames;
}
bool RenderWidgetHostViewQt::OnBeginFrameDerivedImpl(const cc::BeginFrameArgs& args)
{
m_beginFrameSource->OnUpdateVSyncParameters(args.frame_time, args.interval);
if (m_rendererCompositorFrameSink)
m_rendererCompositorFrameSink->OnBeginFrame(args);
else // FIXME: is this else part ever needed?
m_host->Send(new ViewMsg_BeginFrame(m_host->GetRoutingID(), args));
return true;
}
void RenderWidgetHostViewQt::OnBeginFrameSourcePausedChanged(bool paused)
{
// Ignored for now. If the begin frame source is paused, the renderer
// doesn't need to be informed about it and will just not receive more
// begin frames.
}
content::RenderFrameHost *RenderWidgetHostViewQt::getFocusedFrameHost()
{
content::RenderViewHostImpl *viewHost = content::RenderViewHostImpl::From(m_host);
if (!viewHost)
return nullptr;
content::FrameTreeNode *focusedFrame = viewHost->GetDelegate()->GetFrameTree()->GetFocusedFrame();
if (!focusedFrame)
return nullptr;
return focusedFrame->current_frame_host();
}
ui::TextInputType RenderWidgetHostViewQt::getTextInputType() const
{
if (text_input_manager_ && text_input_manager_->GetTextInputState())
return text_input_manager_->GetTextInputState()->type;
return ui::TEXT_INPUT_TYPE_NONE;
}
} // namespace QtWebEngineCore
|