Skip to content

Commit

Permalink
feat(ui): add menu button for zoom actions
Browse files Browse the repository at this point in the history
An event filter was also added to prevent pop-up menu closing while
zoom in/out actions triggered.
  • Loading branch information
Serge45 committed Sep 6, 2020
1 parent 66aff30 commit 1957dd2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
49 changes: 49 additions & 0 deletions src/libs/ui/browsertab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <registry/searchquery.h>

#include <QApplication>
#include <QKeyEvent>
#include <QLabel>
#include <QMenu>
#include <QStyle>
Expand Down Expand Up @@ -124,6 +125,28 @@ BrowserTab::BrowserTab(QWidget *parent)

label->setText(title);
});
m_browserActionButton = new QToolButton();
m_browserActionButton->setAutoRaise(true);
m_browserActionButton->setText(QStringLiteral(""));
m_browserActionButton->setArrowType(Qt::NoArrow);
m_browserActionButton->setPopupMode(QToolButton::InstantPopup);

auto browserActionsMenu = new QMenu(m_browserActionButton);

m_browserZoomInAction = browserActionsMenu->addAction(tr("Zoom In"), [this] () {
m_webControl->zoomIn();
});

m_browserZoomOutAction = browserActionsMenu->addAction(tr("Zoom Out"), [this] () {
m_webControl->zoomOut();
});

m_browserResetZoomAction = browserActionsMenu->addAction(tr("Reset Zoom"), [this] () {
m_webControl->resetZoom();
});

m_browserActionButton->setMenu(browserActionsMenu);
browserActionsMenu->installEventFilter(this);

auto toolBarLayout = new QHBoxLayout();
toolBarLayout->setContentsMargins(4, 0, 4, 0);
Expand All @@ -132,6 +155,7 @@ BrowserTab::BrowserTab(QWidget *parent)
toolBarLayout->addWidget(m_backButton);
toolBarLayout->addWidget(m_forwardButton);
toolBarLayout->addWidget(label, 1);
toolBarLayout->addWidget(m_browserActionButton);

auto toolBarFrame = new ToolBarFrame();
toolBarFrame->setLayout(toolBarLayout);
Expand Down Expand Up @@ -222,3 +246,28 @@ QIcon BrowserTab::docsetIcon(const QUrl &url) const
Registry::Docset *docset = Core::Application::instance()->docsetRegistry()->docsetForUrl(url);
return docset ? docset->icon() : QIcon(QStringLiteral(":/icons/logo/icon.png"));
}

bool BrowserTab::eventFilter(QObject *watched, QEvent *event)
{
if (watched == m_browserActionButton->menu()) {
QAction *triggeredAction = nullptr;

if (event->type() == QEvent::MouseButtonRelease) {
triggeredAction = m_browserActionButton->menu()->activeAction();
} else if (event->type() == QEvent::KeyPress) {
const auto *keyEvent = static_cast<QKeyEvent *>(event);

if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) {
triggeredAction = m_browserActionButton->menu()->activeAction();
}
}

if (triggeredAction
&& (triggeredAction == m_browserZoomInAction || triggeredAction == m_browserZoomOutAction)) {
triggeredAction->trigger();
return true;
}
}

return false;
}
5 changes: 5 additions & 0 deletions src/libs/ui/browsertab.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,17 @@ public slots:
private:
Q_DISABLE_COPY(BrowserTab)
QIcon docsetIcon(const QUrl &url) const;
bool eventFilter(QObject *watched, QEvent *event) override;

// Widgets.
SearchSidebar *m_searchSidebar = nullptr;
Browser::WebControl *m_webControl = nullptr;
QAction *m_browserZoomInAction = nullptr;
QAction *m_browserZoomOutAction = nullptr;
QAction *m_browserResetZoomAction = nullptr;
QToolButton *m_backButton = nullptr;
QToolButton *m_forwardButton = nullptr;
QToolButton *m_browserActionButton = nullptr;
};

} // namespace WidgetUi
Expand Down
13 changes: 7 additions & 6 deletions src/libs/ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,15 @@ MainWindow::MainWindow(Core::Application *app, QWidget *parent)
connect(ui->actionForward, &QAction::triggered, this, [this]() { currentTab()->webControl()->forward(); });
addAction(ui->actionForward);

shortcut = new QShortcut(QKeySequence::ZoomIn, this);
connect(shortcut, &QShortcut::activated, this, [this]() { currentTab()->webControl()->zoomIn(); });
shortcut = new QShortcut(QStringLiteral("Ctrl+="), this);
connect(shortcut, &QShortcut::activated, this, [this]() { currentTab()->webControl()->zoomIn(); });
shortcut = new QShortcut(QKeySequence::ZoomOut, this);
connect(shortcut, &QShortcut::activated, this, [this]() { currentTab()->webControl()->zoomOut(); });
shortcut = new QShortcut(QStringLiteral("Ctrl+0"), this);
connect(shortcut, &QShortcut::activated, this, [this]() { currentTab()->webControl()->resetZoom(); });

ui->actionResetZoom->setShortcut(QKeySequence(QStringLiteral("Ctrl+0")));
ui->actionZoomIn->setShortcut(QKeySequence::ZoomIn);
ui->actionZoomOut->setShortcut(QKeySequence::ZoomOut);
connect(ui->actionResetZoom, &QAction::triggered, this, [this] { currentTab()->webControl()->resetZoom(); });
connect(ui->actionZoomIn, &QAction::triggered, this, [this] { currentTab()->webControl()->zoomIn(); });
connect(ui->actionZoomOut, &QAction::triggered, this, [this] { currentTab()->webControl()->zoomOut(); });

// Tools Menu
connect(ui->actionDocsets, &QAction::triggered, this, [this]() {
Expand Down
20 changes: 19 additions & 1 deletion src/libs/ui/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<x>0</x>
<y>0</y>
<width>900</width>
<height>20</height>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
Expand All @@ -91,6 +91,9 @@
<string>&amp;Edit</string>
</property>
<addaction name="actionFind"/>
<addaction name="actionZoomIn"/>
<addaction name="actionZoomOut"/>
<addaction name="actionResetZoom"/>
<addaction name="separator"/>
<addaction name="actionPreferences"/>
</widget>
Expand Down Expand Up @@ -204,6 +207,21 @@
<string>&amp;Docsets...</string>
</property>
</action>
<action name="actionZoomIn">
<property name="text">
<string>Zoon &amp;In</string>
</property>
</action>
<action name="actionZoomOut">
<property name="text">
<string>Zoom &amp;Out</string>
</property>
</action>
<action name="actionResetZoom">
<property name="text">
<string>&amp;Reset Zoom</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
Expand Down

0 comments on commit 1957dd2

Please sign in to comment.