<commit_before># coding=utf-8
import sys
import platform
import time
import ctypes
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QCursor
from PyQt5.QtCore import QTranslator, QFileInfo, QTimer
from MyConfig import *
from MainWindow import MainWindow

if platform.system() == 'Windows':
    myAppId = 'jalon.pomodoro'  # Arbitrary string
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myAppId)

Config = None
AppIcon = None
EXE_Flag = False


class MainCtrl(MainWindow):
    Run = 0
    ReadyToWork = 1
    ReadyToShortBreak = 2
    ReadyToLoneBreak = 3

    def __init__(self):
        super().__init__(AppIcon, EXE_Flag)
        self.nextTime = 0
        self.status = self.ReadyToWork
        self.lastStatue = self.ReadyToWork
        self.timer = MyTimer(self)
        self.timer.update.connect(self.viewTime)
        self.timer.fullTimeout.connect(self.timeout)

        self.buttonMain.customContextMenuRequested.connect(self.menuRequested)
        self.buttonMain.clicked.connect(self.mainClicked)

    def close(self):
        self.timer.stop()
        super().close()
        print('exit')

    def menuRequested(self):
        if self.status != self.Run:
            self.buttonMenu.exec(QCursor.pos())

    def mainClicked(self):
        if self.status != self.Run:
            if self.status == self.ReadyToWork:
                self.hide()
            self.setStatus(self.Run)
        else:
            self.setStatus(self.lastStatue)

    def timeout(self):
        print('timeout')
        self.showWindow()
        if self.lastStatue != self.ReadyToWork:
            self.setStatus(self.ReadyToWork)
        else:
            self.tomatoCount += 1
            self.setCompleteTomato(self.tomatoCount)
            if self.tomatoCount % Config.longBreakPomodoroNumber == 0:
                self.setStatus(self.ReadyToLoneBreak)
            else:
                self.setStatus(self.ReadyToShortBreak)

    # 状态管理
    def setStatus(self, state):
        self.lastStatue = self.status
        self.status = state
        if state == self.Run:
            self.buttonMain.setText(self.s.strReset)
            self.timer.runTimer(self.nextTime)
        else:
            self.timer.stop()

            if state == self.ReadyToWork:
                self.workView(self.s.strWork)
                self.nextTime = Config.workTime
            elif state == self.ReadyToShortBreak:
                self.breakView(self.s.strBreak)
                self.nextTime = Config.shortTime
            elif state == self.ReadyToLoneBreak:
                self.breakView(self.s.strLongBreak)
                self.nextTime = Config.longTime

            self.viewTime(self.nextTime)

    def setWorkStatus(self):
        self.setStatus(self.ReadyToWork)

    def setBreakStatus(self):
        self.setStatus(self.ReadyToShortBreak)

    def setLoneBreakStatus(self):
        self.setStatus(self.ReadyToLoneBreak)


class MyTimer(QTimer):
    fullTimeout = QtCore.pyqtSignal()
    update = QtCore.pyqtSignal(int)

    def __init__(self, parent):
        super().__init__(parent)
        self.setInterval(100)
        self.timeout.connect(self.onTick)
        self.fullTime = 0
        self.startTime = 0
        self.lastTime = 0

    def onTick(self):
        current_time = time.perf_counter()
        view_time = self.fullTime - (current_time - self.startTime)
        if view_time <= 0:
            self.stop()
            self.fullTimeout.emit()
        elif int(view_time) != self.lastTime:
            self.lastTime = int(view_time)
            self.update.emit(self.lastTime)

    def runTimer(self, full_time):
        self.fullTime = full_time
        self.startTime = time.perf_counter()
        self.lastTime = 0
        super().start()

<commit_msg>简化计时逻辑<commit_after># coding=utf-8
import sys
import platform
import time
import ctypes
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QCursor
from PyQt5.QtCore import QTranslator, QFileInfo, QTimer
from MyConfig import *
from MainWindow import MainWindow

if platform.system() == 'Windows':
    myAppId = 'jalon.pomodoro'  # Arbitrary string
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myAppId)

Config = None
AppIcon = None
EXE_Flag = False


class MainCtrl(MainWindow):
    Run = 0
    ReadyToWork = 1
    ReadyToShortBreak = 2
    ReadyToLoneBreak = 3

    def __init__(self):
        super().__init__(AppIcon, EXE_Flag)
        self.nextTime = 0
        self.status = self.ReadyToWork
        self.lastStatue = self.ReadyToWork
        self.timer = MyTimer(self)
        self.timer.update.connect(self.viewTime)
        self.timer.fullTimeout.connect(self.timeout)

        self.buttonMain.customContextMenuRequested.connect(self.menuRequested)
        self.buttonMain.clicked.connect(self.mainClicked)

    def close(self):
        self.timer.stop()
        super().close()
        print('exit')

    def menuRequested(self):
        if self.status != self.Run:
            self.buttonMenu.exec(QCursor.pos())

    def mainClicked(self):
        if self.status != self.Run:
            if self.status == self.ReadyToWork:
                self.hide()
            self.setStatus(self.Run)
        else:
            self.setStatus(self.lastStatue)

    def timeout(self):
        print('timeout')
        self.showWindow()
        if self.lastStatue != self.ReadyToWork:
            self.setStatus(self.ReadyToWork)
        else:
            self.tomatoCount += 1
            self.setCompleteTomato(self.tomatoCount)
            if self.tomatoCount % Config.longBreakPomodoroNumber == 0:
                self.setStatus(self.ReadyToLoneBreak)
            else:
                self.setStatus(self.ReadyToShortBreak)

    # 状态管理
    def setStatus(self, state):
        self.lastStatue = self.status
        self.status = state
        if state == self.Run:
            self.buttonMain.setText(self.s.strReset)
            self.timer.runTimer(self.nextTime)
        else:
            self.timer.stop()

            if state == self.ReadyToWork:
                self.workView(self.s.strWork)
                self.nextTime = Config.workTime
            elif state == self.ReadyToShortBreak:
                self.breakView(self.s.strBreak)
                self.nextTime = Config.shortTime
            elif state == self.ReadyToLoneBreak:
                self.breakView(self.s.strLongBreak)
                self.nextTime = Config.longTime

            self.viewTime(self.nextTime)

    def setWorkStatus(self):
        self.setStatus(self.ReadyToWork)

    def setBreakStatus(self):
        self.setStatus(self.ReadyToShortBreak)

    def setLoneBreakStatus(self):
        self.setStatus(self.ReadyToLoneBreak)


class MyTimer(QTimer):
    fullTimeout = QtCore.pyqtSignal()
    update = QtCore.pyqtSignal(int)

    def __init__(self, parent):
        super().__init__(parent)
        self.setInterval(1000)
        self.timeout.connect(self.onTick)
        self.fullTime = 0

    def onTick(self):
        self.fullTime -= 1
        if self.fullTime <= 0:
            self.stop()
            self.fullTimeout.emit()
        else:
            self.update.emit(self.fullTime)

    def runTimer(self, full_time):
        self.fullTime = full_time
        super().start()


{'fraction_non_alphanumeric': 0.09636091045386398, 'fraction_numerical': 0.005860706010631048, 'mean_word_length': 2.1807542262678803, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5157040', 'n_tokens_mistral': 2280, 'n_tokens_neox': 2195, 'n_words': 398}