Skip to content

Commit

Permalink
Fix threading stuff (?)
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Jul 19, 2023
1 parent 8a76967 commit 4233d5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Bloxstrap/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ protected override void OnStartup(StartupEventArgs e)
FinalizeExceptionHandling(exception);
});

NotifyIcon?.InitializeContextMenu();

dialog?.ShowBootstrapper();

bootstrapperTask.Wait();
Expand Down
23 changes: 16 additions & 7 deletions Bloxstrap/UI/NotifyIconWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class NotifyIconWrapper : IDisposable
bool _disposed = false;

private readonly System.Windows.Forms.NotifyIcon _notifyIcon;
private readonly MenuContainer _menuContainer = new();
private MenuContainer? _menuContainer;
private RobloxActivity? _activityWatcher;

private ServerInformation? _serverInformationWindow;
Expand All @@ -33,11 +33,16 @@ public NotifyIconWrapper()
};

_notifyIcon.MouseClick += MouseClickEventHandler;
}

_menuContainer.Dispatcher.BeginInvoke(_menuContainer.ShowDialog);
public void InitializeContextMenu()
{
if (_menuContainer is not null)
return;

_menuContainer = new();
_menuContainer.Dispatcher.BeginInvoke(_menuContainer.ShowDialog);
_menuContainer.ServerDetailsMenuItem.Click += (_, _) => ShowServerInformationWindow();

_menuContainer.Closing += (_, _) => App.Logger.WriteLine("[NotifyIconWrapper::NotifyIconWrapper] Context menu container closed");
}

Expand All @@ -53,7 +58,8 @@ public void SetActivityWatcher(RobloxActivity activityWatcher)

public async void OnGameJoin()
{
_menuContainer.Dispatcher.Invoke(() => _menuContainer.ServerDetailsMenuItem.Visibility = Visibility.Visible);
if (_menuContainer is not null)
_menuContainer.Dispatcher.Invoke(() => _menuContainer.ServerDetailsMenuItem.Visibility = Visibility.Visible);

if (App.Settings.Prop.ShowServerDetails)
{
Expand All @@ -64,7 +70,8 @@ public async void OnGameJoin()

public void OnGameLeave(object? sender, EventArgs e)
{
_menuContainer.Dispatcher.Invoke(() => _menuContainer.ServerDetailsMenuItem.Visibility = Visibility.Collapsed);
if (_menuContainer is not null)
_menuContainer.Dispatcher.Invoke(() => _menuContainer.ServerDetailsMenuItem.Visibility = Visibility.Collapsed);

if (_serverInformationWindow is not null && _serverInformationWindow.IsVisible)
_serverInformationWindow.Dispatcher.Invoke(_serverInformationWindow.Close);
Expand All @@ -73,7 +80,7 @@ public void OnGameLeave(object? sender, EventArgs e)

public void MouseClickEventHandler(object? sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button != System.Windows.Forms.MouseButtons.Right)
if (e.Button != System.Windows.Forms.MouseButtons.Right || _menuContainer is null)
return;

_menuContainer.Activate();
Expand Down Expand Up @@ -137,7 +144,9 @@ public void Dispose()

App.Logger.WriteLine($"[NotifyIconWrapper::Dispose] Disposing NotifyIcon");

_menuContainer.Dispatcher.Invoke(_menuContainer.Close);
if (_menuContainer is not null)
_menuContainer.Dispatcher.Invoke(_menuContainer.Close);

_notifyIcon.Dispose();

_disposed = true;
Expand Down

0 comments on commit 4233d5e

Please sign in to comment.