Skip to content

Commit

Permalink
Added light mode support, added installer, added exit button, added p…
Browse files Browse the repository at this point in the history
…ublish profiles
  • Loading branch information
tommaier123 committed Jul 9, 2023
1 parent 5f4e880 commit 502417a
Show file tree
Hide file tree
Showing 25 changed files with 1,096 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ publish/
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
#*.pubxml
*.publishproj

# Microsoft Azure Web App publish settings. Comment the next line if you want to
Expand Down
Binary file added Icons/icon.ico
Binary file not shown.
Binary file added Icons/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions XBatteryStatus.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31025.194
# Visual Studio Version 17
VisualStudioVersion = 17.2.32630.192
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XBatteryStatus", "XBatteryStatus\XBatteryStatus.csproj", "{86B506A1-A575-4162-B557-61E6A73BDD36}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XBatteryStatus", "XBatteryStatus\XBatteryStatus.csproj", "{86B506A1-A575-4162-B557-61E6A73BDD36}"
EndProject
Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "XBatteryStatusInstaller", "XBatteryStatusInstaller\XBatteryStatusInstaller.vdproj", "{B344322B-0436-4014-89CA-F91DC9425D5F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,8 @@ Global
{86B506A1-A575-4162-B557-61E6A73BDD36}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86B506A1-A575-4162-B557-61E6A73BDD36}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86B506A1-A575-4162-B557-61E6A73BDD36}.Release|Any CPU.Build.0 = Release|Any CPU
{B344322B-0436-4014-89CA-F91DC9425D5F}.Debug|Any CPU.ActiveCfg = Debug
{B344322B-0436-4014-89CA-F91DC9425D5F}.Release|Any CPU.ActiveCfg = Release
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
99 changes: 80 additions & 19 deletions XBatteryStatus/MyApplicationContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Microsoft.Toolkit.Uwp.Notifications;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
Expand All @@ -15,6 +14,7 @@ namespace XBatteryStatus
public class MyApplicationContext : ApplicationContext
{
NotifyIcon notifyIcon = new NotifyIcon();
private ContextMenuStrip contextMenu;

private Timer timer1;

Expand All @@ -23,12 +23,21 @@ public class MyApplicationContext : ApplicationContext

private int lastBattery = 100;

private bool lightMode = false;

public MyApplicationContext()
{
notifyIcon.Icon = Properties.Resources.iconQ;
lightMode = IsLightMode();
notifyIcon.Icon = GetIcon(Properties.Resources.iconQ, lightMode);
notifyIcon.Text = "XBatteryStatus: Looking for paired controller";
notifyIcon.Visible = true;

contextMenu = new ContextMenuStrip();
ToolStripMenuItem exitButton = new ToolStripMenuItem("Exit", null, new EventHandler(ExitClicked), "Exit");
contextMenu.Items.Add(exitButton);
notifyIcon.ContextMenuStrip = contextMenu;


FindBleController();

timer1 = new Timer();
Expand Down Expand Up @@ -67,7 +76,7 @@ async private void FindBleController()

if (count == 0)
{
notifyIcon.Icon = Properties.Resources.iconE;
notifyIcon.Icon = GetIcon(Properties.Resources.iconE, lightMode);
notifyIcon.Text = "XBatteryStatus: No paired controller with battery service found";
}
else
Expand All @@ -90,17 +99,19 @@ private async void ReadBattery()
int val = reader.ReadByte();
string notify = val.ToString() + "% - " + pairedGamepad.Name;
notifyIcon.Text = "XBatteryStatus: " + notify;
if (val < 5) notifyIcon.Icon = Properties.Resources.icon00;
else if (val < 15) notifyIcon.Icon = Properties.Resources.icon10;
else if (val < 25) notifyIcon.Icon = Properties.Resources.icon20;
else if (val < 35) notifyIcon.Icon = Properties.Resources.icon30;
else if (val < 45) notifyIcon.Icon = Properties.Resources.icon40;
else if (val < 55) notifyIcon.Icon = Properties.Resources.icon50;
else if (val < 65) notifyIcon.Icon = Properties.Resources.icon60;
else if (val < 75) notifyIcon.Icon = Properties.Resources.icon70;
else if (val < 85) notifyIcon.Icon = Properties.Resources.icon80;
else if (val < 95) notifyIcon.Icon = Properties.Resources.icon90;
else notifyIcon.Icon = Properties.Resources.icon100;
Icon icon = Properties.Resources.icon100;
if (val < 5) icon = Properties.Resources.icon00;
else if (val < 15) icon = Properties.Resources.icon10;
else if (val < 25) icon = Properties.Resources.icon20;
else if (val < 35) icon = Properties.Resources.icon30;
else if (val < 45) icon = Properties.Resources.icon40;
else if (val < 55) icon = Properties.Resources.icon50;
else if (val < 65) icon = Properties.Resources.icon60;
else if (val < 75) icon = Properties.Resources.icon70;
else if (val < 85) icon = Properties.Resources.icon80;
else if (val < 95) icon = Properties.Resources.icon90;

notifyIcon.Icon = GetIcon(icon, lightMode);

if ((lastBattery > 15 && val <= 15) || (lastBattery > 10 && val <= 10) || (lastBattery > 5 && val <= 5))
{
Expand Down Expand Up @@ -143,15 +154,65 @@ private void timer1_Tick(object sender, EventArgs e)
ReadBattery();
}

public void Update()
{
notifyIcon.Visible = pairedGamepad != null && pairedGamepad.ConnectionStatus == BluetoothConnectionStatus.Connected;
ReadBattery();
}

private void ExitClicked(object sender, EventArgs e)
{
Application.Exit();
}

private void OnApplicationExit(object sender, EventArgs e)
{
notifyIcon.Visible = false;
}

public void Update()
public bool IsLightMode()
{
notifyIcon.Visible = pairedGamepad != null && pairedGamepad.ConnectionStatus == BluetoothConnectionStatus.Connected;
ReadBattery();
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize");

if (key != null)
{
object registryValueObject = key.GetValue("AppsUseLightTheme");

if (registryValueObject != null)
{
int registryValue = (int)registryValueObject;
return registryValue == 1;
}
}

return true;
}

public Icon GetIcon(Icon darkModeIcon, bool lightMode)
{
if (!lightMode)
{
return darkModeIcon;
}
else
{
using (Bitmap bitmap = darkModeIcon.ToBitmap())
{

for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
Color pixelColor = bitmap.GetPixel(x, y);
Color invertedColor = Color.FromArgb(pixelColor.A, 255 - pixelColor.R, 255 - pixelColor.G, 255 - pixelColor.B);
bitmap.SetPixel(x, y, invertedColor);
}
}

IntPtr Hicon = bitmap.GetHicon();
return Icon.FromHandle(Hicon);
}
}
}
}
}
23 changes: 22 additions & 1 deletion XBatteryStatus/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;

Expand All @@ -12,8 +15,26 @@ static class Program
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
static void Main(string[] args)
{
var proc = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(proc.ProcessName);

if (processes.Length > 1)
{
foreach (var process in processes)
{
if (process.Id != proc.Id)
{
try
{
process.Kill();
}
catch { }
}
}
}

Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Expand Down
17 changes: 17 additions & 0 deletions XBatteryStatus/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>C:\Users\Max\source\repos\tommaier123\XBatteryStatus\XBatteryStatus\bin\Release\publish</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
</Project>
Binary file removed XBatteryStatus/Resources/icon00.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/icon10.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/icon100.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/icon20.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/icon30.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/icon40.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/icon50.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/icon60.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/icon70.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/icon80.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/icon90.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/iconE.png
Binary file not shown.
Binary file removed XBatteryStatus/Resources/iconQ.png
Binary file not shown.
9 changes: 7 additions & 2 deletions XBatteryStatus/XBatteryStatus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
<ApplicationIcon>iconE.ico</ApplicationIcon>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<ApplicationIcon>icon.ico</ApplicationIcon>
<DebugType>embedded</DebugType>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,6 +19,10 @@
<None Remove="Publish\**" />
</ItemGroup>

<ItemGroup>
<Content Include="icon.ico" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
Expand Down
Binary file added XBatteryStatus/icon.ico
Binary file not shown.
Binary file removed XBatteryStatus/iconE.ico
Binary file not shown.
Loading

0 comments on commit 502417a

Please sign in to comment.