worktimehero

git clone https://git.clttr.info/worktimehero.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

MainForm.cs (9787B)


      1 // BSD 3-Clause License
      2 // Copyright(c) 2018-2020 René Wagner
      3 // https://git.sr.ht/~rwa/worktimehero
      4 
      5 using System;
      6 using System.Drawing;
      7 using System.Windows.Forms;
      8 using WorkTimeHero.Properties;
      9 using IWshRuntimeLibrary;
     10 using System.IO;
     11 using File = System.IO.File;
     12 using System.Diagnostics;
     13 
     14 namespace WorkTimeHero
     15 {
     16     public partial class MainForm : Form
     17     {
     18         private const string TimeFormatter = @"h\:mm";
     19 
     20         private readonly string startUpFolderPathLink =
     21               Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), Application.ProductName + ".lnk");
     22 
     23         private DateTime preDayWorkTime;
     24 
     25         private Boolean initialize = true;
     26 
     27         private WorkTimeCalculator wtc;
     28 
     29         public MainForm()
     30         {
     31             InitializeComponent();
     32         }
     33 
     34         private void MainForm_Load(object sender, EventArgs e)
     35         {
     36             DateTime individualBreakTime = DateTime.MinValue;
     37             if (Settings.Default.IndividualBreakTime.Date == DateTime.Now.Date)
     38             {
     39                 individualBreakTime = Settings.Default.IndividualBreakTime;
     40                 cboIndividualBreak.Checked = true;
     41             }
     42             DateTime startTime = Settings.Default.StartTime.Date == DateTime.Now.Date ? Settings.Default.StartTime : (DateTime.Now + new TimeSpan(0, (int)Settings.Default.StartTimePreamp, 0));
     43 
     44             wtc = new WorkTimeCalculator(startTime , Settings.Default.TargetTime, individualBreakTime, Settings.Default.CurrentBreakStartTime);
     45             dtpIndividualBreak.Value = DateTime.Now.Date.Add(wtc.PlannedBreakTime);
     46             dtpTargetTime.Value = DateTime.Now.Date.Add(Settings.Default.TargetTime);
     47             dtpStartTime.Value = DateTime.Now.Date.Add(startTime.TimeOfDay);
     48             preDayWorkTime = Settings.Default.PreDayWorkTime;
     49             RefreshDisplay();
     50 
     51             if (File.Exists(startUpFolderPathLink))
     52             {
     53                 cboAutostart.Checked = true;
     54             }
     55 
     56             initialize = false;
     57         }
     58 
     59         private void RefreshDisplay()
     60         {
     61             prgPercentage.Value = wtc.WorkTimePercentage;
     62             lblCompleted.Text = string.Format("{1,-20}{0}", wtc.DoneWorkTime.ToString(TimeFormatter), "Arbeitszeit:");
     63             lblTimeDue.Text = string.Format("{1,-20}{0} Uhr", wtc.TimeDue.ToString(TimeFormatter), "Arbeitsende:");
     64             if (wtc.AdditionalWorkTime > new TimeSpan())
     65             {
     66                 lblRemaining.Text = string.Format("{1,-20}{0}", wtc.AdditionalWorkTime.ToString(TimeFormatter), "Überstunden:");
     67             }
     68             else
     69             {
     70                 lblRemaining.Text = string.Format("{1,-20}{0}", wtc.RemainingWorkTime.ToString(TimeFormatter), "Verbleibend:");
     71             }
     72             lblPreDay.Text = string.Format("{1,-20}{0}", preDayWorkTime.ToString(TimeFormatter), "Nettozeit " + preDayWorkTime.ToString("d.M."));
     73             
     74             if (wtc.IndividualBreakTime.Date == DateTime.Now.Date)
     75             {
     76                 dtpIndividualBreak.Value = wtc.IndividualBreakTime;
     77                 cboIndividualBreak.Checked = true;
     78             }
     79 
     80             if (wtc.IsInBreak)
     81             {
     82                 btnSwitchBreak.BackColor = Color.Yellow;
     83                 lblCurrentBreak.Text = "Pause seit " + wtc.CurrentBreakStartTime.ToString(@"hh\:mm");
     84             }
     85             else
     86             {
     87                 btnSwitchBreak.BackColor = SystemColors.Control;
     88                 lblCurrentBreak.Text = string.Empty;
     89             }
     90 
     91             if ( wtc.AdditionalWorkTime.TotalMinutes > 0)
     92             {
     93                 lblTimeDue.BackColor = Color.LightGreen;
     94             }
     95             else
     96             {
     97                 lblTimeDue.BackColor = Color.Yellow;
     98             }
     99 
    100             RefreshTooltip();
    101             RefreshIcons();
    102         }
    103 
    104         private void RefreshTooltip()
    105         {
    106             string toolTipInfo;
    107             if (wtc.AdditionalWorkTime.TotalMinutes > 0)
    108             {
    109                 toolTipInfo = wtc.AdditionalWorkTime.ToString(TimeFormatter) + " Überstunden";
    110                 if (!wtc.NotificationShown)
    111                 {
    112                     ntiWorkTimeHero.ShowBalloonTip(10, "Fertig!", "Sollzeit für heute erreicht!", ToolTipIcon.Info);
    113                     wtc.NotificationShown = true;
    114                 }
    115             }
    116             else
    117             {
    118                 toolTipInfo = wtc.WorkTimePercentage + "% - noch " + wtc.RemainingWorkTime.ToString(TimeFormatter) + " Stunden";
    119             }
    120 
    121             ntiWorkTimeHero.Text = toolTipInfo + " - WorkTimeHero";
    122         }
    123 
    124         private void RefreshIcons()
    125         {
    126             Icon newIcon = Resources.clock_play_icon;
    127             ToolTipIcon newToolTipIcon = ToolTipIcon.Info;
    128             if (wtc.IsInBreak)
    129             {
    130                 newIcon = Resources.clock_pause_icon;
    131             }
    132             else
    133             {
    134                 //if (wtc.WorkTimePercentage > 25)
    135                 //{
    136                 //    newIcon = Resources.clock_15;
    137                 //}
    138                 if (wtc.WorkTimePercentage > 50)
    139                 {
    140                     newIcon = Resources.clock_go_icon;
    141                 }
    142                 if (wtc.RemainingWorkTime.TotalMinutes <= 0)
    143                 {
    144                     newIcon = Resources.clock_error_icon;
    145                     newToolTipIcon = ToolTipIcon.Warning;
    146                 }
    147                 if (wtc.DoneWorkTime.TotalMinutes >= 600)
    148                 {
    149                     newIcon = Resources.clock_red_icon;
    150                     newToolTipIcon = ToolTipIcon.Error;
    151                 }
    152             }
    153             ntiWorkTimeHero.Icon = newIcon;
    154             Icon = newIcon;
    155             ntiWorkTimeHero.BalloonTipIcon = newToolTipIcon;
    156         }
    157         
    158         private void RefreshWtc()
    159         {
    160             DateTime individualBreakTime = cboIndividualBreak.Checked ? dtpIndividualBreak.Value : DateTime.MinValue;
    161 
    162             wtc = new WorkTimeCalculator(dtpStartTime.Value, dtpTargetTime.Value.TimeOfDay, individualBreakTime, wtc.CurrentBreakStartTime);
    163 
    164             RefreshDisplay();
    165         }
    166 
    167         private void refreshTimer_Tick(object sender, EventArgs e)
    168         {
    169             RefreshDisplay();
    170         }
    171 
    172         private void dtpStartTime_ValueChanged(object sender, EventArgs e)
    173         {
    174             if ( initialize ) { return; }
    175             RefreshWtc();
    176         }
    177 
    178         private void dtpIndividualBreak_ValueChanged(object sender, EventArgs e)
    179         {
    180             if (initialize) { return; }
    181             if ( cboIndividualBreak.Checked ) { RefreshWtc(); }
    182         }
    183 
    184         private void cboIndividualBreak_CheckedChanged(object sender, EventArgs e)
    185         {
    186             dtpIndividualBreak.Enabled = cboIndividualBreak.Checked;
    187 
    188             if (initialize) { return; }
    189             
    190             RefreshWtc();
    191         }
    192 
    193         private void ntiWorkTimeHero_MouseDoubleClick(object sender, MouseEventArgs e)
    194         {
    195             ToggleVisiblity();
    196         }
    197 
    198         private void ToggleVisiblity()
    199         {
    200             if (Visible)
    201             {
    202                 Hide();
    203             }
    204             else
    205             {
    206                 Show();
    207             }
    208         }
    209 
    210         private void cboAutostart_CheckedChanged(object sender, EventArgs e)
    211         {
    212             if (initialize) { return; }
    213             if (cboAutostart.Checked)
    214             {
    215                 WshShell wshShell = new WshShell();
    216 
    217                 IWshShortcut shortcut;
    218                 shortcut =
    219                   (IWshShortcut)wshShell.CreateShortcut(startUpFolderPathLink);
    220 
    221                 shortcut.TargetPath = Application.ExecutablePath;
    222                 shortcut.WorkingDirectory = Application.StartupPath;
    223                 shortcut.Description = "Launch WorkTimeHero";
    224                 shortcut.Save();
    225             }
    226             else
    227             {
    228                 File.Delete(startUpFolderPathLink);
    229             }
    230         }
    231 
    232         private void tsmiBeenden_Click(object sender, EventArgs e)
    233         {
    234             Close();
    235         }
    236 
    237         private void tsmiToogleView_Click(object sender, EventArgs e)
    238         {
    239             ToggleVisiblity();
    240         }
    241 
    242         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    243         {
    244             Settings.Default.StartTime = wtc.StartTime;
    245             if (cboIndividualBreak.Checked) { Settings.Default.IndividualBreakTime = wtc.IndividualBreakTime;  }
    246             Settings.Default.CurrentBreakStartTime = wtc.CurrentBreakStartTime;
    247             Settings.Default.TargetTime = wtc.TargetTime;
    248             Settings.Default.Save();
    249 
    250             if ( MessageBox.Show("Is scho so weit?", "Feierabend", MessageBoxButtons.YesNo) == DialogResult.Yes)
    251             {
    252                 Settings.Default.PreDayWorkTime = DateTime.Now.Date.Add(wtc.DoneWorkTime);
    253                 Settings.Default.Save();
    254             }
    255         }
    256 
    257         private void dtpTargetTime_ValueChanged(object sender, EventArgs e)
    258         {
    259             if (initialize) { return; }
    260             RefreshWtc();
    261         }
    262 
    263         private void btnSwitchBreak_Click(object sender, EventArgs e)
    264         {
    265             if (initialize) { return; }
    266             if (!wtc.IsInBreak)
    267             {
    268                 wtc.StartBreak();
    269             }
    270             else
    271             {
    272                 wtc.StopBreak();
    273             }
    274             RefreshDisplay();
    275         }
    276 
    277         private void btnInfo_Click(object sender, EventArgs e)
    278         {
    279             new AboutBox().ShowDialog();
    280         }
    281     }
    282 }