Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 15 additions & 3 deletions Timer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,24 @@
Foreground="White" BorderThickness="0" Click="PausePlayButton_Click">
<Image x:Name="playPauseImg" MaxHeight="20" Source="pack://application:,,,/Assets/play.png"/>
</Button>
<TextBlock x:Name="TimerText" Width="70" TextAlignment="Center" FontSize="24" Margin="0" VerticalAlignment="Center"
Foreground="White" Text="30:00"/>
<!--TextBlock x:Name="TimerText" Width="70" TextAlignment="Center" FontSize="24" Margin="0"
VerticalAlignment="Center" Foreground="White" Text="30:00"/-->
<StackPanel Orientation="Horizontal" Width="70" HorizontalAlignment="Center">
<TextBox x:Name="MinutesBox" Width="30" MaxLength="2" Background="Transparent" PreviewTextInput="NumberOnly_PreviewTextInput"
DataObject.Pasting="NumberOnly_Pasting" Margin="0,0,2,0" Opacity="1" BorderThickness="0,0,0,0" VerticalAlignment="Center"
Foreground="White" Text="30" TextAlignment="Center" FontSize="24"/>
<TextBlock Text=":" VerticalAlignment="Center" Foreground="White" FontSize="20"/>
<TextBox x:Name="SecondsBox" Width="30" MaxLength="2" Background="Transparent" PreviewTextInput="NumberOnly_PreviewTextInput"
DataObject.Pasting="NumberOnly_Pasting" Margin="2,0,2,0" Opacity="1" BorderThickness="0,0,0,0" VerticalAlignment="Center"
Foreground="White" Text="00" TextAlignment="Center" FontSize="24"/>
<Button Content="Start"/>
</StackPanel>


<Button x:Name="ResetButton" Style="{StaticResource BtnStyle}" Width="40" Margin ="0,0,0,0" Background="Transparent"
Foreground="White" BorderThickness="0" Click="ResetButton_Click">
<Image x:Name="resetImg" MaxHeight="22" Source="pack://application:,,,/Assets/undo.png"/>
</Button>
</StackPanel>
</Grid>
</UserControl>
</UserControl>
41 changes: 37 additions & 4 deletions Timer.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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()
{
Expand All @@ -26,15 +30,37 @@ 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)
{
try
{
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
{
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion app.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Remove this element if your application requires this virtualization for backwards
compatibility.
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="asInvoker" uiAccess="true" />
</requestedPrivileges>
</security>
</trustInfo>
Expand Down