Still vainly trying to get Equals to work

I am a lot closer at the moment, but there is some bit of logic matching that is escaping me. I think I'm just too tired at the moment, and I need a sleep. I bet this will seem simple in the morning.
This commit is contained in:
Terry MacDonald 2021-09-06 22:08:22 +12:00
parent 0c32b206c0
commit c59f236524
6 changed files with 212 additions and 210 deletions

View File

@ -202,12 +202,9 @@ namespace DisplayMagician.UIForms
// if the item was removed from the list during this // if the item was removed from the list during this
// list refresh, then we select this profile only if it // list refresh, then we select this profile only if it
// is the currently used Profile // is the currently used Profile
if (_selectedProfile is ProfileItem && _selectedProfile.Equals(profile)) if (ProfileRepository.IsActiveProfile(_selectedProfile))
newItem.Selected = true; newItem.Selected = true;
//ProfileRepository.ProfileValidityLookup[profile.Name] = profile.IsPossible;
// Add it to the list! // Add it to the list!
ilv_saved_profiles.Items.Add(newItem, _profileAdaptor); ilv_saved_profiles.Items.Add(newItem, _profileAdaptor);
@ -351,15 +348,12 @@ namespace DisplayMagician.UIForms
// We're in 'save' mode! // We're in 'save' mode!
// Check we're not already saving this profile // Check we're not already saving this profile
foreach (ProfileItem savedProfile in ProfileRepository.AllProfiles) string previouslySavedProfileName = "";
if (ProfileRepository.ContainsCurrentProfile(out previouslySavedProfileName))
{ {
//if (String.Equals(txt_profile_save_name.Text, savedProfile.Name, StringComparison.InvariantCultureIgnoreCase)) MessageBox.Show($"Sorry, this display profile was already saved as '{previouslySavedProfileName}'.", "Profile already saved", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (savedProfile.Equals(_selectedProfile))
{
MessageBox.Show($"Sorry, this display profile was already saved as '{savedProfile.Name}'.", "Profile already saved", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
}
// So we've already passed the check that says this profile is unique // So we've already passed the check that says this profile is unique
@ -434,10 +428,10 @@ namespace DisplayMagician.UIForms
private void btn_view_current_Click(object sender, EventArgs e) private void btn_view_current_Click(object sender, EventArgs e)
{ {
// Reload the profiles in case we swapped to another program to change it
ProfileRepository.GetActiveProfile();
// Refresh the profiles to see whats valid // Refresh the profiles to see whats valid
ProfileRepository.IsPossibleRefresh(); ProfileRepository.IsPossibleRefresh();
// Reload the profiles in case we swapped to another program to change it
ProfileRepository.UpdateActiveProfile();
// Change to the current selected Profile // Change to the current selected Profile
ChangeSelectedProfile(ProfileRepository.GetActiveProfile()); ChangeSelectedProfile(ProfileRepository.GetActiveProfile());
// Refresh the Profile UI // Refresh the Profile UI
@ -460,7 +454,7 @@ namespace DisplayMagician.UIForms
switch (m.Msg) switch (m.Msg)
{ {
case WM_DISPLAYCHANGE: case WM_DISPLAYCHANGE:
ProfileRepository.UpdateActiveProfile(); btn_view_current.PerformClick();
break; break;
} }

View File

@ -12,7 +12,7 @@ using System.Drawing.Imaging;
namespace DisplayMagicianShared.AMD namespace DisplayMagicianShared.AMD
{ {
public class AMDProfileItem : ProfileItem public class AMDProfileItem : ProfileItem, IEquatable<AMDProfileItem>
{ {
private static List<AMDProfileItem> _allSavedProfiles = new List<AMDProfileItem>(); private static List<AMDProfileItem> _allSavedProfiles = new List<AMDProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -364,51 +364,46 @@ namespace DisplayMagicianShared.AMD
return _screens; return _screens;
} }
/*public override int CompareTo(object obj) // The object specific Equals
public bool Equals(AMDProfileItem other)
{ {
if (!(obj is AMDProfileItem)) throw new ArgumentException("Object to CompareTo is not a AMDProfileItem"); ; // Check references
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
AMDProfileItem otherProfile = (AMDProfileItem)obj; // Check the object fields
return this.Name.CompareTo(otherProfile.Name); return base.Equals(other) &&
}*/ AMDDisplayConfig == other.AMDDisplayConfig &&
WindowsDisplayConfig == other.WindowsDisplayConfig;
// The public override for the Object.Equals
public override bool Equals(object obj)
{
return EqualsDerived(obj) &&
obj.GetType() == typeof(AMDProfileItem);
} }
// Profiles are equal if their Viewports are equal // The public override for the Object.Equals
public override bool EqualsDerived(object obj) public bool Equals(Object obj)
{ {
return base.EqualsDerived(obj) && // Check references
!object.ReferenceEquals(obj, null) && if (ReferenceEquals(null, obj)) return false;
obj is AMDProfileItem && if (ReferenceEquals(this, obj)) return true;
((AMDProfileItem)obj).AMDDisplayConfig == this.AMDDisplayConfig && // If different types then can't be true
((AMDProfileItem)obj).WindowsDisplayConfig == this.WindowsDisplayConfig; if (obj.GetType() == this.GetType()) return false;
// Check the object fields as this must the same object as obj, and we need to test in more detail
return Equals((AMDProfileItem)obj);
} }
public override int GetHashCode() public override int GetHashCode()
{ {
// Calculate the hash code for the product. // Calculate the hash code for the product.
return (AMDDisplayConfig, WindowsDisplayConfig, ProfileDisplayIdentifiers).GetHashCode(); return (base.GetHashCode(), AMDDisplayConfig, WindowsDisplayConfig).GetHashCode();
} }
public static bool operator ==(AMDProfileItem lhs, AMDProfileItem rhs) public static bool operator ==(AMDProfileItem lhs, AMDProfileItem rhs)
{ {
if (object.ReferenceEquals(lhs, rhs)) return Equals(lhs, rhs);
return true;
if (!object.ReferenceEquals(lhs, null) &&
!object.ReferenceEquals(rhs, null) &&
lhs.Equals(rhs))
return true;
return false;
} }
public static bool operator !=(AMDProfileItem lhs, AMDProfileItem rhs) => !(lhs == rhs); public static bool operator !=(AMDProfileItem lhs, AMDProfileItem rhs)
{
return !Equals(lhs, rhs);
}
} }
} }

View File

@ -14,7 +14,7 @@ using DisplayMagicianShared.Windows;
namespace DisplayMagicianShared.NVIDIA namespace DisplayMagicianShared.NVIDIA
{ {
public class NVIDIAProfileItem : ProfileItem public class NVIDIAProfileItem : ProfileItem, IEquatable<NVIDIAProfileItem>
{ {
private static List<NVIDIAProfileItem> _allSavedProfiles = new List<NVIDIAProfileItem>(); private static List<NVIDIAProfileItem> _allSavedProfiles = new List<NVIDIAProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -550,78 +550,49 @@ namespace DisplayMagicianShared.NVIDIA
} }
// The public override for the Object.Equals // The object specific Equals
public override bool Equals(object obj)
{
return EqualsDerived(obj) &&
obj.GetType() == typeof(NVIDIAProfileItem);
}
// Profiles are equal if their Viewports are equal
public override bool EqualsDerived(object obj)
{
return base.EqualsDerived(obj) &&
!object.ReferenceEquals(obj, null) &&
obj is NVIDIAProfileItem &&
((NVIDIAProfileItem)obj).NVIDIADisplayConfig == this.NVIDIADisplayConfig &&
((NVIDIAProfileItem)obj).WindowsDisplayConfig == this.WindowsDisplayConfig;
}
/* // Profiles are equal if their Viewports are equal
public bool Equals(NVIDIAProfileItem other) public bool Equals(NVIDIAProfileItem other)
{ {
// Check references
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
// If parameter is null, return false. // Check the object fields
if (other is null) return base.Equals(other) &&
return false; NVIDIADisplayConfig == other.NVIDIADisplayConfig &&
WindowsDisplayConfig == other.WindowsDisplayConfig;
// Optimization for a common success case.
if (Object.ReferenceEquals(this, other))
return true;
// If run-time types are not exactly the same, return false.
if (this.GetType() != other.GetType())
return false;
// If NVIDIA Display Config is different then return false.
if (!NVIDIADisplayConfig.Equals(other.NVIDIADisplayConfig))
return false;
// If Windows Display Config is different then return false.
if (!WindowsDisplayConfig.Equals(other.WindowsDisplayConfig))
return false;
// If Display Identifiers are different then return false.
if (!ProfileDisplayIdentifiers.SequenceEqual(other.ProfileDisplayIdentifiers))
return false;
// Otherwise if all the tests work, then we're good!
return true;
} }
*/
// The public override for the Object.Equals
public bool Equals(Object obj)
{
// Check references
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
// If different types then can't be true
if (obj.GetType() == this.GetType()) return false;
// Check the object fields as this must the same object as obj, and we need to test in more detail
return Equals((NVIDIAProfileItem)obj);
}
// If Equals() returns true for this object compared to another // If Equals() returns true for this object compared to another
// then GetHashCode() must return the same value for these objects. // then GetHashCode() must return the same value for these objects.
public override int GetHashCode() public override int GetHashCode()
{ {
// Calculate the hash code for the product. // Calculate the hash code for the product, including the base properties
return (NVIDIADisplayConfig, WindowsDisplayConfig, ProfileDisplayIdentifiers).GetHashCode(); return (base.GetHashCode(), NVIDIADisplayConfig, WindowsDisplayConfig).GetHashCode();
} }
public static bool operator ==(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs) public static bool operator ==(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs)
{ {
if (object.ReferenceEquals(lhs, rhs)) return Equals(lhs, rhs);
return true;
if (!object.ReferenceEquals(lhs, null) &&
!object.ReferenceEquals(rhs, null) &&
lhs.Equals(rhs))
return true;
return false;
} }
public static bool operator !=(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs) => !(lhs == rhs); public static bool operator !=(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs)
{
return !Equals(lhs, rhs);
}
} }
} }

View File

@ -52,7 +52,7 @@ namespace DisplayMagicianShared
public int Row; public int Row;
} }
public class ProfileItem : IComparable<ProfileItem> public class ProfileItem : IComparable<ProfileItem>, IEquatable<ProfileItem>
{ {
private static List<ProfileItem> _allSavedProfiles = new List<ProfileItem>(); private static List<ProfileItem> _allSavedProfiles = new List<ProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -291,7 +291,6 @@ namespace DisplayMagicianShared
} }
[JsonConverter(typeof(CustomBitmapConverter))] [JsonConverter(typeof(CustomBitmapConverter))]
//[JsonIgnore]
public virtual Bitmap ProfileTightestBitmap public virtual Bitmap ProfileTightestBitmap
{ {
get get
@ -352,7 +351,6 @@ namespace DisplayMagicianShared
// Copy all our profile data over to the other profile // Copy all our profile data over to the other profile
profile.Name = Name; profile.Name = Name;
//profile.Paths = Paths;
profile.ProfileIcon = ProfileIcon; profile.ProfileIcon = ProfileIcon;
profile.SavedProfileIconCacheFilename = SavedProfileIconCacheFilename; profile.SavedProfileIconCacheFilename = SavedProfileIconCacheFilename;
profile.ProfileBitmap = ProfileBitmap; profile.ProfileBitmap = ProfileBitmap;
@ -571,25 +569,22 @@ namespace DisplayMagicianShared
} }
// The object specific Equals
// The public override for the Object.Equals public bool Equals(ProfileItem other)
public override bool Equals(object obj)
{
return EqualsDerived(obj) &&
obj.GetType() == typeof(ProfileItem);
}
// Profiles are equal if their Viewports are equal
public virtual bool EqualsDerived(object obj)
{ {
// Check references
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
// Check the object fields
// ProfileDisplayIdentifiers may be the same but in different order within the array, so we need to handle // ProfileDisplayIdentifiers may be the same but in different order within the array, so we need to handle
// that fact. // that fact.
int ourMatchedIds = 0; int ourMatchedIds = 0;
List<string> otherDisplayIdentifiers =(obj as ProfileItem).ProfileDisplayIdentifiers; List<string> otherDisplayIdentifiers = other.ProfileDisplayIdentifiers;
foreach (string ourDisplayIdentifier in ProfileDisplayIdentifiers) foreach (string ourDisplayIdentifier in ProfileDisplayIdentifiers)
{ {
if (otherDisplayIdentifiers.Contains(ourDisplayIdentifier)){ if (otherDisplayIdentifiers.Contains(ourDisplayIdentifier))
{
ourMatchedIds++; ourMatchedIds++;
} }
} }
@ -602,12 +597,22 @@ namespace DisplayMagicianShared
} }
} }
return !object.ReferenceEquals(obj, null) && return ProfileDisplayIdentifiers.Count == otherDisplayIdentifiers.Count &&
obj is ProfileItem &&
ProfileDisplayIdentifiers.Count == otherDisplayIdentifiers.Count &&
ourMatchedIds == otherMatchedIds; ourMatchedIds == otherMatchedIds;
} }
// The public override for the Object.Equals
public bool Equals(Object obj)
{
// Check references
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
// If different types then can't be true
if (obj.GetType() == this.GetType()) return false;
// Check the object fields as this must the same object as obj, and we need to test in more detail
return Equals((ProfileItem) obj);
}
// If Equals() returns true for this object compared to another // If Equals() returns true for this object compared to another
// then GetHashCode() must return the same value for these objects. // then GetHashCode() must return the same value for these objects.
public override int GetHashCode() public override int GetHashCode()
@ -619,7 +624,7 @@ namespace DisplayMagicianShared
public static bool operator ==(ProfileItem lhs, ProfileItem rhs) public static bool operator ==(ProfileItem lhs, ProfileItem rhs)
{ {
if (object.ReferenceEquals(lhs, rhs)) /*if (object.ReferenceEquals(lhs, rhs))
return true; return true;
if (!object.ReferenceEquals(lhs, null) && if (!object.ReferenceEquals(lhs, null) &&
@ -627,10 +632,14 @@ namespace DisplayMagicianShared
lhs.Equals(rhs)) lhs.Equals(rhs))
return true; return true;
return false; return false;*/
return Equals(lhs, rhs);
} }
public static bool operator !=(ProfileItem lhs, ProfileItem rhs) => !(lhs == rhs); public static bool operator !=(ProfileItem lhs, ProfileItem rhs)
{
return !Equals(lhs, rhs);
}
// IMPORTANT - This ProfileItem ToString function is required to make the Profile ImageListView work properly! DO NOT DELETE! // IMPORTANT - This ProfileItem ToString function is required to make the Profile ImageListView work properly! DO NOT DELETE!
public override string ToString() public override string ToString()

View File

@ -433,7 +433,17 @@ namespace DisplayMagicianShared
foreach (ProfileItem testProfile in _allProfiles) foreach (ProfileItem testProfile in _allProfiles)
{ {
if (testProfile.Equals(Profile)) if (testProfile.VideoMode == VIDEO_MODE.NVIDIA && (testProfile as NVIDIAProfileItem).Equals(Profile))
{
SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {Profile.Name}");
return true;
}
else if (testProfile.VideoMode == VIDEO_MODE.AMD && (testProfile as AMDProfileItem).Equals(Profile))
{
SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {Profile.Name}");
return true;
}
else if (testProfile.VideoMode == VIDEO_MODE.WINDOWS && (testProfile as AMDProfileItem).Equals(Profile))
{ {
SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {Profile.Name}"); SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {Profile.Name}");
return true; return true;
@ -477,19 +487,37 @@ namespace DisplayMagicianShared
} }
public static bool ContainsCurrentProfile() public static bool ContainsCurrentProfile(out string savedProfileName)
{ {
savedProfileName = "";
if (!(_currentProfile is ProfileItem)) if (!(_currentProfile is ProfileItem))
{
return false; return false;
}
SharedLogger.logger.Debug($"ProfileRepository/ContainsCurrentProfile: Checking if our profile repository contains the display profile currently in use"); SharedLogger.logger.Debug($"ProfileRepository/ContainsCurrentProfile: Checking if our profile repository contains the display profile currently in use");
foreach (ProfileItem testProfile in _allProfiles) foreach (ProfileItem testProfile in _allProfiles)
{ {
// TODO - change for Equals // TODO - change for Equals
if (testProfile.Equals(_currentProfile)) if (testProfile.VideoMode == VIDEO_MODE.NVIDIA && (testProfile as NVIDIAProfileItem).Equals(_currentProfile))
{ {
SharedLogger.logger.Debug($"ProfileRepository/ContainsCurrentProfile: Our profile repository does contain the display profile currently in use"); SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {testProfile.Name}");
savedProfileName = testProfile.Name;
return true;
}
else if (testProfile.VideoMode == VIDEO_MODE.AMD && (testProfile as AMDProfileItem).Equals(_currentProfile))
{
SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {testProfile.Name}");
savedProfileName = testProfile.Name;
return true;
}
else if (testProfile.VideoMode == VIDEO_MODE.WINDOWS && (testProfile as AMDProfileItem).Equals(_currentProfile))
{
SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {testProfile.Name}");
savedProfileName = testProfile.Name;
return true; return true;
} }
@ -577,10 +605,11 @@ namespace DisplayMagicianShared
{ {
ProfileItem activeProfile; //ProfileItem activeProfile;
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Updating the profile currently active (in use now)."); SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Updating the profile currently active (in use now).");
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Attempting to access configuration through NVIDIA, then AMD, then Windows CCD interfaces, in that order."); SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Attempting to access configuration through NVIDIA, then AMD, then Windows CCD interfaces, in that order.");
if (_currentVideoMode == VIDEO_MODE.NVIDIA) if (_currentVideoMode == VIDEO_MODE.NVIDIA)
{ {
@ -588,14 +617,24 @@ namespace DisplayMagicianShared
NVIDIAProfileItem nvidiaProfile = new NVIDIAProfileItem NVIDIAProfileItem nvidiaProfile = new NVIDIAProfileItem
{ {
Name = "Current NVIDIA Display Profile", Name = "Current NVIDIA Display Profile",
//ProfileData = amdLibrary.GetActiveProfile(),
//Screens = amdLibrary.GenerateScreenPositions()
//ProfileDisplayIdentifiers = ProfileRepository.GenerateProfileDisplayIdentifiers()
}; };
//activeProfile.ProfileIcon = new ProfileIcon(activeProfile);
//activeProfile.ProfileBitmap = activeProfile.ProfileIcon.ToBitmap(256, 256);
nvidiaProfile.CreateProfileFromCurrentDisplaySettings(); nvidiaProfile.CreateProfileFromCurrentDisplaySettings();
activeProfile = nvidiaProfile;
if (_profilesLoaded && _allProfiles.Count > 0)
{
foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles)
{
if (nvidiaProfile.Equals(loadedProfile))
{
_currentProfile = loadedProfile;
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The NVIDIA profile {loadedProfile.Name} is currently active (in use now).");
return;
}
}
}
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current NVIDIA profile is a new profile that doesn't already exist in the Profile Repository.");
_currentProfile = nvidiaProfile;
} }
else if (_currentVideoMode == VIDEO_MODE.AMD) else if (_currentVideoMode == VIDEO_MODE.AMD)
{ {
@ -603,14 +642,22 @@ namespace DisplayMagicianShared
AMDProfileItem amdProfile = new AMDProfileItem AMDProfileItem amdProfile = new AMDProfileItem
{ {
Name = "Current AMD Display Profile", Name = "Current AMD Display Profile",
//ProfileData = amdLibrary.GetActiveProfile(),
//Screens = amdLibrary.GenerateScreenPositions()
//ProfileDisplayIdentifiers = ProfileRepository.GenerateProfileDisplayIdentifiers()
}; };
//activeProfile.ProfileIcon = new ProfileIcon(activeProfile);
//activeProfile.ProfileBitmap = activeProfile.ProfileIcon.ToBitmap(256, 256);
amdProfile.CreateProfileFromCurrentDisplaySettings(); amdProfile.CreateProfileFromCurrentDisplaySettings();
activeProfile = amdProfile; if (_profilesLoaded && _allProfiles.Count > 0)
{
foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles)
{
if (amdProfile.Equals(loadedProfile))
{
_currentProfile = loadedProfile;
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The AMD profile {loadedProfile.Name} is currently active (in use now).");
return;
}
}
}
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current AMD profile is a new profile that doesn't already exist in the Profile Repository.");
_currentProfile = amdProfile;
} }
else else
{ {
@ -618,31 +665,23 @@ namespace DisplayMagicianShared
WinProfileItem winProfile = new WinProfileItem WinProfileItem winProfile = new WinProfileItem
{ {
Name = "Current Windows Display Profile", Name = "Current Windows Display Profile",
//Paths = PathInfo.GetActivePaths().Select(info => new DisplayMagicianShared.Topology.Path(info)).ToArray(),
//ProfileDisplayIdentifiers = ProfileRepository.GenerateProfileDisplayIdentifiers()
}; };
//WinProfile.ProfileIcon = new ProfileIcon(activeProfile);
//activeProfile.ProfileBitmap = activeProfile.ProfileIcon.ToBitmap(256, 256);
winProfile.CreateProfileFromCurrentDisplaySettings(); winProfile.CreateProfileFromCurrentDisplaySettings();
activeProfile = winProfile;
}
if (_profilesLoaded && _allProfiles.Count > 0) if (_profilesLoaded && _allProfiles.Count > 0)
{ {
foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles) foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles)
{ {
if (activeProfile.Equals(loadedProfile)) if (winProfile.Equals(loadedProfile))
{ {
_currentProfile = loadedProfile; _currentProfile = loadedProfile;
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The profile {loadedProfile.Name} is currently active (in use now)."); SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The Windows profile {loadedProfile.Name} is currently active (in use now).");
return; return;
} }
} }
} }
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current profile is a new profile that doesn't already exist in the Profile Repository."); SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current Windows profile is a new profile that doesn't already exist in the Profile Repository.");
_currentProfile = activeProfile; _currentProfile = winProfile;
}
} }
public static ProfileItem GetActiveProfile() public static ProfileItem GetActiveProfile()
@ -658,16 +697,22 @@ namespace DisplayMagicianShared
public static bool IsActiveProfile(ProfileItem profile) public static bool IsActiveProfile(ProfileItem profile)
{ {
SharedLogger.logger.Trace($"ProfileRepository/IsActiveProfile: Checking whether the profile {profile.Name} is the currently active profile."); SharedLogger.logger.Trace($"ProfileRepository/IsActiveProfile: Checking whether the profile {profile.Name} is the currently active profile.");
if (profile == null){ if (_currentProfile == null)
{
SharedLogger.logger.Error($"ProfileRepository/IsActiveProfile: The current profile {profile.Name} is null, so can't test it against anything.");
return false;
}
if (profile == null)
{
SharedLogger.logger.Error($"ProfileRepository/IsActiveProfile: The requested profile {profile.Name} is null. Not changing anything, and reporting an error"); SharedLogger.logger.Error($"ProfileRepository/IsActiveProfile: The requested profile {profile.Name} is null. Not changing anything, and reporting an error");
return false; return false;
} }
if (profile is NVIDIAProfileItem && _currentProfile is NVIDIAProfileItem) if (profile is NVIDIAProfileItem)
{ {
NVIDIAProfileItem nvidiaNewProfile = (NVIDIAProfileItem)profile;
NVIDIAProfileItem nvidiaCurrentProfile = (NVIDIAProfileItem)_currentProfile; NVIDIAProfileItem nvidiaCurrentProfile = (NVIDIAProfileItem)_currentProfile;
if (nvidiaNewProfile.Equals(nvidiaCurrentProfile)) if (nvidiaCurrentProfile.Equals(profile))
{ {
SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The NVIDIA profile {profile.Name} is the currently active profile."); SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The NVIDIA profile {profile.Name} is the currently active profile.");
return true; return true;
@ -678,11 +723,10 @@ namespace DisplayMagicianShared
return false; return false;
} }
} }
else if (profile is AMDProfileItem && _currentProfile is AMDProfileItem) else if (_currentProfile is AMDProfileItem)
{ {
AMDProfileItem amdNewProfile = (AMDProfileItem)profile;
AMDProfileItem amdCurrentProfile = (AMDProfileItem)_currentProfile; AMDProfileItem amdCurrentProfile = (AMDProfileItem)_currentProfile;
if (amdNewProfile.Equals(amdCurrentProfile)) if (amdCurrentProfile.Equals(profile))
{ {
SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The AMD profile {profile.Name} is the currently active profile."); SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The AMD profile {profile.Name} is the currently active profile.");
return true; return true;
@ -693,11 +737,10 @@ namespace DisplayMagicianShared
return false; return false;
} }
} }
else if (profile is WinProfileItem && _currentProfile is WinProfileItem) else if (_currentProfile is WinProfileItem)
{ {
WinProfileItem winNewProfile = (WinProfileItem)profile;
WinProfileItem winCurrentProfile = (WinProfileItem)_currentProfile; WinProfileItem winCurrentProfile = (WinProfileItem)_currentProfile;
if (winNewProfile.Equals(winCurrentProfile)) if (winCurrentProfile.Equals(profile))
{ {
SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The Windows CCD profile {profile.Name} is the currently active profile."); SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The Windows CCD profile {profile.Name} is the currently active profile.");
return true; return true;
@ -750,16 +793,11 @@ namespace DisplayMagicianShared
SharedLogger.logger.Error(ex, $"ProfileRepository/LoadProfiles: Tried to parse the JSON in the {_profileStorageJsonFileName} but the JsonConvert threw an exception."); SharedLogger.logger.Error(ex, $"ProfileRepository/LoadProfiles: Tried to parse the JSON in the {_profileStorageJsonFileName} but the JsonConvert threw an exception.");
} }
/*// Populate the Current Profile now so we have stuff to compare against
ProfileItem myCurrentProfile = new NVIDIAProfileItem
{
Name = "Current Display Profile",
};
myCurrentProfile.CreateProfileFromCurrentDisplaySettings();
_currentProfile = myCurrentProfile;
*/
SharedLogger.logger.Debug($"ProfileRepository/LoadProfiles: Finding the current profile in the Profile Repository"); SharedLogger.logger.Debug($"ProfileRepository/LoadProfiles: Finding the current profile in the Profile Repository");
// Update the current active profile
UpdateActiveProfile();
// Go through all the profiles and set up the needed structures (such as the Screens list) // Go through all the profiles and set up the needed structures (such as the Screens list)
// and check if the current profile is used // and check if the current profile is used
foreach (ProfileItem loadedProfile in _allProfiles) foreach (ProfileItem loadedProfile in _allProfiles)

View File

@ -11,7 +11,7 @@ using System.Drawing.Imaging;
namespace DisplayMagicianShared.Windows namespace DisplayMagicianShared.Windows
{ {
public class WinProfileItem : ProfileItem public class WinProfileItem : ProfileItem, IEquatable<WinProfileItem>
{ {
private static List<WinProfileItem> _allSavedProfiles = new List<WinProfileItem>(); private static List<WinProfileItem> _allSavedProfiles = new List<WinProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -319,28 +319,28 @@ namespace DisplayMagicianShared.Windows
return _screens; return _screens;
} }
/*public override int CompareTo(object obj) // The object specific Equals
public bool Equals(WinProfileItem other)
{ {
if (!(obj is WinProfileItem)) throw new ArgumentException("Object to CompareTo is not a WinProfileItem"); ; // Check references
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
WinProfileItem otherProfile = (WinProfileItem)obj; // Check the object fields
return this.Name.CompareTo(otherProfile.Name); return base.Equals(other) &&
}*/ WindowsDisplayConfig == other.WindowsDisplayConfig;
// The public override for the Object.Equals
public override bool Equals(object obj)
{
return EqualsDerived(obj) &&
obj.GetType() == typeof(WinProfileItem);
} }
// Profiles are equal if their Viewports are equal // The public override for the Object.Equals
public override bool EqualsDerived(object obj) public bool Equals(Object obj)
{ {
return base.EqualsDerived(obj) && // Check references
!object.ReferenceEquals(obj, null) && if (ReferenceEquals(null, obj)) return false;
obj is WinProfileItem && if (ReferenceEquals(this, obj)) return true;
((WinProfileItem)obj).WindowsDisplayConfig == this.WindowsDisplayConfig; // If different types then can't be true
if (obj.GetType() == this.GetType()) return false;
// Check the object fields as this must the same object as obj, and we need to test in more detail
return Equals((WinProfileItem)obj);
} }
// If Equals() returns true for this object compared to another // If Equals() returns true for this object compared to another
@ -348,24 +348,19 @@ namespace DisplayMagicianShared.Windows
public override int GetHashCode() public override int GetHashCode()
{ {
// Calculate the hash code for the product. // Calculate the hash code for the product.
return (WindowsDisplayConfig, ProfileDisplayIdentifiers).GetHashCode(); return (base.GetHashCode(), WindowsDisplayConfig).GetHashCode();
} }
public static bool operator ==(WinProfileItem lhs, WinProfileItem rhs) public static bool operator ==(WinProfileItem lhs, WinProfileItem rhs)
{ {
if (object.ReferenceEquals(lhs, rhs)) return Equals(lhs, rhs);
return true;
if (!object.ReferenceEquals(lhs, null) &&
!object.ReferenceEquals(rhs, null) &&
lhs.Equals(rhs))
return true;
return false;
} }
public static bool operator !=(WinProfileItem lhs, WinProfileItem rhs) => !(lhs == rhs); public static bool operator !=(WinProfileItem lhs, WinProfileItem rhs)
{
return !Equals(lhs, rhs);
}
} }
} }