diff --git a/.config/quickshell/ii/modules/common/Persistent.qml b/.config/quickshell/ii/modules/common/Persistent.qml index f0cbcafa..876b2ef6 100644 --- a/.config/quickshell/ii/modules/common/Persistent.qml +++ b/.config/quickshell/ii/modules/common/Persistent.qml @@ -49,6 +49,7 @@ Singleton { property JsonObject pomodoro: JsonObject { property bool running: false property int start: 0 + property bool isBreak: false } property JsonObject stopwatch: JsonObject { property bool running: false diff --git a/.config/quickshell/ii/modules/sidebarRight/pomodoro/PomodoroWidget.qml b/.config/quickshell/ii/modules/sidebarRight/pomodoro/PomodoroWidget.qml index 242b1ceb..64b16a9d 100644 --- a/.config/quickshell/ii/modules/sidebarRight/pomodoro/PomodoroWidget.qml +++ b/.config/quickshell/ii/modules/sidebarRight/pomodoro/PomodoroWidget.qml @@ -45,23 +45,6 @@ Item { } } - Timer { - id: pomodoroTimer - interval: 200 - running: Pomodoro.isPomodoroRunning - repeat: true - onTriggered: Pomodoro.refreshPomodoro() - } - - Timer { - id: stopwatchTimer - interval: 10 - running: Pomodoro.isStopwatchRunning - repeat: true - onTriggered: Pomodoro.refreshStopwatch() - } - - ColumnLayout { anchors.fill: parent spacing: 0 diff --git a/.config/quickshell/ii/modules/sidebarRight/pomodoro/Stopwatch.qml b/.config/quickshell/ii/modules/sidebarRight/pomodoro/Stopwatch.qml index 8f5d6529..f1c428dd 100644 --- a/.config/quickshell/ii/modules/sidebarRight/pomodoro/Stopwatch.qml +++ b/.config/quickshell/ii/modules/sidebarRight/pomodoro/Stopwatch.qml @@ -61,7 +61,9 @@ Item { Layout.preferredWidth: 90 font.pixelSize: Appearance.font.pixelSize.larger - onClicked: Pomodoro.toggleStopwatch() + onClicked: { + Pomodoro.toggleStopwatch() + } colBackground: Pomodoro.isStopwatchRunning ? Appearance.colors.colSecondaryContainer : Appearance.colors.colPrimary colBackgroundHover: Pomodoro.isStopwatchRunning ? Appearance.colors.colSecondaryContainerHover : Appearance.colors.colPrimaryHover @@ -79,7 +81,12 @@ Item { implicitWidth: 90 font.pixelSize: Appearance.font.pixelSize.larger - onClicked: Pomodoro.stopwatchResetOrLaps() + onClicked: { + if (Pomodoro.isStopwatchRunning) + Pomodoro.stopwatchRecordLap() + else + Pomodoro.stopwatchReset() + } enabled: Pomodoro.stopwatchTime !== 0 colBackground: Pomodoro.isStopwatchRunning ? Appearance.colors.colLayer2 : Appearance.colors.colErrorContainer diff --git a/.config/quickshell/ii/services/Pomodoro.qml b/.config/quickshell/ii/services/Pomodoro.qml index 87fe046b..dd60a9f4 100644 --- a/.config/quickshell/ii/services/Pomodoro.qml +++ b/.config/quickshell/ii/services/Pomodoro.qml @@ -21,7 +21,7 @@ Singleton { property string alertSound: Config.options.time.pomodoro.alertSound property bool isPomodoroRunning: Persistent.states.timer.pomodoro.running - property bool isBreak: false + property bool isBreak: Persistent.states.timer.pomodoro.isBreak property bool isPomodoroReset: !isPomodoroRunning property int timeLeft: focusTime property int pomodoroSecondsLeft: focusTime @@ -33,35 +33,24 @@ Singleton { property int stopwatchStart: Persistent.states.timer.stopwatch.start property var stopwatchLaps: Persistent.states.timer.stopwatch.laps + // General Component.onCompleted: { if (!isStopwatchRunning) stopwatchReset() } - // Start and Stop button - function togglePomodoro() { - isPomodoroReset = false - Persistent.states.timer.pomodoro.running = !isPomodoroRunning - if (isPomodoroRunning) { // Pressed Start button - Persistent.states.timer.pomodoro.start = getCurrentTimeInSeconds() - } else { // Pressed Stop button - timeLeft -= (getCurrentTimeInSeconds() - pomodoroStart) - } + function getCurrentTimeInSeconds() { // Pomodoro uses Seconds + return Math.floor(Date.now() / 1000) } - // Reset button - function resetPomodoro() { - Persistent.states.timer.pomodoro.running = false - isBreak = false - isPomodoroReset = true - timeLeft = focusTime - Persistent.states.timer.pomodoro.start = getCurrentTimeInSeconds() - pomodoroCycle = 1 - refreshPomodoro() + function getCurrentTimeIn10ms() { // Stopwatch uses 10ms + return Math.floor(Date.now() / 10) } + // Pomodoro function refreshPomodoro() { + // Work <-> break ? if (getCurrentTimeInSeconds() >= pomodoroStart + timeLeft) { - isBreak = !isBreak + Persistent.states.timer.pomodoro.isBreak = !isBreak Persistent.states.timer.pomodoro.start += timeLeft timeLeft = isBreak ? breakTime : focusTime @@ -86,45 +75,71 @@ Singleton { pomodoroSecondsLeft = (pomodoroStart + timeLeft) - getCurrentTimeInSeconds() } - function getCurrentTimeInSeconds() { // Pomodoro uses Seconds - return Math.floor(Date.now() / 1000) + Timer { + id: pomodoroTimer + interval: 200 + running: root.isPomodoroRunning + repeat: true + onTriggered: Pomodoro.refreshPomodoro() } - function getCurrentTimeIn10ms() { // Stopwatch uses 10ms - return Math.floor(Date.now() / 10) + function togglePomodoro() { + isPomodoroReset = false + Persistent.states.timer.pomodoro.running = !isPomodoroRunning + if (isPomodoroRunning) { // Pressed Start button + Persistent.states.timer.pomodoro.start = getCurrentTimeInSeconds() + } else { // Pressed Stop button + timeLeft -= (getCurrentTimeInSeconds() - pomodoroStart) + } } - function refreshStopwatch() { // stopwatch stores time in 10ms + function resetPomodoro() { + Persistent.states.timer.pomodoro.running = false + Persistent.states.timer.pomodoro.isBreak = false + isPomodoroReset = true + timeLeft = focusTime + Persistent.states.timer.pomodoro.start = getCurrentTimeInSeconds() + pomodoroCycle = 1 + refreshPomodoro() + } + + // Stopwatch + function refreshStopwatch() { // Stopwatch stores time in 10ms stopwatchTime = getCurrentTimeIn10ms() - stopwatchStart } - // Stopwatch functions - function toggleStopwatch() { - Persistent.states.timer.stopwatch.running = !isStopwatchRunning - if (isStopwatchRunning) { - // Resume from paused time by adjusting start time - Persistent.states.timer.stopwatch.start = getCurrentTimeIn10ms() - stopwatchTime - } + Timer { + id: stopwatchTimer + interval: 10 + running: root.isStopwatchRunning + repeat: true + onTriggered: root.refreshStopwatch() } - function stopwatchResetOrLaps() { - if (isStopwatchRunning) { - recordLaps() - } else { - stopwatchReset() - } + function toggleStopwatch() { + if (root.isStopwatchRunning) + root.stopwatchPause() + else + root.stopwatchResume() + } + + function stopwatchPause() { + Persistent.states.timer.stopwatch.running = false + } + + function stopwatchResume() { + Persistent.states.timer.stopwatch.running = true + Persistent.states.timer.stopwatch.start = getCurrentTimeIn10ms() - stopwatchTime } function stopwatchReset() { Persistent.states.timer.stopwatch.running = false stopwatchTime = 0 - stopwatchStart = getCurrentTimeIn10ms() + Persistent.states.timer.stopwatch.start = getCurrentTimeIn10ms() Persistent.states.timer.stopwatch.laps = [] } - function recordLaps() { + function stopwatchRecordLap() { Persistent.states.timer.stopwatch.laps.push(stopwatchTime) - // Reassign to trigger change - // Persistent.states.timer.stopwatch.laps = Persistent.states.timer.stopwatch.laps.slice(0) } }