Skip to content

Commit

Permalink
Consolidate NotifyIcon code
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Jul 17, 2023
1 parent 1e7e4e3 commit 907a3c3
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 50 deletions.
24 changes: 8 additions & 16 deletions Bloxstrap/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public partial class App : Application
public static BuildMetadataAttribute BuildMetadata = Assembly.GetExecutingAssembly().GetCustomAttribute<BuildMetadataAttribute>()!;
public static string Version = Assembly.GetExecutingAssembly().GetName().Version!.ToString()[..^2];

public static NotifyIconWrapper? NotifyIcon { get; private set; }

public static readonly Logger Logger = new();
public static readonly HttpClient HttpClient = new(new HttpClientLoggingHandler(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.All }));


public static readonly JsonManager<Settings> Settings = new();
public static readonly JsonManager<State> State = new();
public static readonly FastFlagManager FastFlags = new();

public static System.Windows.Forms.NotifyIcon Notification { get; private set; } = null!;
public static readonly HttpClient HttpClient = new(new HttpClientLoggingHandler(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.All }));

public static void Terminate(ErrorCode exitCode = ErrorCode.ERROR_SUCCESS)
{
Expand All @@ -54,7 +55,7 @@ public static void Terminate(ErrorCode exitCode = ErrorCode.ERROR_SUCCESS)

Settings.Save();
State.Save();
Notification.Dispose();
NotifyIcon?.Dispose();

Environment.Exit(exitCodeNum);
}
Expand Down Expand Up @@ -134,18 +135,6 @@ protected override void OnStartup(StartupEventArgs e)
}
}

// so this needs to be here because winforms moment
// onclick events will not fire unless this is defined here in the main thread so uhhhhh
// we'll show the icon if we're launching roblox since we're likely gonna be showing a
// bunch of notifications, and always showing it just makes the most sense i guess since it
// indicates that bloxstrap is running, even in the background
Notification = new()
{
Icon = Bloxstrap.Properties.Resources.IconBloxstrap,
Text = ProjectName,
Visible = !IsMenuLaunch
};

// check if installed
using (RegistryKey? registryKey = Registry.CurrentUser.OpenSubKey($@"Software\{ProjectName}"))
{
Expand Down Expand Up @@ -201,6 +190,9 @@ protected override void OnStartup(StartupEventArgs e)
FastFlags.Load();
}

if (!IsMenuLaunch)
NotifyIcon = new();

#if !DEBUG
if (!IsUninstall && !IsFirstRun)
Updater.CheckInstalledVersion();
Expand Down
20 changes: 10 additions & 10 deletions Bloxstrap/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,18 @@ private async Task StartRoblox()
{
activityWatcher = new();
shouldWait = true;
}

if (App.Settings.Prop.UseDiscordRichPresence)
{
App.Logger.WriteLine("[Bootstrapper::StartRoblox] Using Discord Rich Presence");
richPresence = new(activityWatcher!);
}
if (App.Settings.Prop.UseDiscordRichPresence)
{
App.Logger.WriteLine("[Bootstrapper::StartRoblox] Using Discord Rich Presence");
richPresence = new(activityWatcher);
}

if (App.Settings.Prop.ShowServerDetails)
{
App.Logger.WriteLine("[Bootstrapper::StartRoblox] Using server details notifier");
serverNotifier = new(activityWatcher!);
if (App.Settings.Prop.ShowServerDetails)
{
App.Logger.WriteLine("[Bootstrapper::StartRoblox] Using server details notifier");
serverNotifier = new(activityWatcher);
}
}

// launch custom integrations now
Expand Down
14 changes: 2 additions & 12 deletions Bloxstrap/Integrations/ServerNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,7 @@ public async void Notify()

message += "\nClick to copy Instance ID";

App.Logger.WriteLine($"[ServerNotifier::Notify] {message.ReplaceLineEndings("\\n")}");

EventHandler JobIDCopier = new((_, _) => Clipboard.SetText(_activityWatcher.ActivityJobId));

App.Notification.BalloonTipTitle = "Connected to server";
App.Notification.BalloonTipText = message;
App.Notification.BalloonTipClicked += JobIDCopier;
App.Notification.ShowBalloonTip(10);

await Task.Delay(10000);
App.Notification.BalloonTipClicked -= JobIDCopier;
}
App.NotifyIcon?.ShowAlert("Connnected to server", message, 10, (_, _) => Clipboard.SetText(_activityWatcher.ActivityJobId));
}
}
}
72 changes: 72 additions & 0 deletions Bloxstrap/UI/NotifyIconWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Windows.Forms;

namespace Bloxstrap.UI
{
public class NotifyIconWrapper : IDisposable
{
bool _disposed = false;

private readonly NotifyIcon _notifyIcon;
EventHandler? _alertClickHandler;

public NotifyIconWrapper()
{
App.Logger.WriteLine("[NotifyIconWrapper::NotifyIconWrapper] Initializing notification area icon");

_notifyIcon = new()
{
Icon = Properties.Resources.IconBloxstrap,
Text = App.ProjectName,
Visible = true
};
}

public void ShowAlert(string caption, string message, int duration, EventHandler? clickHandler)
{
string id = Guid.NewGuid().ToString()[..8];

App.Logger.WriteLine($"[NotifyIconWrapper::ShowAlert] [{id}] Showing alert for {duration} seconds (clickHandler={clickHandler is not null})");
App.Logger.WriteLine($"[NotifyIconWrapper::ShowAlert] [{id}] {caption}: {message.Replace("\n", "\\n")}");

_notifyIcon.BalloonTipTitle = caption;
_notifyIcon.BalloonTipText = message;

if (_alertClickHandler is not null)
{
App.Logger.WriteLine($"[NotifyIconWrapper::ShowAlert] [{id}] Previous alert still present, erasing click handler");
_notifyIcon.BalloonTipClicked -= _alertClickHandler;
}

_alertClickHandler = clickHandler;
_notifyIcon.BalloonTipClicked += clickHandler;

_notifyIcon.ShowBalloonTip(duration);

Task.Run(async () =>
{
await Task.Delay(duration * 1000);
_notifyIcon.BalloonTipClicked -= clickHandler;
App.Logger.WriteLine($"[NotifyIconWrapper::ShowAlert] [{id}] Duration over, erasing current click handler");
if (_alertClickHandler == clickHandler)
_alertClickHandler = null;
else
App.Logger.WriteLine($"[NotifyIconWrapper::ShowAlert] [{id}] Click handler has been overriden by another alert");
});
}

public void Dispose()
{
if (_disposed)
return;

_notifyIcon.Dispose();

_disposed = true;

GC.SuppressFinalize(this);
}
}
}
19 changes: 7 additions & 12 deletions Bloxstrap/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void CheckInstalledVersion()

FileVersionInfo currentVersionInfo = FileVersionInfo.GetVersionInfo(Environment.ProcessPath);

if (Utility.MD5Hash.FromFile(Environment.ProcessPath) == Utility.MD5Hash.FromFile(Directories.Application))
if (MD5Hash.FromFile(Environment.ProcessPath) == MD5Hash.FromFile(Directories.Application))
return;

MessageBoxResult result;
Expand Down Expand Up @@ -69,18 +69,13 @@ public static void CheckInstalledVersion()

if (isAutoUpgrade)
{
EventHandler ReleaseNotesLauncher = new((_, _) => Utilities.ShellExecute($"https://github.com/{App.ProjectRepository}/releases/tag/v{currentVersionInfo.ProductVersion}"));

App.Notification.BalloonTipTitle = $"Bloxstrap has been upgraded to v{currentVersionInfo.ProductVersion}";
App.Notification.BalloonTipText = "Click here to see what's new in this version";
App.Notification.BalloonTipClicked += ReleaseNotesLauncher;
App.Notification.ShowBalloonTip(30);
App.NotifyIcon?.ShowAlert(
$"Bloxstrap has been upgraded to v{currentVersionInfo.ProductVersion}",
"See what's new in this version",
30,
(_, _) => Utilities.ShellExecute($"https://github.com/{App.ProjectRepository}/releases/tag/v{currentVersionInfo.ProductVersion}")
);

Task.Run(async () =>
{
await Task.Delay(30000);
App.Notification.BalloonTipClicked -= ReleaseNotesLauncher;
});
}
else if (!App.IsQuiet)
{
Expand Down

0 comments on commit 907a3c3

Please sign in to comment.