// 番茄钟 - 绿色工具箱 // 单文件零依赖,C# 5.0 语法(net48 自带 csc 可编译) // 功能:25 分钟专注 + 5 分钟短休,每 4 个番茄一次 15 分钟长休;自动记录到文本 // 边界:不做云同步、不统计图表;关掉窗口计时就结束(不会偷偷后台跑) // 原理:一个 1 秒定时器推进状态机,状态机与界面分离,便于自测 using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; using System.Media; using System.Text; using System.Windows.Forms; namespace GreenToolbox { public class PomState { public const int PhaseWork = 0; public const int PhaseBreak = 1; public const int PhaseLong = 2; public int WorkSec = 25 * 60; public int BreakSec = 5 * 60; public int LongSec = 15 * 60; public int RoundsBeforeLong = 4; public int Phase = PhaseWork; public int Remaining; public bool Running; public int CompletedWork; public int RoundInCycle; public PomState() { Remaining = WorkSec; } public string PhaseName { get { if (Phase == PhaseBreak) return "短休息"; if (Phase == PhaseLong) return "长休息"; return "专注"; } } public int PhaseTotal { get { if (Phase == PhaseBreak) return BreakSec; if (Phase == PhaseLong) return LongSec; return WorkSec; } } /* 已经过去多少(0~1) */ public double Elapsed { get { int total = PhaseTotal; if (total <= 0) return 0; double v = (double)(total - Remaining) / total; if (v < 0) v = 0; if (v > 1) v = 1; return v; } } public static string Format(int sec) { if (sec < 0) sec = 0; int m = sec / 60; int s = sec % 60; return m.ToString("00") + ":" + s.ToString("00"); } /* 每秒调用一次。返回 0=没变化,1=工作结束开始休息,2=短休结束开始工作,3=长休结束,4=跳过 */ public int Tick() { if (!Running) return 0; if (Remaining > 0) Remaining--; if (Remaining > 0) return 0; return Advance(true); } /* 跳过当前阶段:工作的跳过不计入完成数(不虚报) */ public int Skip() { return Advance(false); } private int Advance(bool natural) { if (Phase == PhaseWork) { if (natural) CompletedWork++; RoundInCycle++; bool longTime = RoundInCycle >= RoundsBeforeLong; if (longTime) { Phase = PhaseLong; Remaining = LongSec; RoundInCycle = 0; } else { Phase = PhaseBreak; Remaining = BreakSec; } return natural ? 1 : 4; } if (Phase == PhaseBreak) { Phase = PhaseWork; Remaining = WorkSec; return natural ? 2 : 4; } // 长休结束,回到专注 Phase = PhaseWork; Remaining = WorkSec; RoundInCycle = 0; return natural ? 3 : 4; } public void StartPause() { Running = !Running; } public void ResetPhase() { Remaining = PhaseTotal; Running = false; } public void ResetAll() { Phase = PhaseWork; Remaining = WorkSec; RoundInCycle = 0; Running = false; } /* 设置时长,单位分钟;越界一律夹紧而不是报错(用户输入 0 不该炸) */ public void SetDurations(int work, int brk, int lng, int rounds) { WorkSec = Clamp(work, 1, 180) * 60; BreakSec = Clamp(brk, 1, 60) * 60; LongSec = Clamp(lng, 1, 120) * 60; RoundsBeforeLong = Clamp(rounds, 1, 12); if (RoundInCycle >= RoundsBeforeLong) RoundInCycle = 0; Remaining = PhaseTotal; } public static int Clamp(int v, int lo, int hi) { if (v < lo) return lo; if (v > hi) return hi; return v; } public string SettingsLine() { return WorkSec / 60 + "," + BreakSec / 60 + "," + LongSec / 60 + "," + RoundsBeforeLong; } public static PomState FromSettings(string line) { PomState s = new PomState(); if (string.IsNullOrEmpty(line)) return s; string[] p = line.Trim().Split(','); if (p.Length < 4) return s; int w, b, l, r; if (!int.TryParse(p[0], out w)) return s; if (!int.TryParse(p[1], out b)) return s; if (!int.TryParse(p[2], out l)) return s; if (!int.TryParse(p[3], out r)) return s; s.SetDurations(w, b, l, r); return s; } public static void AppendLog(string path, string task, int minutes, DateTime when) { string line = when.ToString("yyyy-MM-dd HH:mm:ss") + "\t完成番茄\t" + (string.IsNullOrEmpty(task) ? "(没写任务)" : task.Trim()) + "\t" + minutes + " 分钟"; File.AppendAllText(path, line + "\r\n", new UTF8Encoding(true)); } } public class PomodoroForm : Form { private Label lblTime; private Label lblPhase; private Label lblInfo; private TextBox txtTask; private Button btnStart; private Button btnSkip; private Button btnReset; private TextBox txtWork, txtBreak, txtLong, txtRounds; private Button btnApply; private Timer timer; private NotifyIcon tray; private PomState st = new PomState(); private string _logPath; private string _cfgPath; public PomodoroForm() { string dir = DataDir(); _logPath = Path.Combine(dir, "番茄钟记录.txt"); _cfgPath = Path.Combine(dir, "番茄钟设置.txt"); try { if (File.Exists(_cfgPath)) st = PomState.FromSettings(File.ReadAllText(_cfgPath, new UTF8Encoding(false))); } catch { } InitUi(); SyncSettingsBoxes(); UpdateView(); } static string DataDir() { try { string exeDir = Path.GetDirectoryName(Application.ExecutablePath); string d = Path.Combine(exeDir, "番茄钟数据"); Directory.CreateDirectory(d); string probe = Path.Combine(d, ".w"); File.WriteAllText(probe, "x"); File.Delete(probe); return d; } catch { } string app = Path.Combine(Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData), "GreenToolbox", "番茄钟"); Directory.CreateDirectory(app); return app; } private void InitUi() { this.Text = "番茄钟 - 绿色工具箱"; this.Font = new Font("Microsoft YaHei UI", 9.5f); this.BackColor = Color.White; this.ForeColor = Color.FromArgb(34, 40, 36); this.ClientSize = new Size(520, 520); this.MinimumSize = new Size(480, 500); this.StartPosition = FormStartPosition.CenterScreen; lblPhase = new Label(); lblPhase.Text = "专注"; lblPhase.Font = new Font("Microsoft YaHei UI", 12f); lblPhase.ForeColor = Color.FromArgb(0, 145, 88); lblPhase.TextAlign = ContentAlignment.MiddleCenter; lblPhase.Location = new Point(20, 24); lblPhase.Size = new Size(480, 30); this.Controls.Add(lblPhase); lblTime = new Label(); lblTime.Text = "25:00"; lblTime.Font = new Font("Consolas", 64f, FontStyle.Bold); lblTime.TextAlign = ContentAlignment.MiddleCenter; lblTime.Location = new Point(20, 56); lblTime.Size = new Size(480, 110); this.Controls.Add(lblTime); lblInfo = new Label(); lblInfo.Text = ""; lblInfo.TextAlign = ContentAlignment.MiddleCenter; lblInfo.ForeColor = Color.FromArgb(120, 130, 124); lblInfo.Location = new Point(20, 168); lblInfo.Size = new Size(480, 24); this.Controls.Add(lblInfo); Label lbTask = new Label(); lbTask.Text = "这一段做什么"; lbTask.Location = new Point(60, 206); lbTask.AutoSize = true; this.Controls.Add(lbTask); txtTask = new TextBox(); txtTask.Location = new Point(160, 202); txtTask.Size = new Size(300, 26); txtTask.Text = "写代码"; this.Controls.Add(txtTask); btnStart = new Button(); btnStart.Text = "开始"; btnStart.Location = new Point(60, 246); btnStart.Size = new Size(130, 40); btnStart.BackColor = Color.FromArgb(0, 145, 88); btnStart.ForeColor = Color.White; btnStart.FlatStyle = FlatStyle.Flat; btnStart.Click += new EventHandler(StartClick); this.Controls.Add(btnStart); btnSkip = new Button(); btnSkip.Text = "跳过这一段"; btnSkip.Location = new Point(200, 246); btnSkip.Size = new Size(120, 40); btnSkip.Click += new EventHandler(SkipClick); this.Controls.Add(btnSkip); btnReset = new Button(); btnReset.Text = "重置"; btnReset.Location = new Point(330, 246); btnReset.Size = new Size(100, 40); btnReset.Click += new EventHandler(ResetClick); this.Controls.Add(btnReset); Label lbSet = new Label(); lbSet.Text = "时长设置(分钟)"; lbSet.Location = new Point(60, 308); lbSet.AutoSize = true; lbSet.ForeColor = Color.FromArgb(120, 130, 124); this.Controls.Add(lbSet); txtWork = MakeNum(60, 330); Label l1 = MakeTip("专注", 60, 362); this.Controls.Add(l1); txtBreak = MakeNum(140, 330); Label l2 = MakeTip("短休", 140, 362); this.Controls.Add(l2); txtLong = MakeNum(220, 330); Label l3 = MakeTip("长休", 220, 362); this.Controls.Add(l3); txtRounds = MakeNum(300, 330); Label l4 = MakeTip("几个番茄后长休", 288, 362); this.Controls.Add(l4); btnApply = new Button(); btnApply.Text = "应用"; btnApply.Location = new Point(392, 330); btnApply.Size = new Size(70, 28); btnApply.Click += new EventHandler(ApplyClick); this.Controls.Add(btnApply); Label lbLog = new Label(); lbLog.Text = "记录文件:" + _logPath; lbLog.Location = new Point(60, 402); lbLog.Size = new Size(400, 20); lbLog.AutoEllipsis = true; lbLog.ForeColor = Color.FromArgb(150, 158, 153); this.Controls.Add(lbLog); Label lbNote = new Label(); lbNote.Text = "提示:关掉窗口计时就停,不会在后台偷偷计时;完成时会有提示音。"; lbNote.Location = new Point(60, 424); lbNote.AutoSize = true; lbNote.ForeColor = Color.FromArgb(150, 158, 153); this.Controls.Add(lbNote); timer = new Timer(); timer.Interval = 1000; timer.Tick += new EventHandler(TimerTick); tray = new NotifyIcon(); tray.Icon = SystemIcons.Application; tray.Text = "番茄钟"; tray.Visible = true; } private TextBox MakeNum(int x, int y) { TextBox t = new TextBox(); t.Location = new Point(x, y); t.Size = new Size(64, 26); t.TextAlign = HorizontalAlignment.Center; this.Controls.Add(t); return t; } private Label MakeTip(string s, int x, int y) { Label l = new Label(); l.Text = s; l.Location = new Point(x, y); l.AutoSize = true; l.ForeColor = Color.FromArgb(120, 130, 124); return l; } private void SyncSettingsBoxes() { txtWork.Text = (st.WorkSec / 60).ToString(); txtBreak.Text = (st.BreakSec / 60).ToString(); txtLong.Text = (st.LongSec / 60).ToString(); txtRounds.Text = st.RoundsBeforeLong.ToString(); } private void StartClick(object sender, EventArgs e) { st.StartPause(); timer.Enabled = st.Running; UpdateView(); } private void SkipClick(object sender, EventArgs e) { int ev = st.Skip(); timer.Enabled = st.Running; UpdateView(); Balloon(ev, false); if (ev == 4) lblInfo.Text = "已跳过,下一段:" + st.PhaseName; } private void ResetClick(object sender, EventArgs e) { st.ResetAll(); timer.Enabled = false; UpdateView(); } private void ApplyClick(object sender, EventArgs e) { int w = IntOf(txtWork.Text, 25); int b = IntOf(txtBreak.Text, 5); int l = IntOf(txtLong.Text, 15); int r = IntOf(txtRounds.Text, 4); st.SetDurations(w, b, l, r); SyncSettingsBoxes(); try { File.WriteAllText(_cfgPath, st.SettingsLine(), new UTF8Encoding(false)); } catch { } timer.Enabled = st.Running; UpdateView(); lblInfo.Text = "设置已保存(" + st.WorkSec / 60 + " / " + st.BreakSec / 60 + " / " + st.LongSec / 60 + " 分钟,每 " + st.RoundsBeforeLong + " 个长休)"; } static int IntOf(string s, int def) { int v; if (!int.TryParse((s ?? "").Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out v)) return def; return v; } private void TimerTick(object sender, EventArgs e) { int ev = st.Tick(); UpdateView(); if (ev != 0 && ev != 4) { Balloon(ev, true); if (ev == 1 || ev == 3) { int minutes = st.WorkSec / 60; try { PomState.AppendLog(_logPath, txtTask.Text, minutes, DateTime.Now); } catch { } } } } private void Balloon(int ev, bool natural) { try { if (ev == 1) { SystemSounds.Exclamation.Play(); tray.ShowBalloonTip(4000, "番茄完成", st.Phase == PomState.PhaseLong ? "该长休息 " + st.LongSec / 60 + " 分钟了" : "休息 " + st.BreakSec / 60 + " 分钟", ToolTipIcon.Info); } else if (ev == 2) { SystemSounds.Asterisk.Play(); tray.ShowBalloonTip(4000, "休息结束", "开始下一段专注", ToolTipIcon.Info); } else if (ev == 3) { SystemSounds.Exclamation.Play(); tray.ShowBalloonTip(4000, "长休结束", "新的一轮开始", ToolTipIcon.Info); } } catch { } } private void UpdateView() { lblTime.Text = PomState.Format(st.Remaining); lblPhase.Text = st.PhaseName; Color c = st.Phase == PomState.PhaseWork ? Color.FromArgb(0, 145, 88) : st.Phase == PomState.PhaseLong ? Color.FromArgb(60, 110, 190) : Color.FromArgb(200, 130, 40); lblPhase.ForeColor = c; btnStart.Text = st.Running ? "暂停" : "开始"; btnStart.BackColor = st.Running ? Color.FromArgb(200, 130, 40) : Color.FromArgb(0, 145, 88); lblInfo.Text = "已完成 " + st.CompletedWork + " 个番茄 · 本轮第 " + (st.RoundInCycle + (st.Phase == PomState.PhaseWork ? 1 : 0)) + "/" + st.RoundsBeforeLong + " 个" + (st.Running ? "" : " · 已暂停"); this.Text = PomState.Format(st.Remaining) + " " + st.PhaseName + " - 番茄钟"; } protected override void OnFormClosing(FormClosingEventArgs e) { timer.Enabled = false; try { tray.Visible = false; tray.Dispose(); } catch { } base.OnFormClosing(e); } } // ───────────────────────── 自测 ───────────────────────── public static class PomSelfTest { static int _pass, _fail; static List _lines = new List(); static void Check(string name, object got, object want) { bool ok = string.Equals(got == null ? "" : got.ToString(), want == null ? "" : want.ToString()); if (ok) { _pass++; _lines.Add(" [ok] " + name); } else { _fail++; _lines.Add(" [!!] " + name + " got=" + got + " want=" + want); } } public static int Run(string dir) { _pass = 0; _fail = 0; _lines = new List(); _lines.Add("番茄钟 自测 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); string baseDir = string.IsNullOrEmpty(dir) ? Path.Combine(Path.GetTempPath(), "gt-pom-" + Guid.NewGuid().ToString("N").Substring(0, 8)) : dir; try { Directory.CreateDirectory(baseDir); Check("初始 25 分钟", PomState.Format(new PomState().Remaining), "25:00"); Check("格式化 65 秒", PomState.Format(65), "01:05"); Check("格式化 0", PomState.Format(0), "00:00"); Check("格式化 1 小时", PomState.Format(3600), "60:00"); Check("负数不显示负号", PomState.Format(-5), "00:00"); PomState s = new PomState(); Check("初始阶段是专注", s.PhaseName, "专注"); Check("初始未运行", s.Running, false); Check("未运行时 Tick 不动", s.Tick(), 0); Check("剩余还是 1500", s.Remaining, 1500); s.Running = true; s.Tick(); Check("运行后少 1 秒", s.Remaining, 1499); Check("进度约 1/1500", Math.Round(s.Elapsed, 3).ToString(CultureInfo.InvariantCulture), "0.001"); // 工作结束 → 短休 s.Remaining = 1; int ev = s.Tick(); Check("工作结束事件", ev, 1); Check("进入短休", s.PhaseName, "短休息"); Check("短休 5 分钟", s.Remaining, 300); Check("完成数 +1", s.CompletedWork, 1); Check("本轮计数 1", s.RoundInCycle, 1); // 短休结束 → 专注 s.Remaining = 1; ev = s.Tick(); Check("短休结束事件", ev, 2); Check("回到专注", s.PhaseName, "专注"); Check("专注 25 分钟", s.Remaining, 1500); Check("完成数不变", s.CompletedWork, 1); // 第 4 个番茄后应进长休 s.RoundInCycle = 3; s.Remaining = 1; ev = s.Tick(); Check("第 4 个后事件仍是工作结束", ev, 1); Check("进入长休", s.PhaseName, "长休息"); Check("长休 15 分钟", s.Remaining, 900); Check("长休后计数归零", s.RoundInCycle, 0); Check("完成数累计 2", s.CompletedWork, 2); s.Remaining = 1; ev = s.Tick(); Check("长休结束事件", ev, 3); Check("长休后回专注", s.PhaseName, "专注"); Check("长休后 1500 秒", s.Remaining, 1500); // 暂停 s.Running = false; int before = s.Remaining; s.Tick(); Check("暂停时 Tick 不再减", s.Remaining, before); s.StartPause(); Check("StartPause 切换为运行", s.Running, true); s.StartPause(); Check("再切回暂停", s.Running, false); // 跳过:工作不计完成数 PomState sk = new PomState(); sk.Running = true; int cev = sk.Skip(); Check("跳过返回 4", cev, 4); Check("跳过工作不计完成数", sk.CompletedWork, 0); Check("跳过工作直接进短休", sk.PhaseName, "短休息"); sk.Skip(); Check("跳过休息回到专注", sk.PhaseName, "专注"); Check("跳过休息仍是 25 分钟", sk.Remaining, 1500); // 重置 PomState rs = new PomState(); rs.Running = true; rs.Remaining = 100; rs.ResetPhase(); Check("重置当前段回到满时长", rs.Remaining, 1500); Check("重置后暂停", rs.Running, false); rs.RoundInCycle = 3; rs.CompletedWork = 5; rs.ResetAll(); // 重置只清「本轮」;今天完成数属于成果,不该被重置抹掉 Check("重置保留已完成数", rs.CompletedWork, 5); Check("全部重置清空轮次", rs.RoundInCycle, 0); Check("全部重置回到专注段", rs.PhaseName, "专注"); Check("全部重置时长", rs.Remaining, 1500); // 设置与夹紧 PomState cfg = new PomState(); cfg.SetDurations(30, 6, 20, 3); Check("设置专注 30", cfg.Remaining, 1800); Check("设置行", cfg.SettingsLine(), "30,6,20,3"); cfg.SetDurations(0, 0, 0, 0); Check("0 分钟夹到 1", cfg.WorkSec, 60); Check("0 分钟短休夹到 1", cfg.BreakSec, 60); Check("0 轮夹到 1", cfg.RoundsBeforeLong, 1); cfg.SetDurations(999, 999, 999, 99); Check("上限夹到 180", cfg.WorkSec, 180 * 60); Check("短休上限 60", cfg.BreakSec, 60 * 60); Check("轮数上限 12", cfg.RoundsBeforeLong, 12); PomState from = PomState.FromSettings("45,10,25,5"); Check("读设置专注", from.WorkSec, 2700); Check("读设置长休", from.LongSec, 1500); Check("坏设置回默认", PomState.FromSettings("x,y").WorkSec, 1500); Check("空设置回默认", PomState.FromSettings("").Remaining, 1500); // 记录文件 string log = Path.Combine(baseDir, "记录.txt"); PomState.AppendLog(log, "写报告", 25, new DateTime(2026, 10, 19, 9, 30, 0)); PomState.AppendLog(log, "", 25, new DateTime(2026, 10, 19, 10, 0, 0)); string[] lines = File.ReadAllLines(log, new UTF8Encoding(true)); Check("记录两行", lines.Length, 2); Check("记录含任务名", lines[0].IndexOf("写报告") > 0, true); Check("记录含分钟", lines[0].IndexOf("25 分钟") > 0, true); Check("空任务有占位", lines[1].IndexOf("(没写任务)") > 0, true); Check("记录带时间", lines[0].StartsWith("2026-10-19 09:30:00"), true); PomState.AppendLog(log, "第三条", 25, DateTime.Now); Check("追加不覆盖", File.ReadAllLines(log, new UTF8Encoding(true)).Length, 3); } catch (Exception ex) { _fail++; _lines.Add(" [!!] 自测中途异常:" + ex.GetType().Name + " " + ex.Message); } finally { try { Directory.Delete(baseDir, true); } catch { } } _lines.Add(""); _lines.Add(_fail == 0 ? "结果:共 " + (_pass + _fail) + " 项,全部通过 ✓" : "结果:共 " + (_pass + _fail) + " 项,通过 " + _pass + ",失败 " + _fail + " ✗"); string all = string.Join("\r\n", _lines.ToArray()); Console.WriteLine(all); try { File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), "selftest-pomodoro.txt"), all, new UTF8Encoding(true)); } catch { } return _fail == 0 ? 0 : 1; } } static class Program { [STAThread] static void Main(string[] args) { if (args != null && args.Length > 0 && args[0] == "--selftest") { Environment.Exit(PomSelfTest.Run(args.Length >= 2 ? args[1] : null)); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new PomodoroForm()); } } }