-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrayApplicationContext.cs
More file actions
161 lines (137 loc) Β· 6.93 KB
/
Copy pathTrayApplicationContext.cs
File metadata and controls
161 lines (137 loc) Β· 6.93 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
155
156
157
158
159
160
161
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace UiVisualDebugger;
public class TrayApplicationContext : ApplicationContext
{
private readonly NotifyIcon _notifyIcon;
private readonly ContextMenuStrip _rightClickMenu;
private readonly ContextMenuStrip _leftClickMenu;
private readonly GlobalHotkey _hotkey;
private readonly ProcessWatcher _watcher;
private readonly ToolStripMenuItem _autoDetectRightItem;
private readonly ToolStripMenuItem _autoDetectLeftItem;
private readonly ToolStripMenuItem _startupRightItem;
private readonly ToolStripMenuItem _startupLeftItem;
public TrayApplicationContext()
{
_rightClickMenu = new ContextMenuStrip();
_leftClickMenu = new ContextMenuStrip();
bool isStartupOn = StartupManager.IsStartupEnabled();
_autoDetectRightItem = new ToolStripMenuItem("π νκ² μλ κ°μ§ (ON)", null, ToggleAutoDetect) { Checked = true };
_autoDetectLeftItem = new ToolStripMenuItem("π νκ² μλ κ°μ§ (ON)", null, ToggleAutoDetect) { Checked = true };
_startupRightItem = new ToolStripMenuItem($"π μλμ° μμ νλ‘κ·Έλ¨ ({(isStartupOn ? "ON" : "OFF")})", null, ToggleStartup) { Checked = isStartupOn };
_startupLeftItem = new ToolStripMenuItem($"π μλμ° μμ νλ‘κ·Έλ¨ ({(isStartupOn ? "ON" : "OFF")})", null, ToggleStartup) { Checked = isStartupOn };
_rightClickMenu.Items.Add(new ToolStripMenuItem("πΈ μ¦μ μ€λ
μ· λ€ν (F12)", null, OnCaptureClicked));
_rightClickMenu.Items.Add(_autoDetectRightItem);
_rightClickMenu.Items.Add(_startupRightItem);
_rightClickMenu.Items.Add(new ToolStripSeparator());
_rightClickMenu.Items.Add(new ToolStripMenuItem("π κ²°κ³Ό μ μ₯ ν΄λ μ΄κΈ°", null, OnOpenFolderClicked));
_rightClickMenu.Items.Add(new ToolStripSeparator());
_rightClickMenu.Items.Add(new ToolStripMenuItem("β μ’
λ£", null, OnExitClicked));
_leftClickMenu.Items.Add(new ToolStripMenuItem("β‘ [ν΅ λ©λ΄] UiVisualDebugger") { Enabled = false, Font = new Font(Control.DefaultFont, FontStyle.Bold) });
_leftClickMenu.Items.Add(new ToolStripSeparator());
_leftClickMenu.Items.Add(new ToolStripMenuItem("πΈ μ€λ
μ· λ€ν μμ± (F12)", null, OnCaptureClicked));
_leftClickMenu.Items.Add(_autoDetectLeftItem);
_leftClickMenu.Items.Add(_startupLeftItem);
_leftClickMenu.Items.Add(new ToolStripMenuItem("π λ€ν μ μ₯ ν΄λ μ΄κΈ°", null, OnOpenFolderClicked));
_notifyIcon = new NotifyIcon
{
Icon = SystemIcons.Application,
Text = "UiVisualDebugger - Real-Time UI Inspector (F12)",
Visible = true
};
_notifyIcon.MouseClick += NotifyIcon_MouseClick;
_hotkey = new GlobalHotkey(GlobalHotkey.MOD_NONE, Keys.F12);
_hotkey.HotkeyPressed += (s, e) => CaptureCurrentSnapshot("Global Hotkey F12");
_watcher = new ProcessWatcher("PhMeter.WpfApp");
_watcher.ProcessStarted += proc =>
{
_notifyIcon.ShowBalloonTip(3000, "UiVisualDebugger", $"νκ² μ± '{proc.ProcessName}' κ°μ§λ¨! μ€λ
μ· μλ μμ± μ€...", ToolTipIcon.Info);
CaptureCurrentSnapshot("Auto Process Watcher", proc);
};
_watcher.Start();
_notifyIcon.ShowBalloonTip(3000, "UiVisualDebugger μμ£Ό ꡬλ μλ£", "μμ€ν
νΈλ μ΄μμ λκΈ° μ€μ
λλ€.\n[F12] ν€λ₯Ό λλ₯΄λ©΄ μ€λ
μ·μ λ€νν©λλ€.", ToolTipIcon.Info);
}
private void NotifyIcon_MouseClick(object? sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MethodInfo? mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
if (mi != null)
{
_notifyIcon.ContextMenuStrip = _leftClickMenu;
mi.Invoke(_notifyIcon, null);
}
else
{
_leftClickMenu.Show(Cursor.Position);
}
}
else if (e.Button == MouseButtons.Right)
{
_notifyIcon.ContextMenuStrip = _rightClickMenu;
}
}
private void OnCaptureClicked(object? sender, EventArgs e)
{
CaptureCurrentSnapshot("Tray Menu Click");
}
private void ToggleAutoDetect(object? sender, EventArgs e)
{
_watcher.IsEnabled = !_watcher.IsEnabled;
bool isEnabled = _watcher.IsEnabled;
_autoDetectRightItem.Checked = isEnabled;
_autoDetectLeftItem.Checked = isEnabled;
_autoDetectRightItem.Text = $"π νκ² μλ κ°μ§ ({(isEnabled ? "ON" : "OFF")})";
_autoDetectLeftItem.Text = $"π νκ² μλ κ°μ§ ({(isEnabled ? "ON" : "OFF")})";
_notifyIcon.ShowBalloonTip(2000, "UiVisualDebugger", $"μλ νλ‘μΈμ€ κ°μ§ κΈ°λ₯μ΄ {(isEnabled ? "νμ±ν" : "λΉνμ±ν")}λμμ΅λλ€.", ToolTipIcon.Info);
}
private void ToggleStartup(object? sender, EventArgs e)
{
bool currentState = StartupManager.IsStartupEnabled();
bool newState = !currentState;
bool success = StartupManager.SetStartup(newState);
if (success)
{
_startupRightItem.Checked = newState;
_startupLeftItem.Checked = newState;
_startupRightItem.Text = $"π μλμ° μμ νλ‘κ·Έλ¨ ({(newState ? "ON" : "OFF")})";
_startupLeftItem.Text = $"π μλμ° μμ νλ‘κ·Έλ¨ ({(newState ? "ON" : "OFF")})";
_notifyIcon.ShowBalloonTip(2000, "UiVisualDebugger", $"μλμ° μμ νλ‘κ·Έλ¨ λ±λ‘μ΄ {(newState ? "μ€μ " : "ν΄μ ")}λμμ΅λλ€.", ToolTipIcon.Info);
}
else
{
_notifyIcon.ShowBalloonTip(2000, "UiVisualDebugger", "μμ νλ‘κ·Έλ¨ λ μ§μ€νΈλ¦¬ μ€μ λ³κ²½ μ€ν¨", ToolTipIcon.Warning);
}
}
private void OnOpenFolderClicked(object? sender, EventArgs e)
{
string dir = Path.GetFullPath(".");
Process.Start("explorer.exe", dir);
}
private void CaptureCurrentSnapshot(string sourceTrigger, Process? targetProc = null)
{
try
{
string procName = targetProc?.ProcessName ?? "PhMeter.WpfApp";
var (jsonFile, imgFile) = ExternalUiInspector.AttachAndInspect(procName, ".");
_notifyIcon.ShowBalloonTip(2500, "πΈ μ€λ
μ· μμ± μλ£", $"[Trigger: {sourceTrigger}]\n- JSON: {Path.GetFileName(jsonFile)}\n- Image: {Path.GetFileName(imgFile)}", ToolTipIcon.Info);
}
catch (Exception ex)
{
_notifyIcon.ShowBalloonTip(3000, "β μ€λ
μ· μ€λ₯", ex.Message, ToolTipIcon.Warning);
}
}
private void OnExitClicked(object? sender, EventArgs e)
{
_watcher.Dispose();
_hotkey.Dispose();
_notifyIcon.Visible = false;
_notifyIcon.Dispose();
Application.Exit();
}
}