Layoutable.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Linq;
  5. using Avalonia.Logging;
  6. using Avalonia.Platform;
  7. using Avalonia.VisualTree;
  8. namespace Avalonia.Layout
  9. {
  10. /// <summary>
  11. /// Defines how a control aligns itself horizontally in its parent control.
  12. /// </summary>
  13. public enum HorizontalAlignment
  14. {
  15. /// <summary>
  16. /// The control stretches to fill the width of the parent control.
  17. /// </summary>
  18. Stretch,
  19. /// <summary>
  20. /// The control aligns itself to the left of the parent control.
  21. /// </summary>
  22. Left,
  23. /// <summary>
  24. /// The control centers itself in the parent control.
  25. /// </summary>
  26. Center,
  27. /// <summary>
  28. /// The control aligns itself to the right of the parent control.
  29. /// </summary>
  30. Right,
  31. }
  32. /// <summary>
  33. /// Defines how a control aligns itself vertically in its parent control.
  34. /// </summary>
  35. public enum VerticalAlignment
  36. {
  37. /// <summary>
  38. /// The control stretches to fill the height of the parent control.
  39. /// </summary>
  40. Stretch,
  41. /// <summary>
  42. /// The control aligns itself to the top of the parent control.
  43. /// </summary>
  44. Top,
  45. /// <summary>
  46. /// The control centers itself within the parent control.
  47. /// </summary>
  48. Center,
  49. /// <summary>
  50. /// The control aligns itself to the bottom of the parent control.
  51. /// </summary>
  52. Bottom,
  53. }
  54. /// <summary>
  55. /// Implements layout-related functionality for a control.
  56. /// </summary>
  57. public class Layoutable : Visual, ILayoutable
  58. {
  59. /// <summary>
  60. /// Defines the <see cref="DesiredSize"/> property.
  61. /// </summary>
  62. public static readonly DirectProperty<Layoutable, Size> DesiredSizeProperty =
  63. AvaloniaProperty.RegisterDirect<Layoutable, Size>(nameof(DesiredSize), o => o.DesiredSize);
  64. /// <summary>
  65. /// Defines the <see cref="Width"/> property.
  66. /// </summary>
  67. public static readonly StyledProperty<double> WidthProperty =
  68. AvaloniaProperty.Register<Layoutable, double>(nameof(Width), double.NaN);
  69. /// <summary>
  70. /// Defines the <see cref="Height"/> property.
  71. /// </summary>
  72. public static readonly StyledProperty<double> HeightProperty =
  73. AvaloniaProperty.Register<Layoutable, double>(nameof(Height), double.NaN);
  74. /// <summary>
  75. /// Defines the <see cref="MinWidth"/> property.
  76. /// </summary>
  77. public static readonly StyledProperty<double> MinWidthProperty =
  78. AvaloniaProperty.Register<Layoutable, double>(nameof(MinWidth));
  79. /// <summary>
  80. /// Defines the <see cref="MaxWidth"/> property.
  81. /// </summary>
  82. public static readonly StyledProperty<double> MaxWidthProperty =
  83. AvaloniaProperty.Register<Layoutable, double>(nameof(MaxWidth), double.PositiveInfinity);
  84. /// <summary>
  85. /// Defines the <see cref="MinHeight"/> property.
  86. /// </summary>
  87. public static readonly StyledProperty<double> MinHeightProperty =
  88. AvaloniaProperty.Register<Layoutable, double>(nameof(MinHeight));
  89. /// <summary>
  90. /// Defines the <see cref="MaxHeight"/> property.
  91. /// </summary>
  92. public static readonly StyledProperty<double> MaxHeightProperty =
  93. AvaloniaProperty.Register<Layoutable, double>(nameof(MaxHeight), double.PositiveInfinity);
  94. /// <summary>
  95. /// Defines the <see cref="Margin"/> property.
  96. /// </summary>
  97. public static readonly StyledProperty<Thickness> MarginProperty =
  98. AvaloniaProperty.Register<Layoutable, Thickness>(nameof(Margin));
  99. /// <summary>
  100. /// Defines the <see cref="HorizontalAlignment"/> property.
  101. /// </summary>
  102. public static readonly StyledProperty<HorizontalAlignment> HorizontalAlignmentProperty =
  103. AvaloniaProperty.Register<Layoutable, HorizontalAlignment>(nameof(HorizontalAlignment));
  104. /// <summary>
  105. /// Defines the <see cref="VerticalAlignment"/> property.
  106. /// </summary>
  107. public static readonly StyledProperty<VerticalAlignment> VerticalAlignmentProperty =
  108. AvaloniaProperty.Register<Layoutable, VerticalAlignment>(nameof(VerticalAlignment));
  109. /// <summary>
  110. /// Defines the <see cref="UseLayoutRoundingProperty"/> property.
  111. /// </summary>
  112. public static readonly StyledProperty<bool> UseLayoutRoundingProperty =
  113. AvaloniaProperty.Register<Layoutable, bool>(nameof(UseLayoutRounding), defaultValue: true, inherits: true);
  114. private bool _measuring;
  115. private Size? _previousMeasure;
  116. private Rect? _previousArrange;
  117. /// <summary>
  118. /// Initializes static members of the <see cref="Layoutable"/> class.
  119. /// </summary>
  120. static Layoutable()
  121. {
  122. AffectsMeasure(
  123. IsVisibleProperty,
  124. WidthProperty,
  125. HeightProperty,
  126. MinWidthProperty,
  127. MaxWidthProperty,
  128. MinHeightProperty,
  129. MaxHeightProperty,
  130. MarginProperty,
  131. HorizontalAlignmentProperty,
  132. VerticalAlignmentProperty);
  133. }
  134. /// <summary>
  135. /// Occurs when a layout pass completes for the control.
  136. /// </summary>
  137. public event EventHandler LayoutUpdated;
  138. /// <summary>
  139. /// Gets or sets the width of the element.
  140. /// </summary>
  141. public double Width
  142. {
  143. get { return GetValue(WidthProperty); }
  144. set { SetValue(WidthProperty, value); }
  145. }
  146. /// <summary>
  147. /// Gets or sets the height of the element.
  148. /// </summary>
  149. public double Height
  150. {
  151. get { return GetValue(HeightProperty); }
  152. set { SetValue(HeightProperty, value); }
  153. }
  154. /// <summary>
  155. /// Gets or sets the minimum width of the element.
  156. /// </summary>
  157. public double MinWidth
  158. {
  159. get { return GetValue(MinWidthProperty); }
  160. set { SetValue(MinWidthProperty, value); }
  161. }
  162. /// <summary>
  163. /// Gets or sets the maximum width of the element.
  164. /// </summary>
  165. public double MaxWidth
  166. {
  167. get { return GetValue(MaxWidthProperty); }
  168. set { SetValue(MaxWidthProperty, value); }
  169. }
  170. /// <summary>
  171. /// Gets or sets the minimum height of the element.
  172. /// </summary>
  173. public double MinHeight
  174. {
  175. get { return GetValue(MinHeightProperty); }
  176. set { SetValue(MinHeightProperty, value); }
  177. }
  178. /// <summary>
  179. /// Gets or sets the maximum height of the element.
  180. /// </summary>
  181. public double MaxHeight
  182. {
  183. get { return GetValue(MaxHeightProperty); }
  184. set { SetValue(MaxHeightProperty, value); }
  185. }
  186. /// <summary>
  187. /// Gets or sets the margin around the element.
  188. /// </summary>
  189. public Thickness Margin
  190. {
  191. get { return GetValue(MarginProperty); }
  192. set { SetValue(MarginProperty, value); }
  193. }
  194. /// <summary>
  195. /// Gets or sets the element's preferred horizontal alignment in its parent.
  196. /// </summary>
  197. public HorizontalAlignment HorizontalAlignment
  198. {
  199. get { return GetValue(HorizontalAlignmentProperty); }
  200. set { SetValue(HorizontalAlignmentProperty, value); }
  201. }
  202. /// <summary>
  203. /// Gets or sets the element's preferred vertical alignment in its parent.
  204. /// </summary>
  205. public VerticalAlignment VerticalAlignment
  206. {
  207. get { return GetValue(VerticalAlignmentProperty); }
  208. set { SetValue(VerticalAlignmentProperty, value); }
  209. }
  210. /// <summary>
  211. /// Gets the size that this element computed during the measure pass of the layout process.
  212. /// </summary>
  213. public Size DesiredSize
  214. {
  215. get;
  216. private set;
  217. }
  218. /// <summary>
  219. /// Gets a value indicating whether the control's layout measure is valid.
  220. /// </summary>
  221. public bool IsMeasureValid
  222. {
  223. get;
  224. private set;
  225. }
  226. /// <summary>
  227. /// Gets a value indicating whether the control's layouts arrange is valid.
  228. /// </summary>
  229. public bool IsArrangeValid
  230. {
  231. get;
  232. private set;
  233. }
  234. /// <summary>
  235. /// Gets or sets a value that determines whether the element should be snapped to pixel
  236. /// boundaries at layout time.
  237. /// </summary>
  238. public bool UseLayoutRounding
  239. {
  240. get { return GetValue(UseLayoutRoundingProperty); }
  241. set { SetValue(UseLayoutRoundingProperty, value); }
  242. }
  243. /// <summary>
  244. /// Gets the available size passed in the previous layout pass, if any.
  245. /// </summary>
  246. Size? ILayoutable.PreviousMeasure => _previousMeasure;
  247. /// <summary>
  248. /// Gets the layout rect passed in the previous layout pass, if any.
  249. /// </summary>
  250. Rect? ILayoutable.PreviousArrange => _previousArrange;
  251. /// <summary>
  252. /// Creates the visual children of the control, if necessary
  253. /// </summary>
  254. public virtual void ApplyTemplate()
  255. {
  256. }
  257. /// <summary>
  258. /// Carries out a measure of the control.
  259. /// </summary>
  260. /// <param name="availableSize">The available size for the control.</param>
  261. public void Measure(Size availableSize)
  262. {
  263. if (double.IsNaN(availableSize.Width) || double.IsNaN(availableSize.Height))
  264. {
  265. throw new InvalidOperationException("Cannot call Measure using a size with NaN values.");
  266. }
  267. if (!IsMeasureValid || _previousMeasure != availableSize)
  268. {
  269. var previousDesiredSize = DesiredSize;
  270. var desiredSize = default(Size);
  271. IsMeasureValid = true;
  272. try
  273. {
  274. _measuring = true;
  275. desiredSize = MeasureCore(availableSize).Constrain(availableSize);
  276. }
  277. finally
  278. {
  279. _measuring = false;
  280. }
  281. if (IsInvalidSize(desiredSize))
  282. {
  283. throw new InvalidOperationException("Invalid size returned for Measure.");
  284. }
  285. DesiredSize = desiredSize;
  286. _previousMeasure = availableSize;
  287. Logger.Verbose(LogArea.Layout, this, "Measure requested {DesiredSize}", DesiredSize);
  288. if (DesiredSize != previousDesiredSize)
  289. {
  290. this.GetVisualParent<ILayoutable>()?.ChildDesiredSizeChanged(this);
  291. }
  292. }
  293. }
  294. /// <summary>
  295. /// Arranges the control and its children.
  296. /// </summary>
  297. /// <param name="rect">The control's new bounds.</param>
  298. public void Arrange(Rect rect)
  299. {
  300. if (IsInvalidRect(rect))
  301. {
  302. throw new InvalidOperationException("Invalid Arrange rectangle.");
  303. }
  304. if (!IsMeasureValid)
  305. {
  306. Measure(_previousMeasure ?? rect.Size);
  307. }
  308. if (!IsArrangeValid || _previousArrange != rect)
  309. {
  310. Logger.Verbose(LogArea.Layout, this, "Arrange to {Rect} ", rect);
  311. IsArrangeValid = true;
  312. ArrangeCore(rect);
  313. _previousArrange = rect;
  314. LayoutUpdated?.Invoke(this, EventArgs.Empty);
  315. }
  316. }
  317. /// <summary>
  318. /// Invalidates the measurement of the control and queues a new layout pass.
  319. /// </summary>
  320. public void InvalidateMeasure()
  321. {
  322. if (IsMeasureValid)
  323. {
  324. Logger.Verbose(LogArea.Layout, this, "Invalidated measure");
  325. IsMeasureValid = false;
  326. IsArrangeValid = false;
  327. if (((ILayoutable)this).IsAttachedToVisualTree)
  328. {
  329. LayoutManager.Instance?.InvalidateMeasure(this);
  330. InvalidateVisual();
  331. }
  332. }
  333. }
  334. /// <summary>
  335. /// Invalidates the arrangement of the control and queues a new layout pass.
  336. /// </summary>
  337. public void InvalidateArrange()
  338. {
  339. if (IsArrangeValid)
  340. {
  341. Logger.Verbose(LogArea.Layout, this, "Invalidated arrange");
  342. IsArrangeValid = false;
  343. if (((ILayoutable)this).IsAttachedToVisualTree)
  344. {
  345. LayoutManager.Instance?.InvalidateArrange(this);
  346. InvalidateVisual();
  347. }
  348. }
  349. }
  350. /// <inheritdoc/>
  351. void ILayoutable.ChildDesiredSizeChanged(ILayoutable control)
  352. {
  353. if (!_measuring)
  354. {
  355. InvalidateMeasure();
  356. }
  357. }
  358. /// <summary>
  359. /// Marks a property as affecting the control's measurement.
  360. /// </summary>
  361. /// <param name="properties">The properties.</param>
  362. /// <remarks>
  363. /// After a call to this method in a control's static constructor, any change to the
  364. /// property will cause <see cref="InvalidateMeasure"/> to be called on the element.
  365. /// </remarks>
  366. protected static void AffectsMeasure(params AvaloniaProperty[] properties)
  367. {
  368. foreach (var property in properties)
  369. {
  370. property.Changed.Subscribe(AffectsMeasureInvalidate);
  371. }
  372. }
  373. /// <summary>
  374. /// Marks a property as affecting the control's arrangement.
  375. /// </summary>
  376. /// <param name="properties">The properties.</param>
  377. /// <remarks>
  378. /// After a call to this method in a control's static constructor, any change to the
  379. /// property will cause <see cref="InvalidateArrange"/> to be called on the element.
  380. /// </remarks>
  381. protected static void AffectsArrange(params AvaloniaProperty[] properties)
  382. {
  383. foreach (var property in properties)
  384. {
  385. property.Changed.Subscribe(AffectsArrangeInvalidate);
  386. }
  387. }
  388. /// <summary>
  389. /// The default implementation of the control's measure pass.
  390. /// </summary>
  391. /// <param name="availableSize">The size available to the control.</param>
  392. /// <returns>The desired size for the control.</returns>
  393. /// <remarks>
  394. /// This method calls <see cref="MeasureOverride(Size)"/> which is probably the method you
  395. /// want to override in order to modify a control's arrangement.
  396. /// </remarks>
  397. protected virtual Size MeasureCore(Size availableSize)
  398. {
  399. if (IsVisible)
  400. {
  401. var margin = Margin;
  402. ApplyTemplate();
  403. var constrained = LayoutHelper.ApplyLayoutConstraints(
  404. this,
  405. availableSize.Deflate(margin));
  406. var measured = MeasureOverride(constrained);
  407. var width = measured.Width;
  408. var height = measured.Height;
  409. if (!double.IsNaN(Width))
  410. {
  411. width = Width;
  412. }
  413. width = Math.Min(width, MaxWidth);
  414. width = Math.Max(width, MinWidth);
  415. if (!double.IsNaN(Height))
  416. {
  417. height = Height;
  418. }
  419. height = Math.Min(height, MaxHeight);
  420. height = Math.Max(height, MinHeight);
  421. if (UseLayoutRounding)
  422. {
  423. var scale = GetLayoutScale();
  424. width = Math.Ceiling(width * scale) / scale;
  425. height = Math.Ceiling(height * scale) / scale;
  426. }
  427. return NonNegative(new Size(width, height).Inflate(margin));
  428. }
  429. else
  430. {
  431. return new Size();
  432. }
  433. }
  434. /// <summary>
  435. /// Measures the control and its child elements as part of a layout pass.
  436. /// </summary>
  437. /// <param name="availableSize">The size available to the control.</param>
  438. /// <returns>The desired size for the control.</returns>
  439. protected virtual Size MeasureOverride(Size availableSize)
  440. {
  441. double width = 0;
  442. double height = 0;
  443. foreach (ILayoutable child in this.GetVisualChildren())
  444. {
  445. child.Measure(availableSize);
  446. width = Math.Max(width, child.DesiredSize.Width);
  447. height = Math.Max(height, child.DesiredSize.Height);
  448. }
  449. return new Size(width, height);
  450. }
  451. /// <summary>
  452. /// The default implementation of the control's arrange pass.
  453. /// </summary>
  454. /// <param name="finalRect">The control's new bounds.</param>
  455. /// <remarks>
  456. /// This method calls <see cref="ArrangeOverride(Size)"/> which is probably the method you
  457. /// want to override in order to modify a control's arrangement.
  458. /// </remarks>
  459. protected virtual void ArrangeCore(Rect finalRect)
  460. {
  461. if (IsVisible)
  462. {
  463. var margin = Margin;
  464. var originX = finalRect.X + margin.Left;
  465. var originY = finalRect.Y + margin.Top;
  466. var availableSizeMinusMargins = new Size(
  467. Math.Max(0, finalRect.Width - margin.Left - margin.Right),
  468. Math.Max(0, finalRect.Height - margin.Top - margin.Bottom));
  469. var horizontalAlignment = HorizontalAlignment;
  470. var verticalAlignment = VerticalAlignment;
  471. var size = availableSizeMinusMargins;
  472. var scale = GetLayoutScale();
  473. if (horizontalAlignment != HorizontalAlignment.Stretch)
  474. {
  475. size = size.WithWidth(Math.Min(size.Width, DesiredSize.Width - margin.Left - margin.Right));
  476. }
  477. if (verticalAlignment != VerticalAlignment.Stretch)
  478. {
  479. size = size.WithHeight(Math.Min(size.Height, DesiredSize.Height - margin.Top - margin.Bottom));
  480. }
  481. size = LayoutHelper.ApplyLayoutConstraints(this, size);
  482. if (UseLayoutRounding)
  483. {
  484. size = new Size(
  485. Math.Ceiling(size.Width * scale) / scale,
  486. Math.Ceiling(size.Height * scale) / scale);
  487. availableSizeMinusMargins = new Size(
  488. Math.Ceiling(availableSizeMinusMargins.Width * scale) / scale,
  489. Math.Ceiling(availableSizeMinusMargins.Height * scale) / scale);
  490. }
  491. size = ArrangeOverride(size).Constrain(size);
  492. switch (horizontalAlignment)
  493. {
  494. case HorizontalAlignment.Center:
  495. case HorizontalAlignment.Stretch:
  496. originX += (availableSizeMinusMargins.Width - size.Width) / 2;
  497. break;
  498. case HorizontalAlignment.Right:
  499. originX += availableSizeMinusMargins.Width - size.Width;
  500. break;
  501. }
  502. switch (verticalAlignment)
  503. {
  504. case VerticalAlignment.Center:
  505. case VerticalAlignment.Stretch:
  506. originY += (availableSizeMinusMargins.Height - size.Height) / 2;
  507. break;
  508. case VerticalAlignment.Bottom:
  509. originY += availableSizeMinusMargins.Height - size.Height;
  510. break;
  511. }
  512. if (UseLayoutRounding)
  513. {
  514. originX = Math.Floor(originX * scale) / scale;
  515. originY = Math.Floor(originY * scale) / scale;
  516. }
  517. Bounds = new Rect(originX, originY, size.Width, size.Height);
  518. }
  519. }
  520. /// <summary>
  521. /// Positions child elements as part of a layout pass.
  522. /// </summary>
  523. /// <param name="finalSize">The size available to the control.</param>
  524. /// <returns>The actual size used.</returns>
  525. protected virtual Size ArrangeOverride(Size finalSize)
  526. {
  527. foreach (ILayoutable child in this.GetVisualChildren().OfType<ILayoutable>())
  528. {
  529. child.Arrange(new Rect(finalSize));
  530. }
  531. return finalSize;
  532. }
  533. /// <inheritdoc/>
  534. protected override sealed void OnVisualParentChanged(IVisual oldParent, IVisual newParent)
  535. {
  536. foreach (ILayoutable i in this.GetSelfAndVisualDescendants())
  537. {
  538. i.InvalidateMeasure();
  539. }
  540. base.OnVisualParentChanged(oldParent, newParent);
  541. }
  542. /// <summary>
  543. /// Calls <see cref="InvalidateMeasure"/> on the control on which a property changed.
  544. /// </summary>
  545. /// <param name="e">The event args.</param>
  546. private static void AffectsMeasureInvalidate(AvaloniaPropertyChangedEventArgs e)
  547. {
  548. ILayoutable control = e.Sender as ILayoutable;
  549. control?.InvalidateMeasure();
  550. }
  551. /// <summary>
  552. /// Calls <see cref="InvalidateArrange"/> on the control on which a property changed.
  553. /// </summary>
  554. /// <param name="e">The event args.</param>
  555. private static void AffectsArrangeInvalidate(AvaloniaPropertyChangedEventArgs e)
  556. {
  557. ILayoutable control = e.Sender as ILayoutable;
  558. control?.InvalidateArrange();
  559. }
  560. /// <summary>
  561. /// Tests whether any of a <see cref="Rect"/>'s properties incude nagative values,
  562. /// a NaN or Infinity.
  563. /// </summary>
  564. /// <param name="rect">The rect.</param>
  565. /// <returns>True if the rect is invalid; otherwise false.</returns>
  566. private static bool IsInvalidRect(Rect rect)
  567. {
  568. return rect.Width < 0 || rect.Height < 0 ||
  569. double.IsInfinity(rect.X) || double.IsInfinity(rect.Y) ||
  570. double.IsInfinity(rect.Width) || double.IsInfinity(rect.Height) ||
  571. double.IsNaN(rect.X) || double.IsNaN(rect.Y) ||
  572. double.IsNaN(rect.Width) || double.IsNaN(rect.Height);
  573. }
  574. /// <summary>
  575. /// Tests whether any of a <see cref="Size"/>'s properties incude nagative values,
  576. /// a NaN or Infinity.
  577. /// </summary>
  578. /// <param name="size">The size.</param>
  579. /// <returns>True if the size is invalid; otherwise false.</returns>
  580. private static bool IsInvalidSize(Size size)
  581. {
  582. return size.Width < 0 || size.Height < 0 ||
  583. double.IsInfinity(size.Width) || double.IsInfinity(size.Height) ||
  584. double.IsNaN(size.Width) || double.IsNaN(size.Height);
  585. }
  586. /// <summary>
  587. /// Ensures neither component of a <see cref="Size"/> is negative.
  588. /// </summary>
  589. /// <param name="size">The size.</param>
  590. /// <returns>The non-negative size.</returns>
  591. private static Size NonNegative(Size size)
  592. {
  593. return new Size(Math.Max(size.Width, 0), Math.Max(size.Height, 0));
  594. }
  595. private double GetLayoutScale()
  596. {
  597. var result = (VisualRoot as ILayoutRoot)?.LayoutScaling ?? 1.0;
  598. if (result == 0 || double.IsNaN(result) || double.IsInfinity(result))
  599. {
  600. throw new Exception($"Invalid LayoutScaling returned from {VisualRoot.GetType()}");
  601. }
  602. return result;
  603. }
  604. }
  605. }