-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathqultralightwebview.cpp
300 lines (254 loc) · 9.6 KB
/
qultralightwebview.cpp
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
#include "qultralightwebview.h"
#include "mainwindow.h"
#include <QWindow>
#include <QDebug>
#include <QKeyEvent>
#include <QTimer>
#include <QToolBar>
#include <QVBoxLayout>
#include <QWidgetAction>
QUltralightWebView::QUltralightWebView(QWidget *parent, QUrl url) : QWidget(parent)
{
ultralight::App *app = ultralight::App::instance();
_window = ultralight::Window::Create(app->main_monitor(), 0, 0, false, 0);
_overlay = ultralight::Overlay::Create(_window, _window->width(), _window->height(), 0, 0);
_view = _overlay->view();
_view->set_load_listener(this);
_view->set_view_listener(this);
_window->set_listener(this);
QWidget *wvWidget = createWindowContainer(QWindow::fromWinId((WId) _window->native_handle()), this, Qt::FramelessWindowHint);
//Toolbar
QToolBar *toolbar = new QToolBar;
toolbar->setIconSize(QSize(24, 24));
QAction *backAction = new QAction(QIcon(":/icons/back.svg"), tr("&Back"), this);
backAction->setShortcut(QKeySequence("Alt+Left"));
backAction->setToolTip("Go back");
connect(backAction, &QAction::triggered, this, &QUltralightWebView::back);
toolbar->addAction(backAction);
QAction *forwardAction = new QAction(QIcon(":/icons/forward.svg"), tr("&Forward"), this);
forwardAction->setShortcut(QKeySequence("Alt+Right"));
forwardAction->setToolTip("Go forward");
connect(forwardAction, &QAction::triggered, this, &QUltralightWebView::forward);
toolbar->addAction(forwardAction);
QAction *refreshAction = new QAction(QIcon(":/icons/refresh.svg"), tr("&Refresh"), this);
refreshAction->setShortcuts(QKeySequence::Refresh);
refreshAction->setToolTip("Refresh");
connect(refreshAction, &QAction::triggered, this, &QUltralightWebView::reload);
toolbar->addAction(refreshAction);
QAction *stopAction = new QAction(QIcon(":/icons/close.svg"), tr("&Stop"), this);
stopAction->setShortcuts(QKeySequence::Cancel);
stopAction->setToolTip("Stop");
refreshAction->setVisible(false);
connect(stopAction, &QAction::triggered, this, &QUltralightWebView::stop);
toolbar->addAction(stopAction);
_addressBar = new QLineEdit;
_addressBar->setDragEnabled(true);
_addressBar->installEventFilter(this);
connect(_addressBar, &QLineEdit::returnPressed, this, [=]() {
QTimer::singleShot(10, [=]() {
load(MainWindow::sanitizeUrl(_addressBar->text()));
});
});
connect(this, &QUltralightWebView::urlChanged, [=](QUrl url) {
_addressBar->setText(url.toString());
});
connect(this, &QUltralightWebView::loadStarted, [=]() {
refreshAction->setVisible(false);
stopAction->setVisible(true);
});
connect(this, &QUltralightWebView::loadFinished, [=]() {
refreshAction->setVisible(true);
stopAction->setVisible(false);
});
QWidgetAction *addressBarAction = new QWidgetAction(this);
addressBarAction->setDefaultWidget(_addressBar);
toolbar->addAction(addressBarAction);
QFrame *hLine = new QFrame();
hLine->setFrameShape(QFrame::HLine);
hLine->setFrameShadow(QFrame::Sunken);
QVBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(toolbar);
layout->addWidget(hLine);
layout->addWidget(wvWidget);
setLayout(layout);
setFocusPolicy(Qt::WheelFocus);
load(url);
QTimer::singleShot(0, [=]() {
if(!app->is_running())
app->Run();
setFocus();
});
}
QUltralightWebView::~QUltralightWebView()
{
_view->set_load_listener(nullptr);
_view->set_view_listener(nullptr);
_window->set_listener(nullptr);
_window->Close();
}
void QUltralightWebView::load(const QUrl &url)
{
if (!url.isValid())
return;
qDebug() << "Loading" << url;
_view->LoadURL(QUrlToUlString(url));
}
void QUltralightWebView::setUrl(const QUrl &url)
{
load(url);
}
QUrl QUltralightWebView::url() const
{
return _url;
}
QString QUltralightWebView::title() const
{
return _title;
}
bool QUltralightWebView::eventFilter(QObject *obj, QEvent *event)
{
if (obj == _addressBar && event->type() == QEvent::FocusIn)
QTimer::singleShot(0, _addressBar, &QLineEdit::selectAll);
return QWidget::eventFilter(obj, event);
}
void QUltralightWebView::keyPressEvent(QKeyEvent* ev)
{
sendKey(ev);
QWidget::keyPressEvent(ev);
}
void QUltralightWebView::back()
{
_view->GoBack();
}
void QUltralightWebView::forward()
{
_view->GoForward();
}
void QUltralightWebView::reload()
{
_view->Reload();
}
void QUltralightWebView::stop()
{
_view->Stop();
}
int QUltralightWebView::QtModsToUlMods(int mods)
{
int result = 0;
if (mods & Qt::AltModifier)
result |= ultralight::KeyEvent::kMod_AltKey;
if (mods & Qt::ControlModifier)
result |= ultralight::KeyEvent::kMod_CtrlKey;
if (mods & Qt::MetaModifier)
result |= ultralight::KeyEvent::kMod_MetaKey;
if (mods & Qt::ShiftModifier)
result |= ultralight::KeyEvent::kMod_ShiftKey;
return result;
}
QString QUltralightWebView::ulStringToQString(const ultralight::String string)
{
return QString::fromUtf8(string.utf8().data());
}
QUrl QUltralightWebView::ulStringToQUrl(const ultralight::String string)
{
return QUrl(ulStringToQString(string));
}
ultralight::String QUltralightWebView::QStringToUlString(const QString string)
{
return (ultralight::String8) string.toUtf8().constData();
}
ultralight::String QUltralightWebView::QUrlToUlString(const QUrl url)
{
return QStringToUlString(url.toString());
}
void QUltralightWebView::OnResize(ultralight::Window *window, uint32_t width_px, uint32_t height_px)
{
_overlay->Resize(width_px, height_px);
}
//void QUltralightWebView::OnClose(ultralight::Window *window)
//{
// qDebug() << "Not implemented: OnClose" << window;
//}
void QUltralightWebView::OnChangeCursor(ultralight::View* caller, ultralight::Cursor cursor) {
_window->SetCursor(cursor);
}
void QUltralightWebView::OnChangeTitle(ultralight::View *caller, const ultralight::String &title)
{
_title = ulStringToQString(title);
emit titleChanged(_title);
}
void QUltralightWebView::OnChangeURL(ultralight::View *caller, const ultralight::String &url)
{
_url = ulStringToQUrl(url);
emit urlChanged(_url);
}
//void QUltralightWebView::OnChangeTooltip(ultralight::View *caller, const ultralight::String &tooltip)
//{
// qDebug() << "Not implemented: OnChangeTooltip" << tooltip.utf8().data();
//}
//void QUltralightWebView::OnAddConsoleMessage(ultralight::View *caller, ultralight::MessageSource source, ultralight::MessageLevel level, const ultralight::String &message, uint32_t line_number, uint32_t column_number, const ultralight::String &source_id)
//{
// if (level == ultralight::MessageLevel::kMessageLevel_Log || level == ultralight::MessageLevel::kMessageLevel_Info)
// qInfo() << ">>" << ulStringToQString(message);
// else if (level == ultralight::MessageLevel::kMessageLevel_Debug)
// qDebug() << ">>" << ulStringToQString(message);
// else if (level == ultralight::MessageLevel::kMessageLevel_Warning)
// qWarning() << ">>" << ulStringToQString(message);
// else if (level == ultralight::MessageLevel::kMessageLevel_Error)
// qCritical() << ">>" << ulStringToQString(message);
//}
//ultralight::RefPtr<ultralight::View> QUltralightWebView::OnCreateChildView(ultralight::View *caller, const ultralight::String &opener_url, const ultralight::String &target_url, bool is_popup, const ultralight::IntRect &popup_rect)
//{
// qDebug() << "Not implemented: OnCreateChildView";
// return nullptr;
//}
void QUltralightWebView::OnBeginLoading(ultralight::View *caller, uint64_t frame_id, bool is_main_frame, const ultralight::String &url)
{
if (is_main_frame) {
emit loadStarted();
emit loadProgress(0);
}
}
void QUltralightWebView::OnFinishLoading(ultralight::View *caller, uint64_t frame_id, bool is_main_frame, const ultralight::String &url)
{
if (is_main_frame) {
emit loadProgress(100);
emit loadFinished(true);
}
}
void QUltralightWebView::OnFailLoading(ultralight::View *caller, uint64_t frame_id, bool is_main_frame, const ultralight::String &url, const ultralight::String &description, const ultralight::String &error_domain, int error_code)
{
qCritical() << "ERROR" << error_code << "(" << ulStringToQString(error_domain) << ")" << ulStringToQString(description) << "while loading" << ulStringToQUrl(url).toString();
if (is_main_frame) {
emit loadProgress(100);
emit loadFinished(false);
}
}
//void QUltralightWebView::OnWindowObjectReady(ultralight::View *caller, uint64_t frame_id, bool is_main_frame, const ultralight::String &url)
//{
// qDebug() << "Not implemented: OnWindowObjectReady";
//}
//void QUltralightWebView::OnDOMReady(ultralight::View *caller, uint64_t frame_id, bool is_main_frame, const ultralight::String &url)
//{
// qDebug() << "Not implemented: OnDOMReady";
//}
//void QUltralightWebView::OnUpdateHistory(ultralight::View *caller)
//{
// qDebug() << "Not implemented: OnUpdateHistory";
//}
void QUltralightWebView::sendKey(QKeyEvent *event)
{
ultralight::KeyEvent evt;
evt.type = ultralight::KeyEvent::kType_RawKeyDown;
evt.virtual_key_code = event->nativeVirtualKey();
GetKeyIdentifierFromVirtualKeyCode(event->nativeVirtualKey(), evt.key_identifier);
evt.modifiers = QtModsToUlMods(event->modifiers());
_overlay->view()->FireKeyEvent(evt);
// Support typing chars
evt.type = ultralight::KeyEvent::kType_Char;
evt.text = QStringToUlString(event->text());
evt.unmodified_text = evt.text;
_overlay->view()->FireKeyEvent(evt);
}