Skip to content

Commit

Permalink
Move ServerNotifier stuff to NotifyIconWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Jul 19, 2023
1 parent 4705bec commit 5f9f63d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 83 deletions.
10 changes: 4 additions & 6 deletions Bloxstrap/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ private async Task StartRoblox()
List<Process> autocloseProcesses = new();
RobloxActivity? activityWatcher = null;
DiscordRichPresence? richPresence = null;
ServerNotifier? serverNotifier = null;

App.Logger.WriteLine($"[Bootstrapper::StartRoblox] Started Roblox (PID {gameClientPid})");

Expand All @@ -253,16 +252,15 @@ private async Task StartRoblox()
activityWatcher = new();
shouldWait = true;

App.NotifyIcon?.SetActivityWatcher(activityWatcher);

if (App.Settings.Prop.UseDiscordRichPresence)
{
App.Logger.WriteLine("[Bootstrapper::StartRoblox] Using Discord Rich Presence");
richPresence = new(activityWatcher);
}

if (App.Settings.Prop.ShowServerDetails)
{
App.Logger.WriteLine("[Bootstrapper::StartRoblox] Using server details notifier");
serverNotifier = new(activityWatcher);
if (App.NotifyIcon is not null)
App.NotifyIcon.RichPresenceIntegration = richPresence;
}
}

Expand Down
45 changes: 0 additions & 45 deletions Bloxstrap/Integrations/ServerNotifier.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
<ui:UiWindow x:Class="Bloxstrap.UI.Elements.NotifyIconMenu"
<ui:UiWindow x:Class="Bloxstrap.UI.Elements.ContextMenu.MenuContainer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:local="clr-namespace:Bloxstrap.UI.Elements"
xmlns:local="clr-namespace:Bloxstrap.UI.Elements"
mc:Ignorable="d"
Title="NotifyIconMenu"
Title="ContextMenuContainer"
Background="{ui:ThemeResource ApplicationBackgroundBrush}"
MinWidth="0"
MinHeight="0"
Width="0"
Height="0"
ShowInTaskbar="False"
WindowStyle="None"
ResizeMode="NoResize"
Loaded="Window_Loaded">
<ui:UiWindow.ContextMenu>
<ContextMenu>
<MenuItem Header="_Bold"
IsCheckable="True"/>
<MenuItem Header="_Italic"
IsCheckable="True" />
<MenuItem x:Name="VersionMenuItem" IsEnabled="False" />
<Separator />
<MenuItem Header="I_ncrease Font Size" />
<MenuItem Header="_Decrease Font Size" />
<MenuItem x:Name="TestMenuItem" Header="test" IsCheckable="True" Click="TestMenuItem_Click" />
<MenuItem x:Name="RichPresenceMenuItem" Header="Discord Rich Presence" IsCheckable="True" Visibility="Collapsed" Click="RichPresenceMenuItem_Click" />
<MenuItem x:Name="ServerDetailsMenuItem" Header="See server details" Visibility="Collapsed" Click="ServerDetailsMenuItem_Click" />
<MenuItem x:Name="TestMenuItem" Header="Test" IsCheckable="True" Click="TestMenuItem_Click" />
</ContextMenu>
</ui:UiWindow.ContextMenu>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<!--<ProgressBar IsIndeterminate="True" />-->
<TextBlock Margin="16" Text="if this stops spinning ur SCREWED!!!!!!!!!" />
<ui:ProgressRing Margin="16" IsIndeterminate="True" />
</StackPanel>
</ui:UiWindow>
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,44 @@

using Bloxstrap.UI.ViewModels;

namespace Bloxstrap.UI.Elements
namespace Bloxstrap.UI.Elements.ContextMenu
{
/// <summary>
/// Interaction logic for NotifyIconMenu.xaml
/// </summary>
public partial class NotifyIconMenu
public partial class MenuContainer
{
public NotifyIconMenu()
// 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()
{
InitializeComponent();

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

private void Window_Loaded(object? sender, RoutedEventArgs e)
{
// this is an awful hack lmao im so sorry to anyone who reads this
// https://stackoverflow.com/questions/357076/best-way-to-hide-a-window-from-the-alt-tab-program-switcher#:~:text=ShowInTaskbar%20%3D%20false%3B%20Owner%20%3D%20form1,form%27s%20ShowInTaskbar%20property%20to%20false.
// this is done to register the context menu wrapper as a tool window so it doesnt appear in the alt+tab switcher
// https://stackoverflow.com/a/551847/11852173

var wndHelper = new WindowInteropHelper(this);
long exStyle = NativeMethods.GetWindowLongPtr(wndHelper.Handle, NativeMethods.GWL_EXSTYLE).ToInt64();
exStyle |= NativeMethods.WS_EX_TOOLWINDOW;
NativeMethods.SetWindowLongPtr(wndHelper.Handle, NativeMethods.GWL_EXSTYLE, (IntPtr)exStyle);
}

// i tried to use a viewmodel but uhhhhhhh it just didnt work idk
private void RichPresenceMenuItem_Click(object sender, RoutedEventArgs e)
{
Controls.ShowMessageBox($"hi how u doing i am {RichPresenceMenuItem.IsChecked}", MessageBoxImage.Warning);
}

private void ServerDetailsMenuItem_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);
Expand Down
74 changes: 62 additions & 12 deletions Bloxstrap/UI/NotifyIconWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using System.Windows.Forms;
using System.Windows;

using Bloxstrap.UI.Elements;
using Bloxstrap.Integrations;
using Bloxstrap.UI.Elements.ContextMenu;

namespace Bloxstrap.UI
{
public class NotifyIconWrapper : IDisposable
{
bool _disposed = false;

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

public DiscordRichPresence? RichPresenceIntegration;

EventHandler? _alertClickHandler;


public NotifyIconWrapper()
{
App.Logger.WriteLine("[NotifyIconWrapper::NotifyIconWrapper] Initializing notification area icon");
Expand All @@ -27,18 +30,65 @@ public NotifyIconWrapper()

_notifyIcon.MouseClick += MouseClickEventHandler;

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

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

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

public async void OnGameJoin()
{
if (!App.Settings.Prop.ShowServerDetails)
return;

App.Logger.WriteLine($"[NotifyIconWrapper::OnActivityGameJoin] Getting game/server information");

string machineAddress = _activityWatcher!.ActivityMachineAddress;
string machineLocation = "";

// basically nobody has a free public access geolocation api that's accurate,
// the ones that do require an api key which isn't suitable for a client-side application like this
// so, hopefully this is reliable enough?
string locationCity = await App.HttpClient.GetStringAsync($"https://ipinfo.io/{machineAddress}/city");
string locationRegion = await App.HttpClient.GetStringAsync($"https://ipinfo.io/{machineAddress}/region");
string locationCountry = await App.HttpClient.GetStringAsync($"https://ipinfo.io/{machineAddress}/country");

locationCity = locationCity.ReplaceLineEndings("");
locationRegion = locationRegion.ReplaceLineEndings("");
locationCountry = locationCountry.ReplaceLineEndings("");

_contextMenuWrapper.Closing += (_, _) => App.Logger.WriteLine("[NotifyIconWrapper::NotifyIconWrapper] Context menu wrapper closing");
if (String.IsNullOrEmpty(locationCountry))
machineLocation = "N/A";
else if (locationCity == locationRegion)
machineLocation = $"{locationRegion}, {locationCountry}";
else
machineLocation = $"{locationCity}, {locationRegion}, {locationCountry}";

_menuContainer.Dispatcher.Invoke(() => _menuContainer.ServerDetailsMenuItem.Visibility = Visibility.Visible);

ShowAlert("Connnected to server", $"Location: {machineLocation}\nClick to copy Instance ID", 10, (_, _) => System.Windows.Clipboard.SetText(_activityWatcher.ActivityJobId));
}

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

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

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

public void ShowAlert(string caption, string message, int duration, EventHandler? clickHandler)
Expand Down Expand Up @@ -84,7 +134,7 @@ public void Dispose()

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

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

_disposed = true;
Expand Down

0 comments on commit 5f9f63d

Please sign in to comment.