diff --git a/src/Perspex.Base/Perspex.Base.csproj b/src/Perspex.Base/Perspex.Base.csproj index abe7582049..b7227d831b 100644 --- a/src/Perspex.Base/Perspex.Base.csproj +++ b/src/Perspex.Base/Perspex.Base.csproj @@ -46,6 +46,7 @@ + diff --git a/src/Perspex.Base/PerspexDisposable.cs b/src/Perspex.Base/PerspexDisposable.cs new file mode 100644 index 0000000000..28a06bc12d --- /dev/null +++ b/src/Perspex.Base/PerspexDisposable.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Perspex +{ + public abstract class PerspexDisposable : IDisposable + { + public bool IsDisposed { get; private set; } + + public void Dispose() + { + IsDisposed = true; + DoDispose(); + } + + protected void CheckDisposed() + { + if (IsDisposed) + throw new ObjectDisposedException(GetType().FullName); + } + + protected abstract void DoDispose(); + } +} diff --git a/src/Perspex.SceneGraph/Media/FormattedText.cs b/src/Perspex.SceneGraph/Media/FormattedText.cs index 245db90eb6..25ef16cd7e 100644 --- a/src/Perspex.SceneGraph/Media/FormattedText.cs +++ b/src/Perspex.SceneGraph/Media/FormattedText.cs @@ -10,7 +10,7 @@ namespace Perspex.Media /// /// Represents a piece of text with formatting. /// - public class FormattedText : IDisposable + public class FormattedText : PerspexDisposable { /// /// Initializes a new instance of the class. @@ -56,8 +56,16 @@ namespace Perspex.Media /// public Size Constraint { - get { return PlatformImpl.Constraint; } - set { PlatformImpl.Constraint = value; } + get + { + CheckDisposed(); + return PlatformImpl.Constraint; + } + set + { + CheckDisposed(); + PlatformImpl.Constraint = value; + } } /// @@ -124,7 +132,7 @@ namespace Perspex.Media /// /// Disposes of unmanaged resources associated with the formatted text. /// - public void Dispose() + protected override void DoDispose() { PlatformImpl.Dispose(); } @@ -137,6 +145,7 @@ namespace Perspex.Media /// public IEnumerable GetLines() { + CheckDisposed(); return PlatformImpl.GetLines(); } @@ -149,6 +158,7 @@ namespace Perspex.Media /// public TextHitTestResult HitTestPoint(Point point) { + CheckDisposed(); return PlatformImpl.HitTestPoint(point); } @@ -159,6 +169,7 @@ namespace Perspex.Media /// The character bounds. public Rect HitTestTextPosition(int index) { + CheckDisposed(); return PlatformImpl.HitTestTextPosition(index); } @@ -170,6 +181,7 @@ namespace Perspex.Media /// The character bounds. public IEnumerable HitTestTextRange(int index, int length) { + CheckDisposed(); return PlatformImpl.HitTestTextRange(index, length); } @@ -179,6 +191,7 @@ namespace Perspex.Media /// The bounds box of the text. public Size Measure() { + CheckDisposed(); return PlatformImpl.Measure(); } @@ -190,6 +203,7 @@ namespace Perspex.Media /// The length of the text range. public void SetForegroundBrush(Brush brush, int startIndex, int length) { + CheckDisposed(); PlatformImpl.SetForegroundBrush(brush, startIndex, length); } }