DefinitionBase.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. // This source file is adapted from the Windows Presentation Foundation project.
  2. // (https://github.com/dotnet/wpf/)
  3. //
  4. // Licensed to The Avalonia Project under MIT License, courtesy of The .NET Foundation.
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using Avalonia.Reactive;
  10. using Avalonia.Utilities;
  11. namespace Avalonia.Controls
  12. {
  13. /// <summary>
  14. /// DefinitionBase provides core functionality used internally by Grid
  15. /// and ColumnDefinitionCollection / RowDefinitionCollection
  16. /// </summary>
  17. public abstract class DefinitionBase : AvaloniaObject
  18. {
  19. /// <summary>
  20. /// SharedSizeGroup property.
  21. /// </summary>
  22. public string SharedSizeGroup
  23. {
  24. get { return (string)GetValue(SharedSizeGroupProperty); }
  25. set { SetValue(SharedSizeGroupProperty, value); }
  26. }
  27. /// <summary>
  28. /// Callback to notify about entering model tree.
  29. /// </summary>
  30. internal void OnEnterParentTree()
  31. {
  32. this.InheritanceParent = Parent;
  33. if (_sharedState == null)
  34. {
  35. // start with getting SharedSizeGroup value.
  36. // this property is NOT inherited which should result in better overall perf.
  37. string sharedSizeGroupId = SharedSizeGroup;
  38. if (sharedSizeGroupId != null)
  39. {
  40. SharedSizeScope? privateSharedSizeScope = PrivateSharedSizeScope;
  41. if (privateSharedSizeScope != null)
  42. {
  43. _sharedState = privateSharedSizeScope.EnsureSharedState(sharedSizeGroupId);
  44. _sharedState.AddMember(this);
  45. }
  46. }
  47. }
  48. Parent?.InvalidateMeasure();
  49. }
  50. /// <summary>
  51. /// Callback to notify about exiting model tree.
  52. /// </summary>
  53. internal void OnExitParentTree()
  54. {
  55. _offset = 0;
  56. if (_sharedState != null)
  57. {
  58. _sharedState.RemoveMember(this);
  59. _sharedState = null;
  60. }
  61. Parent?.InvalidateMeasure();
  62. }
  63. /// <summary>
  64. /// Performs action preparing definition to enter layout calculation mode.
  65. /// </summary>
  66. internal void OnBeforeLayout(Grid grid)
  67. {
  68. // reset layout state.
  69. _minSize = 0;
  70. LayoutWasUpdated = true;
  71. // defer verification for shared definitions
  72. if (_sharedState != null) { _sharedState.EnsureDeferredValidation(grid); }
  73. }
  74. /// <summary>
  75. /// Updates min size.
  76. /// </summary>
  77. /// <param name="minSize">New size.</param>
  78. internal void UpdateMinSize(double minSize)
  79. {
  80. _minSize = Math.Max(_minSize, minSize);
  81. }
  82. /// <summary>
  83. /// Sets min size.
  84. /// </summary>
  85. /// <param name="minSize">New size.</param>
  86. internal void SetMinSize(double minSize)
  87. {
  88. _minSize = minSize;
  89. }
  90. /// <remarks>
  91. /// This method reflects Grid.SharedScopeProperty state by setting / clearing
  92. /// dynamic property PrivateSharedSizeScopeProperty. Value of PrivateSharedSizeScopeProperty
  93. /// is a collection of SharedSizeState objects for the scope.
  94. /// </remarks>
  95. internal static void OnIsSharedSizeScopePropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
  96. {
  97. if ((bool)e.NewValue!)
  98. {
  99. SharedSizeScope sharedStatesCollection = new SharedSizeScope();
  100. d.SetValue(PrivateSharedSizeScopeProperty, sharedStatesCollection);
  101. }
  102. else
  103. {
  104. d.ClearValue(PrivateSharedSizeScopeProperty);
  105. }
  106. }
  107. /// <remarks>
  108. /// Notifies parent <see cref="Grid"/> or size scope that definition size has been changed.
  109. /// </remarks>
  110. internal static void OnUserSizePropertyChanged(DefinitionBase definition, AvaloniaPropertyChangedEventArgs e)
  111. {
  112. if (definition.Parent == null)
  113. {
  114. return;
  115. }
  116. if (definition._sharedState != null)
  117. {
  118. definition._sharedState.Invalidate();
  119. }
  120. else
  121. {
  122. GridUnitType oldUnitType = ((GridLength)e.OldValue!).GridUnitType;
  123. GridUnitType newUnitType = ((GridLength)e.NewValue!).GridUnitType;
  124. if (oldUnitType != newUnitType)
  125. {
  126. definition.Parent.Invalidate();
  127. }
  128. else
  129. {
  130. definition.Parent.InvalidateMeasure();
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// Returns <c>true</c> if this definition is a part of shared group.
  136. /// </summary>
  137. internal bool IsShared
  138. {
  139. get { return (_sharedState != null); }
  140. }
  141. /// <summary>
  142. /// Internal accessor to user size field.
  143. /// </summary>
  144. internal GridLength UserSize
  145. {
  146. get { return (_sharedState != null ? _sharedState.UserSize : UserSizeValueCache); }
  147. }
  148. /// <summary>
  149. /// Internal accessor to user min size field.
  150. /// </summary>
  151. internal double UserMinSize
  152. {
  153. get { return (UserMinSizeValueCache); }
  154. }
  155. /// <summary>
  156. /// Internal accessor to user max size field.
  157. /// </summary>
  158. internal double UserMaxSize
  159. {
  160. get { return (UserMaxSizeValueCache); }
  161. }
  162. /// <summary>
  163. /// DefinitionBase's index in the parents collection.
  164. /// </summary>
  165. internal int Index
  166. {
  167. get
  168. {
  169. return (_parentIndex);
  170. }
  171. set
  172. {
  173. Debug.Assert(value >= -1);
  174. _parentIndex = value;
  175. }
  176. }
  177. /// <summary>
  178. /// Layout-time user size type.
  179. /// </summary>
  180. internal Grid.LayoutTimeSizeType SizeType
  181. {
  182. get { return (_sizeType); }
  183. set { _sizeType = value; }
  184. }
  185. /// <summary>
  186. /// Returns or sets measure size for the definition.
  187. /// </summary>
  188. internal double MeasureSize
  189. {
  190. get { return (_measureSize); }
  191. set { _measureSize = value; }
  192. }
  193. /// <summary>
  194. /// Returns definition's layout time type sensitive preferred size.
  195. /// </summary>
  196. /// <remarks>
  197. /// Returned value is guaranteed to be true preferred size.
  198. /// </remarks>
  199. internal double PreferredSize
  200. {
  201. get
  202. {
  203. double preferredSize = MinSize;
  204. if (_sizeType != Grid.LayoutTimeSizeType.Auto
  205. && preferredSize < _measureSize)
  206. {
  207. preferredSize = _measureSize;
  208. }
  209. return (preferredSize);
  210. }
  211. }
  212. /// <summary>
  213. /// Returns or sets size cache for the definition.
  214. /// </summary>
  215. internal double SizeCache
  216. {
  217. get { return (_sizeCache); }
  218. set { _sizeCache = value; }
  219. }
  220. /// <summary>
  221. /// Returns min size.
  222. /// </summary>
  223. internal double MinSize
  224. {
  225. get
  226. {
  227. double minSize = _minSize;
  228. if (UseSharedMinimum
  229. && _sharedState != null
  230. && minSize < _sharedState.MinSize)
  231. {
  232. minSize = _sharedState.MinSize;
  233. }
  234. return (minSize);
  235. }
  236. }
  237. /// <summary>
  238. /// Returns min size, always taking into account shared state.
  239. /// </summary>
  240. internal double MinSizeForArrange
  241. {
  242. get
  243. {
  244. double minSize = _minSize;
  245. if (_sharedState != null
  246. && (UseSharedMinimum || !LayoutWasUpdated)
  247. && minSize < _sharedState.MinSize)
  248. {
  249. minSize = _sharedState.MinSize;
  250. }
  251. return (minSize);
  252. }
  253. }
  254. /// <summary>
  255. /// Offset.
  256. /// </summary>
  257. internal double FinalOffset
  258. {
  259. get { return _offset; }
  260. set { _offset = value; }
  261. }
  262. /// <summary>
  263. /// Internal helper to access up-to-date UserSize property value.
  264. /// </summary>
  265. internal abstract GridLength UserSizeValueCache { get; }
  266. /// <summary>
  267. /// Internal helper to access up-to-date UserMinSize property value.
  268. /// </summary>
  269. internal abstract double UserMinSizeValueCache { get; }
  270. /// <summary>
  271. /// Internal helper to access up-to-date UserMaxSize property value.
  272. /// </summary>
  273. internal abstract double UserMaxSizeValueCache { get; }
  274. internal Grid? Parent { get; set; }
  275. /// <summary>
  276. /// SetFlags is used to set or unset one or multiple
  277. /// flags on the object.
  278. /// </summary>
  279. private void SetFlags(bool value, Flags flags)
  280. {
  281. _flags = value ? (_flags | flags) : (_flags & (~flags));
  282. }
  283. /// <summary>
  284. /// CheckFlagsAnd returns <c>true</c> if all the flags in the
  285. /// given bitmask are set on the object.
  286. /// </summary>
  287. private bool CheckFlagsAnd(Flags flags)
  288. {
  289. return ((_flags & flags) == flags);
  290. }
  291. private static void OnSharedSizeGroupPropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
  292. {
  293. DefinitionBase definition = (DefinitionBase)d;
  294. if (definition.Parent != null)
  295. {
  296. string sharedSizeGroupId = (string)e.NewValue!;
  297. if (definition._sharedState != null)
  298. {
  299. // if definition is already registered AND shared size group id is changing,
  300. // then un-register the definition from the current shared size state object.
  301. definition._sharedState.RemoveMember(definition);
  302. definition._sharedState = null;
  303. }
  304. if ((definition._sharedState == null) && (sharedSizeGroupId != null))
  305. {
  306. SharedSizeScope? privateSharedSizeScope = definition.PrivateSharedSizeScope;
  307. if (privateSharedSizeScope != null)
  308. {
  309. // if definition is not registered and both: shared size group id AND private shared scope
  310. // are available, then register definition.
  311. definition._sharedState = privateSharedSizeScope.EnsureSharedState(sharedSizeGroupId);
  312. definition._sharedState.AddMember(definition);
  313. }
  314. }
  315. }
  316. }
  317. /// <remarks>
  318. /// Verifies that Shared Size Group Property string
  319. /// a) not empty.
  320. /// b) contains only letters, digits and underscore ('_').
  321. /// c) does not start with a digit.
  322. /// </remarks>
  323. private static bool SharedSizeGroupPropertyValueValid(string value)
  324. {
  325. // null is default value
  326. if (value == null)
  327. {
  328. return true;
  329. }
  330. string id = (string)value;
  331. if (!string.IsNullOrEmpty(id))
  332. {
  333. int i = -1;
  334. while (++i < id.Length)
  335. {
  336. bool isDigit = Char.IsDigit(id[i]);
  337. if ((i == 0 && isDigit)
  338. || !(isDigit
  339. || Char.IsLetter(id[i])
  340. || '_' == id[i]))
  341. {
  342. break;
  343. }
  344. }
  345. if (i == id.Length)
  346. {
  347. return true;
  348. }
  349. }
  350. return false;
  351. }
  352. /// <remark>
  353. /// OnPrivateSharedSizeScopePropertyChanged is called when new scope enters or
  354. /// existing scope just left. In both cases if the DefinitionBase object is already registered
  355. /// in SharedSizeState, it should un-register and register itself in a new one.
  356. /// </remark>
  357. private static void OnPrivateSharedSizeScopePropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
  358. {
  359. DefinitionBase definition = (DefinitionBase)d;
  360. if (definition.Parent != null)
  361. {
  362. SharedSizeScope privateSharedSizeScope = (SharedSizeScope)e.NewValue!;
  363. if (definition._sharedState != null)
  364. {
  365. // if definition is already registered And shared size scope is changing,
  366. // then un-register the definition from the current shared size state object.
  367. definition._sharedState.RemoveMember(definition);
  368. definition._sharedState = null;
  369. }
  370. if ((definition._sharedState == null) && (privateSharedSizeScope != null))
  371. {
  372. string sharedSizeGroup = definition.SharedSizeGroup;
  373. if (sharedSizeGroup != null)
  374. {
  375. // if definition is not registered and both: shared size group id AND private shared scope
  376. // are available, then register definition.
  377. definition._sharedState = privateSharedSizeScope.EnsureSharedState(definition.SharedSizeGroup);
  378. definition._sharedState.AddMember(definition);
  379. }
  380. }
  381. }
  382. }
  383. /// <summary>
  384. /// Private getter of shared state collection dynamic property.
  385. /// </summary>
  386. private SharedSizeScope? PrivateSharedSizeScope
  387. {
  388. get { return (SharedSizeScope?)GetValue(PrivateSharedSizeScopeProperty); }
  389. }
  390. /// <summary>
  391. /// Convenience accessor to UseSharedMinimum flag
  392. /// </summary>
  393. private bool UseSharedMinimum
  394. {
  395. get { return (CheckFlagsAnd(Flags.UseSharedMinimum)); }
  396. set { SetFlags(value, Flags.UseSharedMinimum); }
  397. }
  398. /// <summary>
  399. /// Convenience accessor to LayoutWasUpdated flag
  400. /// </summary>
  401. private bool LayoutWasUpdated
  402. {
  403. get { return (CheckFlagsAnd(Flags.LayoutWasUpdated)); }
  404. set { SetFlags(value, Flags.LayoutWasUpdated); }
  405. }
  406. private Flags _flags; // flags reflecting various aspects of internal state
  407. internal int _parentIndex = -1; // this instance's index in parent's children collection
  408. private Grid.LayoutTimeSizeType _sizeType; // layout-time user size type. it may differ from _userSizeValueCache.UnitType when calculating "to-content"
  409. private double _minSize; // used during measure to accumulate size for "Auto" and "Star" DefinitionBase's
  410. private double _measureSize; // size, calculated to be the input constraint size for Child.Measure
  411. private double _sizeCache; // cache used for various purposes (sorting, caching, etc) during calculations
  412. private double _offset; // offset of the DefinitionBase from left / top corner (assuming LTR case)
  413. private SharedSizeState? _sharedState; // reference to shared state object this instance is registered with
  414. [System.Flags]
  415. private enum Flags : byte
  416. {
  417. //
  418. // bool flags
  419. //
  420. UseSharedMinimum = 0x00000020, // when "1", definition will take into account shared state's minimum
  421. LayoutWasUpdated = 0x00000040, // set to "1" every time the parent grid is measured
  422. }
  423. /// <summary>
  424. /// Collection of shared states objects for a single scope
  425. /// </summary>
  426. internal class SharedSizeScope
  427. {
  428. /// <summary>
  429. /// Returns SharedSizeState object for a given group.
  430. /// Creates a new StatedState object if necessary.
  431. /// </summary>
  432. internal SharedSizeState EnsureSharedState(string sharedSizeGroup)
  433. {
  434. // check that sharedSizeGroup is not default
  435. Debug.Assert(sharedSizeGroup != null);
  436. SharedSizeState? sharedState = _registry[sharedSizeGroup] as SharedSizeState;
  437. if (sharedState == null)
  438. {
  439. sharedState = new SharedSizeState(this, sharedSizeGroup);
  440. _registry[sharedSizeGroup] = sharedState;
  441. }
  442. return (sharedState);
  443. }
  444. /// <summary>
  445. /// Removes an entry in the registry by the given key.
  446. /// </summary>
  447. internal void Remove(object key)
  448. {
  449. Debug.Assert(_registry.Contains(key));
  450. _registry.Remove(key);
  451. }
  452. private Hashtable _registry = new Hashtable(); // storage for shared state objects
  453. }
  454. /// <summary>
  455. /// Implementation of per shared group state object
  456. /// </summary>
  457. internal class SharedSizeState
  458. {
  459. /// <summary>
  460. /// Default ctor.
  461. /// </summary>
  462. internal SharedSizeState(SharedSizeScope sharedSizeScope, string sharedSizeGroupId)
  463. {
  464. Debug.Assert(sharedSizeScope != null && sharedSizeGroupId != null);
  465. _sharedSizeScope = sharedSizeScope;
  466. _sharedSizeGroupId = sharedSizeGroupId;
  467. _registry = new List<DefinitionBase>();
  468. _layoutUpdated = new EventHandler(OnLayoutUpdated);
  469. _broadcastInvalidation = true;
  470. }
  471. /// <summary>
  472. /// Adds / registers a definition instance.
  473. /// </summary>
  474. internal void AddMember(DefinitionBase member)
  475. {
  476. Debug.Assert(!_registry.Contains(member));
  477. _registry.Add(member);
  478. Invalidate();
  479. }
  480. /// <summary>
  481. /// Removes / un-registers a definition instance.
  482. /// </summary>
  483. /// <remarks>
  484. /// If the collection of registered definitions becomes empty
  485. /// instantiates self removal from owner's collection.
  486. /// </remarks>
  487. internal void RemoveMember(DefinitionBase member)
  488. {
  489. Invalidate();
  490. _registry.Remove(member);
  491. if (_registry.Count == 0)
  492. {
  493. _sharedSizeScope.Remove(_sharedSizeGroupId);
  494. }
  495. }
  496. /// <summary>
  497. /// Propagates invalidations for all registered definitions.
  498. /// Resets its own state.
  499. /// </summary>
  500. internal void Invalidate()
  501. {
  502. _userSizeValid = false;
  503. if (_broadcastInvalidation)
  504. {
  505. for (int i = 0, count = _registry.Count; i < count; ++i)
  506. {
  507. Grid parentGrid = (Grid)(_registry[i].Parent!);
  508. parentGrid.Invalidate();
  509. }
  510. _broadcastInvalidation = false;
  511. }
  512. }
  513. /// <summary>
  514. /// Makes sure that one and only one layout updated handler is registered for this shared state.
  515. /// </summary>
  516. internal void EnsureDeferredValidation(Control layoutUpdatedHost)
  517. {
  518. if (_layoutUpdatedHost == null)
  519. {
  520. _layoutUpdatedHost = layoutUpdatedHost;
  521. _layoutUpdatedHost.LayoutUpdated += _layoutUpdated;
  522. }
  523. }
  524. /// <summary>
  525. /// DefinitionBase's specific code.
  526. /// </summary>
  527. internal double MinSize
  528. {
  529. get
  530. {
  531. if (!_userSizeValid) { EnsureUserSizeValid(); }
  532. return (_minSize);
  533. }
  534. }
  535. /// <summary>
  536. /// DefinitionBase's specific code.
  537. /// </summary>
  538. internal GridLength UserSize
  539. {
  540. get
  541. {
  542. if (!_userSizeValid) { EnsureUserSizeValid(); }
  543. return (_userSize);
  544. }
  545. }
  546. private void EnsureUserSizeValid()
  547. {
  548. _userSize = new GridLength(1, GridUnitType.Auto);
  549. for (int i = 0, count = _registry.Count; i < count; ++i)
  550. {
  551. Debug.Assert(_userSize.GridUnitType == GridUnitType.Auto
  552. || _userSize.GridUnitType == GridUnitType.Pixel);
  553. GridLength currentGridLength = _registry[i].UserSizeValueCache;
  554. if (currentGridLength.GridUnitType == GridUnitType.Pixel)
  555. {
  556. if (_userSize.GridUnitType == GridUnitType.Auto)
  557. {
  558. _userSize = currentGridLength;
  559. }
  560. else if (_userSize.Value < currentGridLength.Value)
  561. {
  562. _userSize = currentGridLength;
  563. }
  564. }
  565. }
  566. // taking maximum with user size effectively prevents squishy-ness.
  567. // this is a "solution" to avoid shared definitions from been sized to
  568. // different final size at arrange time, if / when different grids receive
  569. // different final sizes.
  570. _minSize = _userSize.IsAbsolute ? _userSize.Value : 0.0;
  571. _userSizeValid = true;
  572. }
  573. /// <summary>
  574. /// OnLayoutUpdated handler. Validates that all participating definitions
  575. /// have updated min size value. Forces another layout update cycle if needed.
  576. /// </summary>
  577. private void OnLayoutUpdated(object? sender, EventArgs e)
  578. {
  579. double sharedMinSize = 0;
  580. // accumulate min size of all participating definitions
  581. for (int i = 0, count = _registry.Count; i < count; ++i)
  582. {
  583. sharedMinSize = Math.Max(sharedMinSize, _registry[i].MinSize);
  584. }
  585. bool sharedMinSizeChanged = !MathUtilities.AreClose(_minSize, sharedMinSize);
  586. // compare accumulated min size with min sizes of the individual definitions
  587. for (int i = 0, count = _registry.Count; i < count; ++i)
  588. {
  589. DefinitionBase definitionBase = _registry[i];
  590. // we'll set d.UseSharedMinimum to maintain the invariant:
  591. // d.UseSharedMinimum iff d._minSize < this.MinSize
  592. // i.e. iff d is not a "long-pole" definition.
  593. //
  594. // Measure/Arrange of d's Grid uses d._minSize for long-pole
  595. // definitions, and max(d._minSize, shared size) for
  596. // short-pole definitions. This distinction allows us to react
  597. // to changes in "long-pole-ness" more efficiently and correctly,
  598. // by avoiding remeasures when a long-pole definition changes.
  599. bool useSharedMinimum = !MathUtilities.AreClose(definitionBase._minSize, sharedMinSize);
  600. // before doing that, determine whether d's Grid needs to be remeasured.
  601. // It's important _not_ to remeasure if the last measure is still
  602. // valid, otherwise infinite loops are possible
  603. bool measureIsValid;
  604. if(!definitionBase.UseSharedMinimum)
  605. {
  606. // d was a long-pole. measure is valid iff it's still a long-pole,
  607. // since previous measure didn't use shared size.
  608. measureIsValid = !useSharedMinimum;
  609. }
  610. else if(useSharedMinimum)
  611. {
  612. // d was a short-pole, and still is. measure is valid
  613. // iff the shared size didn't change
  614. measureIsValid = !sharedMinSizeChanged;
  615. }
  616. else
  617. {
  618. // d was a short-pole, but is now a long-pole. This can
  619. // happen in several ways:
  620. // a. d's minSize increased to or past the old shared size
  621. // b. other long-pole definitions decreased, leaving
  622. // d as the new winner
  623. // In the former case, the measure is valid - it used
  624. // d's new larger minSize. In the latter case, the
  625. // measure is invalid - it used the old shared size,
  626. // which is larger than d's (possibly changed) minSize
  627. measureIsValid = (definitionBase.LayoutWasUpdated &&
  628. MathUtilities.GreaterThanOrClose(definitionBase._minSize, this.MinSize));
  629. }
  630. if(!measureIsValid)
  631. {
  632. definitionBase.Parent!.InvalidateMeasure();
  633. }
  634. else if (!MathUtilities.AreClose(sharedMinSize, definitionBase.SizeCache))
  635. {
  636. // if measure is valid then also need to check arrange.
  637. // Note: definitionBase.SizeCache is volatile but at this point
  638. // it contains up-to-date final size
  639. definitionBase.Parent!.InvalidateArrange();
  640. }
  641. // now we can restore the invariant, and clear the layout flag
  642. definitionBase.UseSharedMinimum = useSharedMinimum;
  643. definitionBase.LayoutWasUpdated = false;
  644. }
  645. _minSize = sharedMinSize;
  646. _layoutUpdatedHost!.LayoutUpdated -= _layoutUpdated;
  647. _layoutUpdatedHost = null;
  648. _broadcastInvalidation = true;
  649. }
  650. // the scope this state belongs to
  651. private readonly SharedSizeScope _sharedSizeScope;
  652. // Id of the shared size group this object is servicing
  653. private readonly string _sharedSizeGroupId;
  654. // Registry of participating definitions
  655. private readonly List<DefinitionBase> _registry;
  656. // Instance event handler for layout updated event
  657. private readonly EventHandler _layoutUpdated;
  658. // Control for which layout updated event handler is registered
  659. private Control? _layoutUpdatedHost;
  660. // "true" when broadcasting of invalidation is needed
  661. private bool _broadcastInvalidation;
  662. // "true" when _userSize is up to date
  663. private bool _userSizeValid;
  664. // shared state
  665. private GridLength _userSize;
  666. // shared state
  667. private double _minSize;
  668. }
  669. /// <summary>
  670. /// Private shared size scope property holds a collection of shared state objects for the a given shared size scope.
  671. /// <see cref="OnIsSharedSizeScopePropertyChanged"/>
  672. /// </summary>
  673. internal static readonly AttachedProperty<SharedSizeScope?> PrivateSharedSizeScopeProperty =
  674. AvaloniaProperty.RegisterAttached<DefinitionBase, Control, SharedSizeScope?>(
  675. "PrivateSharedSizeScope",
  676. defaultValue: null,
  677. inherits: true);
  678. /// <summary>
  679. /// Shared size group property marks column / row definition as belonging to a group "Foo" or "Bar".
  680. /// </summary>
  681. /// <remarks>
  682. /// Value of the Shared Size Group Property must satisfy the following rules:
  683. /// <list type="bullet">
  684. /// <item><description>
  685. /// String must not be empty.
  686. /// </description></item>
  687. /// <item><description>
  688. /// String must consist of letters, digits and underscore ('_') only.
  689. /// </description></item>
  690. /// <item><description>
  691. /// String must not start with a digit.
  692. /// </description></item>
  693. /// </list>
  694. /// </remarks>
  695. public static readonly AttachedProperty<string> SharedSizeGroupProperty =
  696. AvaloniaProperty.RegisterAttached<DefinitionBase, Control, string>(
  697. "SharedSizeGroup",
  698. validate: SharedSizeGroupPropertyValueValid);
  699. /// <summary>
  700. /// Static ctor. Used for static registration of properties.
  701. /// </summary>
  702. static DefinitionBase()
  703. {
  704. SharedSizeGroupProperty.Changed.AddClassHandler<DefinitionBase>(OnSharedSizeGroupPropertyChanged);
  705. PrivateSharedSizeScopeProperty.Changed.AddClassHandler<DefinitionBase>(OnPrivateSharedSizeScopePropertyChanged);
  706. }
  707. /// <summary>
  708. /// Marks a property on a definition as affecting the parent grid's measurement.
  709. /// </summary>
  710. /// <param name="properties">The properties.</param>
  711. protected static void AffectsParentMeasure(params AvaloniaProperty[] properties)
  712. {
  713. void Invalidate(AvaloniaPropertyChangedEventArgs e)
  714. {
  715. (e.Sender as DefinitionBase)?.Parent?.InvalidateMeasure();
  716. }
  717. foreach (var property in properties)
  718. {
  719. property.Changed.Subscribe(Invalidate);
  720. }
  721. }
  722. }
  723. }