# The MIT License (MIT)
#
# Copyright (C) 2026 Scott Duensing
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

.section Widgets
.topic widget.timer
.title Timer
.toc 0 Non-Visual Widgets
.toc 0 Timer
.index Timer
.index wgtTimer
.index wgtTimerStart
.index wgtTimerStop
.index wgtTimerSetInterval
.index wgtTimerIsRunning
.index wgtUpdateTimers

.h2 Timer

A non-visual widget that fires its onClick callback at a regular interval. Useful for animations, polling, and periodic updates. Must be explicitly started after creation.

Header: widgets/timer.h

.h3 Creation

.code
WidgetT *tmr = wgtTimer(parent, 1000, true);  // 1 second, repeating
tmr->onClick = onTimerTick;
wgtTimerStart(tmr);
.endcode

.h3 API Functions

.table
  Function                                          Description
  --------                                          -----------
  WidgetT *wgtTimer(parent, intervalMs, repeat)     Create a timer. intervalMs is the interval in milliseconds. repeat: true for repeating, false for one-shot.
  void wgtTimerStart(w)                             Start the timer.
  void wgtTimerStop(w)                              Stop the timer.
  void wgtTimerSetInterval(w, intervalMs)           Change the timer interval.
  int32_t wgtTimerGetInterval(w)                    Returns the current interval in milliseconds.
  bool wgtTimerIsRunning(w)                         Returns true if the timer is currently running.
  void wgtTimerSetEnabled(w, enabled)               Start or stop the timer based on enabled flag.
  void wgtUpdateTimers(void)                        Global tick function. Called by the event loop to advance all active timers.
.endtable

.h3 API Struct (wgtRegisterApi "timer")

.table
  Slot           Function
  ----           --------
  create         wgtTimer
  start          wgtTimerStart
  stop           wgtTimerStop
  setInterval    wgtTimerSetInterval
  isRunning      wgtTimerIsRunning
  updateTimers   wgtUpdateTimers
.endtable

.h3 Events

.table
  Callback     Description
  --------     -----------
  onClick      Fires each time the timer elapses (the BASIC layer surfaces this as the "Timer" event).
.endtable

.h3 Properties (BASIC Interface)

.table
  Property     Type       Access       Description
  --------     ----       ------       -----------
  Enabled      Boolean    Read/Write   Whether the timer is running. Reading returns the running state; writing starts or stops it.
  Interval     Integer    Read/Write   Timer interval in milliseconds.
.endtable

.h3 Methods (BASIC Interface)

.table
  Method       Description
  ------       -----------
  Start        Start the timer.
  Stop         Stop the timer.
.endtable

.h3 Extra Events (BASIC Interface)

.table
  Event        Description
  -----        -----------
  Timer        Fires each time the timer elapses. Default event.
.endtable

.hr
