Blish-HUD icon indicating copy to clipboard operation
Blish-HUD copied to clipboard

Control collection parent fixes

Open eksime opened this issue 1 year ago • 4 comments

Discord Discussion

Breaking change: no

Likely fixes #886

This reworks the internals of the ControlCollection<T> class, with the following changes:

  • null values are unable to be added to the collection, with short-circuits for methods when item is null
  • Cancelling an AddChild or RemoveChild action from a Container should no longer cause the Parent property of the control in question to be in a potentially invalid state.
  • Create a copy of the internal array when iterating, as with some BCL concurrent types, as we can't be sure external code will dispose of the enumerator correctly.
  • Provide (internal) ReaderWriterLockSlim helper methods that allow basic read and write locks to use using (...) { ... } syntax rather than having to use try/finally everywhere.
  • No longer clear Parent on controls when calling Clear - this should be handled by the Container as there's no guarantee that the ControlCollection is being used for parenting/hierarchy purposes.
  • Update Container to explicitly set .Parent property on added or removed children.

Notes:

  • I'm working on the assumption that the intended behaviour for the control collection is that it never contains nulls and never has duplicates, if either of those assumptions are incorrect then I'll need to rework this change.
  • I'm unsure if the indexer setter for ControlCollection<T> should remove the item when attempting to set an indexed position to null or if it should be a noop, currently it removes the item. Additionally, if an index is set to an item that exists elsewhere in the collection, it currently overwrites the index, then removes the duplicate.

eksime avatar May 27 '23 20:05 eksime