From 56ec61c92e1138684edda5b4df296c0d252b0509 Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Mon, 23 May 2022 22:11:37 +0300 Subject: [PATCH] - version 0.1.4 - experiments with new idle tracking logic --- app/config.h | 2 +- app/mainwindow.cpp | 51 ++++++++++++++++++++++++++-------------------- app/mainwindow.h | 10 ++++++++- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/app/config.h b/app/config.h index fc44d52..9d8207c 100644 --- a/app/config.h +++ b/app/config.h @@ -4,7 +4,7 @@ // App version #define QBREAK_VERSION_MAJOR 0 #define QBREAK_VERSION_MINOR 1 -#define QBREAK_VERSION_SUFFIX 3 +#define QBREAK_VERSION_SUFFIX 4 // How often UI is updated - interval in seconds #define INTERVAL_UPDATE_UI (60) diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index 9d25746..6ea2ec7 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -264,34 +264,41 @@ static int msec2min(int msec) void MainWindow::onUpdateUI() { - if (mAppConfig.idle_timeout != 0 && mTimer->isActive()) + if (mAppConfig.idle_timeout != 0 && (mTimer->isActive() || mIdleStart)) { int idle_milliseconds = get_idle_time_dynamically(); if (idle_milliseconds >= mAppConfig.idle_timeout * 60 * 1000) { - // Idle mode is active. Increase the timer interval - mIdleStart = std::chrono::steady_clock::now() - std::chrono::milliseconds(idle_milliseconds); + if (!mIdleStart) + { + // Start idle mode. Save idle start time + mIdleStart = std::chrono::steady_clock::now() - std::chrono::milliseconds(idle_milliseconds); + mIdleRemaining = mTimer->remainingTime() + idle_milliseconds; - // How much time remains ? - int remaining_milliseconds = mTimer->remainingTime(); - - // Change the time - increase by (idle_minutes - mLastIdleMinutes) - int delta_idle_milliseconds = idle_milliseconds - mLastIdleMilliseconds; - - // Paranoidal - if (delta_idle_milliseconds < 0) - delta_idle_milliseconds = 0; - - mTimer->stop(); - mTimer->start(std::chrono::milliseconds(remaining_milliseconds + delta_idle_milliseconds)); - - mLastIdleMilliseconds = idle_milliseconds; - - qDebug() << "Increase remaining time from " << remaining_milliseconds << " by " << delta_idle_milliseconds << ". " - << "New remaining time " << mTimer->remainingTime() << ". Idle in milliseconds " << idle_milliseconds; + // Stop counting + mTimer->stop(); + } + else + { + // Do nothing here - main timer is stopped, idle time & timer remaining duration are recorded already + // How much time remains ? + } + } + else + { + if (mIdleStart) + { + // Idle interval ended + mIdleStart.reset(); + mTimer->setInterval(mIdleRemaining); + mTimer->setSingleShot(true); + mTimer->start(); + } + else + { + // Do nothing here - timer is running already + } } - else - mLastIdleMilliseconds = 0; } if (mTrayIcon) diff --git a/app/mainwindow.h b/app/mainwindow.h index 971dd01..3ca44c8 100644 --- a/app/mainwindow.h +++ b/app/mainwindow.h @@ -6,6 +6,8 @@ #include #include +#include + #include "settings.h" #include "settingsdialog.h" @@ -39,7 +41,13 @@ private: std::chrono::steady_clock::time_point mBreakStartTime; app_settings::config mAppConfig; int mPostponeCount; - std::chrono::steady_clock::time_point mIdleStart; + + // Time when idle was started + std::optional mIdleStart; + + // Time remaining in main timer + int mIdleRemaining; + int mLastIdleMilliseconds; void init();