Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
KamaleiZestri committed Mar 15, 2023
0 parents commit 836590d
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# VSCode
.vscode
Properties
# Csharp output folders
obj
bin

BinaryReceptor
Binary file added Assets/icon.ico
Binary file not shown.
Binary file added Assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions BinaryReceptorWindows.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>false</SelfContained>
<PublishTrimmed>false</PublishTrimmed>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>Assets\icon.ico</ApplicationIcon>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<None Update="Assets\icon.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<EmbeddedResource Include="Assets\icon.ico" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions BinaryReceptorWindows.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Update="Form1.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup>
</Project>
8 changes: 8 additions & 0 deletions Mode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BinaryReceptorWindows
{
public enum Mode
{
BLUETOOTH = 0,
HTTP = 1
}
}
14 changes: 14 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace BinaryReceptorWindows;

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new TrayApp());
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Binary Receptor Windows
Tray icon for Windows. Allows simple operation of [BinaryReceptor](https://github.com/KamaleiZestri/BinaryReceptor)
120 changes: 120 additions & 0 deletions TrayApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;

namespace BinaryReceptorWindows;

public class TrayApp : ApplicationContext
{
private const string TrayIconResource = "BinaryReceptorWindows.Assets.icon.ico";
private const string BinaryReceptorName = "BinaryReceptor";
private string localUrl = "http://localhost:24234";
private string exePath = "BinaryReceptor//BinaryReceptor.exe";
private Mode mode = Mode.BLUETOOTH;
private NotifyIcon trayIcon;
private ToolStripMenuItem menuItemMode;
private ToolStripMenuItem menuItemSwitch;
private ToolStripMenuItem menuItemStart;
private ToolStripMenuItem menuItemStop;
private ToolStripMenuItem menuItemShow;
private ToolStripMenuItem menuItemExit;

public TrayApp()
{
CreateTrayIcon();
}

private void CreateTrayIcon()
{
menuItemMode = new ToolStripMenuItem("Mode: Bluetooth", null, Switch);
menuItemSwitch = new ToolStripMenuItem("Switch Modes", null, Switch);
menuItemStart = new ToolStripMenuItem("Start", null, Start);
menuItemStop = new ToolStripMenuItem("Stop", null, Stop);
menuItemShow = new ToolStripMenuItem("Show", null, Show);
menuItemExit = new ToolStripMenuItem("Exit", null, Exit);

ContextMenuStrip contextMenu = new ContextMenuStrip();
contextMenu.Items.Add(menuItemMode);
contextMenu.Items.Add(menuItemSwitch);
contextMenu.Items.Add(new ToolStripSeparator());
contextMenu.Items.Add(menuItemStart);
contextMenu.Items.Add(menuItemStop);
contextMenu.Items.Add(menuItemShow);
contextMenu.Items.Add(menuItemExit);

contextMenu.Opening += new CancelEventHandler(ContextMenuOnPopup);
using var iconStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(TrayIconResource);
trayIcon = new NotifyIcon()
{
Icon = new Icon(iconStream),
ContextMenuStrip = contextMenu,
Visible = true,
Text = BinaryReceptorName
};
}
private void Switch(object sender, EventArgs e)
{
Stop(null, null);
if (mode == Mode.BLUETOOTH)
mode = Mode.HTTP;
else
mode = Mode.BLUETOOTH;
}
private void Start(object sender, EventArgs e)
{
string modeString = "bluetooth";
if (mode == Mode.BLUETOOTH)
modeString = "bluetooth";
else
modeString = "http";

Process p = new Process();
p.StartInfo.FileName = exePath;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(exePath);
p.StartInfo.Arguments = modeString;
p.Start();
}
private void Stop(object sender, EventArgs e)
{
Process? process = Process.GetProcessesByName(BinaryReceptorName).FirstOrDefault();
if (process == null)
{
return;
}

if (!process.CloseMainWindow())
{
process.Kill();
}
}
private void Show(object sender, EventArgs e)
{
// hack because of this: https://github.com/dotnet/corefx/issues/10361
string url = localUrl.Replace("&", "^&");
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
}
private void Exit(object sender, EventArgs e)
{
Stop(null, null);
trayIcon.Visible = false;
Application.Exit();
}
private void ContextMenuOnPopup(object sender, EventArgs e)
{
bool exeRunning = Process.GetProcessesByName(BinaryReceptorName).Length > 0;
bool running = exeRunning;
bool stopped = !exeRunning;

menuItemStart.Enabled = stopped;
menuItemShow.Enabled = running;
menuItemStop.Enabled = running;

if (mode == Mode.BLUETOOTH)
menuItemMode.Text = "Mode: Bluetooth";
else
menuItemMode.Text = "Mode: HTTP";
}
}

0 comments on commit 836590d

Please sign in to comment.