Skip to content

Commit

Permalink
Unify updating of notify icon
Browse files Browse the repository at this point in the history
  • Loading branch information
nikvoronin committed Mar 2, 2023
1 parent ade3e6b commit 7027858
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions Xm4Battery/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,33 @@ private static Icon CreateLevelIcon( int level = -1 )
private static void Xm4state_BatteryLevelChanged( object? sender, int level )
{
var xm4 = sender as Xm4Entity;

var lastConnectedTime = string.Empty;
var connected = xm4?.IsConnected ?? false;
if ( connected )
_notifyIcon.Icon = CreateLevelIcon( level );
else
lastConnectedTime = $"\n{xm4!.LastConnectedTime.Value:F}";

_notifyIcon.Text = $"{AppName} {level}%{lastConnectedTime}";
UpdateNotifyIcon( xm4!, connected, level );
}

private static void Xm4state_ConnectionChanged( object? sender, bool connected )
{
var xm4 = sender as Xm4Entity;
var level = xm4?.BatteryLevel ?? 0;

UpdateNotifyIcon( xm4!, connected, level );
}

private static void UpdateNotifyIcon(
Xm4Entity xm4,
bool connected,
int level )
{
_notifyIcon.Icon =
connected ? CreateLevelIcon( level )
: _disconnectIcon;

var lastConnectedTime =
var at =
connected ? string.Empty
: $"\n{xm4!.LastConnectedTime.Value:F}";

_notifyIcon.Text = $"{AppName} {level}%{lastConnectedTime}";
_notifyIcon.Text = $"{AppName} {level}%{at}";
}

const string AppName = "XM4 Battery Level";
Expand Down

0 comments on commit 7027858

Please sign in to comment.