[WIP] Partially working shortcut detection

Fixed the profile validitiy detection on load,
but now have issue with the shortcut detection
This commit is contained in:
Terry MacDonald 2021-03-07 17:11:46 +13:00
parent 1588cd6a0b
commit 4531a0d4d5
6 changed files with 93 additions and 53 deletions

View File

@ -505,6 +505,8 @@ namespace DisplayMagician {
{ {
logger.Debug($"Program/ApplyProfile: Starting"); logger.Debug($"Program/ApplyProfile: Starting");
profile.RefreshPossbility();
// We need to check if the profile is valid // We need to check if the profile is valid
if (!profile.IsPossible) if (!profile.IsPossible)
{ {
@ -514,7 +516,7 @@ namespace DisplayMagician {
// We need to check if the profile is the same one that we're on // We need to check if the profile is the same one that we're on
if (profile.IsActive) if (profile.UUID == ProfileRepository.GetActiveProfile().UUID)
{ {
logger.Debug($"Program/ApplyProfile: The supplied profile {profile.Name} is currently in use, so we don't need to apply it."); logger.Debug($"Program/ApplyProfile: The supplied profile {profile.Name} is currently in use, so we don't need to apply it.");
return false; return false;
@ -655,6 +657,8 @@ namespace DisplayMagician {
} }
} }
ProfileRepository.UpdateActiveProfile();
return true; return true;
} }

View File

@ -1963,6 +1963,7 @@ namespace DisplayMagician
// And that we have enough to try and action within the shortcut // And that we have enough to try and action within the shortcut
// (in other words check everything in the shortcut is still valid) // (in other words check everything in the shortcut is still valid)
Errors.Clear();
ShortcutValidity worstError = ShortcutValidity.Valid; ShortcutValidity worstError = ShortcutValidity.Valid;
// Does the profile we want to Use still exist? // Does the profile we want to Use still exist?
@ -1977,7 +1978,7 @@ namespace DisplayMagician
worstError = ShortcutValidity.Error; worstError = ShortcutValidity.Error;
} }
// Is the profile still valid right now? i.e. are all the screens available? // Is the profile still valid right now? i.e. are all the screens available?
if (ProfileToUse != null && !ProfileToUse.IsPossible) if (ProfileToUse != null && !ProfileToUse.IsValid())
{ {
ShortcutError error = new ShortcutError(); ShortcutError error = new ShortcutError();
error.Name = "InvalidProfile"; error.Name = "InvalidProfile";

View File

@ -606,19 +606,21 @@ namespace DisplayMagician
} }
// record the old audio device // record the old audio device
bool needToChangeAudio = false; bool needToChangeAudioDevice = false;
CoreAudioDevice rollbackAudioDevice = _audioController.DefaultPlaybackDevice; CoreAudioDevice rollbackAudioDevice = _audioController.DefaultPlaybackDevice;
double rollbackAudioVolume = 50; double rollbackAudioVolume = 50;
if (rollbackAudioDevice != null) if (rollbackAudioDevice != null)
{
rollbackAudioVolume = _audioController.DefaultPlaybackDevice.Volume; rollbackAudioVolume = _audioController.DefaultPlaybackDevice.Volume;
if (!rollbackAudioDevice.FullName.Equals(shortcutToUse.AudioDevice)) if (!rollbackAudioDevice.FullName.Equals(shortcutToUse.AudioDevice))
{ {
logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.AudioDevice} audio device."); logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.AudioDevice} audio device.");
needToChangeAudio = true; needToChangeAudioDevice = true;
} }
else else
{ {
logger.Debug($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.AudioDevice} audio device so no need to change audio devices."); logger.Debug($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.AudioDevice} audio device so no need to change audio devices.");
}
} }
// Change Audio Device (if one specified) // Change Audio Device (if one specified)
@ -654,16 +656,19 @@ namespace DisplayMagician
CoreAudioDevice rollbackCaptureDevice = _audioController.DefaultCaptureDevice; CoreAudioDevice rollbackCaptureDevice = _audioController.DefaultCaptureDevice;
double rollbackCaptureVolume = 50; double rollbackCaptureVolume = 50;
if (rollbackCaptureDevice != null) if (rollbackCaptureDevice != null)
{
rollbackCaptureVolume = _audioController.DefaultCaptureDevice.Volume; rollbackCaptureVolume = _audioController.DefaultCaptureDevice.Volume;
if (!rollbackCaptureDevice.FullName.Equals(shortcutToUse.CaptureDevice)) if (!rollbackCaptureDevice.FullName.Equals(shortcutToUse.CaptureDevice))
{ {
logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.CaptureDevice} capture (microphone) device."); logger.Debug($"ShortcutRepository/RunShortcut: We need to change to the {shortcutToUse.CaptureDevice} capture (microphone) device.");
needToChangeCaptureDevice = true; needToChangeCaptureDevice = true;
} }
else else
{ {
logger.Debug($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.CaptureDevice} capture (microphone) device so no need to change capture devices."); logger.Debug($"ShortcutRepository/RunShortcut: We're already using the {shortcutToUse.CaptureDevice} capture (microphone) device so no need to change capture devices.");
}
} }
// Change capture Device (if one specified) // Change capture Device (if one specified)
if (shortcutToUse.ChangeCaptureDevice) if (shortcutToUse.ChangeCaptureDevice)
{ {
@ -1325,7 +1330,7 @@ namespace DisplayMagician
} }
// Change Audio Device back (if one specified) // Change Audio Device back (if one specified)
if (needToChangeAudio) if (needToChangeAudioDevice)
{ {
logger.Debug($"ShortcutRepository/RunShortcut: Reverting default audio back to {rollbackAudioDevice.Name} audio device"); logger.Debug($"ShortcutRepository/RunShortcut: Reverting default audio back to {rollbackAudioDevice.Name} audio device");
// use the Audio Device // use the Audio Device
@ -1347,7 +1352,7 @@ namespace DisplayMagician
// Change Capture Device back (if one specified) // Change Capture Device back (if one specified)
if (needToChangeCaptureDevice) if (needToChangeCaptureDevice)
{ {
logger.Debug($"ShortcutRepository/RunShortcut: Reverting default capture (microphone) device back to {rollbackAudioDevice.Name} capture device"); logger.Debug($"ShortcutRepository/RunShortcut: Reverting default capture (microphone) device back to {rollbackCaptureDevice.Name} capture device");
// use the Audio Device // use the Audio Device
rollbackCaptureDevice.SetAsDefault(); rollbackCaptureDevice.SetAsDefault();

View File

@ -213,7 +213,12 @@ namespace DisplayMagician.UIForms
// Add the current slist of profiles into the NotifyIcon context menu // Add the current slist of profiles into the NotifyIcon context menu
foreach (ProfileItem profile in ProfileRepository.AllProfiles) foreach (ProfileItem profile in ProfileRepository.AllProfiles)
{ {
profileToolStripMenuItem.DropDownItems.Add(profile.Name,profile.ProfileBitmap, runProfileToolStripMenuItem_Click); ToolStripMenuItem profileMenuItem = new ToolStripMenuItem(profile.Name, profile.ProfileBitmap, runProfileToolStripMenuItem_Click);
if (profile.IsActive || !profile.IsPossible)
profileMenuItem.Enabled = false;
else
profileMenuItem.Enabled = true;
profileToolStripMenuItem.DropDownItems.Add(profileMenuItem);
} }
// Clear all the shortcuts // Clear all the shortcuts
@ -229,7 +234,12 @@ namespace DisplayMagician.UIForms
// Add the current list of profiles into the NotifyIcon context menu // Add the current list of profiles into the NotifyIcon context menu
foreach (ShortcutItem shortcut in ShortcutRepository.AllShortcuts) foreach (ShortcutItem shortcut in ShortcutRepository.AllShortcuts)
{ {
shortcutToolStripMenuItem.DropDownItems.Add(shortcut.Name,shortcut.ShortcutBitmap, runShortcutToolStripMenuItem_Click); ToolStripMenuItem shortcutMenuItem = new ToolStripMenuItem(shortcut.Name, shortcut.ShortcutBitmap, runShortcutToolStripMenuItem_Click);
if (shortcut.IsValid == ShortcutValidity.Warning || shortcut.IsValid == ShortcutValidity.Error)
shortcutMenuItem.Enabled = false;
else
shortcutMenuItem.Enabled = true;
shortcutToolStripMenuItem.DropDownItems.Add(shortcutMenuItem);
} }
} }

View File

@ -433,7 +433,28 @@ namespace DisplayMagicianShared
return shortcutFileName != null && System.IO.File.Exists(shortcutFileName); return shortcutFileName != null && System.IO.File.Exists(shortcutFileName);
} }
public void RefreshPossbility()
{
// Check each display in this profile and make sure it's currently available
int validDisplayCount = 0;
foreach (string profileDisplayIdentifier in ProfileDisplayIdentifiers)
{
// If this profile has a display that isn't currently available then we need to say it's a no!
if (ProfileRepository.ConnectedDisplayIdentifiers.Contains(profileDisplayIdentifier))
validDisplayCount++;
}
if (validDisplayCount == ProfileDisplayIdentifiers.Count)
{
SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The profile {Name} is possible!");
_isPossible = true;
}
else
{
SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The profile {Name} is NOT possible!");
_isPossible = false;
}
}
} }
// Custom Equality comparer for the Profile class // Custom Equality comparer for the Profile class

View File

@ -25,6 +25,7 @@ namespace DisplayMagicianShared
private static bool _profilesLoaded = false; private static bool _profilesLoaded = false;
public static Version _version = new Version(1, 0, 0); public static Version _version = new Version(1, 0, 0);
private static ProfileItem _currentProfile; private static ProfileItem _currentProfile;
private static List<string> _connectedDisplayIdentifiers = new List<string>();
// Other constants that are useful // Other constants that are useful
public static string AppDataPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DisplayMagician"); public static string AppDataPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DisplayMagician");
@ -135,6 +136,25 @@ namespace DisplayMagicianShared
} }
} }
public static List<string> ConnectedDisplayIdentifiers
{
get
{
if (_connectedDisplayIdentifiers.Count == 0)
// Load the Profiles from storage if they need to be
_connectedDisplayIdentifiers = GenerateAllAvailableDisplayIdentifiers();
return _connectedDisplayIdentifiers;
}
set
{
_connectedDisplayIdentifiers = value;
}
}
#endregion #endregion
#region Class Methods #region Class Methods
@ -611,6 +631,9 @@ namespace DisplayMagicianShared
UpdateActiveProfile(); UpdateActiveProfile();
} }
_profilesLoaded = true; _profilesLoaded = true;
IsPossibleRefresh();
return true; return true;
} }
@ -700,39 +723,15 @@ namespace DisplayMagicianShared
{ {
// We need to refresh the cached answer // We need to refresh the cached answer
// Get the list of connected devices // Get the list of connected devices
List<string> connectedDisplayIdentifiers = GenerateAllAvailableDisplayIdentifiers(); ConnectedDisplayIdentifiers = GenerateAllAvailableDisplayIdentifiers();
if (_profilesLoaded && _allProfiles.Count > 0) if (_profilesLoaded && _allProfiles.Count > 0)
{ {
_profileWarningLookup.Clear();
foreach (ProfileItem loadedProfile in AllProfiles) foreach (ProfileItem loadedProfile in AllProfiles)
{ loadedProfile.RefreshPossbility();
// Check each display in this profile and make sure it's currently available
int validDisplayCount = 0;
foreach (string profileDisplayIdentifier in loadedProfile.ProfileDisplayIdentifiers)
{
// If this profile has a display that isn't currently available then we need to say it's a no!
if (connectedDisplayIdentifiers.Contains(profileDisplayIdentifier))
validDisplayCount++;
}
if (validDisplayCount == loadedProfile.ProfileDisplayIdentifiers.Count)
{
SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The profile {loadedProfile.Name} is possible!");
loadedProfile.IsPossible = true;
_profileWarningLookup[loadedProfile.Name] = false;
}
else
{
SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The profile {loadedProfile.Name} is NOT possible!");
loadedProfile.IsPossible = false;
_profileWarningLookup[loadedProfile.Name] = true;
}
}
} }
} }