forked from qt-creator/qt-creator
App: Use more local methods to trigger memory trim timer
The global event filter function is executed often, for events originating deep in the widget hierarchy even multiple times. Triggering the timer only by mouse and key presses on the main application and on texteditor is less intrusive and still happens often enough (TM). Change-Id: I848d55a347bd62d12e8523965d1750c59da33116 Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -84,10 +84,6 @@
|
||||
#include "client/settings.h"
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_LINUX
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
using namespace ExtensionSystem;
|
||||
|
||||
enum { OptionIndent = 4, DescriptionIndent = 34 };
|
||||
@@ -757,32 +753,5 @@ int main(int argc, char **argv)
|
||||
// shutdown plugin manager on the exit
|
||||
QObject::connect(&app, &QCoreApplication::aboutToQuit, &pluginManager, &PluginManager::shutdown);
|
||||
|
||||
#ifdef Q_OS_LINUX
|
||||
class MemoryTrimmer : public QObject
|
||||
{
|
||||
public:
|
||||
MemoryTrimmer()
|
||||
{
|
||||
m_trimTimer.setSingleShot(true);
|
||||
m_trimTimer.setInterval(60000);
|
||||
// glibc may not actually free memory in free().
|
||||
connect(&m_trimTimer, &QTimer::timeout, this, [] { malloc_trim(0); });
|
||||
}
|
||||
|
||||
bool eventFilter(QObject *, QEvent *e) override
|
||||
{
|
||||
if ((e->type() == QEvent::MouseButtonPress || e->type() == QEvent::KeyPress)
|
||||
&& !m_trimTimer.isActive()) {
|
||||
m_trimTimer.start();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QTimer m_trimTimer;
|
||||
};
|
||||
MemoryTrimmer trimmer;
|
||||
app.installEventFilter(&trimmer);
|
||||
#endif
|
||||
|
||||
return restarter.restartOrExit(app.exec());
|
||||
}
|
||||
|
||||
@@ -832,6 +832,11 @@ void ICore::registerWindow(QWidget *window, const Context &context)
|
||||
new WindowSupport(window, context); // deletes itself when widget is destroyed
|
||||
}
|
||||
|
||||
void ICore::restartTrimmer()
|
||||
{
|
||||
m_mainwindow->restartTrimmer();
|
||||
}
|
||||
|
||||
/*!
|
||||
Opens files using \a filePaths and \a flags like it would be
|
||||
done if they were given to \QC on the command line, or
|
||||
|
||||
@@ -124,6 +124,7 @@ public:
|
||||
static void removeContextObject(IContext *context);
|
||||
|
||||
static void registerWindow(QWidget *window, const Context &context);
|
||||
static void restartTrimmer();
|
||||
|
||||
enum OpenFilesFlags {
|
||||
None = 0,
|
||||
|
||||
@@ -113,6 +113,10 @@
|
||||
#include <QVersionNumber>
|
||||
#include <QWindow>
|
||||
|
||||
#ifdef Q_OS_LINUX
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
using namespace ExtensionSystem;
|
||||
using namespace Utils;
|
||||
|
||||
@@ -228,6 +232,13 @@ MainWindow::MainWindow()
|
||||
});
|
||||
connect(dropSupport, &DropSupport::filesDropped,
|
||||
this, &MainWindow::openDroppedFiles);
|
||||
|
||||
#ifdef Q_OS_LINUX
|
||||
m_trimTimer.setSingleShot(true);
|
||||
m_trimTimer.setInterval(60000);
|
||||
// glibc may not actually free memory in free().
|
||||
connect(&m_trimTimer, &QTimer::timeout, this, [] { malloc_trim(0); });
|
||||
#endif
|
||||
}
|
||||
|
||||
NavigationWidget *MainWindow::navigationWidget(Side side) const
|
||||
@@ -367,6 +378,12 @@ void MainWindow::restart()
|
||||
exit();
|
||||
}
|
||||
|
||||
void MainWindow::restartTrimmer()
|
||||
{
|
||||
if (!m_trimTimer.isActive())
|
||||
m_trimTimer.start();
|
||||
}
|
||||
|
||||
void MainWindow::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
const auto cancelClose = [event] {
|
||||
@@ -419,6 +436,18 @@ void MainWindow::closeEvent(QCloseEvent *event)
|
||||
alreadyClosed = true;
|
||||
}
|
||||
|
||||
void MainWindow::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
restartTrimmer();
|
||||
AppMainWindow::keyPressEvent(event);
|
||||
}
|
||||
|
||||
void MainWindow::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
restartTrimmer();
|
||||
AppMainWindow::mousePressEvent(event);
|
||||
}
|
||||
|
||||
void MainWindow::openDroppedFiles(const QList<DropSupport::FileSpec> &files)
|
||||
{
|
||||
raiseWindow();
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <utils/dropsupport.h>
|
||||
|
||||
#include <QColor>
|
||||
#include <QTimer>
|
||||
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
@@ -118,12 +119,16 @@ public:
|
||||
|
||||
void openFileFromDevice();
|
||||
|
||||
void restartTrimmer();
|
||||
|
||||
public slots:
|
||||
static void openFileWith();
|
||||
void exit();
|
||||
|
||||
protected:
|
||||
void closeEvent(QCloseEvent *event) override;
|
||||
void keyPressEvent(QKeyEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
|
||||
private:
|
||||
static void openFile();
|
||||
@@ -153,6 +158,7 @@ private:
|
||||
void updateModeSelectorStyleMenu();
|
||||
|
||||
ICore *m_coreImpl = nullptr;
|
||||
QTimer m_trimTimer;
|
||||
QStringList m_aboutInformation;
|
||||
Context m_highPrioAdditionalContexts;
|
||||
Context m_lowPrioAdditionalContexts;
|
||||
|
||||
@@ -2372,6 +2372,8 @@ static inline bool isPrintableText(const QString &text)
|
||||
|
||||
void TextEditorWidget::keyPressEvent(QKeyEvent *e)
|
||||
{
|
||||
ICore::restartTrimmer();
|
||||
|
||||
ExecuteOnDestruction eod([&]() { d->clearBlockSelection(); });
|
||||
|
||||
if (!isModifier(e) && mouseHidingEnabled())
|
||||
@@ -5460,6 +5462,8 @@ static bool handleForwardBackwardMouseButtons(QMouseEvent *e)
|
||||
|
||||
void TextEditorWidget::mousePressEvent(QMouseEvent *e)
|
||||
{
|
||||
ICore::restartTrimmer();
|
||||
|
||||
if (e->button() == Qt::LeftButton) {
|
||||
MultiTextCursor multiCursor = multiTextCursor();
|
||||
const QTextCursor &cursor = cursorForPosition(e->pos());
|
||||
|
||||
Reference in New Issue
Block a user