Skip to content

Commit

Permalink
Implement empty-area drag.
Browse files Browse the repository at this point in the history
Uses Qt 5.15's new QWindow::startSystemMove() to implement empty-area
drag, which allows the user to click and drag any empty area on the
menubar, toolbar, or tabbar to move the window around.
  • Loading branch information
phoerious committed Dec 22, 2020
1 parent cd0084f commit 6d73dc7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,14 @@ MainWindow::MainWindow()
connect(m_ui->actionOnlineHelp, SIGNAL(triggered()), SLOT(openOnlineHelp()));
connect(m_ui->actionKeyboardShortcuts, SIGNAL(triggered()), SLOT(openKeyboardShortcuts()));

#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
// Install event filter for empty-area drag
auto* eventFilter = new MainWindowEventFilter(this);
m_ui->menubar->installEventFilter(eventFilter);
m_ui->toolBar->installEventFilter(eventFilter);
m_ui->tabWidget->tabBar()->installEventFilter(eventFilter);
#endif

#ifdef Q_OS_MACOS
setUnifiedTitleAndToolBarOnMac(true);
#endif
Expand Down Expand Up @@ -1798,3 +1806,33 @@ void MainWindow::initViewMenu()
config()->set(Config::GUI_HidePasswords, checked);
});
}

#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)

MainWindowEventFilter::MainWindowEventFilter(MainWindow* mainWindow)
: QObject(mainWindow)
, m_mainWindow(mainWindow)
{
}

/**
* MainWindow event filter to initiate empty-area drag on the toolbar, menubar, and tabbar.
*/
bool MainWindowEventFilter::eventFilter(QObject* watched, QEvent* event)
{
if (!m_mainWindow) {
return QObject::eventFilter(watched, event);
}

if (watched == m_mainWindow->m_ui->menubar || watched == m_mainWindow->m_ui->toolBar
|| watched == m_mainWindow->m_ui->tabWidget->tabBar()) {
if (event->type() == QEvent::MouseButtonPress) {
auto* window = qobject_cast<QWindow*>(m_mainWindow->windowHandle());
window->startSystemMove();
}
}

return QObject::eventFilter(watched, event);
}

#endif
18 changes: 18 additions & 0 deletions src/gui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace Ui

class InactivityTimer;
class SearchWidget;
class MainWindowEventFilter;

class MainWindow : public QMainWindow
{
Expand Down Expand Up @@ -182,8 +183,25 @@ private slots:
QTimer m_updateCheckTimer;
QTimer m_trayIconTriggerTimer;
QSystemTrayIcon::ActivationReason m_trayIconTriggerReason;

friend class MainWindowEventFilter;
};

#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)

class MainWindowEventFilter : public QObject
{
Q_OBJECT

public:
explicit MainWindowEventFilter(MainWindow* mainWindow);
bool eventFilter(QObject* watched, QEvent* event) override;

private:
QPointer<MainWindow> m_mainWindow;
};
#endif

/**
* Return instance of MainWindow created on app load
* non-gui instances will return nullptr
Expand Down

0 comments on commit 6d73dc7

Please sign in to comment.