Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Architecture

Imports Nexamas.UI.Theming


Namespace Nexamas.UI.Scroll

    '========================================================
    ' File: MASScrollContext.vb  (SSOT: Metrics + Layout + Painter)
    '========================================================
    Friend NotInheritable Class MASScrollContext
        Implements IDisposable

        Private ReadOnly _ctx As MASThemeContext
        Private ReadOnly _metrics As ScrollMetrics
        Private ReadOnly _layout As ScrollbarLayout
        Private ReadOnly _painterSkia As ScrollbarPainter

        Private _disposed As Boolean

        Friend Sub New(context As MASThemeContext)
            If context Is Nothing Then Throw New ArgumentNullException(NameOf(context))
            _ctx = context

            Dim d As Single = context.Dpi
            If d <= 0 OrElse Single.IsNaN(d) OrElse Single.IsInfinity(d) Then d = 1.0F

            _metrics = New ScrollMetrics(d)
            _layout = New ScrollbarLayout(_metrics)
            _painterSkia = New ScrollbarPainter(
    metrics:=_metrics,
    ctx:=_ctx,
contract:=MASArchitectureRuntime.ResolveContractOrThrow(GetType(MASScrollBarContract)))
        End Sub

        Friend Sub UpdateDpi(dpi As Single)
            If _disposed Then Return
            _metrics.UpdateDpi(dpi)
        End Sub

        Friend ReadOnly Property Metrics As ScrollMetrics
            Get
                ThrowIfDisposed()
                Return _metrics
            End Get
        End Property

        Friend ReadOnly Property Layout As ScrollbarLayout
            Get
                ThrowIfDisposed()
                Return _layout
            End Get
        End Property

        Friend ReadOnly Property PainterSkia As ScrollbarPainter
            Get
                ThrowIfDisposed()
                Return _painterSkia
            End Get
        End Property

        Private Sub ThrowIfDisposed()
            If _disposed Then Throw New ObjectDisposedException(NameOf(MASScrollContext))
        End Sub

        Public Sub Dispose() Implements IDisposable.Dispose
            If _disposed Then Return
            _disposed = True

            Try
                _painterSkia.Dispose()
            Catch masCaughtException1 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDisposeCleanup(masCaughtException1)
            End Try
        End Sub

    End Class

End Namespace
