Countdown
A wall-clock-difference-driven countdown: no drift across background switches, controlled display and uncontrolled self-advance modes, and the format tokens DD/HH/mm/ss/S.
Spec source:
lh-ui-core/specs/countdown.json· Golden cases:lh-ui-core/cases/countdown.json(10 cases, translated into xUnit tests)
Platform notes
The remaining time is computed as "target wall clock − now" (LhCountdownTokens.Engine) and clamped to ≥ 0, without accumulating subtraction against a DispatcherTimer — no drift across background switches or timer throttling; the DispatcherTimer only triggers a recompute on every step (16ms when millisecond=true, otherwise 250ms). The instance methods Start / Pause / Reset are exposed directly on the control, with no extra controller handle needed.
Basic usage
Uncontrolled mode: pass the total duration via Time, and AutoStart (default true) starts it on mount; LhOnChange reports the remaining milliseconds on every step, and reaching zero fires LhOnFinish once.
HTML mock preview (Windows / WinUI 3 appearance), not an on-device render
var countdown = new LhCountdown { Time = 60 * 60 * 1000 };
countdown.LhOnChange += remaining => { /* Remaining milliseconds */ };
countdown.LhOnFinish += () => { /* Fired once when reaching zero */ };Format tokens
Format supports DD (days) / HH (hours) / mm (minutes) / ss (seconds) / S (milliseconds, three digits); other characters render as literals. Without DD, HH carries the total number of hours (may exceed 24); negative values clamp to zero.
HTML mock preview (Windows / WinUI 3 appearance), not an on-device render
new LhCountdown { Value = 86401500, Format = "DD天HH:mm:ss.S" }; // 01天00:00:01.500
new LhCountdown { Value = 181810000, Format = "HH:mm:ss" }; // 50:30:10 (no DD; HH carries the total hours)
new LhCountdown { Value = -1 }; // 00:00:00 (negative value clamps to zero)Controlled and uncontrolled
Passing Value makes it controlled: the component does not drive itself and displays the external value (controlled write-back goes through the same LhOnChange); passing Time makes it uncontrolled: with AutoStart it runs itself on mount. Disabled grays the component out and pauses the countdown (after resuming it does not continue automatically; call Start to resume).
HTML mock preview (Windows / WinUI 3 appearance), not an on-device render
new LhCountdown { Value = 10 * 1000, Disabled = true }; // Controlled display + disabled graying
countdown.Pause(); // Remembers the remaining time
countdown.Start(); // Resumes from the pause point (no effect when disabled)
countdown.Reset(); // Returns to the Time total duration and can restart per AutoStartAPI
Props
| Prop | Description | Type | Default |
|---|---|---|---|
Value | Remaining milliseconds (controlled display; once passed, the component does not drive itself) | long? | null |
Time | Uncontrolled total duration in ms (runs itself on mount with autoStart) | long | 0 |
Format | Format string; tokens DD/HH/mm/ss/S | string | "HH:mm:ss" |
AutoStart | Whether to start automatically on mount in uncontrolled mode | bool | true |
Millisecond | Frame-by-frame refresh (16ms step / otherwise 250ms) | bool | false |
Disabled | Disabled: grays out and pauses the countdown | bool | false |
AriaLabel | Accessibility name (falls back to the formatted text when unset) | string? | null |
Events
| Event | Payload | Description |
|---|---|---|
LhOnChange | long remaining | Remaining-milliseconds change (fired on every uncontrolled step; controlled write-back goes through the same event) |
LhOnFinish | — | Fired once when the countdown reaches zero |
Methods
| Method | Description |
|---|---|
Start() | Resumes from the pause point (no effect when disabled) |
Pause() | Remembers the remaining time |
Reset() | Returns to the Time total duration and can restart per AutoStart |
Token Reference
| Token | Usage |
|---|---|
countdown.digitMinWidth | Minimum width of digit positions (tabular figures, anti-jitter) |
typography.fontSize.large | Digit font size |
color.text.primary | Digit color |
color.text.secondary | Separator literal color (weakened) |
color.text.disabled | Disabled gray color |
Golden Case Reference
How the 10 assertions of cases/countdown.json map to xUnit tests (tests/LhCountdownTests.cs):
| case id | Assertion | Local test |
|---|---|---|
renders-formatted-remaining | The controlled value renders as 01:01:05 with the default format | Renders_Formatted_Remaining |
negative-clamps-to-zero | Negative values clamp to all zeros | Negative_Clamps_To_Zero |
hours-over-24-without-dd | Without DD, HH carries the total number of hours 50:30:10 | Hours_Over_24_Without_Dd |
dd-format-splits-days | The DD format splits by days and zero-pads milliseconds to three digits | Dd_Format_Splits_Days |
uncontrolled-autostart-ticks | Uncontrolled autoStart runs itself on mount and fires onChange | Uncontrolled_Autostart_Ticks |
finish-emits-once-at-zero | Reaching zero fires onFinish once | Finish_Emits_Once_At_Zero |
controlled-does-not-self-drive | Controlled mode does not drive itself | Controlled_Does_Not_Self_Drive |
autostart-false-waits-for-start | autoStart=false waits for start() to begin counting | Autostart_False_Waits_For_Start |
disabled-pauses-and-greys | Disabled grays out and stops driving (color.text.disabled applied) | Disabled_Pauses_And_Greys |
aria-role-timer | The accessible name falls back to the formatted text | Aria_Name_Falls_Back_To_Formatted_Text |
Plus supplementary assertions: Digit_Min_Width_Token_And_Separator_Weakened / Reset_Returns_To_Total_And_Can_Restart plus the dark-mode suites Dark_Colors_Come_From_Dark_Tokens / Dimensions_Are_Identical_In_Dark_Mode. Run: cd lh-ui-windows && dotnet test.

