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
|
#include <QStackedLayout>
#include <QWebEngineFindTextResult>
#include <QWebEngineHistory>
#include <QWebEngineNewWindowRequest>
#include <QWebEngineProfile>
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <qwebengineprofile.h>
#include <vector>
#include "WindowActionRouter.hpp"
#include "events/PermissionRequestedEvent.hpp"
#include "events/UrlChangedEvent.hpp"
#include "widgets/WebViewStack.hpp"
WebViewId WebViewStack::next_webview_id = 1;
WebViewStack::WebViewStack(const Configuration *configuration, QWebEngineProfile *profile,
QWidget *parent)
: QWidget(parent), configuration(configuration), profile(profile) {
layout = new QStackedLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setStackingMode(QStackedLayout::StackOne);
connect(layout, &QStackedLayout::currentChanged, this,
&WebViewStack::current_webview_title_changed);
connect(profile, &QWebEngineProfile::downloadRequested, this, &WebViewStack::on_download_request);
}
void WebViewStack::open_url(const QUrl &url, OpenType open_type, WebViewId webview_id) {
switch (open_type) {
case OpenType::OpenUrl:
set_webview_url(url, webview_id);
break;
case OpenType::OpenUrlInView:
create_new_webview(url, true);
break;
case OpenType::OpenUrlInBgView:
create_new_webview(url, false);
break;
case OpenType::OpenUrlInWindow:
emit new_window_requested(url);
break;
}
}
WebView *WebViewStack::create_new_webview(const QUrl &url, bool focus) {
auto *webview = new WebView(WebViewStack::next_webview_id++, profile);
webview->setUrl(url);
layout->addWidget(webview);
webview_list.append(webview);
auto *page = webview->page();
connect(page, &QWebEnginePage::newWindowRequested, this, &WebViewStack::on_new_webview_request);
connect(page, &QWebEnginePage::urlChanged, this, [webview](const QUrl &url) {
// TODO: Add window id
auto *event = new UrlChangedEvent(url.toString(), webview->get_id(), 0);
WindowActionRouter::instance().dispatch_event(event);
});
connect(page, &QWebEnginePage::titleChanged, this, [this](const QString & /* title */) {
emit current_webview_title_changed(layout->currentIndex());
});
connect(page, &QWebEnginePage::permissionRequested, this, [webview](QWebEnginePermission perm) {
auto permission = std::make_shared<QWebEnginePermission>(std::move(perm));
// TODO: Add windown id
auto *event = PermissionRequestedEvent::from_permission(permission, webview->get_id(), 0);
WindowActionRouter::instance().dispatch_event(event);
});
if (focus)
focus_webview(webview->get_id());
emit current_webview_title_changed(layout->currentIndex());
return webview;
}
QList<WebViewData> WebViewStack::get_webview_list() {
QList<WebViewData> urls;
for (auto &view : webview_list) {
urls.append(WebViewData{
.id = view->get_id(),
.url = view->url().toString(),
.title = view->title(),
});
}
return urls;
}
void WebViewStack::on_new_webview_request(QWebEngineNewWindowRequest &request) {
switch (request.destination()) {
case QWebEngineNewWindowRequest::InNewTab:
open_url(request.requestedUrl(), OpenType::OpenUrlInView);
break;
case QWebEngineNewWindowRequest::InNewBackgroundTab:
open_url(request.requestedUrl(), OpenType::OpenUrlInBgView);
break;
case QWebEngineNewWindowRequest::InNewWindow:
case QWebEngineNewWindowRequest::InNewDialog:
// NOTE: Using tabs for now instead of windows
auto *webview = create_new_webview(request.requestedUrl(), true);
request.openIn(webview->page());
// emit new_window_requested(request.requestedUrl());
break;
}
}
void WebViewStack::on_download_request(QWebEngineDownloadRequest *download) {
qDebug() << "DOWNLOADING" << download->downloadDirectory() << download->downloadFileName();
emit download->accept();
// TODO: Emit event to see download progress
}
int32_t WebViewStack::get_webview_index(WebViewId webview_id) {
if (webview_id == 0) {
// TODO: Check how to detect "active" window before qapp start (lua init)?
if (window()->isActiveWindow())
webview_id = current_webview_id();
else
return -1;
}
int index = 0;
for (auto &webview : webview_list) {
if (webview->get_id() == webview_id)
return index;
index++;
}
return -1;
}
void WebViewStack::close(WebViewId webview_id) {
auto *webview = get_webview(webview_id);
if (webview == nullptr)
return;
auto webview_index = get_webview_index(webview_id);
if (webview_index < 0)
return;
layout->removeWidget(webview);
webview_list.removeAt(webview_index);
disconnect(webview->page());
webview->deleteLater();
if (webview_list.isEmpty()) {
if (configuration->close_window_when_no_views()) {
emit close_window_requested();
} else {
create_new_webview(configuration->new_view_url(), true);
}
}
}
void WebViewStack::webview_history_back(WebViewId webview_id, qsizetype history_index) {
auto *webview = get_webview(webview_id);
if (webview == nullptr) {
qDebug() << "Invalid webview id" << webview_id;
return;
}
// TODO: Change this
auto *history = webview->history();
for (auto i = abs(history_index); i > 0; i--)
if (history->canGoBack())
history->back();
}
void WebViewStack::webview_history_forward(WebViewId webview_id, qsizetype history_index) {
auto *webview = get_webview(webview_id);
if (webview == nullptr) {
qDebug() << "Invalid webview id" << webview_id;
return;
}
// TODO: Change this
auto *history = webview->history();
for (auto i = abs(history_index); i > 0; i--)
if (history->canGoForward())
history->forward();
}
std::vector<QUrl> WebViewStack::urls() {
std::vector<QUrl> urls;
for (auto &view : webview_list)
urls.push_back(view->url());
return urls;
}
WebViewId WebViewStack::current_webview_id() {
if (webview_list.empty())
return -1;
return current_webview()->get_id();
}
WebView *WebViewStack::current_webview() {
if (webview_list.empty())
return nullptr;
return webview_list.at(current_webview_index());
}
uint32_t WebViewStack::current_webview_index() { return layout->currentIndex(); }
uint32_t WebViewStack::count() { return webview_list.length(); }
void WebViewStack::focus_webview(WebViewId webview_id) {
auto webview_index = get_webview_index(webview_id);
if (webview_index >= 0)
layout->setCurrentIndex((int)webview_index);
}
void WebViewStack::open_devtools(WebViewId webview_id) {
auto *webview = get_webview(webview_id);
if (webview == nullptr) {
qDebug() << "Webview does not exist";
return;
}
webview->open_devtools();
}
void WebViewStack::set_search_text(const QString &text, WebViewId webview_id, bool forward) {
auto *webview = get_webview(webview_id);
if (webview == nullptr) {
qDebug() << "Webview does not exist";
return;
}
QString search_text = text.trimmed();
auto flags =
search_text.isLower() ? QWebEnginePage::FindFlags() : QWebEnginePage::FindCaseSensitively;
if (!forward)
flags |= QWebEnginePage::FindBackward;
webview->page()->findText(search_text, flags, [](const QWebEngineFindTextResult &result) {
qDebug() << result.numberOfMatches() << result.activeMatch();
});
}
WebView *WebViewStack::get_webview(WebViewId webview_id) {
auto webview_index = get_webview_index(webview_id);
if (webview_index < 0)
return nullptr;
return webview_list.at(webview_index);
}
bool WebViewStack::has_webview(WebViewId webview_id) { return get_webview(webview_id) != nullptr; }
QUrl WebViewStack::current_url() {
auto *webview = current_webview();
if (webview == nullptr) {
qDebug() << "No current webview";
return QUrl{configuration->new_view_url()};
}
return webview->url();
}
void WebViewStack::set_current_url(const QUrl &url) {
if (count() == 0) {
create_new_webview(url, true);
return;
}
auto *webview = current_webview();
if (webview == nullptr) {
qDebug() << "No current webview";
return;
}
webview->setUrl(url);
}
void WebViewStack::set_webview_url(const QUrl &url, WebViewId webview_id) {
if (webview_id == 0) {
set_current_url(url);
return;
}
auto *webview = get_webview(webview_id);
if (webview == nullptr) {
qDebug() << "Webview does not exist";
return;
}
webview->setUrl(url);
}
void WebViewStack::scroll(WebViewId webview_id, int deltax, int deltay) {
auto *webview = get_webview(webview_id);
if (webview == nullptr) {
qDebug() << "Webview does not exist";
return;
}
webview->scroll_increment(deltax, deltay);
}
void WebViewStack::scroll_to_top(WebViewId webview_id) {
auto *webview = get_webview(webview_id);
if (webview == nullptr) {
qDebug() << "Webview does not exist";
return;
}
webview->scroll_to_top();
}
void WebViewStack::scroll_to_bottom(WebViewId webview_id) {
auto *webview = get_webview(webview_id);
if (webview == nullptr) {
qDebug() << "Webview does not exist";
return;
}
webview->scroll_to_bottom();
}
void WebViewStack::set_html(const QString &html, WebViewId webview_id) {
auto *webview = get_webview(webview_id);
if (webview == nullptr) {
qDebug() << "Webview does not exist";
return;
}
webview->setHtml(html);
}
void WebViewStack::expose_rpc_function(const QString &name, const RpcFunc & /* unused */,
WebViewId /* unused */) {
qDebug() << "expose_rpc_function: NOT IMPLEMENTED" << name;
}
|