From 52dbde3fd19d9b2176dd6240a09cecd00b8f530c Mon Sep 17 00:00:00 2001 From: Ayman Sayed Date: Fri, 19 Sep 2025 20:31:53 -0400 Subject: [PATCH] Made timer have custom time --- MainWindow.xaml.cs | 10 +++++----- Timer.xaml | 18 +++++++++++++++--- Timer.xaml.cs | 41 +++++++++++++++++++++++++++++++++++++---- app.manifest | 2 +- 4 files changed, 58 insertions(+), 13 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 8b51191..e8d4a9d 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -13,13 +13,13 @@ public partial class MainWindow : Window // P/Invoke declarations [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] - static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); + static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); - //[DllImport("user32.dll")] - //static extern IntPtr GetForegroundWindow(); + [DllImport("user32.dll")] + static extern IntPtr GetForegroundWindow(); - //[DllImport("user32.dll")] - //static extern bool IsIconic(IntPtr hWnd); + [DllImport("user32.dll")] + static extern bool IsIconic(IntPtr hWnd); private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); private const uint SWP_NOSIZE = 0x0001; diff --git a/Timer.xaml b/Timer.xaml index db8740a..88c39da 100644 --- a/Timer.xaml +++ b/Timer.xaml @@ -33,12 +33,24 @@ Foreground="White" BorderThickness="0" Click="PausePlayButton_Click"> - + + + + + + - + \ No newline at end of file diff --git a/Timer.xaml.cs b/Timer.xaml.cs index 908c15d..93cd87d 100644 --- a/Timer.xaml.cs +++ b/Timer.xaml.cs @@ -1,6 +1,8 @@ using System; +using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; using System.Windows.Media.Imaging; using System.Windows.Threading; @@ -13,6 +15,8 @@ public partial class Timer : UserControl private bool _isRunning; BitmapImage playImg, pauseImg; private readonly string curTag = "Timer".PadRight(10); + // Only allow digits on keyboard input + private static readonly Regex _digitsOnly = new Regex("^[0-9]+$"); public Timer() { @@ -26,6 +30,26 @@ public Timer() playImg = new BitmapImage(new Uri("pack://application:,,,/Assets/play.png")); pauseImg = new BitmapImage(new Uri("pack://application:,,,/Assets/pause.png")); } + private void NumberOnly_PreviewTextInput(object sender, TextCompositionEventArgs e) + { + e.Handled = !_digitsOnly.IsMatch(e.Text); + } + + // Block paste operations with non-digits + private void NumberOnly_Pasting(object sender, DataObjectPastingEventArgs e) + { + if (e.DataObject.GetDataPresent(DataFormats.Text)) + { + var text = e.DataObject.GetData(DataFormats.Text) as string; + if (!_digitsOnly.IsMatch(text ?? "")) + e.CancelCommand(); + } + else + { + e.CancelCommand(); + } + } + private void Timer_Tick(object sender, EventArgs e) { @@ -33,8 +57,10 @@ private void Timer_Tick(object sender, EventArgs e) { if (_timeLeft.TotalSeconds > 0) { - _timeLeft = _timeLeft.Add(TimeSpan.FromSeconds(-1)); - TimerText.Text = _timeLeft.ToString(@"mm\:ss"); + _timeLeft = _timeLeft.Subtract(TimeSpan.FromSeconds(1)); + MinutesBox.Text = _timeLeft.ToString("mm"); + SecondsBox.Text = _timeLeft.ToString("ss"); + //TimerText.Text = _timeLeft.ToString(@"mm\:ss"); } else { @@ -61,13 +87,18 @@ private void PausePlayButton_Click(object sender, RoutedEventArgs e) playPauseImg.Source = playImg; Ilog.Info(curTag, "Pause Timer"); } - else + else if (int.TryParse(MinutesBox.Text, out var m) && int.TryParse(SecondsBox.Text, out var s) && m < 60 && s < 60) { + _timeLeft = TimeSpan.FromMinutes(m) + TimeSpan.FromSeconds(s); _timer.Start(); //PausePlayButton.Content = "Pause"; playPauseImg.Source = pauseImg; Ilog.Info(curTag, "Play Timer"); } + else + { + MessageBox.Show("Enter 0–59 for both minutes and seconds."); + } _isRunning = !_isRunning; } @@ -83,7 +114,9 @@ private void ResetButton_Click(object sender, RoutedEventArgs e) Ilog.Info(curTag, "Reset Timer"); _timer.Stop(); _timeLeft = TimeSpan.FromMinutes(30); - TimerText.Text = _timeLeft.ToString(@"mm\:ss"); + MinutesBox.Text = _timeLeft.ToString("mm"); + SecondsBox.Text = _timeLeft.ToString("ss"); + //TimerText.Text = _timeLeft.ToString(@"mm\:ss"); playPauseImg.Source = playImg; _isRunning = false; } diff --git a/app.manifest b/app.manifest index 9ce67d2..070187f 100644 --- a/app.manifest +++ b/app.manifest @@ -16,7 +16,7 @@ Remove this element if your application requires this virtualization for backwards compatibility. --> - +