Skip to content

Commit 0dea589

Browse files
committed
Support for loading icon
1 parent 5951afc commit 0dea589

File tree

12 files changed

+156
-43
lines changed

12 files changed

+156
-43
lines changed

src/browser_widget.cc

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@ BrowserWidget::BrowserWidget(Cef *cef,
55
QWidget *parent)
66
: QWidget(parent), cef_(cef) {
77
cef_widg_ = new CefWidget(cef, url, this);
8-
connect(cef_widg_, &CefWidget::TitleChanged,
9-
this, &BrowserWidget::TitleChanged);
10-
connect(cef_widg_, &CefWidget::FaviconChanged,
11-
this, &BrowserWidget::FaviconChanged);
12-
connect(cef_widg_, &CefWidget::TabOpen,
8+
connect(cef_widg_, &CefWidget::TitleChanged, [this](const QString &title) {
9+
current_title_ = title;
10+
emit TitleChanged();
11+
});
12+
connect(cef_widg_, &CefWidget::FaviconChanged, [this](const QIcon &icon) {
13+
current_favicon_ = icon;
14+
emit FaviconChanged();
15+
});
16+
connect(cef_widg_, &CefWidget::LoadStateChanged,
17+
[this](bool is_loading, bool can_go_back, bool can_go_forward) {
18+
loading_ = is_loading;
19+
can_go_back_ = can_go_back;
20+
can_go_forward_ = can_go_forward;
21+
emit LoadingStateChanged();
22+
});
23+
connect(cef_widg_, &CefWidget::PageOpen,
1324
[this](CefRequestHandler::WindowOpenDisposition type,
1425
const QString &url,
1526
bool user_gesture) {
16-
emit TabOpen((WindowOpenType) type, url, user_gesture);
27+
emit PageOpen((WindowOpenType) type, url, user_gesture);
1728
});
1829

1930
url_edit_ = new QLineEdit(this);
@@ -67,6 +78,26 @@ void BrowserWidget::FocusBrowser() {
6778
cef_widg_->setFocus();
6879
}
6980

81+
QIcon BrowserWidget::CurrentFavicon() {
82+
return current_favicon_;
83+
}
84+
85+
QString BrowserWidget::CurrentTitle() {
86+
return current_title_;
87+
}
88+
89+
bool BrowserWidget::Loading() {
90+
return loading_;
91+
}
92+
93+
bool BrowserWidget::CanGoBack() {
94+
return can_go_back_;
95+
}
96+
97+
bool BrowserWidget::CanGoForward() {
98+
return can_go_forward_;
99+
}
100+
70101
void BrowserWidget::moveEvent(QMoveEvent *) {
71102
this->UpdateStatusBarLocation();
72103
}

src/browser_widget.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class BrowserWidget : public QWidget {
1313

1414
void FocusUrlEdit();
1515
void FocusBrowser();
16+
QIcon CurrentFavicon();
17+
QString CurrentTitle();
18+
bool Loading();
19+
bool CanGoBack();
20+
bool CanGoForward();
1621

1722
// Matches CEF's numbering, don't change
1823
enum WindowOpenType {
@@ -38,13 +43,19 @@ class BrowserWidget : public QWidget {
3843
QLineEdit *url_edit_;
3944
CefWidget *cef_widg_;
4045
QStatusBar *status_bar_;
46+
QIcon current_favicon_;
47+
QString current_title_;
48+
bool loading_ = false;
49+
bool can_go_back_ = false;
50+
bool can_go_forward_ = false;
4151

4252
void UpdateStatusBarLocation();
4353

4454
signals:
45-
void TitleChanged(const QString &title);
46-
void FaviconChanged(const QIcon &icon);
47-
void TabOpen(WindowOpenType type, const QString &url, bool user_gesture);
55+
void TitleChanged();
56+
void FaviconChanged();
57+
void LoadingStateChanged();
58+
void PageOpen(WindowOpenType type, const QString &url, bool user_gesture);
4859
};
4960

5061
#endif // DOOGIE_BROWSERWIDGET_H_

src/cef_handler.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ void CefHandler::OnGotFocus(CefRefPtr<CefBrowser> browser) {
2929
emit FocusObtained();
3030
}
3131

32+
void CefHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
33+
bool is_loading,
34+
bool can_go_back,
35+
bool can_go_forward) {
36+
emit LoadStateChanged(is_loading, can_go_back, can_go_forward);
37+
}
38+
3239
bool CefHandler::OnOpenURLFromTab(CefRefPtr<CefBrowser> browser,
3340
CefRefPtr<CefFrame> frame,
3441
const CefString &target_url,
3542
CefRequestHandler::WindowOpenDisposition target_disposition,
3643
bool user_gesture) {
37-
emit TabOpen(target_disposition,
44+
emit PageOpen(target_disposition,
3845
QString::fromStdString(target_url.ToString()),
3946
user_gesture);
4047
return true;

src/cef_handler.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class CefHandler :
1010
public CefDisplayHandler,
1111
public CefFocusHandler,
1212
public CefLifeSpanHandler,
13+
public CefLoadHandler,
1314
public CefRequestHandler {
1415
Q_OBJECT
1516
public:
@@ -27,6 +28,10 @@ class CefHandler :
2728
return this;
2829
}
2930

31+
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() override {
32+
return this;
33+
}
34+
3035
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() override {
3136
return this;
3237
}
@@ -54,6 +59,12 @@ class CefHandler :
5459
return true;
5560
}
5661

62+
// Load handler overrides...
63+
virtual void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
64+
bool is_loading,
65+
bool can_go_back,
66+
bool can_go_forward);
67+
5768
// Request handler overrides...
5869
virtual bool OnOpenURLFromTab(CefRefPtr<CefBrowser> browser,
5970
CefRefPtr<CefFrame> frame,
@@ -70,9 +81,12 @@ class CefHandler :
7081
// Empty if no URL
7182
void FaviconUrlChanged(const QString &url);
7283
void FocusObtained();
73-
void TabOpen(CefRequestHandler::WindowOpenDisposition type,
74-
const QString &url,
75-
bool user_gesture);
84+
void LoadStateChanged(bool is_loading,
85+
bool can_go_back,
86+
bool can_go_forward);
87+
void PageOpen(CefRequestHandler::WindowOpenDisposition type,
88+
const QString &url,
89+
bool user_gesture);
7690
};
7791

7892
#endif // DOOGIE_CEFHANDLER_H_

src/cef_widget.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ CefWidget::CefWidget(Cef *cef, const QString &url, QWidget *parent)
2222
new CefWidget::FaviconDownloadCallback(this));
2323
}
2424
});
25-
connect(handler_, &CefHandler::TabOpen,
26-
this, &CefWidget::TabOpen);
25+
connect(handler_, &CefHandler::LoadStateChanged,
26+
this, &CefWidget::LoadStateChanged);
27+
connect(handler_, &CefHandler::PageOpen,
28+
this, &CefWidget::PageOpen);
2729

2830
InitBrowser(url);
2931
}

src/cef_widget.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ class CefWidget : public QWidget {
4949
void TitleChanged(const QString &title);
5050
void StatusChanged(const QString &status);
5151
void FaviconChanged(const QIcon &icon);
52-
void TabOpen(CefRequestHandler::WindowOpenDisposition type,
53-
const QString &url,
54-
bool user_gesture);
52+
void LoadStateChanged(bool is_loading,
53+
bool can_go_back,
54+
bool can_go_forward);
55+
void PageOpen(CefRequestHandler::WindowOpenDisposition type,
56+
const QString &url,
57+
bool user_gesture);
5558
};
5659

5760
#endif // DOOGIE_CEFWIDGET_H_

src/doogie.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<RCC>
22
<qresource prefix="/res">
3+
<file>images/loading-icon.gif</file>
34
<file>images/fontawesome/plus.png</file>
45
<file>images/fontawesome/times.png</file>
56
</qresource>

src/images/loading-icon.gif

3.12 KB
Loading

src/page_tree.cc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ void PageTree::NewBrowser() {
7272
browser->FocusUrlEdit();
7373
}
7474

75+
QMovie* PageTree::LoadingIconMovie() {
76+
if (!loading_icon_movie_) {
77+
loading_icon_movie_ = new QMovie(":/res/images/loading-icon.gif");
78+
// We use a bad method to update the icon on frames of this,
79+
// so slow it down.
80+
loading_icon_movie_->setSpeed(70);
81+
}
82+
return loading_icon_movie_;
83+
}
84+
85+
QIcon PageTree::CloseButtonIcon() {
86+
if (close_button_icon_.isNull()) {
87+
close_button_icon_ = QIcon(":/res/images/fontawesome/times.png");
88+
}
89+
return close_button_icon_;
90+
}
91+
7592
Qt::DropActions PageTree::supportedDropActions() const {
7693
// return Qt::MoveAction;
7794
return QTreeWidget::supportedDropActions();
@@ -199,7 +216,7 @@ void PageTree::AddBrowser(QPointer<BrowserWidget> browser,
199216
setCurrentItem(browser_item);
200217
}
201218
// Make all tab opens open as child
202-
connect(browser, &BrowserWidget::TabOpen,
219+
connect(browser, &BrowserWidget::PageOpen,
203220
[this, browser_item](BrowserWidget::WindowOpenType type,
204221
const QString &url,
205222
bool user_gesture) {

src/page_tree.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class PageTree : public QTreeWidget {
1111
public:
1212
explicit PageTree(BrowserStack *browser_stack, QWidget *parent = nullptr);
1313
void NewBrowser();
14+
QMovie* LoadingIconMovie();
15+
QIcon CloseButtonIcon();
1416
protected:
1517
virtual Qt::DropActions supportedDropActions() const override;
1618
virtual void dropEvent(QDropEvent *event) override;
@@ -22,6 +24,8 @@ class PageTree : public QTreeWidget {
2224
int end) override;
2325
private:
2426
BrowserStack *browser_stack_ = nullptr;
27+
QMovie* loading_icon_movie_ = nullptr;
28+
QIcon close_button_icon_;
2529
bool close_dragging_ = false;
2630
PageTreeItem* close_dragging_on_ = nullptr;
2731
QRubberBand* rubber_band_ = nullptr;

0 commit comments

Comments
 (0)