-
Notifications
You must be signed in to change notification settings - Fork 59
Add Timer component #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ProGraMajster
wants to merge
3
commits into
modern-forms:main
Choose a base branch
from
ProGraMajster:feature/timer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+153
−0
Open
Add Timer component #113
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| using System.ComponentModel; | ||
| using Modern.WindowKit.Threading; | ||
|
|
||
| namespace Modern.Forms | ||
| { | ||
| /// <summary> | ||
| /// Implements a timer that raises an event at user-defined intervals. | ||
| /// This timer is intended for UI-related scenarios and raises its <see cref="Tick"/> | ||
| /// event on the UI thread. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Use this component when periodic work must be performed on the UI thread, | ||
| /// such as updating UI state, animations, or scheduling lightweight tasks. | ||
| /// | ||
| /// This timer uses <see cref="DispatcherTimer"/> internally and is therefore | ||
| /// integrated with the current UI dispatcher. | ||
| /// </remarks> | ||
| public class Timer : Component | ||
| { | ||
| private DispatcherTimer? dispatcherTimer; | ||
| private int interval = 100; | ||
| private bool enabled; | ||
| private EventHandler? onTimer; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Timer"/> class. | ||
| /// </summary> | ||
| public Timer () | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Occurs when the specified timer interval has elapsed and the timer is enabled. | ||
| /// </summary> | ||
| public event EventHandler Tick { | ||
| add => onTimer += value; | ||
| remove => onTimer -= value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a value indicating whether the timer is running. | ||
| /// </summary> | ||
| [DefaultValue (false)] | ||
| public bool Enabled { | ||
| get => enabled; | ||
| set { | ||
| if (enabled == value) | ||
| return; | ||
|
|
||
| enabled = value; | ||
|
|
||
| if (enabled) | ||
| StartTimer (); | ||
| else | ||
| StopTimer (); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the time, in milliseconds, between timer ticks. | ||
| /// </summary> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// Thrown when the value is less than 1. | ||
| /// </exception> | ||
| [DefaultValue (100)] | ||
| public int Interval { | ||
| get => interval; | ||
| set { | ||
| ArgumentOutOfRangeException.ThrowIfLessThan (value, 1); | ||
|
|
||
| if (interval == value) | ||
| return; | ||
|
|
||
| interval = value; | ||
|
|
||
| if (dispatcherTimer is not null) | ||
| dispatcherTimer.Interval = TimeSpan.FromMilliseconds (interval); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Starts the timer. | ||
| /// </summary> | ||
| public void Start () => Enabled = true; | ||
|
|
||
| /// <summary> | ||
| /// Stops the timer. | ||
| /// </summary> | ||
| public void Stop () => Enabled = false; | ||
|
|
||
| /// <summary> | ||
| /// Raises the <see cref="Tick"/> event. | ||
| /// </summary> | ||
| /// <param name="e">An <see cref="EventArgs"/> that contains the event data.</param> | ||
| protected virtual void OnTick (EventArgs e) | ||
| { | ||
| onTimer?.Invoke (this, e); | ||
| } | ||
|
|
||
| private void StartTimer () | ||
| { | ||
| if (dispatcherTimer is null) { | ||
| dispatcherTimer = new DispatcherTimer (); | ||
| dispatcherTimer.Tick += DispatcherTimer_Tick; | ||
| } | ||
|
|
||
| dispatcherTimer.Interval = TimeSpan.FromMilliseconds (interval); | ||
| dispatcherTimer.Start (); | ||
| } | ||
|
|
||
| private void StopTimer () | ||
| { | ||
| dispatcherTimer?.Stop (); | ||
| } | ||
|
|
||
| private void DispatcherTimer_Tick (object? sender, EventArgs e) | ||
| { | ||
| OnTick (EventArgs.Empty); | ||
jpobst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Releases the resources used by the <see cref="Timer"/>. | ||
| /// </summary> | ||
| /// <param name="disposing"> | ||
| /// <see langword="true"/> to release managed resources; otherwise, <see langword="false"/>. | ||
| /// </param> | ||
| protected override void Dispose (bool disposing) | ||
| { | ||
| if (disposing) { | ||
| enabled = false; | ||
| StopTimer (); | ||
|
|
||
| if (dispatcherTimer is not null) { | ||
| dispatcherTimer.Tick -= DispatcherTimer_Tick; | ||
| dispatcherTimer = null; | ||
| } | ||
|
|
||
| onTimer = null; | ||
| } | ||
|
|
||
| base.Dispose (disposing); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns a string that represents the current timer. | ||
| /// </summary> | ||
| /// <returns>A string containing the type name and interval.</returns> | ||
| public override string ToString () | ||
| { | ||
| return $"{base.ToString ()}, Interval: {Interval}"; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.