Skip to content

Commit

Permalink
Add context menu option for toggling rich presence
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Jul 20, 2023
1 parent b677ce5 commit 05185e9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
3 changes: 1 addition & 2 deletions Bloxstrap/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ private async Task StartRoblox()
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);
}
}

Expand Down
16 changes: 15 additions & 1 deletion Bloxstrap/Integrations/DiscordRichPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class DiscordRichPresence : IDisposable
private readonly RobloxActivity _activityWatcher;

private RichPresence? _currentPresence;
private bool _visible = true;
private string? _initialStatus;
private long _currentUniverseId;
private DateTime? _timeStartedUniverse;
Expand Down Expand Up @@ -89,6 +90,18 @@ public void SetStatus(string status)
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<bool> SetCurrentGame()
{
if (!_activityWatcher.ActivityInGame)
Expand Down Expand Up @@ -195,7 +208,8 @@ public void UpdatePresence()
return;
}

_rpcClient.SetPresence(_currentPresence);
if (_visible)
_rpcClient.SetPresence(_currentPresence);
}

public void Dispose()
Expand Down
3 changes: 1 addition & 2 deletions Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
<ContextMenu>
<MenuItem x:Name="VersionMenuItem" IsEnabled="False" />
<Separator />
<MenuItem x:Name="RichPresenceMenuItem" Header="Discord Rich Presence" IsCheckable="True" Visibility="Collapsed" Click="RichPresenceMenuItem_Click" />
<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="TestMenuItem" Header="Test" IsCheckable="True" Click="TestMenuItem_Click" />
</ContextMenu>
</ui:UiWindow.ContextMenu>
</ui:UiWindow>
10 changes: 0 additions & 10 deletions Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,5 @@ private void Window_Loaded(object? sender, RoutedEventArgs e)
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);
}
}
}
49 changes: 34 additions & 15 deletions Bloxstrap/UI/NotifyIconWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public class NotifyIconWrapper : IDisposable

private readonly System.Windows.Forms.NotifyIcon _notifyIcon;
private MenuContainer? _menuContainer;

private RobloxActivity? _activityWatcher;
private DiscordRichPresence? _richPresenceHandler;

private ServerInformation? _serverInformationWindow;

public DiscordRichPresence? RichPresenceIntegration;

EventHandler? _alertClickHandler;

Expand All @@ -35,6 +36,32 @@ public NotifyIconWrapper()
_notifyIcon.MouseClick += MouseClickEventHandler;
}

#region Handler registers
public void SetRichPresenceHandler(DiscordRichPresence richPresenceHandler)
{
if (_richPresenceHandler is not null)
return;

_richPresenceHandler = richPresenceHandler;

if (_menuContainer is null)
return;

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

public void SetActivityWatcher(RobloxActivity activityWatcher)
{
if (_activityWatcher is not null)
return;

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

#region Context menu
public void InitializeContextMenu()
{
if (_menuContainer is not null)
Expand All @@ -43,18 +70,19 @@ public void InitializeContextMenu()
_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 SetActivityWatcher(RobloxActivity activityWatcher)
public void MouseClickEventHandler(object? sender, System.Windows.Forms.MouseEventArgs e)
{
if (_activityWatcher is not null)
if (e.Button != System.Windows.Forms.MouseButtons.Right || _menuContainer is null)
return;

_activityWatcher = activityWatcher;
_activityWatcher.OnGameJoin += (_, _) => Task.Run(OnGameJoin);
_activityWatcher.OnGameLeave += OnGameLeave;
_menuContainer.Activate();
_menuContainer.ContextMenu.IsOpen = true;
}
#endregion

public async void OnGameJoin()
{
Expand All @@ -78,15 +106,6 @@ 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 || _menuContainer is null)
return;

_menuContainer.Activate();
_menuContainer.ContextMenu.IsOpen = true;
}

public void ShowServerInformationWindow()
{
if (_serverInformationWindow is null)
Expand Down

0 comments on commit 05185e9

Please sign in to comment.