From 9d7f9f7ba3e33df0c6b57cfc0e14057c69534ac4 Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Thu, 20 Jul 2023 20:51:32 +0100 Subject: [PATCH] Fix dialog modal blocking, reorganize UI code --- Bloxstrap/App.xaml.cs | 4 +- .../Elements/ContextMenu/MenuContainer.xaml | 9 ++- .../ContextMenu/MenuContainer.xaml.cs | 68 ++++++++++++++++- Bloxstrap/UI/NotifyIconWrapper.cs | 74 ++----------------- 4 files changed, 80 insertions(+), 75 deletions(-) diff --git a/Bloxstrap/App.xaml.cs b/Bloxstrap/App.xaml.cs index e35be17..41cdb98 100644 --- a/Bloxstrap/App.xaml.cs +++ b/Bloxstrap/App.xaml.cs @@ -314,9 +314,9 @@ namespace Bloxstrap 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(); diff --git a/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml b/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml index cc41190..59c0fde 100644 --- a/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml +++ b/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml @@ -15,14 +15,15 @@ ShowInTaskbar="False" WindowStyle="None" ResizeMode="NoResize" - Loaded="Window_Loaded"> + Loaded="Window_Loaded" + Closed="Window_Closed"> - - - + + + diff --git a/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs b/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs index 448ed45..c687fc3 100644 --- a/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs +++ b/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs @@ -13,7 +13,7 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; -using Bloxstrap.UI.ViewModels; +using Bloxstrap.Integrations; namespace Bloxstrap.UI.Elements.ContextMenu { @@ -24,13 +24,47 @@ namespace Bloxstrap.UI.Elements.ContextMenu { // 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 @@ -42,5 +76,35 @@ namespace Bloxstrap.UI.Elements.ContextMenu 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(); + }); + } } } diff --git a/Bloxstrap/UI/NotifyIconWrapper.cs b/Bloxstrap/UI/NotifyIconWrapper.cs index c590bc9..958cd82 100644 --- a/Bloxstrap/UI/NotifyIconWrapper.cs +++ b/Bloxstrap/UI/NotifyIconWrapper.cs @@ -17,9 +17,6 @@ namespace Bloxstrap.UI private RobloxActivity? _activityWatcher; private DiscordRichPresence? _richPresenceHandler; - private ServerInformation? _serverInformationWindow; - private LogTracer? _logTracerWindow; - EventHandler? _alertClickHandler; public NotifyIconWrapper() @@ -43,11 +40,6 @@ namespace Bloxstrap.UI return; _richPresenceHandler = richPresenceHandler; - - if (_menuContainer is null) - return; - - _menuContainer.Dispatcher.Invoke(() => _menuContainer.RichPresenceMenuItem.Visibility = Visibility.Visible); } public void SetActivityWatcher(RobloxActivity activityWatcher) @@ -56,11 +48,9 @@ namespace Bloxstrap.UI 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 @@ -70,12 +60,8 @@ namespace Bloxstrap.UI 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) @@ -91,52 +77,8 @@ namespace Bloxstrap.UI #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 @@ -183,9 +125,7 @@ namespace Bloxstrap.UI 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;