Skip to content

Commit

Permalink
Fix dialog modal blocking, reorganize UI code
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Jul 20, 2023
1 parent 53a6488 commit 9d7f9f7
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 75 deletions.
4 changes: 2 additions & 2 deletions Bloxstrap/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ protected override void OnStartup(StartupEventArgs e)
FinalizeExceptionHandling(exception);
});

NotifyIcon?.InitializeContextMenu();

// this ordering is very important as all wpf windows are shown as modal dialogs, mess it up and you'll end up blocking input to one of them
dialog?.ShowBootstrapper();
NotifyIcon?.InitializeContextMenu();

bootstrapperTask.Wait();

Expand Down
9 changes: 5 additions & 4 deletions Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
ShowInTaskbar="False"
WindowStyle="None"
ResizeMode="NoResize"
Loaded="Window_Loaded">
Loaded="Window_Loaded"
Closed="Window_Closed">
<ui:UiWindow.ContextMenu>
<ContextMenu>
<MenuItem x:Name="VersionMenuItem" IsEnabled="False" />
<Separator />
<MenuItem x:Name="RichPresenceMenuItem" Header="Discord Rich Presence" IsCheckable="True" IsChecked="True" Visibility="Collapsed" />
<MenuItem x:Name="ServerDetailsMenuItem" Header="See server details" Visibility="Collapsed" />
<MenuItem x:Name="LogTracerMenuItem" Header="Open log tracer" Visibility="Collapsed" />
<MenuItem x:Name="RichPresenceMenuItem" Header="Discord Rich Presence" IsCheckable="True" IsChecked="True" Visibility="Collapsed" Click="RichPresenceMenuItem_Click" />
<MenuItem x:Name="ServerDetailsMenuItem" Header="See server details" Visibility="Collapsed" Click="ServerDetailsMenuItem_Click" />
<MenuItem x:Name="LogTracerMenuItem" Header="Open log tracer" Visibility="Collapsed" Click="LogTracerMenuItem_Click" />
</ContextMenu>
</ui:UiWindow.ContextMenu>
</ui:UiWindow>
68 changes: 66 additions & 2 deletions Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Bloxstrap.UI.ViewModels;
using Bloxstrap.Integrations;

namespace Bloxstrap.UI.Elements.ContextMenu
{
Expand All @@ -24,13 +24,47 @@ public partial class MenuContainer
{
// i wouldve gladly done this as mvvm but turns out that data binding just does not work with menuitems for some reason so idk this sucks

public MenuContainer()
private readonly RobloxActivity? _activityWatcher;
private readonly DiscordRichPresence? _richPresenceHandler;

private LogTracer? _logTracerWindow;
private ServerInformation? _serverInformationWindow;

public MenuContainer(RobloxActivity? activityWatcher, DiscordRichPresence? richPresenceHandler)
{
InitializeComponent();

_activityWatcher = activityWatcher;
_richPresenceHandler = richPresenceHandler;

if (_activityWatcher is not null)
{
LogTracerMenuItem.Visibility = Visibility.Visible;

_activityWatcher.OnGameJoin += ActivityWatcher_OnGameJoin;
_activityWatcher.OnGameLeave += ActivityWatcher_OnGameLeave;
}

if (_richPresenceHandler is not null)
RichPresenceMenuItem.Visibility = Visibility.Visible;

VersionMenuItem.Header = $"{App.ProjectName} v{App.Version}";
}

public void ShowServerInformationWindow()
{
if (_serverInformationWindow is null)
{
_serverInformationWindow = new ServerInformation(_activityWatcher!);
_serverInformationWindow.Closed += (_, _) => _serverInformationWindow = null;
}

if (!_serverInformationWindow.IsVisible)
_serverInformationWindow.Show();

_serverInformationWindow.Activate();
}

private void Window_Loaded(object? sender, RoutedEventArgs e)
{
// this is an awful hack lmao im so sorry to anyone who reads this
Expand All @@ -42,5 +76,35 @@ private void Window_Loaded(object? sender, RoutedEventArgs e)
exStyle |= NativeMethods.WS_EX_TOOLWINDOW;
NativeMethods.SetWindowLongPtr(wndHelper.Handle, NativeMethods.GWL_EXSTYLE, (IntPtr)exStyle);
}

private void Window_Closed(object sender, EventArgs e) => App.Logger.WriteLine("[MenuContainer::Window_Closed] Context menu container closed");

private void RichPresenceMenuItem_Click(object sender, RoutedEventArgs e) => _richPresenceHandler?.SetVisibility(((MenuItem)sender).IsChecked);

private void ServerDetailsMenuItem_Click(object sender, RoutedEventArgs e) => ShowServerInformationWindow();

private void LogTracerMenuItem_Click(object sender, RoutedEventArgs e)
{
if (_logTracerWindow is null)
{
_logTracerWindow = new LogTracer(_activityWatcher!);
_logTracerWindow.Closed += (_, _) => _logTracerWindow = null;;
}

if (!_logTracerWindow.IsVisible)
_logTracerWindow.Show();

_logTracerWindow.Activate();
}

private void ActivityWatcher_OnGameJoin(object? sender, EventArgs e) => Dispatcher.Invoke(() => ServerDetailsMenuItem.Visibility = Visibility.Visible);

private void ActivityWatcher_OnGameLeave(object? sender, EventArgs e)
{
Dispatcher.Invoke(() => {
ServerDetailsMenuItem.Visibility = Visibility.Collapsed;
_serverInformationWindow?.Close();
});
}
}
}
74 changes: 7 additions & 67 deletions Bloxstrap/UI/NotifyIconWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public class NotifyIconWrapper : IDisposable
private RobloxActivity? _activityWatcher;
private DiscordRichPresence? _richPresenceHandler;

private ServerInformation? _serverInformationWindow;
private LogTracer? _logTracerWindow;

EventHandler? _alertClickHandler;

public NotifyIconWrapper()
Expand All @@ -43,11 +40,6 @@ public void SetRichPresenceHandler(DiscordRichPresence richPresenceHandler)
return;

_richPresenceHandler = richPresenceHandler;

if (_menuContainer is null)
return;

_menuContainer.Dispatcher.Invoke(() => _menuContainer.RichPresenceMenuItem.Visibility = Visibility.Visible);
}

public void SetActivityWatcher(RobloxActivity activityWatcher)
Expand All @@ -56,11 +48,9 @@ public void SetActivityWatcher(RobloxActivity activityWatcher)
return;

_activityWatcher = activityWatcher;
_activityWatcher.OnGameJoin += (_, _) => Task.Run(OnGameJoin);
_activityWatcher.OnGameLeave += OnGameLeave;

if (App.Settings.Prop.OhHeyYouFoundMe && _menuContainer is not null)
_menuContainer.Dispatcher.Invoke(() => _menuContainer.LogTracerMenuItem.Visibility = Visibility.Visible);
if (App.Settings.Prop.ShowServerDetails)
_activityWatcher.OnGameJoin += (_, _) => Task.Run(OnGameJoin);
}
#endregion

Expand All @@ -70,12 +60,8 @@ public void InitializeContextMenu()
if (_menuContainer is not null)
return;

_menuContainer = new();
_menuContainer.Dispatcher.BeginInvoke(_menuContainer.ShowDialog);
_menuContainer.RichPresenceMenuItem.Click += (_, _) => _richPresenceHandler?.SetVisibility(_menuContainer.RichPresenceMenuItem.IsChecked);
_menuContainer.ServerDetailsMenuItem.Click += (_, _) => ShowServerInformationWindow();
_menuContainer.LogTracerMenuItem.Click += (_, _) => ShowLogTracerWindow();
_menuContainer.Closing += (_, _) => App.Logger.WriteLine("[NotifyIconWrapper::NotifyIconWrapper] Context menu container closed");
_menuContainer = new(_activityWatcher, _richPresenceHandler);
_menuContainer.ShowDialog();
}

public void MouseClickEventHandler(object? sender, System.Windows.Forms.MouseEventArgs e)
Expand All @@ -91,52 +77,8 @@ public void MouseClickEventHandler(object? sender, System.Windows.Forms.MouseEve
#region Activity handlers
public async void OnGameJoin()
{
if (_menuContainer is not null)
_menuContainer.Dispatcher.Invoke(() => _menuContainer.ServerDetailsMenuItem.Visibility = Visibility.Visible);

if (App.Settings.Prop.ShowServerDetails)
{
string serverLocation = await _activityWatcher!.GetServerLocation();
ShowAlert("Connnected to server", $"Location: {serverLocation}\nClick for more information", 10, (_, _) => ShowServerInformationWindow());
}
}

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

if (_serverInformationWindow is not null && _serverInformationWindow.IsVisible)
_serverInformationWindow.Dispatcher.Invoke(_serverInformationWindow.Close);
}
#endregion

#region Window handlers
public void ShowServerInformationWindow()
{
if (_serverInformationWindow is null)
{
_serverInformationWindow = new ServerInformation(_activityWatcher!);
_serverInformationWindow.Closed += (_, _) => _serverInformationWindow = null;
}

if (!_serverInformationWindow.IsVisible)
_serverInformationWindow.Show();

_serverInformationWindow.Activate();
}

public void ShowLogTracerWindow()
{
if (_logTracerWindow is null)
{
_logTracerWindow = new LogTracer(_activityWatcher!);
_logTracerWindow.Closed += (_, _) => _logTracerWindow = null;
}

if (!_logTracerWindow.IsVisible)
_logTracerWindow.Show();

_logTracerWindow.Activate();
string serverLocation = await _activityWatcher!.GetServerLocation();
ShowAlert("Connnected to server", $"Location: {serverLocation}\nClick for more information", 10, (_, _) => _menuContainer?.ShowServerInformationWindow());
}
#endregion

Expand Down Expand Up @@ -183,9 +125,7 @@ public void Dispose()

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

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

_menuContainer?.Dispatcher.Invoke(_menuContainer.Close);
_notifyIcon.Dispose();

_disposed = true;
Expand Down

0 comments on commit 9d7f9f7

Please sign in to comment.