Skip to content

Commit

Permalink
Refactor PropertyStore.GetInteger
Browse files Browse the repository at this point in the history
  • Loading branch information
elachlan committed Aug 1, 2024
1 parent da14311 commit 588fa23
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,14 @@ internal int ImeWmCharsToIgnore
// after all messages are sent, corresponding WM_CHAR messages are also sent. (in non-unicode
// windows two WM_CHAR messages are sent per char in the IME). We need to keep a counter
// not to process each character twice or more.
get => Properties.GetInteger(s_imeWmCharsToIgnoreProperty);
get => Properties.GetValueOrDefault<int>(s_imeWmCharsToIgnoreProperty);
set
{
// WM_CHAR is not send after WM_IME_CHAR when the composition has been closed by either, changing the conversion mode or
// dissociating the IME (for instance when loosing focus and conversion is forced to complete).
if (ImeWmCharsToIgnore != ImeCharsToIgnoreDisabled)
{
Properties.SetInteger(s_imeWmCharsToIgnoreProperty, value);
Properties.AddValue(s_imeWmCharsToIgnoreProperty, value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,18 +447,14 @@ public DrawMode DrawMode
[SRDescription(nameof(SR.ComboBoxDropDownWidthDescr))]
public int DropDownWidth
{
get
{
int dropDownWidth = Properties.GetInteger(s_propDropDownWidth, out bool found);
return found ? dropDownWidth : Width;
}
get => Properties.TryGetValue(s_propDropDownWidth, out int dropDownWidth) ? dropDownWidth : Width;
set
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);

if (Properties.GetInteger(s_propDropDownWidth) != value)
if (Properties.GetValueOrDefault<int>(s_propDropDownWidth) != value)
{
Properties.SetInteger(s_propDropDownWidth, value);
Properties.AddValue(s_propDropDownWidth, value);
if (IsHandleCreated)
{
PInvoke.SendMessage(this, PInvoke.CB_SETDROPPEDWIDTH, (WPARAM)value);
Expand All @@ -477,35 +473,24 @@ public int DropDownWidth
[DefaultValue(106)]
public int DropDownHeight
{
get
{
int dropDownHeight = Properties.GetInteger(s_propDropDownHeight, out bool found);
if (found)
{
return dropDownHeight;
}
else
{
return DefaultDropDownHeight;
}
}
get => Properties.TryGetValue(s_propDropDownHeight, out int dropDownHeight) ? dropDownHeight : DefaultDropDownHeight;
set
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);

if (Properties.GetInteger(s_propDropDownHeight) != value)
if (Properties.GetValueOrDefault<int>(s_propDropDownHeight) != value)
{
Properties.SetInteger(s_propDropDownHeight, value);
Properties.AddValue(s_propDropDownHeight, value);

// The dropDownHeight is not reflected unless the
// combobox integralHeight == false..
// ComboBox integralHeight == false..
IntegralHeight = false;
}
}
}

/// <summary>
/// Indicates whether the DropDown of the combo is currently dropped down.
/// Indicates whether the DropDown of the combo is currently dropped down.
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
Expand Down Expand Up @@ -636,37 +621,29 @@ public int ItemHeight
drawMode == DrawMode.OwnerDrawVariable ||
!IsHandleCreated)
{
int itemHeight = Properties.GetInteger(s_propItemHeight, out bool found);
if (found)
{
return itemHeight;
}
else
{
return FontHeight + 2;
}
return Properties.TryGetValue(s_propItemHeight, out int itemHeight) ? itemHeight : FontHeight + 2;
}

// Note that the above if clause deals with the case when the handle has not yet been created
Debug.Assert(IsHandleCreated, "Handle should be created at this point");

int h = (int)PInvoke.SendMessage(this, PInvoke.CB_GETITEMHEIGHT);
if (h == -1)
int height = (int)PInvoke.SendMessage(this, PInvoke.CB_GETITEMHEIGHT);
if (height == -1)
{
throw new Win32Exception();
}

return h;
return height;
}
set
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);

ResetHeightCache();

if (Properties.GetInteger(s_propItemHeight) != value)
if (Properties.GetValueOrDefault<int>(s_propItemHeight) != value)
{
Properties.SetInteger(s_propItemHeight, value);
Properties.AddValue(s_propItemHeight, value);
if (DrawMode != DrawMode.Normal)
{
UpdateItemHeight();
Expand Down Expand Up @@ -759,7 +736,7 @@ public int MaxLength
{
get
{
return Properties.GetInteger(s_propMaxLength);
return Properties.GetValueOrDefault<int>(s_propMaxLength);
}
set
{
Expand All @@ -770,7 +747,7 @@ public int MaxLength

if (MaxLength != value)
{
Properties.SetInteger(s_propMaxLength, value);
Properties.AddValue(s_propMaxLength, value);
if (IsHandleCreated)
{
PInvoke.SendMessage(this, PInvoke.CB_LIMITTEXT, (WPARAM)value);
Expand Down
18 changes: 9 additions & 9 deletions src/System.Windows.Forms/src/System/Windows/Forms/Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,7 @@ public Form[] OwnedForms
get
{
Form?[]? ownedForms = (Form?[]?)Properties.GetObject(s_propOwnedForms);
int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount);
int ownedFormsCount = Properties.GetValueOrDefault<int>(s_propOwnedFormsCount);

Form[] result = new Form[ownedFormsCount];
if (ownedFormsCount > 0)
Expand Down Expand Up @@ -2635,7 +2635,7 @@ public void AddOwnedForm(Form? ownedForm)
}

Form?[]? ownedForms = (Form?[]?)Properties.GetObject(s_propOwnedForms);
int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount);
int ownedFormsCount = Properties.GetValueOrDefault<int>(s_propOwnedFormsCount);

// Make sure this isn't already in the list:
for (int i = 0; i < ownedFormsCount; i++)
Expand Down Expand Up @@ -3324,7 +3324,7 @@ protected override void Dispose(bool disposing)
Properties.SetObject(s_propDialogOwner, null);

Form?[]? ownedForms = (Form?[]?)Properties.GetObject(s_propOwnedForms);
int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount);
int ownedFormsCount = Properties.GetValueOrDefault<int>(s_propOwnedFormsCount);

for (int i = ownedFormsCount - 1; i >= 0; i--)
{
Expand Down Expand Up @@ -4580,7 +4580,7 @@ internal void RaiseFormClosedOnAppExit()
{
// Fire FormClosed event on all the forms that this form owns and are not in the Application.OpenForms collection
// This is to be consistent with what WmClose does.
int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount);
int ownedFormsCount = Properties.GetValueOrDefault<int>(s_propOwnedFormsCount);
if (ownedFormsCount > 0)
{
Form[] ownedForms = OwnedForms;
Expand Down Expand Up @@ -4610,7 +4610,7 @@ internal bool RaiseFormClosingOnAppExit()
{
// Fire FormClosing event on all the forms that this form owns and are not in the Application.OpenForms collection
// This is to be consistent with what WmClose does.
int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount);
int ownedFormsCount = Properties.GetValueOrDefault<int>(s_propOwnedFormsCount);
if (ownedFormsCount > 0)
{
Form[] ownedForms = OwnedForms;
Expand Down Expand Up @@ -4712,7 +4712,7 @@ public void RemoveOwnedForm(Form? ownedForm)
}

Form?[]? ownedForms = (Form?[]?)Properties.GetObject(s_propOwnedForms);
int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount);
int ownedFormsCount = Properties.GetValueOrDefault<int>(s_propOwnedFormsCount);

if (ownedForms is not null)
{
Expand All @@ -4734,7 +4734,7 @@ public void RemoveOwnedForm(Form? ownedForm)
}
}

Properties.SetInteger(s_propOwnedFormsCount, ownedFormsCount);
Properties.AddValue(s_propOwnedFormsCount, ownedFormsCount);
}
}

Expand Down Expand Up @@ -6150,7 +6150,7 @@ private void WmClose(ref Message m)

// Call OnClosing/OnFormClosing on all the forms that current form owns.
Form[] ownedForms = OwnedForms;
int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount);
int ownedFormsCount = Properties.GetValueOrDefault<int>(s_propOwnedFormsCount);
for (int i = ownedFormsCount - 1; i >= 0; i--)
{
FormClosingEventArgs cfe = new(CloseReason.FormOwnerClosing, e.Cancel);
Expand Down Expand Up @@ -6216,7 +6216,7 @@ private void WmClose(ref Message m)

// Call OnClosed/OnFormClosed on all the forms that current form owns.
Form[] ownedForms = OwnedForms;
int ownedFormsCount = Properties.GetInteger(s_propOwnedFormsCount);
int ownedFormsCount = Properties.GetValueOrDefault<int>(s_propOwnedFormsCount);
for (int i = ownedFormsCount - 1; i >= 0; i--)
{
fc = new FormClosedEventArgs(CloseReason.FormOwnerClosing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,9 @@ internal static bool HasLayoutBounds(IArrangedElement element)
/// </para>
/// </remarks>
internal static BitVector32 GetLayoutState(IArrangedElement element) =>
new BitVector32(element.Properties.GetInteger(s_layoutStateProperty));
element.Properties.GetValueOrDefault<BitVector32>(s_layoutStateProperty);

internal static void SetLayoutState(IArrangedElement element, BitVector32 state) =>
element.Properties.SetInteger(s_layoutStateProperty, state.Data);
element.Properties.AddValue(s_layoutStateProperty, state);
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,23 +279,23 @@ private static Size TryCalculatePreferredSizeRow(
}

public static bool GetWrapContents(IArrangedElement container) =>
container.Properties.GetInteger(s_wrapContentsProperty) == 0;
!container.Properties.TryGetValue(s_wrapContentsProperty, out bool wrap) || wrap;

public static void SetWrapContents(IArrangedElement container, bool value)
{
container.Properties.SetInteger(s_wrapContentsProperty, value ? 0 : 1);
container.Properties.AddValue(s_wrapContentsProperty, value);
LayoutTransaction.DoLayout(container, container, PropertyNames.WrapContents);
Debug.Assert(GetWrapContents(container) == value, "GetWrapContents should return the same value as we set");
}

public static FlowDirection GetFlowDirection(IArrangedElement container) =>
(FlowDirection)container.Properties.GetInteger(s_flowDirectionProperty);
container.Properties.GetValueOrDefault<FlowDirection>(s_flowDirectionProperty);

public static void SetFlowDirection(IArrangedElement container, FlowDirection value)
{
SourceGenerated.EnumValidator.Validate(value);

container.Properties.SetInteger(s_flowDirectionProperty, (int)value);
container.Properties.AddValue(s_flowDirectionProperty, value);
LayoutTransaction.DoLayout(container, container, PropertyNames.FlowDirection);
Debug.Assert(GetFlowDirection(container) == value, "GetFlowDirection should return the same value as we set");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ internal class PropertyStore
/// </summary>
public static int CreateKey() => s_currentKey++;

// REMOVE
/// <summary>
/// Retrieves an integer value from our property list.
/// This will set value to zero and return false if the
/// list does not contain the given key.
/// </summary>
public int GetInteger(int key) => GetInteger(key, out _);

// REMOVE
/// <summary>
/// Retrieves an integer value from our property list.
Expand Down

0 comments on commit 588fa23

Please sign in to comment.