Add and use helper method DoWithTempMute (void and generic flavours)

This commit is contained in:
YoshiRulz 2020-12-01 03:10:33 +10:00 committed by James Groom
parent cc4acd9c64
commit aad612593b
12 changed files with 106 additions and 126 deletions

View File

@ -175,6 +175,21 @@ namespace BizHawk.Client.EmuHawk
Sound.Instance.StartSound();
return result;
}
public static void DoWithTempMute(this IDialogController dialogController, Action action)
{
dialogController.StopSound();
action();
dialogController.StartSound();
}
public static T DoWithTempMute<T>(this IDialogController dialogController, Func<T> action)
{
dialogController.StopSound();
var ret = action();
dialogController.StartSound();
return ret;
}
}
public static class ListViewExtensions

View File

@ -199,9 +199,7 @@ namespace BizHawk.Client.EmuHawk.ToolExtensions
Message = "Number of recent files to track",
InitialValue = recent.MAX_RECENT_FILES.ToString()
};
mainForm.StopSound();
var result = prompt.ShowDialog();
mainForm.StartSound();
var result = mainForm.DoWithTempMute(() => prompt.ShowDialog());
if (result == DialogResult.OK)
{
int val = int.Parse(prompt.PromptText);
@ -216,22 +214,22 @@ namespace BizHawk.Client.EmuHawk.ToolExtensions
public static void HandleLoadError(this RecentFiles recent, IMainFormForTools mainForm, string path, string encodedPath = null)
{
mainForm.StopSound();
if (recent.Frozen)
mainForm.DoWithTempMute(() =>
{
MessageBox.Show($"Could not open {path}", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
// ensure topmost, not to have to minimize everything to see and use our modal window, if it somehow got covered
var result = MessageBox.Show(new Form { TopMost = true }, $"Could not open {path}\nRemove from list?", "File not found", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
if (result == DialogResult.Yes)
if (recent.Frozen)
{
recent.Remove(encodedPath ?? path);
MessageBox.Show($"Could not open {path}", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
mainForm.StartSound();
else
{
// ensure topmost, not to have to minimize everything to see and use our modal window, if it somehow got covered
var result = MessageBox.Show(new Form { TopMost = true }, $"Could not open {path}\nRemove from list?", "File not found", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
if (result == DialogResult.Yes)
{
recent.Remove(encodedPath ?? path);
}
}
});
}
public static IEnumerable<ToolStripItem> MenuItems(this IMemoryDomains domains, Action<string> setCallback, string selected = "", int? maxSize = null)

View File

@ -1605,9 +1605,7 @@ namespace BizHawk.Client.EmuHawk
Message = "Enter a hexadecimal value"
};
MainForm.StopSound();
var result = inputPrompt.ShowHawkDialog(this);
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => inputPrompt.ShowHawkDialog(this));
if (result == DialogResult.OK && inputPrompt.PromptText.IsHex())
{

View File

@ -738,9 +738,7 @@ namespace BizHawk.Client.EmuHawk
{
if (LuaImp.ScriptList.Changes && !string.IsNullOrEmpty(LuaImp.ScriptList.Filename))
{
MainForm.StopSound();
var result = MessageBox.Show("Save changes to session?", "Lua Console", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => MessageBox.Show("Save changes to session?", "Lua Console", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3));
if (result == DialogResult.Yes)
{
SaveOrSaveAs();

View File

@ -577,9 +577,7 @@ namespace BizHawk.Client.EmuHawk
var point = Cursor.Position;
point.Offset(i.Width / -2, i.Height / -2);
MainForm.StopSound();
var result = i.ShowHawkDialog(this, position: point);
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => i.ShowHawkDialog(this, position: point));
if (result.IsOk())
{
branch.UserText = i.PromptText;

View File

@ -196,9 +196,7 @@ namespace BizHawk.Client.EmuHawk
var point = Cursor.Position;
point.Offset(i.Width / -2, i.Height / -2);
Tastudio.MainForm.StopSound();
var result = i.ShowHawkDialog(this, position: point);
Tastudio.MainForm.StartSound();
var result = Tastudio.MainForm.DoWithTempMute(() => i.ShowHawkDialog(this, position: point));
if (!result.IsOk())
{
return;
@ -253,9 +251,7 @@ namespace BizHawk.Client.EmuHawk
point.Offset(i.Width / -2, i.Height / -2);
}
Tastudio.MainForm.StopSound();
var result = i.ShowHawkDialog(this, position: point);
Tastudio.MainForm.StartSound();
var result = Tastudio.MainForm.DoWithTempMute(() => i.ShowHawkDialog(this, position: point));
if (result == DialogResult.OK)
{

View File

@ -106,15 +106,12 @@ namespace BizHawk.Client.EmuHawk
if (CurrentTasMovie != null && CurrentTasMovie.Changes)
{
MainForm.StopSound();
var result = MessageBox.Show(
var result = MainForm.DoWithTempMute(() => MessageBox.Show(
"Save Changes?",
"Tastudio",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button3);
MainForm.StartSound();
MessageBoxDefaultButton.Button3));
if (result == DialogResult.Yes)
{
_exiting = true; // Asking to save changes should only ever be called when closing something

View File

@ -182,20 +182,21 @@ namespace BizHawk.Client.EmuHawk
else
{
_autosaveTimer.Stop();
MainForm.StopSound();
MessageStatusLabel.Text = "Saving...";
Cursor = Cursors.WaitCursor;
Update();
CurrentTasMovie.SaveBackup();
if (Settings.AutosaveInterval > 0)
MainForm.DoWithTempMute(() =>
{
_autosaveTimer.Start();
}
MessageStatusLabel.Text = "Saving...";
Cursor = Cursors.WaitCursor;
Update();
CurrentTasMovie.SaveBackup();
if (Settings.AutosaveInterval > 0)
{
_autosaveTimer.Start();
}
MessageStatusLabel.Text = "Backup .tasproj saved to \"Movie backups\" path.";
Settings.RecentTas.Add(CurrentTasMovie.Filename);
Cursor = Cursors.Default;
MainForm.StartSound();
MessageStatusLabel.Text = "Backup .tasproj saved to \"Movie backups\" path.";
Settings.RecentTas.Add(CurrentTasMovie.Filename);
Cursor = Cursors.Default;
});
}
}
@ -283,15 +284,12 @@ namespace BizHawk.Client.EmuHawk
var file = new FileInfo(bk2.Filename);
if (file.Exists)
{
MainForm.StopSound();
var result = MessageBox.Show(
var result = MainForm.DoWithTempMute(() => MessageBox.Show(
"Overwrite Existing File?",
"Tastudio",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button3);
MainForm.StartSound();
MessageBoxDefaultButton.Button3));
if (result == DialogResult.Yes)
{
bk2.Save();
@ -829,9 +827,7 @@ namespace BizHawk.Client.EmuHawk
InitialValue = CurrentTasMovie.ChangeLog.MaxSteps.ToString()
};
MainForm.StopSound();
var result = prompt.ShowDialog();
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => prompt.ShowDialog());
if (result.IsOk())
{
int val = 0;
@ -860,9 +856,7 @@ namespace BizHawk.Client.EmuHawk
InitialValue = Settings.BranchCellHoverInterval.ToString()
};
MainForm.StopSound();
var result = prompt.ShowDialog();
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => prompt.ShowDialog());
if (result.IsOk())
{
int val = int.Parse(prompt.PromptText);
@ -883,9 +877,7 @@ namespace BizHawk.Client.EmuHawk
InitialValue = Settings.SeekingCutoffInterval.ToString()
};
MainForm.StopSound();
var result = prompt.ShowDialog();
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => prompt.ShowDialog());
if (result.IsOk())
{
int val = int.Parse(prompt.PromptText);
@ -906,9 +898,7 @@ namespace BizHawk.Client.EmuHawk
InitialValue = (Settings.AutosaveInterval / 1000).ToString()
};
MainForm.StopSound();
var result = prompt.ShowDialog();
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => prompt.ShowDialog());
if (result.IsOk())
{
uint val = uint.Parse(prompt.PromptText) * 1000;
@ -1188,9 +1178,7 @@ namespace BizHawk.Client.EmuHawk
Message = "Frames per tick:",
InitialValue = TasView.ScrollSpeed.ToString()
};
MainForm.StopSound();
var result = inputPrompt.ShowDialog();
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => inputPrompt.ShowDialog());
if (result == DialogResult.OK)
{
TasView.ScrollSpeed = int.Parse(inputPrompt.PromptText);

View File

@ -791,63 +791,65 @@ namespace BizHawk.Client.EmuHawk
else
{
_autosaveTimer?.Stop();
MainForm.StopSound();
MessageStatusLabel.Text = "Saving...";
Cursor = Cursors.WaitCursor;
Update();
CurrentTasMovie.Save();
if (Settings.AutosaveInterval > 0)
MainForm.DoWithTempMute(() =>
{
_autosaveTimer?.Start();
}
MessageStatusLabel.Text = "Saving...";
Cursor = Cursors.WaitCursor;
Update();
CurrentTasMovie.Save();
if (Settings.AutosaveInterval > 0)
{
_autosaveTimer?.Start();
}
MessageStatusLabel.Text = "File saved.";
Settings.RecentTas.Add(CurrentTasMovie.Filename);
Cursor = Cursors.Default;
MainForm.StartSound();
MessageStatusLabel.Text = "File saved.";
Settings.RecentTas.Add(CurrentTasMovie.Filename);
Cursor = Cursors.Default;
});
}
}
private void SaveAsTas()
{
_autosaveTimer.Stop();
MainForm.StopSound();
ClearLeftMouseStates();
var filename = CurrentTasMovie.Filename;
if (string.IsNullOrWhiteSpace(filename) || filename == DefaultTasProjName())
MainForm.DoWithTempMute(() =>
{
filename = SuggestedTasProjName();
}
ClearLeftMouseStates();
var filename = CurrentTasMovie.Filename;
if (string.IsNullOrWhiteSpace(filename) || filename == DefaultTasProjName())
{
filename = SuggestedTasProjName();
}
var file = SaveFileDialog(
filename,
Config.PathEntries.MovieAbsolutePath(),
"Tas Project Files",
"tasproj",
this
var file = SaveFileDialog(
filename,
Config.PathEntries.MovieAbsolutePath(),
"Tas Project Files",
"tasproj",
this
);
if (file != null)
{
CurrentTasMovie.Filename = file.FullName;
MessageStatusLabel.Text = "Saving...";
Cursor = Cursors.WaitCursor;
Update();
CurrentTasMovie.Save();
Settings.RecentTas.Add(CurrentTasMovie.Filename);
UpdateWindowTitle();
MessageStatusLabel.Text = "File saved.";
Cursor = Cursors.Default;
}
if (file != null)
{
CurrentTasMovie.Filename = file.FullName;
MessageStatusLabel.Text = "Saving...";
Cursor = Cursors.WaitCursor;
Update();
CurrentTasMovie.Save();
Settings.RecentTas.Add(CurrentTasMovie.Filename);
UpdateWindowTitle();
MessageStatusLabel.Text = "File saved.";
Cursor = Cursors.Default;
}
// keep insisting
if (Settings.AutosaveInterval > 0)
{
_autosaveTimer.Start();
}
// keep insisting
if (Settings.AutosaveInterval > 0)
{
_autosaveTimer.Start();
}
MainForm.SetWindowText();
MainForm.StartSound();
MainForm.SetWindowText();
});
}
protected override string WindowTitle

View File

@ -351,9 +351,7 @@ namespace BizHawk.Client.EmuHawk
InitialValue = MaxLines.ToString()
};
MainForm.StopSound();
var result = prompt.ShowHawkDialog(this);
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => prompt.ShowHawkDialog(this));
if (result == DialogResult.OK)
{
var max = int.Parse(prompt.PromptText);
@ -374,9 +372,7 @@ namespace BizHawk.Client.EmuHawk
InitialValue = FileSizeCap.ToString()
};
MainForm.StopSound();
var result = prompt.ShowHawkDialog(this);
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => prompt.ShowHawkDialog(this));
if (result == DialogResult.OK)
{
FileSizeCap = int.Parse(prompt.PromptText);

View File

@ -898,9 +898,7 @@ namespace BizHawk.Client.EmuHawk
Message = "Enter a hexadecimal value"
};
MainForm.StopSound();
var result = prompt.ShowHawkDialog(this);
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => prompt.ShowHawkDialog(this));
while (result.IsOk())
{
try

View File

@ -153,9 +153,7 @@ namespace BizHawk.Client.EmuHawk
{
if (_watches.Changes)
{
MainForm.StopSound();
var result = MessageBox.Show("Save Changes?", "RAM Watch", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => MessageBox.Show("Save Changes?", "RAM Watch", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3));
if (result == DialogResult.Yes)
{
if (string.IsNullOrWhiteSpace(_watches.CurrentFileName))
@ -428,9 +426,7 @@ namespace BizHawk.Client.EmuHawk
TextInputType = InputPrompt.InputType.Text
};
MainForm.StopSound();
var result = inputPrompt.ShowHawkDialog();
MainForm.StartSound();
var result = MainForm.DoWithTempMute(() => inputPrompt.ShowHawkDialog());
if (result == DialogResult.OK)
{