Skip to content

Commit 2bb424c

Browse files
author
Jeremy Whiting
committed
CODA-Q: Only close current tab when ctrl-w is pressed.
Instead of closing the whole current window only close the currently open tab when ctrl-w is pressed. Pass through all other key events to parent class to keep existing functionality. Signed-off-by: Jeremy Whiting <[email protected]> Change-Id: Ic89b15d214d6c32c1252e8ebd0457602ffbe4b3b
1 parent a150c98 commit 2bb424c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

qt/Window.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <QTabBar>
2020
#include <QWidget>
2121
#include <QCloseEvent>
22+
#include <QKeyEvent>
2223
#include <LibreOfficeKit/LibreOfficeKit.h>
2324
#include "qt.hpp"
2425
#include "Window.hpp"
@@ -157,3 +158,31 @@ void Window::closeEvent(QCloseEvent * ev) {
157158
QMainWindow::closeEvent(ev);
158159
}
159160

161+
void Window::keyPressEvent(QKeyEvent * ev)
162+
{
163+
// Check for Ctrl+W to close the current tab instead of the window
164+
if (ev->key() == Qt::Key_W && ev->modifiers() & Qt::ControlModifier)
165+
{
166+
QTabWidget* tabWidget = qobject_cast<QTabWidget*>(centralWidget());
167+
if (tabWidget && tabWidget->count() > 0)
168+
{
169+
// Close the current tab instead of the window
170+
int currentIndex = tabWidget->currentIndex();
171+
if (currentIndex >= 0)
172+
{
173+
QWidget* w = tabWidget->widget(currentIndex);
174+
quint64 ownerPtr = w->property("webview_owner").toULongLong();
175+
WebView* webView = reinterpret_cast<WebView*>((quintptr)ownerPtr);
176+
if (webView)
177+
{
178+
webView->closeTab();
179+
}
180+
ev->accept();
181+
return;
182+
}
183+
}
184+
}
185+
186+
// Let the parent class handle other key events
187+
QMainWindow::keyPressEvent(ev);
188+
}

qt/Window.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public slots:
3131
private:
3232

3333
void closeEvent(QCloseEvent * ev) override;
34+
void keyPressEvent(QKeyEvent * ev) override;
3435
WebView * owner_;
3536
std::function<void()> closeCallback_;
3637
};

0 commit comments

Comments
 (0)