|
1 | 1 | using System;
|
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Linq; |
4 |
| -using System.Text; |
5 |
| -using System.Threading.Tasks; |
6 | 2 | using System.Windows;
|
7 |
| -using System.Windows.Controls; |
8 |
| -using System.Windows.Data; |
9 |
| -using System.Windows.Documents; |
10 |
| -using System.Windows.Input; |
11 |
| -using System.Windows.Media; |
12 |
| -using System.Windows.Media.Imaging; |
13 |
| -using System.Windows.Navigation; |
14 |
| -using System.Windows.Shapes; |
| 3 | +using System.Windows.Threading; |
| 4 | +using Time_Tracker.Resources.Classes; |
15 | 5 |
|
16 | 6 | namespace Time_Tracker
|
17 | 7 | {
|
18 |
| - /// <summary> |
19 |
| - /// Interaktionslogik für MainWindow.xaml |
20 |
| - /// </summary> |
21 | 8 | public partial class MainWindow : Window
|
22 | 9 | {
|
| 10 | + private Database oDatabase; |
| 11 | + private DateTime dStart; |
| 12 | + private DispatcherTimer oTimer = new DispatcherTimer(); |
| 13 | + |
23 | 14 | public MainWindow()
|
24 | 15 | {
|
25 | 16 | InitializeComponent();
|
| 17 | + SetStartupPosition(); |
| 18 | + |
| 19 | + oTimer.Tick += new EventHandler(Timer_Tick); |
| 20 | + oTimer.Interval = new TimeSpan(0, 0, 1); |
| 21 | + |
| 22 | + oDatabase = new Database(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\TimeTracker"); |
| 23 | + } |
| 24 | + |
| 25 | + private void SetStartupPosition() |
| 26 | + { |
| 27 | + double iScreenHeight = SystemParameters.WorkArea.Height; |
| 28 | + double iScreenWidth = SystemParameters.WorkArea.Width; |
| 29 | + |
| 30 | + this.Left = iScreenWidth - this.Width - 10; |
| 31 | + this.Top = iScreenHeight - this.Height - 10; |
| 32 | + } |
| 33 | + |
| 34 | + private void UiTrayIcon_TrayMouseDoubleClick(object sender, RoutedEventArgs e) |
| 35 | + { |
| 36 | + this.Visibility = Visibility.Visible; |
| 37 | + this.Activate(); |
| 38 | + } |
| 39 | + |
| 40 | + private void Window_Deactivated(object sender, EventArgs e) |
| 41 | + { |
| 42 | + this.Visibility = Visibility.Collapsed; |
| 43 | + } |
| 44 | + |
| 45 | + private void Window_Loaded(object sender, RoutedEventArgs e) |
| 46 | + { |
| 47 | + this.Visibility = Visibility.Collapsed; |
| 48 | + } |
| 49 | + |
| 50 | + private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) |
| 51 | + { |
| 52 | + if (oTimer.IsEnabled) |
| 53 | + { |
| 54 | + if(MessageBox.Show("If you quit the App now your current timer will be lost. Quit anyway?", |
| 55 | + "Quit?", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No) |
| 56 | + { |
| 57 | + e.Cancel = true; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void MenuItem_Quit_Click(object sender, RoutedEventArgs e) |
| 63 | + { |
| 64 | + Application.Current.Shutdown(0); |
| 65 | + } |
| 66 | + |
| 67 | + private void UiClose_Click(object sender, RoutedEventArgs e) |
| 68 | + { |
| 69 | + this.Close(); |
| 70 | + } |
| 71 | + |
| 72 | + private void UiStart_Click(object sender, RoutedEventArgs e) |
| 73 | + { |
| 74 | + //Do nothing if timer is already running. |
| 75 | + if (oTimer.IsEnabled) |
| 76 | + { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + //-1 second to create a 1 second elapsed time right away (looks weird if it starts at 0). |
| 81 | + dStart = DateTime.Now.AddSeconds(-1); |
| 82 | + this.uiActivity.Text = ""; |
| 83 | + this.uiDescription.Text = ""; |
| 84 | + |
| 85 | + //Call the tick event directly to make it start right away. |
| 86 | + Timer_Tick(null, null); |
| 87 | + oTimer.Start(); |
| 88 | + } |
| 89 | + |
| 90 | + private void UiStop_Click(object sender, RoutedEventArgs e) |
| 91 | + { |
| 92 | + oTimer.Stop(); |
| 93 | + |
| 94 | + DateTime dEnd = DateTime.Now; |
| 95 | + |
| 96 | + oDatabase.Save(new Database.TimeRecord(0, this.dStart, dEnd, this.uiActivity.Text, this.uiDescription.Text)); |
| 97 | + |
| 98 | + //Start new timer |
| 99 | + UiStart_Click(null, null); |
| 100 | + } |
| 101 | + |
| 102 | + private void Timer_Tick(object sender, EventArgs e) |
| 103 | + { |
| 104 | + this.uiTime.Content = DateTime.Parse((DateTime.Now - dStart).ToString()).ToString("HH:mm:ss"); |
26 | 105 | }
|
27 | 106 | }
|
28 | 107 | }
|
0 commit comments