Files
MPF/MPF.UI.Core/Windows/MainWindow.xaml.cs

454 lines
16 KiB
C#
Raw Normal View History

2018-06-14 20:40:41 -04:00
using System;
2023-10-08 23:46:24 -04:00
using System.IO;
2023-10-08 20:52:29 -04:00
using System.Windows;
using System.Windows.Controls;
2023-10-08 23:06:17 -04:00
using MPF.Core;
2023-10-09 11:35:49 -04:00
using MPF.Core.UI.ViewModels;
2023-10-08 23:15:24 -04:00
using SabreTools.RedumpLib.Data;
using WPFCustomMessageBox;
2023-10-08 23:46:24 -04:00
using WinForms = System.Windows.Forms;
2023-09-25 21:39:28 -04:00
namespace MPF.UI.Core.Windows
2018-05-12 19:48:18 -04:00
{
public partial class MainWindow : WindowBase
2018-05-12 19:48:18 -04:00
{
/// <summary>
/// Read-only access to the current main view model
/// </summary>
2023-10-11 11:49:56 -04:00
public MainViewModel MainViewModel => DataContext as MainViewModel ?? new MainViewModel();
/// <summary>
/// Constructor
/// </summary>
public MainWindow() => InitializeComponent();
2020-09-10 23:21:18 -07:00
/// <summary>
/// Handler for MainWindow OnContentRendered event
/// </summary>
protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
2023-10-08 20:52:29 -04:00
// Disable buttons until we load fully
2023-10-08 22:49:46 -04:00
MainViewModel.StartStopButtonEnabled = false;
MainViewModel.MediaScanButtonEnabled = false;
MainViewModel.UpdateVolumeLabelEnabled = false;
MainViewModel.CopyProtectScanButtonEnabled = false;
2023-10-08 20:52:29 -04:00
// Add the click handlers to the UI
AddEventHandlers();
// Display the debug option in the menu, if necessary
if (MainViewModel.Options.ShowDebugViewMenuItem)
DebugViewMenuItem.Visibility = Visibility.Visible;
MainViewModel.Init(LogOutput.EnqueueLog, DisplayUserMessage, ShowDiscInformationWindow);
2023-10-09 11:35:49 -04:00
// Set the UI color scheme according to the options
if (MainViewModel.Options.EnableDarkMode)
EnableDarkMode();
else
EnableLightMode();
// Check for updates, if necessary
if (MainViewModel.Options.CheckForUpdatesOnStartup)
CheckForUpdates(showIfSame: false);
More and more cleanup (#86) * Namespace cleanups * Make dump validation instanced * Add note to Tasks * Move stuff to DumpEnvironment Most of the stuff in Tasks.cs acted on a single input parameter, namely a DumpEnvironment. Since that's the case, it was more logical to wrap those into DumpEnvironment and make them instance methods rather than keep them static. Due to this change, quite a few methods changed access, and some had to be marked internal due to wanting them to be tested separately. * Gut Tasks * One less thing in MainWindow * Remove explicit cast * Wrong check * Create helper method * Disable scan button on dump * Remove unnecessary getters/setters * Method name/description cleanup * Address TODO * Namespace cleanups * Make dump validation instanced * Add note to Tasks * Move stuff to DumpEnvironment Most of the stuff in Tasks.cs acted on a single input parameter, namely a DumpEnvironment. Since that's the case, it was more logical to wrap those into DumpEnvironment and make them instance methods rather than keep them static. Due to this change, quite a few methods changed access, and some had to be marked internal due to wanting them to be tested separately. * Gut Tasks * One less thing in MainWindow * Remove explicit cast * Wrong check * Create helper method * Disable scan button on dump * Remove unnecessary getters/setters * Method name/description cleanup * Address TODO * Clean up OnContentRendered * Namespace cleanups * Make dump validation instanced * Add note to Tasks * Move stuff to DumpEnvironment Most of the stuff in Tasks.cs acted on a single input parameter, namely a DumpEnvironment. Since that's the case, it was more logical to wrap those into DumpEnvironment and make them instance methods rather than keep them static. Due to this change, quite a few methods changed access, and some had to be marked internal due to wanting them to be tested separately. * Gut Tasks * One less thing in MainWindow * Remove explicit cast * Wrong check * Create helper method * Disable scan button on dump * Remove unnecessary getters/setters * Method name/description cleanup * Address TODO * Clean up OnContentRendered * Update event handlers
2018-07-05 21:34:04 -07:00
}
2023-10-08 20:52:29 -04:00
#region UI Functionality
/// <summary>
/// Add all event handlers
/// </summary>
public void AddEventHandlers()
{
// Menu Bar Click
AboutMenuItem.Click += AboutClick;
AppExitMenuItem.Click += AppExitClick;
CheckForUpdatesMenuItem.Click += CheckForUpdatesClick;
DebugViewMenuItem.Click += DebugViewClick;
OptionsMenuItem.Click += OptionsMenuItemClick;
// User Area Click
CopyProtectScanButton.Click += CopyProtectScanButtonClick;
EnableParametersCheckBox.Click += EnableParametersCheckBoxClick;
MediaScanButton.Click += MediaScanButtonClick;
UpdateVolumeLabel.Click += UpdateVolumeLabelClick;
OutputPathBrowseButton.Click += OutputPathBrowseButtonClick;
StartStopButton.Click += StartStopButtonClick;
// User Area SelectionChanged
SystemTypeComboBox.SelectionChanged += SystemTypeComboBoxSelectionChanged;
MediaTypeComboBox.SelectionChanged += MediaTypeComboBoxSelectionChanged;
DriveLetterComboBox.SelectionChanged += DriveLetterComboBoxSelectionChanged;
DriveSpeedComboBox.SelectionChanged += DriveSpeedComboBoxSelectionChanged;
DumpingProgramComboBox.SelectionChanged += DumpingProgramComboBoxSelectionChanged;
// User Area TextChanged
OutputPathTextBox.TextChanged += OutputPathTextBoxTextChanged;
}
2023-10-08 23:46:24 -04:00
/// <summary>
/// Browse for an output file path
/// </summary>
public void BrowseFile()
{
// Get the current path, if possible
string currentPath = MainViewModel.OutputPath;
2023-10-11 11:49:56 -04:00
if (string.IsNullOrWhiteSpace(currentPath) && !string.IsNullOrWhiteSpace(MainViewModel.Options.DefaultOutputPath))
2023-10-08 23:46:24 -04:00
currentPath = Path.Combine(MainViewModel.Options.DefaultOutputPath, "track.bin");
2023-10-11 11:49:56 -04:00
else if (string.IsNullOrWhiteSpace(currentPath))
currentPath = "track.bin";
2023-10-08 23:46:24 -04:00
if (string.IsNullOrWhiteSpace(currentPath))
currentPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "track.bin");
// Get the full path
currentPath = Path.GetFullPath(currentPath);
// Get the directory
2023-10-11 11:49:56 -04:00
var directory = Path.GetDirectoryName(currentPath);
2023-10-08 23:46:24 -04:00
// Get the filename
string filename = Path.GetFileName(currentPath);
WinForms.FileDialog fileDialog = new WinForms.SaveFileDialog
{
FileName = filename,
InitialDirectory = directory,
};
WinForms.DialogResult result = fileDialog.ShowDialog();
if (result == WinForms.DialogResult.OK)
{
MainViewModel.OutputPath = fileDialog.FileName;
}
}
/// <summary>
/// Check for available updates
/// </summary>
/// <param name="showIfSame">True to show the box even if it's the same, false to only show if it's different</param>
public void CheckForUpdates(bool showIfSame)
{
2023-10-11 11:49:56 -04:00
(bool different, string message, var url) = MainViewModel.CheckForUpdates();
// If we have a new version, put it in the clipboard
if (different)
Clipboard.SetText(url);
if (showIfSame || different)
CustomMessageBox.Show(this, message, "Version Update Check", MessageBoxButton.OK, different ? MessageBoxImage.Exclamation : MessageBoxImage.Information);
}
/// <summary>
/// Display a user message using a CustomMessageBox
/// </summary>
/// <param name="title">Title to display to the user</param>
/// <param name="message">Message to display to the user</param>
/// <param name="optionCount">Number of options to display</param>
/// <param name="flag">true for inquiry, false otherwise</param>
/// <returns>true for positive, false for negative, null for neutral</returns>
public bool? DisplayUserMessage(string title, string message, int optionCount, bool flag)
{
// Set the correct button style
MessageBoxButton button;
switch (optionCount)
{
case 1:
button = MessageBoxButton.OK;
break;
case 2:
button = MessageBoxButton.YesNo;
break;
case 3:
button = MessageBoxButton.YesNoCancel;
break;
// This should not happen, but default to "OK"
default:
button = MessageBoxButton.OK;
break;
}
// Set the correct icon
MessageBoxImage image = flag ? MessageBoxImage.Question : MessageBoxImage.Exclamation;
// Display and get the result
MessageBoxResult result = CustomMessageBox.Show(this, message, title, button, image);
switch (result)
{
case MessageBoxResult.OK:
case MessageBoxResult.Yes:
return true;
case MessageBoxResult.No:
return false;
case MessageBoxResult.Cancel:
case MessageBoxResult.None:
default:
return null;
}
}
2023-10-08 23:06:17 -04:00
/// <summary>
/// Build a dummy SubmissionInfo and display it for testing
/// </summary>
public void ShowDebugDiscInfoWindow()
{
var submissionInfo = MainViewModel.CreateDebugSubmissionInfo();
2023-10-08 23:15:24 -04:00
var result = ShowDiscInformationWindow(submissionInfo);
2023-10-08 23:06:17 -04:00
InfoTool.ProcessSpecialFields(result.Item2);
}
2023-10-08 23:15:24 -04:00
/// <summary>
/// Show the disc information window
/// </summary>
/// <param name="submissionInfo">SubmissionInfo object to display and possibly change</param>
/// <returns>Dialog open result</returns>
2023-10-11 11:49:56 -04:00
#if NET48
2023-10-08 23:15:24 -04:00
public (bool?, SubmissionInfo) ShowDiscInformationWindow(SubmissionInfo submissionInfo)
2023-10-11 11:49:56 -04:00
#else
public (bool?, SubmissionInfo?) ShowDiscInformationWindow(SubmissionInfo? submissionInfo)
#endif
2023-10-08 23:15:24 -04:00
{
if (MainViewModel.Options.ShowDiscEjectReminder)
CustomMessageBox.Show(this, "It is now safe to eject the disc", "Eject", MessageBoxButton.OK, MessageBoxImage.Information);
2023-10-08 23:15:24 -04:00
var discInformationWindow = new DiscInformationWindow(MainViewModel.Options, submissionInfo)
{
Focusable = true,
Owner = this,
ShowActivated = true,
ShowInTaskbar = true,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
};
bool? result = discInformationWindow.ShowDialog();
// Copy back the submission info changes, if necessary
if (result == true)
2023-10-11 11:49:56 -04:00
#if NET48
2023-10-08 23:15:24 -04:00
submissionInfo = discInformationWindow.DiscInformationViewModel.SubmissionInfo.Clone() as SubmissionInfo;
2023-10-11 11:49:56 -04:00
#else
submissionInfo = (discInformationWindow.DiscInformationViewModel.SubmissionInfo.Clone() as SubmissionInfo)!;
#endif
2023-10-08 23:15:24 -04:00
2023-10-11 11:49:56 -04:00
#if NET48
2023-10-08 23:15:24 -04:00
return (result, submissionInfo);
2023-10-11 11:49:56 -04:00
#else
return (result, submissionInfo!);
#endif
2023-10-08 23:15:24 -04:00
}
2023-10-08 20:52:29 -04:00
/// <summary>
2023-10-08 22:49:46 -04:00
/// Show the Options window
2023-10-08 20:52:29 -04:00
/// </summary>
2023-10-08 22:49:46 -04:00
public void ShowOptionsWindow()
2023-10-08 20:52:29 -04:00
{
2023-10-08 22:49:46 -04:00
var optionsWindow = new OptionsWindow(MainViewModel.Options)
2023-10-08 20:52:29 -04:00
{
2023-10-08 22:49:46 -04:00
Focusable = true,
Owner = this,
ShowActivated = true,
ShowInTaskbar = true,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
};
optionsWindow.Closed += OnOptionsUpdated;
2023-10-08 22:49:46 -04:00
optionsWindow.Show();
2023-10-08 20:52:29 -04:00
}
2023-10-09 11:35:49 -04:00
/// <summary>
/// Recolor all UI elements for light mode
/// </summary>
private static void EnableLightMode()
{
var theme = new LightModeTheme();
theme.Apply();
}
/// <summary>
/// Recolor all UI elements for dark mode
/// </summary>
private static void EnableDarkMode()
{
var theme = new DarkModeTheme();
theme.Apply();
}
2023-10-08 20:52:29 -04:00
#endregion
#region Event Handlers
/// <summary>
/// Handler for OptionsWindow OnUpdated event
/// </summary>
2023-10-11 11:49:56 -04:00
#if NET48
public void OnOptionsUpdated(object sender, EventArgs e)
2023-10-11 11:49:56 -04:00
#else
public void OnOptionsUpdated(object? sender, EventArgs e)
#endif
{
bool savedSettings = (sender as OptionsWindow)?.OptionsViewModel?.SavedSettings ?? false;
2023-10-11 11:49:56 -04:00
var options = (sender as OptionsWindow)?.OptionsViewModel?.Options;
MainViewModel.UpdateOptions(savedSettings, options);
}
2023-10-08 20:52:29 -04:00
#region Menu Bar
/// <summary>
/// Handler for AboutMenuItem Click event
/// </summary>
public void AboutClick(object sender, RoutedEventArgs e)
{
string aboutText = MainViewModel.CreateAboutText();
CustomMessageBox.Show(this, aboutText, "About", MessageBoxButton.OK, MessageBoxImage.Information);
}
2023-10-08 20:52:29 -04:00
/// <summary>
/// Handler for AppExitMenuItem Click event
/// </summary>
public void AppExitClick(object sender, RoutedEventArgs e) =>
2023-10-09 00:13:40 -04:00
Application.Current.Shutdown();
2023-10-08 20:52:29 -04:00
/// <summary>
/// Handler for CheckForUpdatesMenuItem Click event
/// </summary>
public void CheckForUpdatesClick(object sender, RoutedEventArgs e)
=> CheckForUpdates(showIfSame: true);
2023-10-08 20:52:29 -04:00
/// <summary>
/// Handler for DebugViewMenuItem Click event
/// </summary>
public void DebugViewClick(object sender, RoutedEventArgs e) =>
2023-10-08 23:06:17 -04:00
ShowDebugDiscInfoWindow();
2023-10-08 20:52:29 -04:00
/// <summary>
/// Handler for OptionsMenuItem Click event
/// </summary>
public void OptionsMenuItemClick(object sender, RoutedEventArgs e) =>
2023-10-08 22:49:46 -04:00
ShowOptionsWindow();
2023-10-08 20:52:29 -04:00
#endregion
#region User Area
/// <summary>
/// Handler for CopyProtectScanButton Click event
/// </summary>
public async void CopyProtectScanButtonClick(object sender, RoutedEventArgs e)
{
2023-10-11 11:49:56 -04:00
var (output, error) = await MainViewModel.ScanAndShowProtection();
if (!MainViewModel.LogPanelExpanded)
{
2023-10-11 11:49:56 -04:00
if (!string.IsNullOrEmpty(output) && string.IsNullOrEmpty(error))
CustomMessageBox.Show(this, output, "Detected Protection(s)", MessageBoxButton.OK, MessageBoxImage.Information);
else
CustomMessageBox.Show(this, "An exception occurred, see the log for details", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
2023-10-08 20:52:29 -04:00
/// <summary>
/// Handler for DriveLetterComboBox SelectionChanged event
/// </summary>
public void DriveLetterComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.InitializeUIValues(removeEventHandlers: true, rescanDrives: false);
}
/// <summary>
/// Handler for DriveSpeedComboBox SelectionChanged event
/// </summary>
public void DriveSpeedComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.EnsureDiscInformation();
}
/// <summary>
/// Handler for DumpingProgramComboBox SelectionChanged event
/// </summary>
public void DumpingProgramComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.ChangeDumpingProgram();
}
/// <summary>
/// Handler for EnableParametersCheckBox Click event
/// </summary>
2023-10-08 22:49:46 -04:00
public void EnableParametersCheckBoxClick(object sender, RoutedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.ToggleParameters();
}
2023-10-08 20:52:29 -04:00
/// <summary>
/// Handler for MediaScanButton Click event
/// </summary>
public void MediaScanButtonClick(object sender, RoutedEventArgs e) =>
MainViewModel.InitializeUIValues(removeEventHandlers: true, rescanDrives: true);
/// <summary>
/// Handler for MediaTypeComboBox SelectionChanged event
/// </summary>
public void MediaTypeComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
2023-10-09 00:13:40 -04:00
MainViewModel.ChangeMediaType(e.RemovedItems, e.AddedItems);
2023-10-08 20:52:29 -04:00
}
/// <summary>
/// Handler for OutputPathBrowseButton Click event
/// </summary>
2023-10-08 23:46:24 -04:00
public void OutputPathBrowseButtonClick(object sender, RoutedEventArgs e)
{
BrowseFile();
MainViewModel.EnsureDiscInformation();
}
2023-10-08 20:52:29 -04:00
/// <summary>
/// Handler for OutputPathTextBox TextChanged event
/// </summary>
public void OutputPathTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.EnsureDiscInformation();
}
/// <summary>
/// Handler for StartStopButton Click event
/// </summary>
public void StartStopButtonClick(object sender, RoutedEventArgs e) =>
MainViewModel.ToggleStartStop();
/// <summary>
/// Handler for SystemTypeComboBox SelectionChanged event
/// </summary>
public void SystemTypeComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.ChangeSystem();
}
/// <summary>
/// Handler for UpdateVolumeLabel Click event
/// </summary>
public void UpdateVolumeLabelClick(object sender, RoutedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
{
if (MainViewModel.Options.FastUpdateLabel)
MainViewModel.FastUpdateLabel(removeEventHandlers: true);
else
MainViewModel.InitializeUIValues(removeEventHandlers: true, rescanDrives: false);
}
}
#endregion
#endregion // Event Handlers
2018-05-12 19:48:18 -04:00
}
}