Skip to content

Commit b1b3c74

Browse files
committed
feat(ui): add menu button for zoom actions
An event filter was also added to prevent pop-up menu closing while zoom in/out actions triggered.
1 parent 66aff30 commit b1b3c74

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

src/libs/ui/browsertab.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <registry/searchquery.h>
3535

3636
#include <QApplication>
37+
#include <QKeyEvent>
3738
#include <QLabel>
3839
#include <QMenu>
3940
#include <QStyle>
@@ -124,6 +125,28 @@ BrowserTab::BrowserTab(QWidget *parent)
124125

125126
label->setText(title);
126127
});
128+
m_browserActionButton = new QToolButton();
129+
m_browserActionButton->setAutoRaise(true);
130+
m_browserActionButton->setText(QStringLiteral(""));
131+
m_browserActionButton->setArrowType(Qt::NoArrow);
132+
m_browserActionButton->setPopupMode(QToolButton::InstantPopup);
133+
134+
auto browserActionsMenu = new QMenu(m_browserActionButton);
135+
136+
m_browserZoomInAction = browserActionsMenu->addAction(tr("Zoom In"), [this] () {
137+
m_webControl->zoomIn();
138+
});
139+
140+
m_browserZoomOutAction = browserActionsMenu->addAction(tr("Zoom Out"), [this] () {
141+
m_webControl->zoomOut();
142+
});
143+
144+
m_browserResetZoomAction = browserActionsMenu->addAction(tr("Reset Zoom"), [this] () {
145+
m_webControl->resetZoom();
146+
});
147+
148+
m_browserActionButton->setMenu(browserActionsMenu);
149+
browserActionsMenu->installEventFilter(this);
127150

128151
auto toolBarLayout = new QHBoxLayout();
129152
toolBarLayout->setContentsMargins(4, 0, 4, 0);
@@ -132,6 +155,7 @@ BrowserTab::BrowserTab(QWidget *parent)
132155
toolBarLayout->addWidget(m_backButton);
133156
toolBarLayout->addWidget(m_forwardButton);
134157
toolBarLayout->addWidget(label, 1);
158+
toolBarLayout->addWidget(m_browserActionButton);
135159

136160
auto toolBarFrame = new ToolBarFrame();
137161
toolBarFrame->setLayout(toolBarLayout);
@@ -222,3 +246,28 @@ QIcon BrowserTab::docsetIcon(const QUrl &url) const
222246
Registry::Docset *docset = Core::Application::instance()->docsetRegistry()->docsetForUrl(url);
223247
return docset ? docset->icon() : QIcon(QStringLiteral(":/icons/logo/icon.png"));
224248
}
249+
250+
bool BrowserTab::eventFilter(QObject *watched, QEvent *event)
251+
{
252+
if (watched == m_browserActionButton->menu()) {
253+
QAction *triggeredAction = nullptr;
254+
255+
if (event->type() == QEvent::MouseButtonRelease) {
256+
triggeredAction = m_browserActionButton->menu()->activeAction();
257+
} else if (event->type() == QEvent::KeyPress) {
258+
const auto *keyEvent = static_cast<QKeyEvent *>(event);
259+
260+
if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) {
261+
triggeredAction = m_browserActionButton->menu()->activeAction();
262+
}
263+
}
264+
265+
if (triggeredAction
266+
&& (triggeredAction == m_browserZoomInAction || triggeredAction == m_browserZoomOutAction)) {
267+
triggeredAction->trigger();
268+
return true;
269+
}
270+
}
271+
272+
return false;
273+
}

src/libs/ui/browsertab.h

+5
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,17 @@ public slots:
6666
private:
6767
Q_DISABLE_COPY(BrowserTab)
6868
QIcon docsetIcon(const QUrl &url) const;
69+
bool eventFilter(QObject *watched, QEvent *event) override;
6970

7071
// Widgets.
7172
SearchSidebar *m_searchSidebar = nullptr;
7273
Browser::WebControl *m_webControl = nullptr;
74+
QAction *m_browserZoomInAction = nullptr;
75+
QAction *m_browserZoomOutAction = nullptr;
76+
QAction *m_browserResetZoomAction = nullptr;
7377
QToolButton *m_backButton = nullptr;
7478
QToolButton *m_forwardButton = nullptr;
79+
QToolButton *m_browserActionButton = nullptr;
7580
};
7681

7782
} // namespace WidgetUi

src/libs/ui/mainwindow.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,15 @@ MainWindow::MainWindow(Core::Application *app, QWidget *parent)
130130
connect(ui->actionForward, &QAction::triggered, this, [this]() { currentTab()->webControl()->forward(); });
131131
addAction(ui->actionForward);
132132

133-
shortcut = new QShortcut(QKeySequence::ZoomIn, this);
134-
connect(shortcut, &QShortcut::activated, this, [this]() { currentTab()->webControl()->zoomIn(); });
135133
shortcut = new QShortcut(QStringLiteral("Ctrl+="), this);
136134
connect(shortcut, &QShortcut::activated, this, [this]() { currentTab()->webControl()->zoomIn(); });
137-
shortcut = new QShortcut(QKeySequence::ZoomOut, this);
138-
connect(shortcut, &QShortcut::activated, this, [this]() { currentTab()->webControl()->zoomOut(); });
139-
shortcut = new QShortcut(QStringLiteral("Ctrl+0"), this);
140-
connect(shortcut, &QShortcut::activated, this, [this]() { currentTab()->webControl()->resetZoom(); });
135+
136+
ui->actionResetZoom->setShortcut(QKeySequence(QStringLiteral("Ctrl+0")));
137+
ui->actionZoomIn->setShortcut(QKeySequence::ZoomIn);
138+
ui->actionZoomOut->setShortcut(QKeySequence::ZoomOut);
139+
connect(ui->actionResetZoom, &QAction::triggered, this, [this] { currentTab()->webControl()->resetZoom(); });
140+
connect(ui->actionZoomIn, &QAction::triggered, this, [this] { currentTab()->webControl()->zoomIn(); });
141+
connect(ui->actionZoomOut, &QAction::triggered, this, [this] { currentTab()->webControl()->zoomOut(); });
141142

142143
// Tools Menu
143144
connect(ui->actionDocsets, &QAction::triggered, this, [this]() {

src/libs/ui/mainwindow.ui

+19-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<x>0</x>
6666
<y>0</y>
6767
<width>900</width>
68-
<height>20</height>
68+
<height>23</height>
6969
</rect>
7070
</property>
7171
<widget class="QMenu" name="menuFile">
@@ -91,6 +91,9 @@
9191
<string>&amp;Edit</string>
9292
</property>
9393
<addaction name="actionFind"/>
94+
<addaction name="actionZoomIn"/>
95+
<addaction name="actionZoomOut"/>
96+
<addaction name="actionResetZoom"/>
9497
<addaction name="separator"/>
9598
<addaction name="actionPreferences"/>
9699
</widget>
@@ -204,6 +207,21 @@
204207
<string>&amp;Docsets...</string>
205208
</property>
206209
</action>
210+
<action name="actionZoomIn">
211+
<property name="text">
212+
<string>Zoon &amp;In</string>
213+
</property>
214+
</action>
215+
<action name="actionZoomOut">
216+
<property name="text">
217+
<string>Zoom &amp;Out</string>
218+
</property>
219+
</action>
220+
<action name="actionResetZoom">
221+
<property name="text">
222+
<string>Reset Zoom</string>
223+
</property>
224+
</action>
207225
</widget>
208226
<layoutdefault spacing="6" margin="11"/>
209227
<resources/>

0 commit comments

Comments
 (0)