Consolidate NotifyIcon code

This commit is contained in:
pizzaboxer 2023-07-17 22:11:57 +01:00
parent 1e7e4e37c9
commit 907a3c3517
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
5 changed files with 99 additions and 50 deletions

View File

@ -31,14 +31,15 @@ namespace Bloxstrap
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)
{
@ -54,7 +55,7 @@ namespace Bloxstrap
Settings.Save();
State.Save();
Notification.Dispose();
NotifyIcon?.Dispose();
Environment.Exit(exitCodeNum);
}
@ -134,18 +135,6 @@ namespace Bloxstrap
}
}
// 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}"))
{
@ -201,6 +190,9 @@ namespace Bloxstrap
FastFlags.Load();
}
if (!IsMenuLaunch)
NotifyIcon = new();
#if !DEBUG
if (!IsUninstall && !IsFirstRun)
Updater.CheckInstalledVersion();

View File

@ -252,18 +252,18 @@ namespace Bloxstrap
{
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

View File

@ -39,17 +39,7 @@ namespace Bloxstrap.Integrations
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));
}
}
}

View File

@ -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);
}
}
}

View File

@ -14,7 +14,7 @@ namespace Bloxstrap
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;
@ -69,18 +69,13 @@ namespace Bloxstrap
if (isAutoUpgrade)
{
EventHandler ReleaseNotesLauncher = new((_, _) => Utilities.ShellExecute($"https://github.com/{App.ProjectRepository}/releases/tag/v{currentVersionInfo.ProductVersion}"));
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}")
);
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);
Task.Run(async () =>
{
await Task.Delay(30000);
App.Notification.BalloonTipClicked -= ReleaseNotesLauncher;
});
}
else if (!App.IsQuiet)
{