-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrayApplicationContext.cs
More file actions
154 lines (133 loc) · 4.89 KB
/
Copy pathTrayApplicationContext.cs
File metadata and controls
154 lines (133 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Drawing;
using System.Windows.Forms;
namespace BoxBreathingTray
{
/// <summary>
/// Manages the system tray icon and overall application lifecycle.
/// The animated icon IS the app — no main window is shown by default.
/// </summary>
public class TrayApplicationContext : ApplicationContext
{
private readonly NotifyIcon _notifyIcon;
private readonly BreathingSettings _settings;
private readonly AnimationEngine _engine;
private readonly Timer _eyeBreakReminderTimer;
private SettingsForm? _settingsForm;
private ToolStripMenuItem? _pauseResumeItem;
public TrayApplicationContext()
{
_settings = BreathingSettings.LoadOrDefault();
_notifyIcon = new NotifyIcon
{
Visible = true,
Text = BuildTooltipText(false),
ContextMenuStrip = BuildContextMenu()
};
_notifyIcon.MouseClick += OnNotifyIconMouseClick;
_engine = new AnimationEngine(_settings, icon =>
{
var previousIcon = _notifyIcon.Icon;
_notifyIcon.Icon = icon;
previousIcon?.Dispose();
});
_eyeBreakReminderTimer = new Timer();
_eyeBreakReminderTimer.Tick += OnEyeBreakReminderTick;
ApplyReminderSettings();
_engine.Start();
}
private ContextMenuStrip BuildContextMenu()
{
var menu = new ContextMenuStrip();
var openSettings = new ToolStripMenuItem("⚙ Settings…");
openSettings.Click += OpenSettings;
_pauseResumeItem = new ToolStripMenuItem("⏸ Pause");
_pauseResumeItem.Click += (_, __) => TogglePause();
var separator = new ToolStripSeparator();
var exitItem = new ToolStripMenuItem("✕ Exit");
exitItem.Click += (_, __) =>
{
_engine.Stop();
_notifyIcon.Visible = false;
Application.Exit();
};
menu.Items.Add(openSettings);
menu.Items.Add(_pauseResumeItem);
menu.Items.Add(separator);
menu.Items.Add(exitItem);
return menu;
}
private string BuildTooltipText(bool paused)
{
string status = paused ? "Paused" : "Running";
return $"Box Breathing ({status}) — {_settings.SecondsPerSide:0.0}s/side";
}
private void TogglePause()
{
bool isPaused = _engine.TogglePause();
if (_pauseResumeItem != null)
{
_pauseResumeItem.Text = isPaused ? "▶ Resume" : "⏸ Pause";
}
_notifyIcon.Text = BuildTooltipText(isPaused);
}
private void OnNotifyIconMouseClick(object? sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
TogglePause();
}
}
private void OpenSettings(object? sender, EventArgs e)
{
if (_settingsForm == null || _settingsForm.IsDisposed)
{
_settingsForm = new SettingsForm(_settings);
_settingsForm.SettingsChanged += () =>
{
_settings.Save();
_engine.ApplySettings(_settings);
ApplyReminderSettings();
_notifyIcon.Text = BuildTooltipText(_engine.IsPaused);
};
_settingsForm.Show();
}
else
{
_settingsForm.BringToFront();
}
}
private void ApplyReminderSettings()
{
_eyeBreakReminderTimer.Stop();
if (!_settings.EnableEyeBreakReminder)
{
return;
}
int maxMinutes = int.MaxValue / (60 * 1000);
int intervalMinutes = Math.Clamp(_settings.EyeBreakReminderMinutes, 1, maxMinutes);
int intervalMs = intervalMinutes * 60 * 1000;
_eyeBreakReminderTimer.Interval = intervalMs;
_eyeBreakReminderTimer.Start();
}
private void OnEyeBreakReminderTick(object? sender, EventArgs e)
{
_notifyIcon.BalloonTipTitle = "Eye Break Reminder";
_notifyIcon.BalloonTipText = "Look at something far away and blink for a moment.";
_notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
_notifyIcon.ShowBalloonTip(4000);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_engine.Stop();
_eyeBreakReminderTimer.Stop();
_eyeBreakReminderTimer.Dispose();
_notifyIcon.Dispose();
_settingsForm?.Dispose();
}
base.Dispose(disposing);
}
}
}