Skip to content

Commit

Permalink
Update another set of PropertyStore usages (#12209)
Browse files Browse the repository at this point in the history
This actually removes one of the legacy methods, so yay progress.

In Form the owned forms set is now a List<Form> rather than a manually managed array.
  • Loading branch information
JeremyKuhne authored Sep 24, 2024
1 parent 572782a commit af25023
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 687 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,10 @@ private ActiveXImpl ActiveXInstance
throw new NotSupportedException(SR.AXTopLevelSource);
}

activeXImpl = new ActiveXImpl(this);

// PERF: IsActiveX is called quite a bit - checked everywhere from sizing to event raising. Using a
// state bit to track instead of fetching from the property store.
SetExtendedState(ExtendedStates.IsActiveX, true);
Properties.AddValue(s_activeXImplProperty, activeXImpl);
activeXImpl = Properties.AddValue(s_activeXImplProperty, new ActiveXImpl(this));
}

return activeXImpl;
Expand Down
12 changes: 3 additions & 9 deletions src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1976,8 +1976,7 @@ internal HFONT FontHandle
{
if (!Properties.TryGetValue(s_fontHandleWrapperProperty, out FontHandleWrapper? fontHandle))
{
fontHandle = new FontHandleWrapper(font);
Properties.AddValue(s_fontHandleWrapperProperty, fontHandle);
fontHandle = Properties.AddValue(s_fontHandleWrapperProperty, new FontHandleWrapper(font));
}

return fontHandle.Handle;
Expand All @@ -2003,10 +2002,7 @@ internal HFONT FontHandle

if (fontHandle is null)
{
font = ambientFont;
fontHandle = new FontHandleWrapper(font);

Properties.AddValue(s_fontHandleWrapperProperty, fontHandle);
fontHandle = Properties.AddValue(s_fontHandleWrapperProperty, new FontHandleWrapper(ambientFont));
}

return fontHandle.Handle;
Expand All @@ -2028,9 +2024,7 @@ protected int FontHeight

if (TryGetExplicitlySetFont(out Font? font))
{
fontHeight = font.Height;
Properties.AddValue(s_fontHeightProperty, fontHeight);
return fontHeight;
return Properties.AddValue(s_fontHeightProperty, font.Height);
}

// Ask the parent if it has the font height.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public Image? Image
{
get
{
Image? image = (Image?)Properties.GetObject(s_propImage);
Image? image = Properties.GetValueOrDefault<Image>(s_propImage);

if (image is null && ImageList is not null && ImageIndexer.ActualIndex >= 0)
{
Expand All @@ -382,28 +382,28 @@ public Image? Image
}
set
{
if (Image != value)
if (Image == value)
{
StopAnimate();
return;
}

Properties.SetObject(s_propImage, value);
if (value is not null)
{
ImageIndex = -1;
ImageList = null;
}
StopAnimate();

// Hook up the frame changed event
//
Animate();
Invalidate();
Properties.AddOrRemoveValue(s_propImage, value);
if (value is not null)
{
ImageIndex = -1;
ImageList = null;
}

// Hook up the frame changed event
Animate();
Invalidate();
}
}

/// <summary>
/// Gets or sets the index value of the images displayed on the
/// <see cref="Label"/>.
/// Gets or sets the index value of the images displayed on the <see cref="Label"/>.
/// </summary>
[TypeConverter(typeof(ImageIndexConverter))]
[Editor($"System.Windows.Forms.Design.ImageIndexEditor, {AssemblyRef.SystemDesign}", typeof(UITypeEditor))]
Expand Down Expand Up @@ -442,7 +442,7 @@ public int ImageIndex
if (value != ImageList.Indexer.DefaultIndex)
{
// Image.set calls ImageIndex = -1
Properties.SetObject(s_propImage, null);
Properties.RemoveValue(s_propImage);
}

ImageIndexer.Index = value;
Expand Down Expand Up @@ -472,7 +472,7 @@ public string? ImageKey
}

// Image.set calls ImageIndex = -1
Properties.SetObject(s_propImage, null);
Properties.RemoveValue(s_propImage);

ImageIndexer.Key = value;
Invalidate();
Expand All @@ -484,18 +484,15 @@ internal LabelImageIndexer ImageIndexer
get
{
// Demand create the ImageIndexer property
if ((!(Properties.GetObject(s_propImageIndex, out bool found) is LabelImageIndexer imageIndexer)) || (!found))
if (!Properties.TryGetValue(s_propImageIndex, out LabelImageIndexer? imageIndexer))
{
imageIndexer = new LabelImageIndexer(this);
ImageIndexer = imageIndexer;
}

return imageIndexer;
}
set
{
Properties.SetObject(s_propImageIndex, value);
}
set => Properties.AddOrRemoveValue(s_propImageIndex, value);
}

/// <summary>
Expand All @@ -507,38 +504,36 @@ internal LabelImageIndexer ImageIndexer
[SRCategory(nameof(SR.CatAppearance))]
public ImageList? ImageList
{
get => (ImageList?)Properties.GetObject(s_propImageList);
get => Properties.GetValueOrDefault<ImageList>(s_propImageList);
set
{
if (ImageList == value)
ImageList? imageList = ImageList;

if (imageList == value)
{
return;
}

EventHandler recreateHandler = new(ImageListRecreateHandle);
EventHandler disposedHandler = new(DetachImageList);

// Remove the previous imagelist handle recreate handler
ImageList? imageList = ImageList;
if (imageList is not null)
{
imageList.RecreateHandle -= recreateHandler;
imageList.Disposed -= disposedHandler;
imageList.RecreateHandle -= ImageListRecreateHandle;
imageList.Disposed -= DetachImageList;
}

// Make sure we don't have an Image as well as an ImageList
if (value is not null)
{
Properties.SetObject(s_propImage, null); // Image.set calls ImageList = null
Properties.RemoveValue(s_propImage);
}

Properties.SetObject(s_propImageList, value);
Properties.AddOrRemoveValue(s_propImageList, value);

// Add the new ImageList handle recreate handler
if (value is not null)
{
value.RecreateHandle += recreateHandler;
value.Disposed += disposedHandler;
value.RecreateHandle += ImageListRecreateHandle;
value.Disposed += DetachImageList;
}

Invalidate();
Expand Down Expand Up @@ -858,7 +853,7 @@ internal void AdjustSize()
private void Animate(bool animate)
{
bool currentlyAnimating = _labelState[s_stateAnimating] != 0;
if (animate == currentlyAnimating || Properties.GetObject(s_propImage) is not Image image)
if (animate == currentlyAnimating || !Properties.TryGetValue(s_propImage, out Image? image))
{
return;
}
Expand Down Expand Up @@ -951,17 +946,14 @@ protected override void Dispose(bool disposing)
StopAnimate();

// Holding on to images and image list is a memory leak.
if (ImageList is not null)
if (ImageList is { } imageList)
{
ImageList.Disposed -= DetachImageList;
ImageList.RecreateHandle -= ImageListRecreateHandle;
Properties.SetObject(s_propImageList, null);
imageList.Disposed -= DetachImageList;
imageList.RecreateHandle -= ImageListRecreateHandle;
Properties.RemoveValue(s_propImageList);
}

if (Image is not null)
{
Properties.SetObject(s_propImage, null);
}
Properties.RemoveValue(s_propImage);

_textToolTip?.Dispose();
_textToolTip = null;
Expand Down Expand Up @@ -1411,7 +1403,7 @@ protected override void SetBoundsCore(int x, int y, int width, int height, Bound

private void ResetImage() => Image = null;

private bool ShouldSerializeImage() => Properties.ContainsObjectThatIsNotNull(s_propImage);
private bool ShouldSerializeImage() => Properties.ContainsKey(s_propImage);

internal override void SetToolTip(ToolTip toolTip)
{
Expand Down
Loading

0 comments on commit af25023

Please sign in to comment.