From 05185e9e9a9043e67920dec5f56547a4dc834cbb Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Thu, 20 Jul 2023 15:11:22 +0100 Subject: [PATCH] Add context menu option for toggling rich presence --- Bloxstrap/Bootstrapper.cs | 3 +- Bloxstrap/Integrations/DiscordRichPresence.cs | 16 +++++- .../Elements/ContextMenu/MenuContainer.xaml | 3 +- .../ContextMenu/MenuContainer.xaml.cs | 10 ---- Bloxstrap/UI/NotifyIconWrapper.cs | 51 +++++++++++++------ 5 files changed, 52 insertions(+), 31 deletions(-) diff --git a/Bloxstrap/Bootstrapper.cs b/Bloxstrap/Bootstrapper.cs index 52a1da0..8fc9475 100644 --- a/Bloxstrap/Bootstrapper.cs +++ b/Bloxstrap/Bootstrapper.cs @@ -259,8 +259,7 @@ namespace Bloxstrap App.Logger.WriteLine("[Bootstrapper::StartRoblox] Using Discord Rich Presence"); richPresence = new(activityWatcher); - if (App.NotifyIcon is not null) - App.NotifyIcon.RichPresenceIntegration = richPresence; + App.NotifyIcon?.SetRichPresenceHandler(richPresence); } } diff --git a/Bloxstrap/Integrations/DiscordRichPresence.cs b/Bloxstrap/Integrations/DiscordRichPresence.cs index 35628d0..cd6fd84 100644 --- a/Bloxstrap/Integrations/DiscordRichPresence.cs +++ b/Bloxstrap/Integrations/DiscordRichPresence.cs @@ -8,6 +8,7 @@ namespace Bloxstrap.Integrations private readonly RobloxActivity _activityWatcher; private RichPresence? _currentPresence; + private bool _visible = true; private string? _initialStatus; private long _currentUniverseId; private DateTime? _timeStartedUniverse; @@ -89,6 +90,18 @@ namespace Bloxstrap.Integrations UpdatePresence(); } + public void SetVisibility(bool visible) + { + App.Logger.WriteLine($"[DiscordRichPresence::SetVisibility] Setting presence visibility ({visible})"); + + _visible = visible; + + if (_visible) + UpdatePresence(); + else + _rpcClient.ClearPresence(); + } + public async Task SetCurrentGame() { if (!_activityWatcher.ActivityInGame) @@ -195,7 +208,8 @@ namespace Bloxstrap.Integrations return; } - _rpcClient.SetPresence(_currentPresence); + if (_visible) + _rpcClient.SetPresence(_currentPresence); } public void Dispose() diff --git a/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml b/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml index 9454823..824de2c 100644 --- a/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml +++ b/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml @@ -20,9 +20,8 @@ - + - diff --git a/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs b/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs index adad0a4..448ed45 100644 --- a/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs +++ b/Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs @@ -42,15 +42,5 @@ namespace Bloxstrap.UI.Elements.ContextMenu exStyle |= NativeMethods.WS_EX_TOOLWINDOW; NativeMethods.SetWindowLongPtr(wndHelper.Handle, NativeMethods.GWL_EXSTYLE, (IntPtr)exStyle); } - - private void RichPresenceMenuItem_Click(object sender, RoutedEventArgs e) - { - Controls.ShowMessageBox($"hi how u doing i am {RichPresenceMenuItem.IsChecked}", MessageBoxImage.Warning); - } - - private void TestMenuItem_Click(object sender, RoutedEventArgs e) - { - Controls.ShowMessageBox($"hi how u doing i am {TestMenuItem.IsChecked}", MessageBoxImage.Warning); - } } } diff --git a/Bloxstrap/UI/NotifyIconWrapper.cs b/Bloxstrap/UI/NotifyIconWrapper.cs index c2f6549..3bc54ac 100644 --- a/Bloxstrap/UI/NotifyIconWrapper.cs +++ b/Bloxstrap/UI/NotifyIconWrapper.cs @@ -13,11 +13,12 @@ namespace Bloxstrap.UI private readonly System.Windows.Forms.NotifyIcon _notifyIcon; private MenuContainer? _menuContainer; + private RobloxActivity? _activityWatcher; + private DiscordRichPresence? _richPresenceHandler; private ServerInformation? _serverInformationWindow; - public DiscordRichPresence? RichPresenceIntegration; EventHandler? _alertClickHandler; @@ -35,15 +36,18 @@ namespace Bloxstrap.UI _notifyIcon.MouseClick += MouseClickEventHandler; } - public void InitializeContextMenu() + #region Handler registers + public void SetRichPresenceHandler(DiscordRichPresence richPresenceHandler) { - if (_menuContainer is not null) + if (_richPresenceHandler 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"); + _richPresenceHandler = richPresenceHandler; + + if (_menuContainer is null) + return; + + _menuContainer.Dispatcher.Invoke(() => _menuContainer.RichPresenceMenuItem.Visibility = Visibility.Visible); } public void SetActivityWatcher(RobloxActivity activityWatcher) @@ -55,6 +59,30 @@ namespace Bloxstrap.UI _activityWatcher.OnGameJoin += (_, _) => Task.Run(OnGameJoin); _activityWatcher.OnGameLeave += OnGameLeave; } + #endregion + + #region Context menu + public void InitializeContextMenu() + { + if (_menuContainer is not null) + return; + + _menuContainer = new(); + _menuContainer.Dispatcher.BeginInvoke(_menuContainer.ShowDialog); + _menuContainer.ServerDetailsMenuItem.Click += (_, _) => ShowServerInformationWindow(); + _menuContainer.RichPresenceMenuItem.Click += (_, _) => _richPresenceHandler?.SetVisibility(_menuContainer.RichPresenceMenuItem.IsChecked); + _menuContainer.Closing += (_, _) => App.Logger.WriteLine("[NotifyIconWrapper::NotifyIconWrapper] Context menu container closed"); + } + + public void MouseClickEventHandler(object? sender, System.Windows.Forms.MouseEventArgs e) + { + if (e.Button != System.Windows.Forms.MouseButtons.Right || _menuContainer is null) + return; + + _menuContainer.Activate(); + _menuContainer.ContextMenu.IsOpen = true; + } + #endregion public async void OnGameJoin() { @@ -78,15 +106,6 @@ namespace Bloxstrap.UI } - public void MouseClickEventHandler(object? sender, System.Windows.Forms.MouseEventArgs e) - { - if (e.Button != System.Windows.Forms.MouseButtons.Right || _menuContainer is null) - return; - - _menuContainer.Activate(); - _menuContainer.ContextMenu.IsOpen = true; - } - public void ShowServerInformationWindow() { if (_serverInformationWindow is null)