Skip to content

Commit

Permalink
Add window for showing server information
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Jul 19, 2023
1 parent ce1c2bc commit fbd5692
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<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="ServerDetailsMenuItem" Header="See server details" Visibility="Collapsed" Click="ServerDetailsMenuItem_Click" />
<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>
Expand Down
5 changes: 0 additions & 5 deletions Bloxstrap/UI/Elements/ContextMenu/MenuContainer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ 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
46 changes: 46 additions & 0 deletions Bloxstrap/UI/Elements/ContextMenu/ServerInformation.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<ui:UiWindow x:Class="Bloxstrap.UI.Elements.ContextMenu.ServerInformation"
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:local="clr-namespace:Bloxstrap.UI.Elements.ContextMenu"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:models="clr-namespace:Bloxstrap.UI.ViewModels.ContextMenu"
d:DataContext="{d:DesignInstance Type=models:ServerInformationViewModel}"
mc:Ignorable="d"
Title="Server information"
MinWidth="0"
MinHeight="0"
Width="400"
Height="230"
ResizeMode="NoResize"
Background="{ui:ThemeResource ApplicationBackgroundBrush}"
ExtendsContentIntoTitleBar="True"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<ui:TitleBar Grid.Row="0" Grid.ColumnSpan="2" Padding="8" x:Name="RootTitleBar" Title="Server information" ShowMinimize="False" ShowMaximize="False" CanMaximize="False" KeyboardNavigation.TabNavigation="None" Icon="pack://application:,,,/Bloxstrap.ico" />

<StackPanel Grid.Row="1" Margin="12">
<TextBlock Text="Instance ID" FontSize="16" FontWeight="Medium" />
<TextBlock Text="{Binding InstanceId, Mode=OneWay}" />

<TextBlock Margin="0,16,0,0" Text="Server location" FontSize="16" FontWeight="Medium" />
<TextBlock Text="{Binding ServerLocation, Mode=OneWay}" />
</StackPanel>

<Border Grid.Row="2" Margin="0,10,0,0" Padding="15" Background="{ui:ThemeResource SolidBackgroundFillColorSecondaryBrush}">
<StackPanel Orientation="Horizontal" FlowDirection="LeftToRight" HorizontalAlignment="Right">
<Button MinWidth="100" Content="Copy instance ID" Command="{Binding CopyInstanceIdCommand, Mode=OneTime}" />
<Button Margin="12,0,0,0" MinWidth="100" Content="Close" Command="{Binding CloseWindowCommand, Mode=OneTime}" />
</StackPanel>
</Border>
</Grid>
</Grid>
</ui:UiWindow>
30 changes: 30 additions & 0 deletions Bloxstrap/UI/Elements/ContextMenu/ServerInformation.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Bloxstrap.UI.ViewModels.ContextMenu;

namespace Bloxstrap.UI.Elements.ContextMenu
{
/// <summary>
/// Interaction logic for ServerInformation.xaml
/// </summary>
public partial class ServerInformation
{
public ServerInformation(RobloxActivity activityWatcher)
{
DataContext = new ServerInformationViewModel(this, activityWatcher);
InitializeComponent();
}
}
}
2 changes: 1 addition & 1 deletion Bloxstrap/UI/Elements/FluentMessageBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</Grid>

<Border Grid.Row="2" Margin="0,10,0,0" Padding="15" Background="{ui:ThemeResource SolidBackgroundFillColorSecondaryBrush}">
<StackPanel Grid.Row="2" Orientation="Horizontal" FlowDirection="LeftToRight" HorizontalAlignment="Right">
<StackPanel Orientation="Horizontal" FlowDirection="LeftToRight" HorizontalAlignment="Right">
<Button x:Name="ButtonOne" MinWidth="100" Content="Button 1" />
<Button x:Name="ButtonTwo" MinWidth="100" Margin="12,0,0,0" Content="Button 2" />
<Button x:Name="ButtonThree" MinWidth="100" Margin="12,0,0,0" Content="Button 3" />
Expand Down
32 changes: 29 additions & 3 deletions Bloxstrap/UI/NotifyIconWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ namespace Bloxstrap.UI
{
public class NotifyIconWrapper : IDisposable
{
// lol who needs properly structured mvvm and xaml when you have the absolute catastrophe that this is

bool _disposed = false;

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

private ServerInformation? _serverInformationWindow;

public DiscordRichPresence? RichPresenceIntegration;

EventHandler? _alertClickHandler;
Expand All @@ -31,6 +35,9 @@ public NotifyIconWrapper()
_notifyIcon.MouseClick += MouseClickEventHandler;

_menuContainer.Dispatcher.BeginInvoke(_menuContainer.ShowDialog);

_menuContainer.ServerDetailsMenuItem.Click += (_, _) => ShowServerInformationWindow();

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

Expand All @@ -46,17 +53,22 @@ public void SetActivityWatcher(RobloxActivity activityWatcher)

public async void OnGameJoin()
{
string serverLocation = await _activityWatcher!.GetServerLocation();

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

if (App.Settings.Prop.ShowServerDetails)
ShowAlert("Connnected to server", $"Location: {serverLocation}", 10, (_, _) => Clipboard.SetText(_activityWatcher.ActivityJobId));
{
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);

}

public void MouseClickEventHandler(object? sender, System.Windows.Forms.MouseEventArgs e)
Expand All @@ -68,6 +80,20 @@ public void MouseClickEventHandler(object? sender, System.Windows.Forms.MouseEve
_menuContainer.ContextMenu.IsOpen = true;
}

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

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

_serverInformationWindow.Activate();
}

public void ShowAlert(string caption, string message, int duration, EventHandler? clickHandler)
{
string id = Guid.NewGuid().ToString()[..8];
Expand Down
34 changes: 34 additions & 0 deletions Bloxstrap/UI/ViewModels/ContextMenu/ServerInformationViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Windows.Input;

using CommunityToolkit.Mvvm.Input;

using Bloxstrap.UI.Elements.ContextMenu;

namespace Bloxstrap.UI.ViewModels.ContextMenu
{
internal class ServerInformationViewModel : NotifyPropertyChangedViewModel
{
private readonly ServerInformation _window;
private readonly RobloxActivity _activityWatcher;

public string InstanceId => _activityWatcher.ActivityJobId;
public string ServerLocation { get; private set; } = "Loading, please wait...";

public ICommand CopyInstanceIdCommand => new RelayCommand(CopyInstanceId);
public ICommand CloseWindowCommand => new RelayCommand(_window.Close);

public ServerInformationViewModel(ServerInformation window, RobloxActivity activityWatcher)
{
_window = window;
_activityWatcher = activityWatcher;

Task.Run(async () =>
{
ServerLocation = await _activityWatcher.GetServerLocation();
OnPropertyChanged(nameof(ServerLocation));
});
}

private void CopyInstanceId() => Clipboard.SetText(InstanceId);
}
}

0 comments on commit fbd5692

Please sign in to comment.