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
|
#include <QStackedLayout>
#include <QWebEngineNewWindowRequest>
#include <algorithm>
#include <vector>
#include "widgets/WebViewStack.hpp"
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);
createNewWebView(configuration->newTabUrl, true);
}
void WebViewStack::openUrl(QUrl url, OpenType openType) {
switch (openType) {
case OpenType::OpenUrl:
setCurrentUrl(url);
break;
case OpenType::OpenUrlInTab:
createNewWebView(url, true);
break;
case OpenType::OpenUrlInBgTab:
createNewWebView(url, false);
break;
case OpenType::OpenUrlInWindow:
createNewWebView(url, true);
break;
}
}
WebView *WebViewStack::createNewWebView(QUrl url, bool focus) {
auto webview = new WebView(profile);
webview->setUrl(url);
layout->addWidget(webview);
webViewList.append(webview);
connect(webview->page(), &QWebEnginePage::newWindowRequested, this,
&WebViewStack::onNewWebViewRequest);
if (focus)
focusWebView(webViewList.length() - 1);
return webview;
}
QList<Tab> WebViewStack::getTabList() {
QList<Tab> urls;
for (auto &view : webViewList)
urls.append(Tab{.url = view->url().toString(), .title = view->title()});
return urls;
}
void WebViewStack::onNewWebViewRequest(
const QWebEngineNewWindowRequest &request) {
switch (request.destination()) {
case QWebEngineNewWindowRequest::InNewTab:
createNewWebView(request.requestedUrl(), true);
break;
case QWebEngineNewWindowRequest::InNewBackgroundTab:
createNewWebView(request.requestedUrl(), false);
break;
case QWebEngineNewWindowRequest::InNewWindow:
// TODO: Impl
createNewWebView(request.requestedUrl(), true);
break;
case QWebEngineNewWindowRequest::InNewDialog:
// TODO: Impl
createNewWebView(request.requestedUrl(), true);
break;
}
}
void WebViewStack::next() {
if (webViewList.isEmpty())
return;
auto index = currentWebViewIndex() + 1;
auto total = webViewList.length();
index = index >= total ? index % total : index;
focusWebView(index);
}
void WebViewStack::previous() {
if (webViewList.isEmpty())
return;
int index = ((int)currentWebViewIndex()) - 1;
qsizetype total = webViewList.length();
index = index < 0 ? total + index : index;
focusWebView(index);
}
void WebViewStack::closeCurrent() { close(currentWebViewIndex()); }
void WebViewStack::close(int32_t index) {
if (index < 0 || index >= webViewList.length())
return;
auto webview = webViewList.at(index);
layout->removeWidget(webview);
webViewList.removeAt(index);
disconnect(webview->page());
webview->deleteLater();
focusWebView(currentWebViewIndex());
if (webViewList.isEmpty()) {
createNewWebView(configuration->newTabUrl, true);
}
}
std::vector<QUrl> WebViewStack::urls() {
std::vector<QUrl> urls;
for (auto &view : webViewList)
urls.push_back(view->url());
return urls;
}
u_int32_t WebViewStack::currentWebViewIndex() { return layout->currentIndex(); }
u_int32_t WebViewStack::count() { return webViewList.length(); }
void WebViewStack::focusWebView(int32_t index) {
if (webViewList.isEmpty())
return;
index = std::max((long long)0,
std::min((long long)index, webViewList.length() - 1));
layout->setCurrentIndex(index);
}
QUrl WebViewStack::currentUrl() {
if (currentWebViewIndex() >= webViewList.length())
return QUrl("");
return webViewList.at(currentWebViewIndex())->url();
}
void WebViewStack::setCurrentUrl(QUrl url) {
if (currentWebViewIndex() >= webViewList.length())
return;
webViewList.at(currentWebViewIndex())->setUrl(url);
}
|