aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/widgets/navigationbar.cpp
blob: 89e1e503e71a298777d4fb34b57b064274f16576 (plain)
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
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "navigationbar.h"
#include "configuration.h"
#include "urllineedit.h"
#include "util.h"
#include "webengine/webview.h"
#include <QHBoxLayout>
#include <QMenu>
#include <QShortcut>
#include <QStyle>
#include <QToolBar>
#include <QToolButton>
#include <QWebEngineHistory>
#include "webengine/webprofile.h"

NavigationBar::NavigationBar(QWidget *parent)
    : QToolBar(parent)
{
    Configuration config;

    // Back button
    backAction = addAction(Util::icon(QStyle::SP_ArrowBack), tr("Back"));
    setShortcut(backAction, "shortcuts.navigation.back");
    connect(backAction, &QAction::triggered, this, [this]() {
        m_view->history()->back();
    });

    auto *backMenu = new QMenu(this);
    connect(backMenu, &QMenu::aboutToShow, this, [this, backMenu]() {
        backMenu->clear();
        const auto history = m_view->history()->backItems(10);
        for(const QWebEngineHistoryItem &item : history) {
            auto *action = backMenu->addAction(item.title());
            connect(action, &QAction::triggered, this, [item, this]() {
                m_view->history()->goToItem(item);
            });
        }
    });
    backAction->setMenu(backMenu);

    auto *backMenuShortcut = new QShortcut(QKeySequence(config.value<QString>("shortcuts.navigation.backmenu").value()), this);
    connect(backMenuShortcut, &QShortcut::activated, backMenu, [this, backMenu]() {
        if(backAction->isEnabled()) {
            auto *widget = this->widgetForAction(backAction);
            backMenu->exec(this->mapToGlobal(widget->pos() + QPoint(0, widget->height())));
        }
    });

    // Forward button
    forwardAction = addAction(Util::icon(QStyle::SP_ArrowForward), tr("Forward"));
    setShortcut(forwardAction, "shortcuts.navigation.forward");
    connect(forwardAction, &QAction::triggered, this, [this]() {
        m_view->history()->forward();
    });

    auto *forwardMenu = new QMenu(this);
    connect(forwardMenu, &QMenu::aboutToShow, this, [this, forwardMenu]() {
        forwardMenu->clear();
        const auto history = m_view->history()->forwardItems(10);
        for(const QWebEngineHistoryItem &item : history) {
            auto *action = forwardMenu->addAction(item.title());
            connect(action, &QAction::triggered, this, [item, this]() {
                m_view->history()->goToItem(item);
            });
        }
    });
    forwardAction->setMenu(forwardMenu);

    auto *forwardMenuShortcut = new QShortcut(QKeySequence(config.value<QString>("shortcuts.navigation.forwardmenu").value()), this);
    connect(forwardMenuShortcut, &QShortcut::activated, forwardMenu, [this, forwardMenu]() {
        if(forwardAction->isEnabled()) {
            auto *widget = this->widgetForAction(forwardAction);
            forwardMenu->exec(this->mapToGlobal(widget->pos() + QPoint(0, widget->height())));
        }
    });

    // Stop/Refresh button
    stopReloadAction = addAction(Util::icon(QStyle::SP_BrowserReload), tr("Refresh"));
    setShortcut(stopReloadAction, "shortcuts.navigation.refresh");
    connect(stopReloadAction, &QAction::triggered, this, [this]() {
        if(m_view->isLoaded())
            m_view->reload();
        else
            m_view->stop();
    });

    auto *reloadShortcut = new QShortcut(QKeySequence(config.value<QString>("shortcuts.navigation.reload").value()), this);
    connect(reloadShortcut, &QShortcut::activated, this, [this]() {
        m_view->triggerPageAction(QWebEnginePage::ReloadAndBypassCache);
    });

    // Home button
    homeAction = addAction(Util::icon(QStyle::SP_DirHomeIcon), tr("Home"));
    setShortcut(homeAction, "shortcuts.navigation.home");
    connect(homeAction, &QAction::triggered, this, [this]() {
        m_view->load(m_view->profile()->homepage());
    });
}

void NavigationBar::connectWebView(WebView *view)
{
    m_view = view;

    disconnect(loadStartedConnection);
    disconnect(loadFinishedConnection);

    if(view == nullptr) {
        backAction->setEnabled(false);
        forwardAction->setEnabled(false);
        stopReloadAction->setEnabled(false);
        homeAction->setEnabled(false);

        return;
    }

    if(view->isLoaded()) {
        update_loadFinished();
    } else {
        update_loadStarted();
    }

    loadStartedConnection = connect(view, &QWebEngineView::loadStarted, this, &NavigationBar::update_loadStarted);
    loadFinishedConnection = connect(view, &QWebEngineView::loadFinished, this, &NavigationBar::update_loadFinished);
    stopReloadAction->setEnabled(true);
    homeAction->setEnabled(true);
}

void NavigationBar::update_loadStarted()
{
    backAction->setEnabled(m_view->history()->canGoForward());
    forwardAction->setEnabled(m_view->history()->canGoForward());
    stopReloadAction->setIcon(Util::icon(QStyle::SP_BrowserStop));
}

void NavigationBar::update_loadFinished()
{
    backAction->setEnabled(m_view->history()->canGoBack());
    forwardAction->setEnabled(m_view->history()->canGoForward());
    stopReloadAction->setIcon(Util::icon(QStyle::SP_BrowserReload));
}