[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");
profile.RefreshPossbility();
// We need to check if the profile is valid
if (!profile.IsPossible)
{
@ -514,7 +516,7 @@ namespace DisplayMagician {
// 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.");
return false;
@ -655,6 +657,8 @@ namespace DisplayMagician {
}
}
ProfileRepository.UpdateActiveProfile();
return true;
}

View File

@ -1963,6 +1963,7 @@ namespace DisplayMagician
// And that we have enough to try and action within the shortcut
// (in other words check everything in the shortcut is still valid)
Errors.Clear();
ShortcutValidity worstError = ShortcutValidity.Valid;
// Does the profile we want to Use still exist?
@ -1977,7 +1978,7 @@ namespace DisplayMagician
worstError = ShortcutValidity.Error;
}
// 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();
error.Name = "InvalidProfile";

View File

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

View File

@ -213,7 +213,12 @@ namespace DisplayMagician.UIForms
// Add the current slist of profiles into the NotifyIcon context menu
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
@ -229,7 +234,12 @@ namespace DisplayMagician.UIForms
// Add the current list of profiles into the NotifyIcon context menu
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);
}
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

View File

@ -25,6 +25,7 @@ namespace DisplayMagicianShared
private static bool _profilesLoaded = false;
public static Version _version = new Version(1, 0, 0);
private static ProfileItem _currentProfile;
private static List<string> _connectedDisplayIdentifiers = new List<string>();
// Other constants that are useful
public static string AppDataPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DisplayMagician");
@ -135,8 +136,27 @@ 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
#region Class Methods
public static bool AddProfile(ProfileItem profile)
{
@ -611,6 +631,9 @@ namespace DisplayMagicianShared
UpdateActiveProfile();
}
_profilesLoaded = true;
IsPossibleRefresh();
return true;
}
@ -700,39 +723,15 @@ namespace DisplayMagicianShared
{
// We need to refresh the cached answer
// Get the list of connected devices
List<string> connectedDisplayIdentifiers = GenerateAllAvailableDisplayIdentifiers();
ConnectedDisplayIdentifiers = GenerateAllAvailableDisplayIdentifiers();
if (_profilesLoaded && _allProfiles.Count > 0)
{
_profileWarningLookup.Clear();
foreach (ProfileItem loadedProfile in AllProfiles)
{
// 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;
}
}
loadedProfile.RefreshPossbility();
}
}