Data directory changes (#10)

* Data Directory Changes

1. Have implemented a new data directory selection window.
2. Have removed the data directory move button from global settings.

* Added restart message
This commit is contained in:
Brett Hewitson 2021-12-01 21:14:38 +10:00 committed by GitHub
parent d4b8b51362
commit b0bced67a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 626 additions and 123 deletions

View file

@ -0,0 +1,95 @@
<Window x:Class="ServerManagerTool.Windows.DataDirectoryWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinHeight="400" MinWidth="500" Height="400" Width="500" WindowStyle="ToolWindow" WindowStartupLocation="CenterOwner" ShowInTaskbar="True" ResizeMode="CanResize"
Icon="../Art/favicon.ico" Title="{DynamicResource DataDirectory_Title}"
x:Name="DataDirectory">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="..\Globalization\en-US\en-US.xaml"/>
<ResourceDictionary Source="..\Styles\Default.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid Background="{StaticResource GradientBackground}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Margin="5" Text="{DynamicResource DataDirectory_InformationLabel}" Foreground="DarkCyan" VerticalAlignment="Center" TextWrapping="Wrap" />
<TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Margin="5" Text="{DynamicResource DataDirectory_SelectionLabel}" FontWeight="Bold" VerticalAlignment="Center" TextWrapping="Wrap" />
<ListBox x:Name="DriveSelectionListBox" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Margin="5" ItemsSource="{Binding DriveInformation, ElementName=DataDirectory}" HorizontalContentAlignment="Stretch" Background="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}">
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="False" />
<Condition Property="IsSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="True" />
<Condition Property="IsSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="{Binding Line1}" FontSize="12" FontWeight="Bold" VerticalAlignment="Center" Padding="5,5,5,0"/>
<Label Grid.Row="1" Content="{Binding Line2}" VerticalAlignment="Center" Padding="5,0,5,5"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label Grid.Row="3" Grid.Column="0" Content="{DynamicResource DataDirectory_FolderLabel}" ToolTip="{DynamicResource DataDirectory_FolderTooltip}" />
<TextBox Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,0,5,0" Text="{Binding FolderName, ElementName=DataDirectory, Mode=TwoWay}" VerticalContentAlignment="Center" MaxLength="50" ToolTip="{DynamicResource DataDirectory_FolderTooltip}" />
<Button Grid.Row="4" Grid.Column="0" Content="{DynamicResource DataDirectory_RefreshButtonLabel}" Margin="5" MinWidth="75" HorizontalAlignment="Left" Click="Refresh_Click" Visibility="Hidden"/>
<Button Grid.Row="4" Grid.Column="1" Content="{DynamicResource DataDirectory_OkButtonLabel}" Margin="5" MinWidth="75" HorizontalAlignment="Right" Click="Ok_Click"/>
<Button Grid.Row="4" Grid.Column="2" Content="{DynamicResource DataDirectory_CancelButtonLabel}" Margin="5" MinWidth="75" HorizontalAlignment="Left" IsCancel="True"/>
</Grid>
</Window>

View file

@ -0,0 +1,169 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using WPFSharp.Globalizer;
namespace ServerManagerTool.Windows
{
/// <summary>
/// Interaction logic for DriveSelectionWindow.xaml
/// </summary>
public partial class DataDirectoryWindow : Window
{
private readonly GlobalizedApplication _globalizer = GlobalizedApplication.Instance;
public static readonly DependencyProperty DriveInformationProperty = DependencyProperty.Register(nameof(DriveInformation), typeof(List<DriveInfoDisplay>), typeof(DataDirectoryWindow), new PropertyMetadata(null));
public static readonly DependencyProperty FolderNameProperty = DependencyProperty.Register(nameof(FolderName), typeof(string), typeof(DataDirectoryWindow), new PropertyMetadata(null));
public DataDirectoryWindow()
{
InitializeComponent();
PopulateDriveInformation();
}
public List<DriveInfoDisplay> DriveInformation
{
get { return (List<DriveInfoDisplay>)GetValue(DriveInformationProperty); }
set { SetValue(DriveInformationProperty, value); }
}
public string FolderName
{
get { return (string)GetValue(FolderNameProperty); }
set { SetValue(FolderNameProperty, value); }
}
private void PopulateDriveInformation()
{
this.FolderName = Config.Default.DefaultDataDirectoryName;
this.DriveInformation = DriveInfo.GetDrives().Where(d => d.IsReady && d.DriveType == DriveType.Fixed).Select(d => new DriveInfoDisplay(d)).ToList();
var installationFolder = Path.GetPathRoot(Assembly.GetEntryAssembly().Location);
if (!installationFolder.EndsWith(@"\"))
installationFolder += @"\";
foreach (var driveInfo in DriveInformation)
{
if (driveInfo.DriveInfo.RootDirectory.FullName.Equals(installationFolder))
{
this.DriveSelectionListBox.SelectedItem = driveInfo;
break;
}
}
}
private void Ok_Click(object sender, RoutedEventArgs e)
{
try
{
var result = CreateDataDirectory();
if (result == MessageBoxResult.Yes)
{
MessageBox.Show(_globalizer.GetResourceString("DataDirectory_RestartLabel"), _globalizer.GetResourceString("DataDirectory_RestartTitle"), MessageBoxButton.OK, MessageBoxImage.Information);
this.DialogResult = true;
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, _globalizer.GetResourceString("DataDirectory_ErrorTitle"));
}
}
private void Refresh_Click(object sender, RoutedEventArgs e)
{
PopulateDriveInformation();
}
private MessageBoxResult CreateDataDirectory()
{
var selectedDrive = this.DriveSelectionListBox.SelectedItem as DriveInfoDisplay;
if (selectedDrive is null)
{
return MessageBoxResult.None;
}
var invalidCharacters = Path.GetInvalidFileNameChars();
if (string.IsNullOrWhiteSpace(FolderName) || FolderName.Any(c => invalidCharacters.Contains(c)))
{
throw new Exception(_globalizer.GetResourceString("DataDirectory_FolderErrorLabel"));
}
var newDataFolder = Path.Combine(selectedDrive.DriveInfo.RootDirectory.FullName, FolderName);
var confirm = MessageBox.Show(string.Format(_globalizer.GetResourceString("Application_DataDirectory_ConfirmLabel"), Path.Combine(newDataFolder, Config.Default.ProfilesDir), Path.Combine(newDataFolder, Config.Default.SteamCmdDir)), _globalizer.GetResourceString("Application_DataDirectory_ConfirmTitle"), MessageBoxButton.YesNo, MessageBoxImage.Question);
if (confirm == MessageBoxResult.Yes)
{
if (newDataFolder.EndsWith(@"\"))
newDataFolder = newDataFolder.Substring(0, newDataFolder.Length - 1);
Config.Default.DataDir = newDataFolder;
}
return confirm;
}
}
public class DriveInfoDisplay
{
private const decimal DIVISOR = 1024M;
// Load all suffixes in an array
private static readonly string[] suffixes = { "Bytes", "KB", "MB", "GB", "TB", "PB" };
private readonly GlobalizedApplication _globalizer = GlobalizedApplication.Instance;
public DriveInfoDisplay(DriveInfo driveInfo)
{
DriveInfo = driveInfo;
}
public DriveInfo DriveInfo
{
get;
set;
}
public string Line1
{
get
{
if (DriveInfo is null)
return string.Empty;
var volumeLabel = string.IsNullOrWhiteSpace(DriveInfo.VolumeLabel) ? _globalizer.GetResourceString("DataDirectory_LocalDiskLabel") : DriveInfo.VolumeLabel;
return $"{volumeLabel} ({DriveInfo.Name.Replace(@"\", string.Empty)})";
}
}
public string Line2
{
get
{
if (DriveInfo is null)
return string.Empty;
return string.Format(_globalizer.GetResourceString("DataDirectory_DriveLine2Label"), FormatSize(DriveInfo.TotalFreeSpace), FormatSize(DriveInfo.TotalSize));
}
}
public static string FormatSize(long bytes)
{
var counter = 0;
var number = (decimal)bytes;
while (number / DIVISOR >= 1)
{
number /= DIVISOR;
counter++;
}
return string.Format("{0:n2} {1}", number, suffixes[counter]);
}
}
}

View file

@ -71,7 +71,7 @@
<Label Grid.Row="4" Grid.Column="0" Margin="1" Content="{DynamicResource GlobalSettings_DataDirectoryLabel}" VerticalAlignment="Center"/>
<TextBox Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Margin="1" Text="{Binding CurrentConfig.DataDir, Mode=TwoWay}" IsReadOnly="True" IsReadOnlyCaretVisible="True" VerticalContentAlignment="Center" />
<Button Grid.Row="4" Grid.Column="3" Grid.ColumnSpan="2" Margin="5,1,0,1" VerticalAlignment="Center" HorizontalAlignment="Left" Content="{DynamicResource DataDirectoryButtonContent}" Click="SetDataDir_Click"/>
<Button Grid.Row="4" Grid.Column="3" Grid.ColumnSpan="2" Margin="5,1,0,1" VerticalAlignment="Center" HorizontalAlignment="Left" Content="{DynamicResource DataDirectoryButtonContent}" Click="SetDataDir_Click" Visibility="Hidden"/>
<Label Grid.Row="5" Grid.Column="0" Margin="1" Content="{DynamicResource GlobalSettings_BackupDirectoryLabel}" VerticalAlignment="Center"/>
<TextBox Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="2" Margin="1" Text="{Binding CurrentConfig.BackupPath, Mode=TwoWay}" IsReadOnly="True" IsReadOnlyCaretVisible="True" VerticalContentAlignment="Center"/>