Repository navigation
Expand file tree
/
Copy pathStructBindLauncher.cs
More file actions
268 lines (240 loc) · 9.4 KB
/
Copy pathStructBindLauncher.cs
File metadata and controls
268 lines (240 loc) · 9.4 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
namespace StructBind
{
static class Program
{
private static Process serverProcess = null;
private static NotifyIcon trayIcon = null;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Mutex to ensure single instance
bool createdNew;
using (Mutex mutex = new Mutex(true, "StructBind_SingleInstance_App_Mutex", out createdNew))
{
if (!createdNew)
{
OpenBrowser("http://127.0.0.1:8000");
return;
}
string exeDir = AppDomain.CurrentDomain.BaseDirectory;
string projectDir = exeDir;
if (Directory.Exists(Path.Combine(exeDir, "protein-explorer")))
{
projectDir = Path.Combine(exeDir, "protein-explorer");
}
string runScript = Path.Combine(projectDir, "run_offline.py");
if (!File.Exists(runScript))
{
MessageBox.Show(
"Could not locate 'run_offline.py'.\nPlease ensure StructBind.exe is located in the StructBind folder.",
"StructBind - Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return;
}
string pythonPath = FindPythonExecutable();
if (string.IsNullOrEmpty(pythonPath))
{
MessageBox.Show(
"Python 3.10+ runtime was not detected on this system.\nPlease install Python or add it to your PATH.",
"StructBind - Python Required",
MessageBoxButtons.OK,
MessageBoxIcon.Warning
);
return;
}
// Start backend if port 8000 is not already responding
if (!IsPortOpen("127.0.0.1", 8000))
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = pythonPath;
psi.Arguments = "\"" + runScript + "\" --no-browser";
psi.WorkingDirectory = projectDir;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = false;
try
{
serverProcess = Process.Start(psi);
}
catch (Exception ex)
{
MessageBox.Show("Failed to launch StructBind backend server:\n" + ex.Message, "StructBind Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Wait up to 15 seconds for the server to bind
bool ready = false;
for (int i = 0; i < 30; i++)
{
Thread.Sleep(500);
if (IsPortOpen("127.0.0.1", 8000))
{
ready = true;
break;
}
if (serverProcess != null && serverProcess.HasExited)
{
break;
}
}
if (!ready)
{
string errDetail = "";
if (serverProcess != null && serverProcess.HasExited)
{
try { errDetail = serverProcess.StandardError.ReadToEnd(); } catch { }
}
string msg = "StructBind backend server process failed to initialize on 127.0.0.1:8000.";
if (!string.IsNullOrEmpty(errDetail))
{
msg += "\n\nDetails:\n" + errDetail;
}
MessageBox.Show(msg, "StructBind Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
// Open App window (Edge app mode or default browser)
OpenBrowser("http://127.0.0.1:8000");
// Initialize System Tray Icon
InitTrayIcon(exeDir);
AppDomain.CurrentDomain.ProcessExit += (s, e) => ShutdownServer();
Application.ApplicationExit += (s, e) => ShutdownServer();
Application.Run();
}
}
private static void InitTrayIcon(string baseDir)
{
trayIcon = new NotifyIcon();
trayIcon.Text = "StructBind - Computational Biology Platform";
string icoPath = Path.Combine(baseDir, "StructBind.ico");
if (File.Exists(icoPath))
{
trayIcon.Icon = new Icon(icoPath);
}
else
{
trayIcon.Icon = SystemIcons.Application;
}
ContextMenu contextMenu = new ContextMenu();
contextMenu.MenuItems.Add("Open StructBind Window", (s, e) => OpenBrowser("http://127.0.0.1:8000"));
contextMenu.MenuItems.Add("-");
contextMenu.MenuItems.Add("Exit StructBind", (s, e) => {
ShutdownServer();
trayIcon.Visible = false;
Application.Exit();
});
trayIcon.ContextMenu = contextMenu;
trayIcon.DoubleClick += (s, e) => OpenBrowser("http://127.0.0.1:8000");
trayIcon.Visible = true;
}
private static void OpenBrowser(string url)
{
// Try Edge App Mode (creates a dedicated frameless app window)
string edgePath = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
if (!File.Exists(edgePath))
{
edgePath = @"C:\Program Files\Microsoft\Edge\Application\msedge.exe";
}
if (File.Exists(edgePath))
{
try
{
Process.Start(new ProcessStartInfo
{
FileName = edgePath,
Arguments = "--app=" + url + " --window-size=1366,860",
UseShellExecute = true
});
return;
}
catch { }
}
// Fallback to default browser
try
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
catch { }
}
private static bool IsPortOpen(string host, int port)
{
try
{
using (TcpClient client = new TcpClient())
{
IAsyncResult result = client.BeginConnect(host, port, null, null);
bool success = result.AsyncWaitHandle.WaitOne(400);
if (!success) return false;
client.EndConnect(result);
return true;
}
}
catch
{
return false;
}
}
private static string FindPythonExecutable()
{
string userHome = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string[] candidates = new string[]
{
Path.Combine(userHome, @"AppData\Local\Python\bin\python.exe"),
Path.Combine(userHome, @"AppData\Local\Python\pythoncore-3.14-64\python.exe"),
Path.Combine(userHome, @"AppData\Local\Programs\Python\Launcher\py.exe"),
Path.Combine(userHome, @"AppData\Local\Programs\Python\Python314\python.exe"),
@"C:\Python312\python.exe",
@"C:\Python311\python.exe",
@"C:\Python310\python.exe"
};
foreach (string candidate in candidates)
{
if (File.Exists(candidate))
{
return candidate;
}
}
// Check system PATH
string pathEnv = Environment.GetEnvironmentVariable("PATH") ?? "";
foreach (string dir in pathEnv.Split(';'))
{
string p = dir.Trim();
if (!string.IsNullOrEmpty(p))
{
string target = Path.Combine(p, "python.exe");
if (File.Exists(target)) return target;
string targetPy = Path.Combine(p, "py.exe");
if (File.Exists(targetPy)) return targetPy;
}
}
return null;
}
private static void ShutdownServer()
{
try
{
if (serverProcess != null && !serverProcess.HasExited)
{
serverProcess.Kill();
serverProcess.Dispose();
serverProcess = null;
}
}
catch { }
}
}
}