Do not snapshot session when idle

There is no reason for a tab title to change if there is no QT event.
So, instead of snapshoting session regularly, we do it only if there is
some QT events. To catch all QT events an event handler is installed on the
QCoreApplication instance.
wilder
Benoît DÉCHAMPS 4 years ago committed by Tomaz Canabrava
parent 715da3c2b8
commit 3d6c839bcd
  1. 55
      src/session/SessionController.cpp
  2. 7
      src/session/SessionController.h

@ -114,7 +114,7 @@ SessionController::SessionController(Session *sessionParam, TerminalDisplay *vie
, _findAction(nullptr) , _findAction(nullptr)
, _findNextAction(nullptr) , _findNextAction(nullptr)
, _findPreviousAction(nullptr) , _findPreviousAction(nullptr)
, _interactionTimer(nullptr) , _snapshotTimer(nullptr)
, _searchStartLine(0) , _searchStartLine(0)
, _prevSearchResultLine(0) , _prevSearchResultLine(0)
, _codecAction(nullptr) , _codecAction(nullptr)
@ -209,28 +209,18 @@ SessionController::SessionController(Session *sessionParam, TerminalDisplay *vie
connect(session(), &Konsole::Session::flowControlEnabledChanged, view(), &Konsole::TerminalDisplay::setFlowControlWarningEnabled); connect(session(), &Konsole::Session::flowControlEnabledChanged, view(), &Konsole::TerminalDisplay::setFlowControlWarningEnabled);
view()->setFlowControlWarningEnabled(session()->flowControlEnabled()); view()->setFlowControlWarningEnabled(session()->flowControlEnabled());
// take a snapshot of the session state every so often when // take a snapshot of the session state shortly after any event occurs
// user activity occurs
// //
// the timer is owned by the session so that it will be destroyed along // Don't link the timer with the session or we will have an use after
// with the session // free in SessionController::eventFilter
_interactionTimer = new QTimer(session()); _snapshotTimer = new QTimer(nullptr);
_interactionTimer->setSingleShot(true); _snapshotTimer->setSingleShot(true);
_interactionTimer->setInterval(500); _snapshotTimer->setInterval(500);
connect(_interactionTimer, &QTimer::timeout, this, &Konsole::SessionController::snapshot); connect(_snapshotTimer, &QTimer::timeout, this, &Konsole::SessionController::snapshot);
connect(view(), &Konsole::TerminalDisplay::compositeFocusChanged, this, [this](bool focused) { // Install an event handler on QCoreApplication
if (focused) { // This will catch all events: key strokes, mouse moves/clicks...
interactionHandler(); // but also all changes that occur in all tabs
} QCoreApplication::instance()->installEventFilter(this);
});
connect(view(), &Konsole::TerminalDisplay::keyPressedSignal, this, &Konsole::SessionController::interactionHandler);
// take a snapshot of the session state periodically in the background
auto backgroundTimer = new QTimer(session());
backgroundTimer->setSingleShot(false);
backgroundTimer->setInterval(2000);
connect(backgroundTimer, &QTimer::timeout, this, &Konsole::SessionController::snapshot);
backgroundTimer->start();
// xterm '10;?' request // xterm '10;?' request
connect(session(), &Konsole::Session::getForegroundColor, this, &Konsole::SessionController::sendForegroundColor); connect(session(), &Konsole::Session::getForegroundColor, this, &Konsole::SessionController::sendForegroundColor);
@ -255,6 +245,8 @@ SessionController::SessionController(Session *sessionParam, TerminalDisplay *vie
SessionController::~SessionController() SessionController::~SessionController()
{ {
QCoreApplication::instance()->removeEventFilter(this);
delete _snapshotTimer;
_allControllers.remove(this); _allControllers.remove(this);
if (factory() != nullptr) { if (factory() != nullptr) {
@ -317,11 +309,6 @@ void SessionController::viewFocusChangeHandler(bool focused)
} }
} }
void SessionController::interactionHandler()
{
_interactionTimer->start();
}
void SessionController::snapshot() void SessionController::snapshot()
{ {
Q_ASSERT(!session().isNull()); Q_ASSERT(!session().isNull());
@ -1305,6 +1292,20 @@ void SessionController::searchClosed()
searchHistory(false); searchHistory(false);
} }
bool SessionController::eventFilter(QObject *obj, QEvent *event)
{
// Forward event to super class
bool ret = ViewProperties::eventFilter(obj, event);
// Start the snapshot timer if:
// - it is not already started
// - the event is not a timer timeout
if(!_snapshotTimer->isActive() && event->type() != QEvent::Timer) {
_snapshotTimer->start();
}
return ret;
}
void SessionController::updateFilterList(const Profile::Ptr &profile) void SessionController::updateFilterList(const Profile::Ptr &profile)
{ {
if (profile != SessionManager::instance()->sessionProfile(session())) { if (profile != SessionManager::instance()->sessionProfile(session())) {

@ -219,6 +219,10 @@ public Q_SLOTS:
/** Close the incremental search */ /** Close the incremental search */
void searchClosed(); // called when the user clicks on the void searchClosed(); // called when the user clicks on the
protected:
/** Start snapshot timer if needed */
bool eventFilter(QObject *obj, QEvent *event) override;
private Q_SLOTS: private Q_SLOTS:
// menu item handlers // menu item handlers
void openBrowser(); void openBrowser();
@ -277,7 +281,6 @@ private Q_SLOTS:
const QExplicitlySharedDataPointer<Profile> &profile); // Called when the profile has changed, so we might need to change the list of filters const QExplicitlySharedDataPointer<Profile> &profile); // Called when the profile has changed, so we might need to change the list of filters
void viewFocusChangeHandler(bool focused); void viewFocusChangeHandler(bool focused);
void interactionHandler();
void snapshot(); // called periodically as the user types void snapshot(); // called periodically as the user types
// to take a snapshot of the state of the // to take a snapshot of the state of the
// foreground process in the terminal // foreground process in the terminal
@ -338,7 +341,7 @@ private:
QAction *_findNextAction; QAction *_findNextAction;
QAction *_findPreviousAction; QAction *_findPreviousAction;
QTimer *_interactionTimer; QTimer *_snapshotTimer;
int _searchStartLine; int _searchStartLine;
int _prevSearchResultLine; int _prevSearchResultLine;

Loading…
Cancel
Save