Repository navigation
Expand file tree
/
Copy pathLogWindow.cs
More file actions
85 lines (72 loc) · 2.44 KB
/
Copy pathLogWindow.cs
File metadata and controls
85 lines (72 loc) · 2.44 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
namespace V2XAmbilight;
/// <summary>
/// Simple floating log window accessible from the tray menu.
/// Thread-safe: Append() can be called from any thread.
/// </summary>
internal sealed class LogWindow : Form
{
private readonly RichTextBox _box;
private bool _allowClose;
private const int MaxLines = 500;
public LogWindow()
{
Text = "V2X Ambilight — Log";
Size = new Size(560, 340);
MinimumSize = new Size(360, 200);
StartPosition = FormStartPosition.Manual;
Location = new Point(
Screen.PrimaryScreen!.WorkingArea.Right - Width - 16,
Screen.PrimaryScreen!.WorkingArea.Bottom - Height - 16);
FormBorderStyle = FormBorderStyle.SizableToolWindow;
ShowInTaskbar = false;
_box = new RichTextBox
{
Dock = DockStyle.Fill,
ReadOnly = true,
BackColor = Color.FromArgb(18, 18, 18),
ForeColor = Color.FromArgb(220, 220, 220),
Font = new Font("Consolas", 9f),
BorderStyle = BorderStyle.None,
WordWrap = false,
ScrollBars = RichTextBoxScrollBars.Both,
};
var clear = new Button
{
Text = "Clear",
Dock = DockStyle.Bottom,
Height = 26,
};
clear.Click += (_, _) => _box.Clear();
Controls.Add(_box);
Controls.Add(clear);
// Hide instead of close when user clicks X, but allow real disposal on app exit
FormClosing += (_, e) => { if (!_allowClose) { e.Cancel = true; Hide(); } };
// Force handle creation so BeginInvoke works before the window is ever shown
_ = Handle;
Append("Log started.");
}
public void AllowClose() => _allowClose = true;
public void Append(string message)
{
string line = $"[{DateTime.Now:HH:mm:ss}] {message}";
if (_box.InvokeRequired)
{
_box.BeginInvoke(() => AppendLine(line));
}
else
{
AppendLine(line);
}
}
private void AppendLine(string line)
{
// Trim oldest lines if over limit
if (_box.Lines.Length > MaxLines)
{
_box.Select(0, _box.GetFirstCharIndexFromLine(MaxLines / 2));
_box.SelectedText = "";
}
_box.AppendText(line + Environment.NewLine);
_box.ScrollToCaret();
}
}