UI component Razor file for a run timer. Renders main time label and optional milliseconds, formats seconds into H:MM:SS or MM:SS and a .mmm milliseconds label, and provides a BuildHash based on milliseconds and the ShowMilliseconds flag.
@using Sandbox
@using Sandbox.UI
@inherits Panel
@namespace BlockParty
@attribute [StyleSheet( "RunTimer.razor.scss" )]
<root class="run-timer">
<label class="main">@FormatTime( Seconds )</label>
@if ( ShowMilliseconds )
{
<label class="ms">@FormatMilliseconds( Seconds )</label>
}
</root>
@code
{
[Parameter] public float Seconds { get; set; }
[Parameter] public bool ShowMilliseconds { get; set; }
private static string FormatTime( float seconds )
{
int total = (int)System.MathF.Max( 0f, seconds );
int hours = total / 3600;
int minutes = (total / 60) % 60;
int secs = total % 60;
return hours > 0 ? $"{hours:00}:{minutes:00}:{secs:00}" : $"{minutes:00}:{secs:00}";
}
private static string FormatMilliseconds( float seconds )
{
int totalMs = (int)(System.MathF.Max( 0f, seconds ) * 1000f);
return $".{totalMs % 1000:000}";
}
protected override int BuildHash()
{
int totalMs = (int)(System.MathF.Max( 0f, Seconds ) * 1000f);
return System.HashCode.Combine( totalMs, ShowMilliseconds );
}
}