Make red error when sc profile gone

This will inform the user to choose another
profile when the profile is used in a shortcut
and yet the profile doesn't exist any longer
This commit is contained in:
Terry MacDonald 2021-03-06 17:43:07 +13:00
parent cac7810ed5
commit 07ceed3ec4
6 changed files with 122 additions and 59 deletions

View File

@ -30,6 +30,15 @@ namespace DisplayMagician
NoGame,
}
public enum ShortcutValidity
{
Valid,
Warning,
Error,
}
public struct StartProgram
{
public int Priority;
@ -1926,8 +1935,7 @@ namespace DisplayMagician
return multiIcon;
}
public (bool,string) IsValid()
public (ShortcutValidity, string) IsValid()
{
// Do some validation checks to make sure the shortcut is sensible
// And that we have enough to try and action within the shortcut
@ -1937,11 +1945,11 @@ namespace DisplayMagician
// Is the profile still valid right now? i.e. are all the screens available?
if (ProfileToUse == null)
{
return (false,string.Format("The profile does not exist (probably deleted) and cannot be used."));
return (ShortcutValidity.Error, string.Format("The profile does not exist (probably deleted) and cannot be used."));
}
if (!ProfileToUse.IsPossible)
{
return (false, string.Format("The profile '{0}' is not valid right now and cannot be used.", ProfileToUse.Name));
return (ShortcutValidity.Warning, string.Format("The profile '{0}' is not valid right now and cannot be used.", ProfileToUse.Name));
}
// Is the main application still installed?
if (Category.Equals(ShortcutCategory.Application))
@ -1949,10 +1957,11 @@ namespace DisplayMagician
// We need to check if the Application still exists
if (!System.IO.File.Exists(ExecutableNameAndPath))
{
return (false, string.Format("The application executable '{0}' does not exist, or cannot be accessed by DisplayMagician.", ExecutableNameAndPath));
return (ShortcutValidity.Warning, string.Format("The application executable '{0}' does not exist, or cannot be accessed by DisplayMagician.", ExecutableNameAndPath));
}
} else if (Category.Equals(ShortcutCategory.Game))
}
else if (Category.Equals(ShortcutCategory.Game))
{
// If the game is a Steam Game we check for that
if (GameLibrary.Equals(SupportedGameLibrary.Steam))
@ -1962,13 +1971,13 @@ namespace DisplayMagician
// Check if Steam is installed and error if it isn't
if (!SteamLibrary.IsSteamInstalled)
{
return (false, Language.Steam_executable_file_not_found);
return (ShortcutValidity.Error, Language.Steam_executable_file_not_found);
}
// We need to look up details about the game
if (!SteamLibrary.ContainsSteamGame(GameAppId))
{
return (false, string.Format("The Steam Game with AppID '{0}' is not installed on this computer.", GameAppId));
return (ShortcutValidity.Error, string.Format("The Steam Game with AppID '{0}' is not installed on this computer.", GameAppId));
}
}
// If the game is a Uplay Game we check for that
@ -1978,13 +1987,13 @@ namespace DisplayMagician
// Check if Steam is installed and error if it isn't
if (!UplayLibrary.IsUplayInstalled)
{
return (false, "Cannot find the Uplay executable! Uplay doesn't appear to be installed");
return (ShortcutValidity.Error, "Cannot find the Uplay executable! Uplay doesn't appear to be installed");
}
// We need to look up details about the game
if (!UplayLibrary.ContainsUplayGame(GameAppId))
{
return (false, string.Format("The Uplay Game with AppID '{0}' is not installed on this computer.", GameAppId));
return (ShortcutValidity.Error, string.Format("The Uplay Game with AppID '{0}' is not installed on this computer.", GameAppId));
}
}
@ -2001,11 +2010,11 @@ namespace DisplayMagician
if (audioDevice.FullName.Equals(AudioDevice))
{
if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Disabled)
return (false, $"The Audio Device {AudioDevice} is disabled, so the shortcut '{Name}' cannot be used. You need to enable the audio device to use this shortcut, or edit the shortcut to change the audio device.");
return (ShortcutValidity.Warning, $"The Audio Device {AudioDevice} is disabled, so the shortcut '{Name}' cannot be used. You need to enable the audio device to use this shortcut, or edit the shortcut to change the audio device.");
if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.NotPresent)
return (false, $"The Audio Device {AudioDevice} is not present, so the shortcut '{Name}' cannot be used.");
return (ShortcutValidity.Warning, $"The Audio Device {AudioDevice} is not present, so the shortcut '{Name}' cannot be used.");
if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Unplugged)
return (false, $"The Audio Device {AudioDevice} is unplugged, so the shortcut '{Name}' cannot be used. You need to plug in the audio device to use this shortcut, or edit the shortcut to change the audio device.");
return (ShortcutValidity.Warning, $"The Audio Device {AudioDevice} is unplugged, so the shortcut '{Name}' cannot be used. You need to plug in the audio device to use this shortcut, or edit the shortcut to change the audio device.");
}
}
}
@ -2019,21 +2028,22 @@ namespace DisplayMagician
if (captureDevice.FullName.Equals(CaptureDevice))
{
if (captureDevice.State == AudioSwitcher.AudioApi.DeviceState.Disabled)
return (false, $"The Capture Device {CaptureDevice} is disabled, so the shortcut '{Name}' cannot be used. You need to enable the capture device to use this shortcut, or edit the shortcut to change the capture device.");
return (ShortcutValidity.Warning, $"The Capture Device {CaptureDevice} is disabled, so the shortcut '{Name}' cannot be used. You need to enable the capture device to use this shortcut, or edit the shortcut to change the capture device.");
if (captureDevice.State == AudioSwitcher.AudioApi.DeviceState.NotPresent)
return (false, $"The Capture Device {CaptureDevice} is not present, so the shortcut '{Name}' cannot be used.");
return (ShortcutValidity.Warning, $"The Capture Device {CaptureDevice} is not present, so the shortcut '{Name}' cannot be used.");
if (captureDevice.State == AudioSwitcher.AudioApi.DeviceState.Unplugged)
return (false, $"The Capture Device {CaptureDevice} is unplugged, so the shortcut '{Name}' cannot be used. You need to plug in the capture device to use this shortcut, or edit the shortcut to change the capture device.");
return (ShortcutValidity.Warning, $"The Capture Device {CaptureDevice} is unplugged, so the shortcut '{Name}' cannot be used. You need to plug in the capture device to use this shortcut, or edit the shortcut to change the capture device.");
}
}
}
// TODO Do all the specified pre-start apps still exist?
return (true, "Shortcut is valid");
return (ShortcutValidity.Valid, "Shortcut is valid");
}
// ReSharper disable once FunctionComplexityOverflow
// ReSharper disable once CyclomaticComplexity
public bool CreateShortcut(string shortcutFileName)

View File

@ -26,7 +26,8 @@ namespace DisplayMagician
#region Class Variables
// Common items to the class
private static List<ShortcutItem> _allShortcuts = new List<ShortcutItem>();
public static Dictionary<string, bool> _shortcutValidityLookup = new Dictionary<string, bool>();
public static Dictionary<string, bool> _shortcutWarningLookup = new Dictionary<string, bool>();
public static Dictionary<string, bool> _shortcutErrorLookup = new Dictionary<string, bool>();
private static bool _shortcutsLoaded = false;
// Other constants that are useful
private static string AppShortcutStoragePath = Path.Combine(Program.AppDataPath, $"Shortcuts");
@ -74,6 +75,8 @@ namespace DisplayMagician
// Load the Shortcuts from storage
LoadShortcuts();
IsValidRefresh();
}
#endregion
@ -91,7 +94,7 @@ namespace DisplayMagician
}
}
public static Dictionary<string, bool> ShortcutValidityLookup
public static Dictionary<string, bool> ShortcutWarningLookup
{
get
{
@ -99,10 +102,21 @@ namespace DisplayMagician
// Load the Shortcuts from storage if they need to be
LoadShortcuts();
return _shortcutValidityLookup;
return _shortcutWarningLookup;
}
}
public static Dictionary<string, bool> ShortcutErrorLookup
{
get
{
if (!_shortcutsLoaded)
// Load the Shortcuts from storage if they need to be
LoadShortcuts();
return _shortcutErrorLookup;
}
}
public static int ShortcutCount
{
@ -457,6 +471,8 @@ namespace DisplayMagician
logger.Debug($"ShortcutRepository/LoadShortcuts: Couldn't find the {_shortcutStorageJsonFileName} shortcut JSON file that contains the Shortcuts");
}
_shortcutsLoaded = true;
IsValidRefresh();
return true;
}
@ -523,6 +539,28 @@ namespace DisplayMagician
return false;
}
public static void IsValidRefresh()
{
// We need to refresh the cached answer
// Get the list of connected devices
_shortcutWarningLookup.Clear();
_shortcutErrorLookup.Clear();
foreach (ShortcutItem loadedShortcut in AllShortcuts)
{
_shortcutWarningLookup[loadedShortcut.Name] = false;
_shortcutErrorLookup[loadedShortcut.Name] = false;
(ShortcutValidity result, string thing) = (loadedShortcut.IsValid());
if (result == ShortcutValidity.Warning)
ShortcutWarningLookup[loadedShortcut.Name] = true;
if (result == ShortcutValidity.Error)
ShortcutErrorLookup[loadedShortcut.Name] = true;
}
}
// ReSharper disable once CyclomaticComplexity
public static void RunShortcut(ShortcutItem shortcutToUse, NotifyIcon notifyIcon = null)
{
@ -535,8 +573,8 @@ namespace DisplayMagician
if (!(shortcutToUse is ShortcutItem))
return;
(bool valid, string reason) = shortcutToUse.IsValid();
if (!valid)
(ShortcutValidity valid, string reason) = shortcutToUse.IsValid();
if (valid == ShortcutValidity.Error || valid == ShortcutValidity.Warning)
{
logger.Error($"ShortcutRepository/RunShortcut: Cannot run the shortcut {shortcutToUse.Name} as it isn't valid");
MessageBox.Show(

View File

@ -96,14 +96,20 @@ namespace DisplayMagician.UIForms
{
Rectangle pos = Utility.GetSizedImageBounds(img, new Rectangle(bounds.Location + itemPadding, ImageListView.ThumbnailSize));
if (ShortcutRepository.ShortcutValidityLookup[item.Text])
if (ShortcutRepository.ShortcutErrorLookup[item.Text])
{
// Draw the full color image as the shortcuts is not invalid
g.DrawImage(img, pos);
// The shortcut is permanently invalid (game removed or profile deleted)
// so we make the image grayscale
Image grayImg = ImageUtils.MakeGrayscale(img);
g.DrawImage(grayImg, pos);
// Draw a warning triangle over it
// right in the centre
g.DrawImage(Properties.Resources.Error, pos.X + 30, pos.Y + 30, 40, 40);
}
else
else if (ShortcutRepository.ShortcutWarningLookup[item.Text])
{
// THe shortcut is invalid
// The shortcut is temporaily invalid (e.g. screens aren't right at the moment)
// so we make the image grayscale
Image grayImg = ImageUtils.MakeGrayscale(img);
g.DrawImage(grayImg, pos);
@ -112,6 +118,12 @@ namespace DisplayMagician.UIForms
// right in the centre
g.DrawImage(Properties.Resources.Warning, pos.X + 30, pos.Y + 30, 40, 40);
}
else
{
// Draw the full color image as the shortcut is fine!
g.DrawImage(img, pos);
}
// Draw image border
if (Math.Min(pos.Width, pos.Height) > 32)
@ -254,12 +266,7 @@ namespace DisplayMagician.UIForms
{
Rectangle pos = Utility.GetSizedImageBounds(img, new Rectangle(bounds.Location + itemPadding, ImageListView.ThumbnailSize));
if (ProfileRepository.ProfileValidityLookup[item.Text])
{
// Draw the full color image as the shortcuts is not invalid
g.DrawImage(img, pos);
}
else
if (ProfileRepository.ProfileWarningLookup[item.Text])
{
// THe shortcut is invalid
// so we make the image grayscale
@ -270,6 +277,11 @@ namespace DisplayMagician.UIForms
// right in the centre
g.DrawImage(Properties.Resources.Warning, pos.X + 30, pos.Y + 30, 40, 40);
}
else
{
// Draw the full color image as the shortcuts is not invalid
g.DrawImage(img, pos);
}
// Draw image border
if (Math.Min(pos.Width, pos.Height) > 32)

View File

@ -848,6 +848,16 @@ namespace DisplayMagician.UIForms
}
}
if (!foundChosenProfileInLoadedProfiles && !String.IsNullOrWhiteSpace(_shortcutToEdit.ProfileUUID))
{
MessageBox.Show(
@"The Display Profile used by this Shortcut no longer exists and cannot be used. You need to choose a new Display Profile for this Shortcut.",
@"Display Profile no longer exists",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
// If we get to the end of the loaded profiles and haven't
// found a matching profile, then we need to show the current profile
// that we're running now
@ -878,13 +888,9 @@ namespace DisplayMagician.UIForms
}
MessageBox.Show(
@"The Display Profile used by this Shortcut no longer exists and cannot be used. You need to choose a new Display Profile for this Shortcut. We have selected the current Display Profile, but you can choose another profile if you wish.",
@"Display Profile no longer exists",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
// Now start populating the other fields if they need it
_uuid = _shortcutToEdit.UUID;

View File

@ -60,7 +60,7 @@ namespace DisplayMagician.UIForms
ImageListViewItem newItem = null;
ilv_saved_shortcuts.Items.Clear();
ShortcutRepository.ShortcutValidityLookup.Clear();
ShortcutRepository.IsValidRefresh();
foreach (ShortcutItem loadedShortcut in ShortcutRepository.AllShortcuts.OrderBy(s => s.Name))
{
@ -70,9 +70,6 @@ namespace DisplayMagician.UIForms
if (_selectedShortcut is ShortcutItem && _selectedShortcut.Equals(loadedShortcut))
newItem.Selected = true;
(bool result, string thing) = loadedShortcut.IsValid();
ShortcutRepository.ShortcutValidityLookup[loadedShortcut.Name] = result;
//ilv_saved_profiles.Items.Add(newItem);
ilv_saved_shortcuts.Items.Add(newItem, _shortcutAdaptor);
}
@ -98,9 +95,9 @@ namespace DisplayMagician.UIForms
// if shortcut is not valid then ask if the user
// really wants to save it to desktop
(bool result, string validityMessage) = _selectedShortcut.IsValid();
if (!result)
{
(ShortcutValidity valid, string reason) = _selectedShortcut.IsValid();
if (valid == ShortcutValidity.Error || valid == ShortcutValidity.Warning)
{
// We ask the user of they still want to save the desktop shortcut
if (MessageBox.Show($"The shortcut '{_selectedShortcut.Name}' isn't valid for some reason so a desktop shortcut wouldn't work until the shortcut is fixed. Has your hardware or screen layout changed from when the shortcut was made? We recommend that you edit the shortcut to make it valid again, or reverse the hardware changes you made. Do you still want to save the desktop shortcut?", $"Still save the '{_selectedShortcut.Name}' Desktop Shortcut?", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.No)
return;
@ -188,7 +185,7 @@ namespace DisplayMagician.UIForms
if (_selectedShortcut == null)
return;
if (!ShortcutRepository.ShortcutValidityLookup[_selectedShortcut.Name])
if (!ShortcutRepository.ShortcutWarningLookup[_selectedShortcut.Name])
return;
// Run the selected shortcut
@ -197,16 +194,16 @@ namespace DisplayMagician.UIForms
private void SetRunOption()
{
if (ShortcutRepository.ShortcutValidityLookup[_selectedShortcut.Name])
if (ShortcutRepository.ShortcutWarningLookup[_selectedShortcut.Name] || ShortcutRepository.ShortcutErrorLookup[_selectedShortcut.Name])
{
btn_run.Visible = true;
cms_shortcuts.Items[1].Enabled = true;
btn_run.Visible = false;
cms_shortcuts.Items[1].Enabled = false;
}
else
{
btn_run.Visible = false;
cms_shortcuts.Items[1].Enabled = false;
btn_run.Visible = true;
cms_shortcuts.Items[1].Enabled = true;
}
}
@ -276,8 +273,8 @@ namespace DisplayMagician.UIForms
return;
// Only run the if shortcut is valid
(bool result, string validityMessage) = _selectedShortcut.IsValid();
if (!result)
(ShortcutValidity valid, string reason) = _selectedShortcut.IsValid();
if (valid == ShortcutValidity.Error || valid == ShortcutValidity.Warning)
{
// We tell the user the reason that we couldnt run the shortcut
if (MessageBox.Show($"The shortcut '{_selectedShortcut.Name}' isn't valid for some reason so we cannot run the application or game. Has your hardware or screen layout changed from when the shortcut was made? We recommend that you edit the shortcut to make it valid again, or reverse the hardware changes you made. Do you want to do that now?", $"Edit the '{_selectedShortcut.Name}' Shortcut?", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.No)

View File

@ -21,7 +21,7 @@ namespace DisplayMagicianShared
#region Class Variables
// Common items to the class
private static List<ProfileItem> _allProfiles = new List<ProfileItem>();
public static Dictionary<string, bool> _profileValidityLookup = new Dictionary<string, bool>();
public static Dictionary<string, bool> _profileWarningLookup = new Dictionary<string, bool>();
private static bool _profilesLoaded = false;
public static Version _version = new Version(1, 0, 0);
private static ProfileItem _currentProfile;
@ -90,7 +90,7 @@ namespace DisplayMagicianShared
}
}
public static Dictionary<string, bool> ProfileValidityLookup
public static Dictionary<string, bool> ProfileWarningLookup
{
get
{
@ -98,7 +98,7 @@ namespace DisplayMagicianShared
// Load the Profiles from storage if they need to be
LoadProfiles();
return _profileValidityLookup;
return _profileWarningLookup;
}
}
@ -705,7 +705,7 @@ namespace DisplayMagicianShared
if (_profilesLoaded && _allProfiles.Count > 0)
{
_profileValidityLookup.Clear();
_profileWarningLookup.Clear();
foreach (ProfileItem loadedProfile in AllProfiles)
{
@ -722,14 +722,14 @@ namespace DisplayMagicianShared
{
SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The profile {loadedProfile.Name} is possible!");
loadedProfile.IsPossible = true;
_profileValidityLookup[loadedProfile.Name] = true;
_profileWarningLookup[loadedProfile.Name] = false;
}
else
{
SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The profile {loadedProfile.Name} is NOT possible!");
loadedProfile.IsPossible = false;
_profileValidityLookup[loadedProfile.Name] = false;
_profileWarningLookup[loadedProfile.Name] = true;
}
}