Skip to content

Commit

Permalink
Refactor ContainsInteger usage
Browse files Browse the repository at this point in the history
  • Loading branch information
elachlan committed Jul 31, 2024
1 parent 5d30cba commit 96f178e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 67 deletions.
8 changes: 4 additions & 4 deletions src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6976,9 +6976,9 @@ protected virtual void OnFontChanged(EventArgs e)

Invalidate();

if (Properties.ContainsInteger(s_fontHeightProperty))
if (Properties.ContainsKey(s_fontHeightProperty))
{
Properties.SetInteger(s_fontHeightProperty, -1);
Properties.AddValue(s_fontHeightProperty, -1);
}

// Cleanup any font handle wrapper.
Expand Down Expand Up @@ -10936,9 +10936,9 @@ private protected void SetScaledFont(Font scaledFont, bool raiseOnFontChangedEve
// Dispose old FontHandle.
DisposeFontHandle();

if (Properties.ContainsInteger(s_fontHeightProperty))
if (Properties.ContainsKey(s_fontHeightProperty))
{
Properties.SetInteger(s_fontHeightProperty, scaledFont.Height);
Properties.AddValue(s_fontHeightProperty, scaledFont.Height);
}

if (!raiseOnFontChangedEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3433,15 +3433,15 @@ private bool ShouldSerializeAutoCompleteCustomSource()

internal bool ShouldSerializeDropDownWidth()
{
return (Properties.ContainsInteger(s_propDropDownWidth));
return (Properties.ContainsKey(s_propDropDownWidth));
}

/// <summary>
/// Indicates whether the itemHeight property should be persisted.
/// </summary>
internal bool ShouldSerializeItemHeight()
{
return (Properties.ContainsInteger(s_propItemHeight));
return (Properties.ContainsKey(s_propItemHeight));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,32 +442,32 @@ public override object Clone()

CloneInternal(dataGridViewCell);

if (Properties.ContainsObject(s_propLinkCellActiveLinkColor))
if (Properties.ContainsKey(s_propLinkCellActiveLinkColor))
{
dataGridViewCell.ActiveLinkColorInternal = ActiveLinkColor;
}

if (Properties.ContainsInteger(s_propLinkCellUseColumnTextForLinkValue))
if (Properties.ContainsKey(s_propLinkCellUseColumnTextForLinkValue))
{
dataGridViewCell.UseColumnTextForLinkValueInternal = UseColumnTextForLinkValue;
}

if (Properties.ContainsInteger(s_propLinkCellLinkBehavior))
if (Properties.ContainsKey(s_propLinkCellLinkBehavior))
{
dataGridViewCell.LinkBehaviorInternal = LinkBehavior;
}

if (Properties.ContainsObject(s_propLinkCellLinkColor))
if (Properties.ContainsKey(s_propLinkCellLinkColor))
{
dataGridViewCell.LinkColorInternal = LinkColor;
}

if (Properties.ContainsInteger(s_propLinkCellTrackVisitedState))
if (Properties.ContainsKey(s_propLinkCellTrackVisitedState))
{
dataGridViewCell.TrackVisitedStateInternal = TrackVisitedState;
}

if (Properties.ContainsObject(s_propLinkCellVisitedLinkColor))
if (Properties.ContainsKey(s_propLinkCellVisitedLinkColor))
{
dataGridViewCell.VisitedLinkColorInternal = VisitedLinkColor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1688,30 +1688,29 @@ public virtual RightToLeft RightToLeft
{
get
{
int rightToLeft = Properties.GetInteger(s_rightToLeftProperty, out bool found);
if (!found)
if (!Properties.TryGetValue(s_rightToLeftProperty, out RightToLeft rightToLeft))
{
rightToLeft = (int)RightToLeft.Inherit;
rightToLeft = RightToLeft.Inherit;
}

if (((RightToLeft)rightToLeft) == RightToLeft.Inherit)
if (rightToLeft == RightToLeft.Inherit)
{
if (Owner is not null)
{
rightToLeft = (int)Owner.RightToLeft;
rightToLeft = Owner.RightToLeft;
}
else if (ParentInternal is not null)
{
// case for Overflow & Grip
rightToLeft = (int)ParentInternal.RightToLeft;
rightToLeft = ParentInternal.RightToLeft;
}
else
{
rightToLeft = (int)DefaultRightToLeft;
rightToLeft = DefaultRightToLeft;
}
}

return (RightToLeft)rightToLeft;
return rightToLeft;
}

set
Expand All @@ -1720,9 +1719,9 @@ public virtual RightToLeft RightToLeft

RightToLeft oldValue = RightToLeft;

if (Properties.ContainsInteger(s_rightToLeftProperty) || value != RightToLeft.Inherit)
if (Properties.ContainsKey(s_rightToLeftProperty) || value != RightToLeft.Inherit)
{
Properties.SetInteger(s_rightToLeftProperty, (int)value);
Properties.AddValue(s_rightToLeftProperty, value);
}

if (oldValue != RightToLeft)
Expand Down Expand Up @@ -2990,7 +2989,7 @@ protected virtual void OnParentForeColorChanged(EventArgs e)
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected internal virtual void OnParentRightToLeftChanged(EventArgs e)
{
if (!Properties.ContainsInteger(s_rightToLeftProperty) || ((RightToLeft)Properties.GetInteger(s_rightToLeftProperty)) == RightToLeft.Inherit)
if (!Properties.TryGetValue(s_rightToLeftProperty, out RightToLeft rightToLeft) || rightToLeft == RightToLeft.Inherit)
{
OnRightToLeftChanged(e);
}
Expand All @@ -3006,13 +3005,12 @@ protected virtual void OnOwnerChanged(EventArgs e)
if (Owner is not null)
{
// check if we need to fire OnRightToLeftChanged
int rightToLeft = Properties.GetInteger(s_rightToLeftProperty, out bool found);
if (!found)
if (!Properties.TryGetValue(s_rightToLeftProperty, out RightToLeft rightToLeft))
{
rightToLeft = (int)RightToLeft.Inherit;
rightToLeft = RightToLeft.Inherit;
}

if ((rightToLeft == (int)RightToLeft.Inherit) && RightToLeft != DefaultRightToLeft)
if (rightToLeft == RightToLeft.Inherit && RightToLeft != DefaultRightToLeft)
{
OnRightToLeftChanged(EventArgs.Empty);
}
Expand Down Expand Up @@ -3422,13 +3420,12 @@ private bool ShouldSerializeImageIndex()
[EditorBrowsable(EditorBrowsableState.Never)]
internal virtual bool ShouldSerializeRightToLeft()
{
int rightToLeft = Properties.GetInteger(s_rightToLeftProperty, out bool found);
if (!found)
if (!Properties.TryGetValue(s_rightToLeftProperty, out RightToLeft rightToLeft))
{
return false;
}

return rightToLeft != (int)DefaultRightToLeft;
return rightToLeft != DefaultRightToLeft;
}

private bool ShouldSerializeTextDirection()
Expand Down
66 changes: 32 additions & 34 deletions src/System.Windows.Forms/src/System/Windows/Forms/Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ public partial class Form : ContainerControl
private static readonly int s_propMaximizedBounds = PropertyStore.CreateKey();
private static readonly int s_propOwnedFormsCount = PropertyStore.CreateKey();

private static readonly int s_propMinTrackSizeWidth = PropertyStore.CreateKey();
private static readonly int s_propMinTrackSizeHeight = PropertyStore.CreateKey();
private static readonly int s_propMaxTrackSizeWidth = PropertyStore.CreateKey();
private static readonly int s_propMaxTrackSizeHeight = PropertyStore.CreateKey();
private static readonly int s_propMinTrackSize = PropertyStore.CreateKey();
private static readonly int s_propMaxTrackSize = PropertyStore.CreateKey();

private static readonly int s_propFormMdiParent = PropertyStore.CreateKey();
private static readonly int s_propActiveMdiChild = PropertyStore.CreateKey();
Expand Down Expand Up @@ -1239,15 +1237,7 @@ public event EventHandler? MaximizedBoundsChanged
[DefaultValue(typeof(Size), "0, 0")]
public override Size MaximumSize
{
get
{
if (Properties.ContainsInteger(s_propMaxTrackSizeWidth))
{
return new Size(Properties.GetInteger(s_propMaxTrackSizeWidth), Properties.GetInteger(s_propMaxTrackSizeHeight));
}

return Size.Empty;
}
get => Properties.TryGetValue(s_propMaxTrackSize, out Size maximumSize) ? maximumSize : Size.Empty;
set
{
if (!value.Equals(MaximumSize))
Expand All @@ -1264,20 +1254,28 @@ public override Size MaximumSize

private void UpdateMaximumSize(Size value, bool updateFormSize = true)
{
Properties.SetInteger(s_propMaxTrackSizeWidth, value.Width);
Properties.SetInteger(s_propMaxTrackSizeHeight, value.Height);
Properties.AddValue(s_propMaxTrackSize, value);

// Bump minimum size if necessary
if (!MinimumSize.IsEmpty && !value.IsEmpty)
{
if (Properties.GetInteger(s_propMinTrackSizeWidth) > value.Width)
Size minimumSize = MinimumSize;
bool shouldUpdate = false;
if (minimumSize.Width > value.Width)
{
minimumSize.Width = value.Width;
shouldUpdate = true;
}

if (minimumSize.Height > value.Height)
{
Properties.SetInteger(s_propMinTrackSizeWidth, value.Width);
minimumSize.Height = value.Height;
shouldUpdate = true;
}

if (Properties.GetInteger(s_propMinTrackSizeHeight) > value.Height)
if (shouldUpdate)
{
Properties.SetInteger(s_propMinTrackSizeHeight, value.Height);
Properties.AddValue(s_propMinTrackSize, minimumSize);
}
}

Expand Down Expand Up @@ -1356,15 +1354,7 @@ public MenuStrip? MainMenuStrip
[RefreshProperties(RefreshProperties.Repaint)]
public override Size MinimumSize
{
get
{
if (Properties.ContainsInteger(s_propMinTrackSizeWidth))
{
return new Size(Properties.GetInteger(s_propMinTrackSizeWidth), Properties.GetInteger(s_propMinTrackSizeHeight));
}

return DefaultMinimumSize;
}
get => Properties.TryGetValue(s_propMinTrackSize, out Size minimumSize) ? minimumSize : DefaultMinimumSize;
set
{
if (!value.Equals(MinimumSize))
Expand All @@ -1385,20 +1375,28 @@ public override Size MinimumSize

private void UpdateMinimumSize(Size value, bool updateFormSize = true)
{
Properties.SetInteger(s_propMinTrackSizeWidth, value.Width);
Properties.SetInteger(s_propMinTrackSizeHeight, value.Height);
Properties.AddValue(s_propMinTrackSize, value);

// Bump maximum size if necessary
if (!MaximumSize.IsEmpty && !value.IsEmpty)
{
if (Properties.GetInteger(s_propMaxTrackSizeWidth) < value.Width)
Size maximumSize = MaximumSize;
bool shouldUpdate = false;
if (maximumSize.Width < value.Width)
{
maximumSize.Width = value.Width;
shouldUpdate = true;
}

if (maximumSize.Height < value.Height)
{
Properties.SetInteger(s_propMaxTrackSizeWidth, value.Width);
maximumSize.Height = value.Height;
shouldUpdate = true;
}

if (Properties.GetInteger(s_propMaxTrackSizeHeight) < value.Height)
if (shouldUpdate)
{
Properties.SetInteger(s_propMaxTrackSizeHeight, value.Height);
Properties.AddValue(s_propMaxTrackSize, maximumSize);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ internal class PropertyStore
/// </summary>
public bool ContainsKey(int key) => _values.ContainsKey(key);

// REMOVE
public bool ContainsInteger(int key) => _values.ContainsKey(key);

// REMOVE
public bool ContainsObject(int key) => _values.ContainsKey(key);

Expand Down

0 comments on commit 96f178e

Please sign in to comment.