diff --git a/DisplayMagician/UIForms/DisplayProfileForm.cs b/DisplayMagician/UIForms/DisplayProfileForm.cs index 8cdbaed..4792983 100644 --- a/DisplayMagician/UIForms/DisplayProfileForm.cs +++ b/DisplayMagician/UIForms/DisplayProfileForm.cs @@ -202,12 +202,9 @@ namespace DisplayMagician.UIForms // if the item was removed from the list during this // list refresh, then we select this profile only if it // is the currently used Profile - if (_selectedProfile is ProfileItem && _selectedProfile.Equals(profile)) + if (ProfileRepository.IsActiveProfile(_selectedProfile)) newItem.Selected = true; - - //ProfileRepository.ProfileValidityLookup[profile.Name] = profile.IsPossible; - // Add it to the list! ilv_saved_profiles.Items.Add(newItem, _profileAdaptor); @@ -351,14 +348,11 @@ namespace DisplayMagician.UIForms // We're in 'save' mode! // Check we're not already saving this profile - foreach (ProfileItem savedProfile in ProfileRepository.AllProfiles) - { - //if (String.Equals(txt_profile_save_name.Text, savedProfile.Name, StringComparison.InvariantCultureIgnoreCase)) - if (savedProfile.Equals(_selectedProfile)) - { - MessageBox.Show($"Sorry, this display profile was already saved as '{savedProfile.Name}'.", "Profile already saved", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } + string previouslySavedProfileName = ""; + if (ProfileRepository.ContainsCurrentProfile(out previouslySavedProfileName)) + { + MessageBox.Show($"Sorry, this display profile was already saved as '{previouslySavedProfileName}'.", "Profile already saved", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; } // 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) { - // Reload the profiles in case we swapped to another program to change it - ProfileRepository.GetActiveProfile(); // Refresh the profiles to see whats valid ProfileRepository.IsPossibleRefresh(); + // Reload the profiles in case we swapped to another program to change it + ProfileRepository.UpdateActiveProfile(); // Change to the current selected Profile ChangeSelectedProfile(ProfileRepository.GetActiveProfile()); // Refresh the Profile UI @@ -460,7 +454,7 @@ namespace DisplayMagician.UIForms switch (m.Msg) { case WM_DISPLAYCHANGE: - ProfileRepository.UpdateActiveProfile(); + btn_view_current.PerformClick(); break; } diff --git a/DisplayMagicianShared/AMD/AMDProfileItem.cs b/DisplayMagicianShared/AMD/AMDProfileItem.cs index 4f57b94..0fdc2ce 100644 --- a/DisplayMagicianShared/AMD/AMDProfileItem.cs +++ b/DisplayMagicianShared/AMD/AMDProfileItem.cs @@ -12,8 +12,8 @@ using System.Drawing.Imaging; namespace DisplayMagicianShared.AMD { - public class AMDProfileItem : ProfileItem - { + public class AMDProfileItem : ProfileItem, IEquatable + { private static List _allSavedProfiles = new List(); private ProfileIcon _profileIcon; private Bitmap _profileBitmap, _profileShortcutBitmap; @@ -364,51 +364,46 @@ namespace DisplayMagicianShared.AMD 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; - return this.Name.CompareTo(otherProfile.Name); - }*/ - - // The public override for the Object.Equals - public override bool Equals(object obj) - { - return EqualsDerived(obj) && - obj.GetType() == typeof(AMDProfileItem); + // Check the object fields + return base.Equals(other) && + AMDDisplayConfig == other.AMDDisplayConfig && + WindowsDisplayConfig == other.WindowsDisplayConfig; } - // Profiles are equal if their Viewports are equal - public override bool EqualsDerived(object obj) + // The public override for the Object.Equals + public bool Equals(Object obj) { - return base.EqualsDerived(obj) && - !object.ReferenceEquals(obj, null) && - obj is AMDProfileItem && - ((AMDProfileItem)obj).AMDDisplayConfig == this.AMDDisplayConfig && - ((AMDProfileItem)obj).WindowsDisplayConfig == this.WindowsDisplayConfig; + // 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((AMDProfileItem)obj); } public override int GetHashCode() { // 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) { - if (object.ReferenceEquals(lhs, rhs)) - return true; - - if (!object.ReferenceEquals(lhs, null) && - !object.ReferenceEquals(rhs, null) && - lhs.Equals(rhs)) - return true; - - return false; + return Equals(lhs, rhs); } - public static bool operator !=(AMDProfileItem lhs, AMDProfileItem rhs) => !(lhs == rhs); + public static bool operator !=(AMDProfileItem lhs, AMDProfileItem rhs) + { + return !Equals(lhs, rhs); + } } } \ No newline at end of file diff --git a/DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs b/DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs index a0b0f79..51b1a1d 100644 --- a/DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs +++ b/DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs @@ -14,7 +14,7 @@ using DisplayMagicianShared.Windows; namespace DisplayMagicianShared.NVIDIA { - public class NVIDIAProfileItem : ProfileItem + public class NVIDIAProfileItem : ProfileItem, IEquatable { private static List _allSavedProfiles = new List(); private ProfileIcon _profileIcon; @@ -548,80 +548,51 @@ namespace DisplayMagicianShared.NVIDIA return _screens; } - - // The public override for the Object.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 + // The object specific Equals 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. - if (other is null) - return false; - - // 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; + // Check the object fields + return base.Equals(other) && + NVIDIADisplayConfig == other.NVIDIADisplayConfig && + WindowsDisplayConfig == other.WindowsDisplayConfig; } -*/ + + // 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 // then GetHashCode() must return the same value for these objects. public override int GetHashCode() { - // Calculate the hash code for the product. - return (NVIDIADisplayConfig, WindowsDisplayConfig, ProfileDisplayIdentifiers).GetHashCode(); + // Calculate the hash code for the product, including the base properties + return (base.GetHashCode(), NVIDIADisplayConfig, WindowsDisplayConfig).GetHashCode(); } public static bool operator ==(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs) - { - if (object.ReferenceEquals(lhs, rhs)) - return true; - - if (!object.ReferenceEquals(lhs, null) && - !object.ReferenceEquals(rhs, null) && - lhs.Equals(rhs)) - return true; - - return false; + { + return Equals(lhs, rhs); } - public static bool operator !=(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs) => !(lhs == rhs); + public static bool operator !=(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs) + { + return !Equals(lhs, rhs); + } } } \ No newline at end of file diff --git a/DisplayMagicianShared/ProfileItem.cs b/DisplayMagicianShared/ProfileItem.cs index c3b2df1..4d75d93 100644 --- a/DisplayMagicianShared/ProfileItem.cs +++ b/DisplayMagicianShared/ProfileItem.cs @@ -52,7 +52,7 @@ namespace DisplayMagicianShared public int Row; } - public class ProfileItem : IComparable + public class ProfileItem : IComparable, IEquatable { private static List _allSavedProfiles = new List(); private ProfileIcon _profileIcon; @@ -291,7 +291,6 @@ namespace DisplayMagicianShared } [JsonConverter(typeof(CustomBitmapConverter))] - //[JsonIgnore] public virtual Bitmap ProfileTightestBitmap { get @@ -352,7 +351,6 @@ namespace DisplayMagicianShared // Copy all our profile data over to the other profile profile.Name = Name; - //profile.Paths = Paths; profile.ProfileIcon = ProfileIcon; profile.SavedProfileIconCacheFilename = SavedProfileIconCacheFilename; profile.ProfileBitmap = ProfileBitmap; @@ -571,25 +569,22 @@ namespace DisplayMagicianShared } - - // The public override for the Object.Equals - 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) + // The object specific Equals + public bool Equals(ProfileItem other) { + // 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 // that fact. int ourMatchedIds = 0; - List otherDisplayIdentifiers =(obj as ProfileItem).ProfileDisplayIdentifiers; + List otherDisplayIdentifiers = other.ProfileDisplayIdentifiers; foreach (string ourDisplayIdentifier in ProfileDisplayIdentifiers) { - if (otherDisplayIdentifiers.Contains(ourDisplayIdentifier)){ + if (otherDisplayIdentifiers.Contains(ourDisplayIdentifier)) + { ourMatchedIds++; } } @@ -602,12 +597,22 @@ namespace DisplayMagicianShared } } - return !object.ReferenceEquals(obj, null) && - obj is ProfileItem && - ProfileDisplayIdentifiers.Count == otherDisplayIdentifiers.Count && + return ProfileDisplayIdentifiers.Count == otherDisplayIdentifiers.Count && 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 // then GetHashCode() must return the same value for these objects. public override int GetHashCode() @@ -619,7 +624,7 @@ namespace DisplayMagicianShared public static bool operator ==(ProfileItem lhs, ProfileItem rhs) { - if (object.ReferenceEquals(lhs, rhs)) + /*if (object.ReferenceEquals(lhs, rhs)) return true; if (!object.ReferenceEquals(lhs, null) && @@ -627,10 +632,14 @@ namespace DisplayMagicianShared lhs.Equals(rhs)) 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! public override string ToString() diff --git a/DisplayMagicianShared/ProfileRepository.cs b/DisplayMagicianShared/ProfileRepository.cs index 31e7e14..fed8997 100644 --- a/DisplayMagicianShared/ProfileRepository.cs +++ b/DisplayMagicianShared/ProfileRepository.cs @@ -433,12 +433,22 @@ namespace DisplayMagicianShared 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}"); + return true; + } + } SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository doesn't contain a profile called {Profile.Name}"); return false; @@ -477,21 +487,39 @@ namespace DisplayMagicianShared } - public static bool ContainsCurrentProfile() + public static bool ContainsCurrentProfile(out string savedProfileName) { + savedProfileName = ""; + if (!(_currentProfile is ProfileItem)) + { return false; + } + SharedLogger.logger.Debug($"ProfileRepository/ContainsCurrentProfile: Checking if our profile repository contains the display profile currently in use"); foreach (ProfileItem testProfile in _allProfiles) { // 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; + } } @@ -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: Attempting to access configuration through NVIDIA, then AMD, then Windows CCD interfaces, in that order."); if (_currentVideoMode == VIDEO_MODE.NVIDIA) { @@ -588,14 +617,24 @@ namespace DisplayMagicianShared NVIDIAProfileItem nvidiaProfile = new NVIDIAProfileItem { 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(); - 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) { @@ -603,46 +642,46 @@ namespace DisplayMagicianShared AMDProfileItem amdProfile = new AMDProfileItem { 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(); - 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 { SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Neither NVIDIA NVAPI or AMD ADL Drivers are installed, so using the built in Windows CCD library interface for this display profile."); WinProfileItem winProfile = new WinProfileItem { 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(); - activeProfile = winProfile; - } - - if (_profilesLoaded && _allProfiles.Count > 0) - { - foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles) + if (_profilesLoaded && _allProfiles.Count > 0) { - if (activeProfile.Equals(loadedProfile)) + foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles) { - _currentProfile = loadedProfile; - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The profile {loadedProfile.Name} is currently active (in use now)."); - return; + if (winProfile.Equals(loadedProfile)) + { + _currentProfile = loadedProfile; + SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The Windows profile {loadedProfile.Name} is currently active (in use now)."); + return; + } } } + SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current Windows profile is a new profile that doesn't already exist in the Profile Repository."); + _currentProfile = winProfile; } - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current profile is a new profile that doesn't already exist in the Profile Repository."); - _currentProfile = activeProfile; - } public static ProfileItem GetActiveProfile() @@ -658,16 +697,22 @@ namespace DisplayMagicianShared public static bool IsActiveProfile(ProfileItem 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"); return false; } - if (profile is NVIDIAProfileItem && _currentProfile is NVIDIAProfileItem) + if (profile is NVIDIAProfileItem) { - NVIDIAProfileItem nvidiaNewProfile = (NVIDIAProfileItem)profile; 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."); return true; @@ -678,11 +723,10 @@ namespace DisplayMagicianShared return false; } } - else if (profile is AMDProfileItem && _currentProfile is AMDProfileItem) + else if (_currentProfile is AMDProfileItem) { - AMDProfileItem amdNewProfile = (AMDProfileItem)profile; 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."); return true; @@ -693,11 +737,10 @@ namespace DisplayMagicianShared return false; } } - else if (profile is WinProfileItem && _currentProfile is WinProfileItem) + else if (_currentProfile is WinProfileItem) { - WinProfileItem winNewProfile = (WinProfileItem)profile; 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."); 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."); } - /*// 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"); + // Update the current active profile + UpdateActiveProfile(); + // Go through all the profiles and set up the needed structures (such as the Screens list) // and check if the current profile is used foreach (ProfileItem loadedProfile in _allProfiles) diff --git a/DisplayMagicianShared/Windows/WinProfileItem.cs b/DisplayMagicianShared/Windows/WinProfileItem.cs index 76ebb10..49e1d52 100644 --- a/DisplayMagicianShared/Windows/WinProfileItem.cs +++ b/DisplayMagicianShared/Windows/WinProfileItem.cs @@ -11,7 +11,7 @@ using System.Drawing.Imaging; namespace DisplayMagicianShared.Windows { - public class WinProfileItem : ProfileItem + public class WinProfileItem : ProfileItem, IEquatable { private static List _allSavedProfiles = new List(); private ProfileIcon _profileIcon; @@ -319,28 +319,28 @@ namespace DisplayMagicianShared.Windows 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; - return this.Name.CompareTo(otherProfile.Name); - }*/ - - // The public override for the Object.Equals - public override bool Equals(object obj) - { - return EqualsDerived(obj) && - obj.GetType() == typeof(WinProfileItem); + // Check the object fields + return base.Equals(other) && + WindowsDisplayConfig == other.WindowsDisplayConfig; } - // Profiles are equal if their Viewports are equal - public override bool EqualsDerived(object obj) + // The public override for the Object.Equals + public bool Equals(Object obj) { - return base.EqualsDerived(obj) && - !object.ReferenceEquals(obj, null) && - obj is WinProfileItem && - ((WinProfileItem)obj).WindowsDisplayConfig == this.WindowsDisplayConfig; + // 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((WinProfileItem)obj); } // If Equals() returns true for this object compared to another @@ -348,24 +348,19 @@ namespace DisplayMagicianShared.Windows public override int GetHashCode() { // Calculate the hash code for the product. - return (WindowsDisplayConfig, ProfileDisplayIdentifiers).GetHashCode(); + return (base.GetHashCode(), WindowsDisplayConfig).GetHashCode(); } public static bool operator ==(WinProfileItem lhs, WinProfileItem rhs) { - if (object.ReferenceEquals(lhs, rhs)) - return true; - - if (!object.ReferenceEquals(lhs, null) && - !object.ReferenceEquals(rhs, null) && - lhs.Equals(rhs)) - return true; - - return false; + return Equals(lhs, rhs); } - public static bool operator !=(WinProfileItem lhs, WinProfileItem rhs) => !(lhs == rhs); + public static bool operator !=(WinProfileItem lhs, WinProfileItem rhs) + { + return !Equals(lhs, rhs); + } } } \ No newline at end of file