Compare commits

...

2 Commits

Author SHA1 Message Date
e26c015e09 fix: 优化休息时间计算逻辑,使用计数器避免时间跳变 2026-01-17 13:26:14 +08:00
ff8c7f032d fix: 界面细节优化 2026-01-17 13:19:08 +08:00
2 changed files with 18 additions and 9 deletions

View File

@@ -16,6 +16,7 @@ namespace TimerApp
private DateTime _lastWorkStartTime; private DateTime _lastWorkStartTime;
private TimeSpan _accumulatedWorkTime; private TimeSpan _accumulatedWorkTime;
private DateTime _restStartTime; private DateTime _restStartTime;
private int _restElapsedSeconds; // 休息已过秒数(用于避免时间计算导致的跳变)
// 配置 (默认值) // 配置 (默认值)
public TimeSpan WorkDuration { get; set; } = TimeSpan.FromMinutes(20); public TimeSpan WorkDuration { get; set; } = TimeSpan.FromMinutes(20);
@@ -73,10 +74,12 @@ namespace TimerApp
if (CurrentState == MonitorState.Resting) if (CurrentState == MonitorState.Resting)
{ {
// 休息模式逻辑 // 休息模式逻辑
TimeSpan timeInRest = DateTime.Now - _restStartTime; // 使用计数器而不是时间差,避免秒数跳变
TimeSpan remainingRest = RestDuration - timeInRest; _restElapsedSeconds++;
int totalRestSeconds = (int)RestDuration.TotalSeconds;
int remainingSeconds = totalRestSeconds - _restElapsedSeconds;
if (remainingRest <= TimeSpan.Zero) if (remainingSeconds <= 0)
{ {
// 休息结束 // 休息结束
RestEnded?.Invoke(this, EventArgs.Empty); RestEnded?.Invoke(this, EventArgs.Empty);
@@ -84,6 +87,7 @@ namespace TimerApp
} }
else else
{ {
TimeSpan remainingRest = TimeSpan.FromSeconds(remainingSeconds);
RestProgressChanged?.Invoke(this, remainingRest); RestProgressChanged?.Invoke(this, remainingRest);
} }
} }
@@ -130,6 +134,7 @@ namespace TimerApp
{ {
// 触发休息 // 触发休息
_restStartTime = DateTime.Now; _restStartTime = DateTime.Now;
_restElapsedSeconds = 0; // 重置休息计数器
ChangeState(MonitorState.Resting); ChangeState(MonitorState.Resting);
RestStarted?.Invoke(this, EventArgs.Empty); RestStarted?.Invoke(this, EventArgs.Empty);
} }
@@ -145,6 +150,7 @@ namespace TimerApp
public void ForceRest() public void ForceRest()
{ {
_restStartTime = DateTime.Now; _restStartTime = DateTime.Now;
_restElapsedSeconds = 0; // 重置休息计数器
ChangeState(MonitorState.Resting); ChangeState(MonitorState.Resting);
RestStarted?.Invoke(this, EventArgs.Empty); RestStarted?.Invoke(this, EventArgs.Empty);
} }
@@ -158,8 +164,11 @@ namespace TimerApp
} }
else if (CurrentState == MonitorState.Resting) else if (CurrentState == MonitorState.Resting)
{ {
TimeSpan timeInRest = DateTime.Now - _restStartTime; // 使用计数器计算剩余时间,保持一致性
TimeSpan remaining = RestDuration - timeInRest; int totalRestSeconds = (int)RestDuration.TotalSeconds;
int remainingSeconds = totalRestSeconds - _restElapsedSeconds;
if (remainingSeconds < 0) remainingSeconds = 0;
TimeSpan remaining = TimeSpan.FromSeconds(remainingSeconds);
RestProgressChanged?.Invoke(this, remaining); RestProgressChanged?.Invoke(this, remaining);
} }
} }

8
MainForm.Designer.cs generated
View File

@@ -190,9 +190,9 @@ namespace TimerApp
// //
this.label1.AutoSize = true; this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30))))); this.label1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
this.label1.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.label1.Font = new System.Drawing.Font("Microsoft YaHei UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.label1.ForeColor = System.Drawing.Color.LightGray; this.label1.ForeColor = System.Drawing.Color.LightGray;
this.label1.Location = new System.Drawing.Point(40, 20); this.label1.Location = new System.Drawing.Point(40, 22);
this.label1.Name = "label1"; this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23); this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 0; this.label1.TabIndex = 0;
@@ -246,9 +246,9 @@ namespace TimerApp
// //
this.label2.AutoSize = true; this.label2.AutoSize = true;
this.label2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30))))); this.label2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
this.label2.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.label2.Font = new System.Drawing.Font("Microsoft YaHei UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.label2.ForeColor = System.Drawing.Color.LightGray; this.label2.ForeColor = System.Drawing.Color.LightGray;
this.label2.Location = new System.Drawing.Point(40, 60); this.label2.Location = new System.Drawing.Point(40, 62);
this.label2.Name = "label2"; this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 23); this.label2.Size = new System.Drawing.Size(100, 23);
this.label2.TabIndex = 4; this.label2.TabIndex = 4;