GUI: Add Cancel button to plugin configuration dialog

This commit is contained in:
Katy Coe
2020-12-29 18:23:16 +01:00
parent dbee37d4de
commit aecd41846d
3 changed files with 37 additions and 8 deletions

View File

@@ -211,10 +211,15 @@
</local:OptionTemplateSelector>
</ListBox.ItemTemplateSelector>
</ListBox>
<!-- Accept button -->
<DockPanel Grid.Row="2" VerticalAlignment="Bottom" Margin="10">
<!-- Reset options to defaults -->
<Button Name="resetButton" DockPanel.Dock="Left" Click="resetButton_Click" Style="{StaticResource LightBoxButton}" Width="150" FontSize="18" Padding="5">Defaults</Button>
<!-- Cancel button -->
<Button Name="cancelButton" DockPanel.Dock="Right" HorizontalAlignment="Right" Click="cancelButton_Click" Style="{StaticResource LightBoxButton}" IsDefault="True" Width="150" FontSize="18" Padding="5" Margin="10,0,0,0">Cancel</Button>
<!-- Accept button -->
<Button Name="okButton" DockPanel.Dock="Right" HorizontalAlignment="Right" Click="okButton_Click" Style="{StaticResource LightBoxButton}" IsDefault="True" Width="150" FontSize="18" Padding="5">OK</Button>
</DockPanel>
</Grid>

View File

@@ -71,7 +71,11 @@ namespace Il2CppInspectorGUI
public partial class PluginConfigurationDialog : Window
{
// Item to configure
public IPlugin Plugin { get; private set; }
private ManagedPlugin ManagedPlugin { get; set; }
public IPlugin Plugin => ManagedPlugin.Plugin;
// Options when window was opened
public Dictionary<string, object> OriginalOptions { get; private set; }
// This helps us find XAML elements withing a DataTemplate
// Adapted from https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-find-datatemplate-generated-elements?view=netframeworkdesktop-4.8
@@ -135,10 +139,13 @@ namespace Il2CppInspectorGUI
}
// Initialize configuration dialog window
public PluginConfigurationDialog(IPlugin plugin) {
public PluginConfigurationDialog(ManagedPlugin plugin) {
InitializeComponent();
DataContext = this;
Plugin = plugin;
ManagedPlugin = plugin;
// Copy current options
OriginalOptions = new Dictionary<string, object>(Plugin.Options.ToDictionary(o => o.Name, o => o.Value));
// Validate options once they have loaded
lstOptions.ItemContainerGenerator.StatusChanged += OptionsListBoxStatusChanged;
@@ -210,6 +217,11 @@ namespace Il2CppInspectorGUI
// Check options validity before allowing the dialog to close either by clicking OK or the close icon
private void Window_Closing(object sender, CancelEventArgs e) {
// Do nothing if user clicked Cancel
if (DialogResult == false)
return;
// Don't allow the window to close if any of the options are invalid
if (!IsValid(lstOptions)) {
MessageBox.Show("One or more options are invalid.", "Il2CppInspector Plugin Configuration");
@@ -224,8 +236,8 @@ namespace Il2CppInspectorGUI
// Reset a plugin's settings
private void resetButton_Click(object sender, RoutedEventArgs e) {
// Get new context
Plugin = PluginManager.Reset(Plugin);
// Get new context (updates ManagedPlugin)
PluginManager.Reset(Plugin);
// Validate options once they have loaded
lstOptions.ItemContainerGenerator.StatusChanged += OptionsListBoxStatusChanged;
@@ -253,5 +265,17 @@ namespace Il2CppInspectorGUI
// Remove validation errors for disabled options
ValidateAllOptions();
}
private void cancelButton_Click(object sender, RoutedEventArgs e) {
// Revert changes
foreach (var option in OriginalOptions)
ManagedPlugin[option.Key] = option.Value;
// Replace options in ListBox
lstOptions.ItemsSource = Plugin.Options;
DialogResult = false;
}
}
}

View File

@@ -48,7 +48,7 @@ namespace Il2CppInspector.GUI
private void btnConfig_Click(object sender, RoutedEventArgs e) {
var plugin = (ManagedPlugin) ((Button) sender).DataContext;
var configDlg = new Il2CppInspectorGUI.PluginConfigurationDialog(plugin.Plugin);
var configDlg = new Il2CppInspectorGUI.PluginConfigurationDialog(plugin);
configDlg.Owner = this;
configDlg.ShowDialog();
}