98 lines
2.9 KiB
C#
98 lines
2.9 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Net.Sockets;
|
|
using System.Reflection;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
|
|
class TrayApp : ApplicationContext
|
|
{
|
|
private NotifyIcon tray;
|
|
private int port = 8000;
|
|
private string phpExe = @"C:\wamp64\bin\php\php8.3.28\php.exe";
|
|
private string projectDir;
|
|
|
|
public TrayApp(string dir)
|
|
{
|
|
projectDir = dir;
|
|
StartServer();
|
|
BuildTray();
|
|
}
|
|
|
|
void StartServer()
|
|
{
|
|
bool inUse = false;
|
|
try {
|
|
TcpClient tcp = new TcpClient();
|
|
tcp.Connect("127.0.0.1", port);
|
|
tcp.Close();
|
|
inUse = true;
|
|
} catch {}
|
|
|
|
if (!inUse) {
|
|
ProcessStartInfo psi = new ProcessStartInfo();
|
|
psi.FileName = phpExe;
|
|
psi.Arguments = "artisan serve --host=0.0.0.0 --port=" + port;
|
|
psi.WorkingDirectory = projectDir;
|
|
psi.WindowStyle = ProcessWindowStyle.Hidden;
|
|
psi.CreateNoWindow = true;
|
|
psi.UseShellExecute = false;
|
|
Process.Start(psi);
|
|
}
|
|
}
|
|
|
|
void BuildTray()
|
|
{
|
|
Icon icon = SystemIcons.Application;
|
|
string ico = Path.Combine(projectDir, "public", "favicon.ico");
|
|
if (File.Exists(ico)) { try { icon = new Icon(ico); } catch {} }
|
|
|
|
tray = new NotifyIcon();
|
|
tray.Icon = icon;
|
|
tray.Text = "Matab Panel";
|
|
tray.Visible = true;
|
|
|
|
ContextMenuStrip menu = new ContextMenuStrip();
|
|
|
|
ToolStripMenuItem openItem = new ToolStripMenuItem("Open Matab Panel");
|
|
openItem.Font = new Font(openItem.Font, FontStyle.Bold);
|
|
openItem.Click += delegate { OpenBrowser(); };
|
|
menu.Items.Add(openItem);
|
|
|
|
menu.Items.Add(new ToolStripSeparator());
|
|
|
|
ToolStripMenuItem stopItem = new ToolStripMenuItem("Stop Server");
|
|
stopItem.Click += delegate {
|
|
foreach (Process p in Process.GetProcessesByName("php"))
|
|
try { p.Kill(); } catch {}
|
|
tray.Visible = false;
|
|
ExitThread();
|
|
};
|
|
menu.Items.Add(stopItem);
|
|
|
|
tray.ContextMenuStrip = menu;
|
|
tray.DoubleClick += delegate { OpenBrowser(); };
|
|
|
|
tray.BalloonTipTitle = "Matab Panel";
|
|
tray.BalloonTipText = "Server running - http://localhost:" + port;
|
|
tray.BalloonTipIcon = ToolTipIcon.Info;
|
|
tray.ShowBalloonTip(4000);
|
|
}
|
|
|
|
void OpenBrowser()
|
|
{
|
|
ProcessStartInfo psi = new ProcessStartInfo("http://localhost:" + port);
|
|
psi.UseShellExecute = true;
|
|
Process.Start(psi);
|
|
}
|
|
|
|
[STAThread]
|
|
static void Main()
|
|
{
|
|
string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
string projectDir = Path.GetFullPath(Path.Combine(exeDir, ".."));
|
|
Application.Run(new TrayApp(projectDir));
|
|
}
|
|
}
|