Updated the Equality and Comparable operators for ProfileItems

And in doing so, managed to break the Profile image logic!
This commit is contained in:
Terry MacDonald 2021-09-02 14:44:35 +12:00
parent 65bbd1ab66
commit cab056b7fd
4 changed files with 175 additions and 62 deletions

View File

@ -12,7 +12,7 @@ using System.Drawing.Imaging;
namespace DisplayMagicianShared.AMD namespace DisplayMagicianShared.AMD
{ {
public class AMDProfileItem : ProfileItem, IComparable public class AMDProfileItem : ProfileItem, IEquatable<AMDProfileItem>, IComparable
{ {
private static List<AMDProfileItem> _allSavedProfiles = new List<AMDProfileItem>(); private static List<AMDProfileItem> _allSavedProfiles = new List<AMDProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -363,12 +363,16 @@ namespace DisplayMagicianShared.AMD
return _screens; return _screens;
} }
public int CompareTo(object obj)
{
if (!(obj is AMDProfileItem)) throw new ArgumentException("Object to CompareTo is not a AMDProfileItem"); ;
AMDProfileItem otherProfile = (AMDProfileItem)obj;
return this.Name.CompareTo(otherProfile.Name);
}
// The public override for the Object.Equals // The public override for the Object.Equals
public override bool Equals(object obj) public override bool Equals(object obj) => this.Equals(obj as AMDProfileItem);
{
return this.Equals(obj as AMDProfileItem);
}
// Profiles are equal if their Viewports are equal // Profiles are equal if their Viewports are equal
public bool Equals(AMDProfileItem other) public bool Equals(AMDProfileItem other)
@ -406,9 +410,26 @@ namespace DisplayMagicianShared.AMD
{ {
// Calculate the hash code for the product. // Calculate the hash code for the product.
return (AMDDisplayConfig, WindowsDisplayConfig, ProfileDisplayIdentifiers).GetHashCode(); return (AMDDisplayConfig, WindowsDisplayConfig, ProfileDisplayIdentifiers).GetHashCode();
} }
public static bool operator ==(AMDProfileItem lhs, AMDProfileItem rhs)
{
if (lhs is null)
{
if (rhs is null)
{
return true;
}
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return lhs.Equals(rhs);
}
public static bool operator !=(AMDProfileItem lhs, AMDProfileItem rhs) => !(lhs == rhs);
} }
} }

View File

@ -14,7 +14,7 @@ using DisplayMagicianShared.Windows;
namespace DisplayMagicianShared.NVIDIA namespace DisplayMagicianShared.NVIDIA
{ {
public class NVIDIAProfileItem : ProfileItem, IComparable public class NVIDIAProfileItem : ProfileItem, IEquatable<ProfileItem>, IComparable
{ {
private static List<NVIDIAProfileItem> _allSavedProfiles = new List<NVIDIAProfileItem>(); private static List<NVIDIAProfileItem> _allSavedProfiles = new List<NVIDIAProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -140,12 +140,16 @@ namespace DisplayMagicianShared.NVIDIA
// 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.WindowsDisplayConfig = WindowsDisplayConfig;
profile.NVIDIADisplayConfig = NVIDIADisplayConfig;
profile.ProfileIcon = ProfileIcon; profile.ProfileIcon = ProfileIcon;
profile.SavedProfileIconCacheFilename = SavedProfileIconCacheFilename; profile.SavedProfileIconCacheFilename = SavedProfileIconCacheFilename;
profile.ProfileBitmap = ProfileBitmap; profile.ProfileBitmap = ProfileBitmap;
profile.ProfileTightestBitmap = ProfileTightestBitmap; profile.ProfileTightestBitmap = ProfileTightestBitmap;
profile.ProfileDisplayIdentifiers = ProfileDisplayIdentifiers; profile.ProfileDisplayIdentifiers = ProfileDisplayIdentifiers;
profile.WallpaperMode = WallpaperMode;
profile.WallpaperBitmapFilename = WallpaperBitmapFilename;
profile.WallpaperStyle = WallpaperStyle;
return true; return true;
} }
@ -544,12 +548,16 @@ namespace DisplayMagicianShared.NVIDIA
return _screens; return _screens;
} }
public int CompareTo(object obj)
{
if (!(obj is NVIDIAProfileItem)) throw new ArgumentException("Object to CompareTo is not a NVIDIAProfileItem"); ;
NVIDIAProfileItem otherProfile = (NVIDIAProfileItem)obj;
return this.Name.CompareTo(otherProfile.Name);
}
// The public override for the Object.Equals // The public override for the Object.Equals
public override bool Equals(object obj) public override bool Equals(object obj) => this.Equals(obj as NVIDIAProfileItem);
{
return this.Equals(obj as NVIDIAProfileItem);
}
// Profiles are equal if their Viewports are equal // Profiles are equal if their Viewports are equal
public bool Equals(NVIDIAProfileItem other) public bool Equals(NVIDIAProfileItem other)
@ -592,6 +600,23 @@ namespace DisplayMagicianShared.NVIDIA
} }
public static bool operator ==(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs)
{
if (lhs is null)
{
if (rhs is null)
{
return true;
}
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return lhs.Equals(rhs);
}
public static bool operator !=(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs) => !(lhs == rhs);
} }
} }

View File

@ -50,7 +50,7 @@ namespace DisplayMagicianShared
public int Row; public int Row;
} }
public class ProfileItem : IComparable public class ProfileItem : IEquatable<ProfileItem>, IComparable
{ {
private static List<ProfileItem> _allSavedProfiles = new List<ProfileItem>(); private static List<ProfileItem> _allSavedProfiles = new List<ProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -385,35 +385,6 @@ namespace DisplayMagicianShared
return false; return false;
} }
public override int GetHashCode()
{
// Get hash code for the ProfileDisplayIdentifiers field if it is not null.
int hashIds = ProfileDisplayIdentifiers == null ? 0 : ProfileDisplayIdentifiers.GetHashCode();
// Get Paths too
//int hashPaths = Paths == null ? 0 : Paths.GetHashCode();
int hashPaths = 0;
// Calculate the hash code for the product.
return (hashIds, hashPaths).GetHashCode();
}
public override string ToString()
{
return (Name ?? Language.UN_TITLED_PROFILE);
}
public int CompareTo(object obj)
{
if (!(obj is ProfileItem)) throw new ArgumentException("Object to CompareTo is not a Shortcut"); ;
ProfileItem otherProfile = (ProfileItem)obj;
return this.Name.CompareTo(otherProfile.Name);
}
// ReSharper disable once FunctionComplexityOverflow // ReSharper disable once FunctionComplexityOverflow
// ReSharper disable once CyclomaticComplexity // ReSharper disable once CyclomaticComplexity
@ -530,7 +501,75 @@ namespace DisplayMagicianShared
{ {
return new List<ScreenPosition>(); return new List<ScreenPosition>();
} }
/*public int CompareTo(ProfileItem other)
{
return this.Name.CompareTo(other.Name);
}*/
public int CompareTo(object obj)
{
if (!(obj is ProfileItem)) throw new ArgumentException("Object to CompareTo is not a Shortcut"); ;
ProfileItem otherProfile = (ProfileItem)obj;
return this.Name.CompareTo(otherProfile.Name);
}
// The public override for the Object.Equals
public override bool Equals(object obj) => this.Equals(obj as ProfileItem);
// Profiles are equal if their Viewports are equal
public bool Equals(ProfileItem other)
{
// 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 (!this.ProfileDisplayIdentifiers.SequenceEqual(other.ProfileDisplayIdentifiers))
{
return false;
}
// Otherwise if all the tests work, then we're good!
return true;
}
// 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 (ProfileDisplayIdentifiers).GetHashCode();
}
public static bool operator ==(ProfileItem lhs, ProfileItem rhs)
{
if (lhs is null)
{
if (rhs is null)
{
return true;
}
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return lhs.Equals(rhs);
}
public static bool operator !=(ProfileItem lhs, ProfileItem rhs) => !(lhs == rhs);
} }
}
}

View File

@ -11,7 +11,7 @@ using System.Drawing.Imaging;
namespace DisplayMagicianShared.Windows namespace DisplayMagicianShared.Windows
{ {
public class WinProfileItem : ProfileItem, IComparable public class WinProfileItem : ProfileItem, IEquatable<WinProfileItem>, IComparable
{ {
private static List<WinProfileItem> _allSavedProfiles = new List<WinProfileItem>(); private static List<WinProfileItem> _allSavedProfiles = new List<WinProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -102,15 +102,13 @@ namespace DisplayMagicianShared.Windows
public override bool IsValid() public override bool IsValid()
{ {
if ( if (WinLibrary.GetLibrary().IsValidConfig(_windowsDisplayConfig) &&
ProfileIcon is ProfileIcon && ProfileIcon is ProfileIcon &&
System.IO.File.Exists(SavedProfileIconCacheFilename) && System.IO.File.Exists(SavedProfileIconCacheFilename) &&
ProfileBitmap is Bitmap && ProfileBitmap is Bitmap &&
ProfileTightestBitmap is Bitmap && ProfileTightestBitmap is Bitmap &&
ProfileDisplayIdentifiers.Count > 0) ProfileDisplayIdentifiers.Count > 0)
{ return true;
return true;
}
else else
return false; return false;
} }
@ -133,7 +131,9 @@ namespace DisplayMagicianShared.Windows
profile.ProfileBitmap = ProfileBitmap; profile.ProfileBitmap = ProfileBitmap;
profile.ProfileTightestBitmap = ProfileTightestBitmap; profile.ProfileTightestBitmap = ProfileTightestBitmap;
profile.ProfileDisplayIdentifiers = ProfileDisplayIdentifiers; profile.ProfileDisplayIdentifiers = ProfileDisplayIdentifiers;
profile.Screens = Screens; profile.WallpaperMode = WallpaperMode;
profile.WallpaperBitmapFilename = WallpaperBitmapFilename;
profile.WallpaperStyle = WallpaperStyle;
return true; return true;
} }
@ -223,6 +223,10 @@ namespace DisplayMagicianShared.Windows
public override List<ScreenPosition> GetScreenPositions() public override List<ScreenPosition> GetScreenPositions()
{ {
// Set up some colours
Color primaryScreenColor = Color.FromArgb(0, 174, 241); // represents Primary screen blue
Color normalScreenColor = Color.FromArgb(155, 155, 155); // represents normal screen colour (gray)
// Now we create the screens structure from the AMD profile information // Now we create the screens structure from the AMD profile information
_screens = new List<ScreenPosition>(); _screens = new List<ScreenPosition>();
@ -233,7 +237,7 @@ namespace DisplayMagicianShared.Windows
// Return an empty screen if we have no Display Config Paths to use! // Return an empty screen if we have no Display Config Paths to use!
return _screens; return _screens;
} }
foreach (var path in _windowsDisplayConfig.DisplayConfigPaths) foreach (var path in _windowsDisplayConfig.DisplayConfigPaths)
{ {
// For each path we go through and get the relevant info we need. // For each path we go through and get the relevant info we need.
@ -242,13 +246,18 @@ namespace DisplayMagicianShared.Windows
// Set some basics about the screen // Set some basics about the screen
ScreenPosition screen = new ScreenPosition(); ScreenPosition screen = new ScreenPosition();
screen.Library = "WINDOWS"; screen.Library = "WINDOWS";
screen.IsSpanned = false;
screen.Colour = normalScreenColor; // this is the default unless overridden by the primary screen
UInt32 sourceId = path.SourceInfo.Id;
UInt32 targetId = path.TargetInfo.Id; UInt32 targetId = path.TargetInfo.Id;
// Go through the screens as Windows knows them, and then enhance the info with Mosaic data if it applies
foreach (DISPLAYCONFIG_MODE_INFO displayMode in _windowsDisplayConfig.DisplayConfigModes) foreach (DISPLAYCONFIG_MODE_INFO displayMode in _windowsDisplayConfig.DisplayConfigModes)
{ {
// Find the matching Display Config Source Mode // Find the matching Display Config Source Mode
if (displayMode.InfoType != DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE && displayMode.Id == targetId) if (displayMode.InfoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE && displayMode.Id == sourceId)
{ {
screen.Name = targetId.ToString(); screen.Name = targetId.ToString();
//screen.DisplayConnector = displayMode.DisplayConnector; //screen.DisplayConnector = displayMode.DisplayConnector;
@ -261,7 +270,9 @@ namespace DisplayMagicianShared.Windows
if (screen.ScreenX == 0 && screen.ScreenY == 0) if (screen.ScreenX == 0 && screen.ScreenY == 0)
{ {
screen.IsPrimary = true; screen.IsPrimary = true;
screen.Colour = primaryScreenColor;
} }
break;
} }
} }
@ -289,28 +300,28 @@ namespace DisplayMagicianShared.Windows
screen.HDRSupported = false; screen.HDRSupported = false;
screen.HDREnabled = false; screen.HDREnabled = false;
} }
break;
} }
} }
// No spanning in a windows ccd system
screen.IsSpanned = false;
screen.Colour = Color.FromArgb(195, 195, 195); // represents normal screen colour
_screens.Add(screen); _screens.Add(screen);
} }
} }
return _screens; return _screens;
} }
public int CompareTo(object obj)
{
if (!(obj is WinProfileItem)) throw new ArgumentException("Object to CompareTo is not a WinProfileItem"); ;
WinProfileItem otherProfile = (WinProfileItem)obj;
return this.Name.CompareTo(otherProfile.Name);
}
// The public override for the Object.Equals // The public override for the Object.Equals
public override bool Equals(object obj) public override bool Equals(object obj) => this.Equals(obj as WinProfileItem);
{
return this.Equals(obj as WinProfileItem);
}
// Profiles are equal if their Viewports are equal // Profiles are equal if their Viewports are equal
public bool Equals(WinProfileItem other) public bool Equals(WinProfileItem other)
@ -349,6 +360,23 @@ namespace DisplayMagicianShared.Windows
} }
public static bool operator ==(WinProfileItem lhs, WinProfileItem rhs)
{
if (lhs is null)
{
if (rhs is null)
{
return true;
}
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return lhs.Equals(rhs);
}
public static bool operator !=(WinProfileItem lhs, WinProfileItem rhs) => !(lhs == rhs);
} }
} }