diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/App.xaml.cs b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/App.xaml.cs index 71164530543bbfdae2f42882600f2a76f380158e..2898071bfcc4378aaee649515433bdb47221fce3 100644 --- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/App.xaml.cs +++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/App.xaml.cs @@ -11,7 +11,8 @@ namespace InnoLabProjektDektopApp /// </summary> public partial class App : Application { - private readonly NotifyIconManager _notifyIcon = new(); + private readonly NotifyIconManager _notifyIconManager = new(); + private static ProcessMonitor _processMonitor = new(); internal static ProcessMonitor GetProcessMonitor() @@ -27,13 +28,13 @@ namespace InnoLabProjektDektopApp protected override void OnStartup(StartupEventArgs e) { - _notifyIcon.Initialize(); + _notifyIconManager.Initialize(); base.OnStartup(e); } protected override void OnExit(ExitEventArgs e) { - _notifyIcon.Dispose(); + _notifyIconManager.Dispose(); base.OnExit(e); } diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs index 23cc27e6b5f5f23f05bd09c2ca83103647bb02cd..ae8df9217331ab2cc5bd170fef083378132c4aab 100644 --- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs +++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs @@ -31,17 +31,20 @@ namespace InnoLabProjektDektopApp.Services private readonly List<BreakInfo> breakInfoList = []; public bool IsBreak { get; private set;} + public bool IsPause { get; private set; } private DateTime breakStartTime; + public int currentCycle = 1; public ProcessMonitor() { IsBreak = false; + IsPause = false; blockedProcesses = LoadBlockedProcesses(); } public async void StartMonitoring() { - if (isMonitoring || IsBreak) + if (isMonitoring || IsBreak || IsPause) { return; } @@ -55,27 +58,30 @@ namespace InnoLabProjektDektopApp.Services // check if any distracting processes are already running var processes = GetRunningProcessList(); - foreach (var process in processes) + await Task.Run(() => { - if (IsProcessOnBlockedList(process.Key)) + foreach (var process in processes) { - var result = MessageBox.Show($"Are you sure you want to get distracted by {process.Key}?", "Distracting Process Detected", - MessageBoxButton.YesNo, - MessageBoxImage.Question, - MessageBoxResult.No, - MessageBoxOptions.DefaultDesktopOnly - ); - if (result == MessageBoxResult.No) + if (IsProcessOnBlockedList(process.Key)) { - KillProcessesByName(process.Key); - } - else - { - TrackProcess(process.Key, DateTime.Now); + var result = MessageBox.Show($"Are you sure you want to get distracted by {process.Key}?", "Distracting Process Detected", + MessageBoxButton.YesNo, + MessageBoxImage.Question, + MessageBoxResult.No, + MessageBoxOptions.DefaultDesktopOnly + ); + if (result == MessageBoxResult.No) + { + KillProcessesByName(process.Key); + } + else + { + TrackProcess(process.Key, DateTime.Now); + } } } - } - + }); + await Task.Run(() => { while (isMonitoring) @@ -122,6 +128,29 @@ namespace InnoLabProjektDektopApp.Services breakInfoList.Add(new BreakInfo(breakStartTime, DateTime.Now)); IsBreak = false; StartMonitoring(); + currentCycle++; + } + + public void StartPause() + { + if (IsPause) + { + return; + } + IsPause = true; + breakStartTime = DateTime.Now; + StopMonitoring(); + } + + public void EndPause() + { + if (!IsPause) + { + return; + } + breakInfoList.Add(new BreakInfo(breakStartTime, DateTime.Now)); + IsPause = false; + StartMonitoring(); } public void FinishSession() @@ -142,7 +171,7 @@ namespace InnoLabProjektDektopApp.Services processInfoList.Clear(); breakInfoList.Clear(); processStartTimes.Clear(); - + currentCycle = 1; } public int CalculateCurrentDistractionStage(DateTime endTime) diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Utils/NotifyIconManager.cs b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Utils/NotifyIconManager.cs index dc6b7a155a3e0e2f36a0d3c87cde9d3b05663363..27055c33bd6f6ca925b4c7fc25fad24bafa49ee9 100644 --- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Utils/NotifyIconManager.cs +++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Utils/NotifyIconManager.cs @@ -60,30 +60,44 @@ namespace InnoLabProjektDektopApp.Utils _notifyIcon.ContextMenuStrip.Items.Clear(); LoadSettings(); - if (_processMonitor.isMonitoring || _processMonitor.IsBreak) + if (_processMonitor.isMonitoring || _processMonitor.IsBreak || _processMonitor.IsPause) { - _notifyIcon.ContextMenuStrip.Items.Add(new Forms.ToolStripLabel($"Focussession 1/{_settings.cycles}", null, false)); + _notifyIcon.ContextMenuStrip.Items.Add(new Forms.ToolStripLabel($"Focussession {_processMonitor.currentCycle}/{_settings.cycles}", null, false)); // Add the countdown label (initialize it if it doesn't exist) - _countdownLabel = new Forms.ToolStripLabel($"Countdown: {_countdownTime:mm\\:ss}"); + string label_text = (_processMonitor.IsBreak ? "Break: " : "Focus: ") + $"{_countdownTime:mm\\:ss}" + (_processMonitor.IsPause ? " (Paused)" : ""); + + _countdownLabel = new Forms.ToolStripLabel(label_text); _notifyIcon.ContextMenuStrip.Items.Add(_countdownLabel); - if (!_processMonitor.IsBreak) + if (!_processMonitor.IsBreak && !_processMonitor.IsPause) { - _notifyIcon.ContextMenuStrip.Items.Add("Take a Break", Drawing.Image.FromFile("Assets/pause.png"), (sender, args) => + _notifyIcon.ContextMenuStrip.Items.Add("Pause", Drawing.Image.FromFile("Assets/pause.png"), (sender, args) => { _countdownTimer.Stop(); - _processMonitor.StartBreak(); + _processMonitor.StartPause(); RerenderContextMenu(true); }); } + if (_processMonitor.IsPause) + { + _notifyIcon.ContextMenuStrip.Items.Add("Continue", Drawing.Image.FromFile("Assets/skip.png"), (sender, args) => + { + _countdownTimer.Start(); + _processMonitor.EndPause(); + RerenderContextMenu(true); + }); + } + if (_processMonitor.IsBreak) { _notifyIcon.ContextMenuStrip.Items.Add("Skip Break", Drawing.Image.FromFile("Assets/skip.png"), (sender, args) => { - _countdownTimer.Start(); _processMonitor.EndBreak(); + _countdownTimer.Stop(); + StartCountdown(TimeSpan.FromMinutes(_settings.focusPeriod)); RerenderContextMenu(true); }); } + _notifyIcon.ContextMenuStrip.Items.Add("End Session", Drawing.Image.FromFile("Assets/end.png"), (sender, args) => { _processMonitor.StopMonitoring(); @@ -107,12 +121,12 @@ namespace InnoLabProjektDektopApp.Utils Window mainWindow = Application.Current.MainWindow; NavigationService mainNavigation = ((System.Windows.Navigation.NavigationWindow)mainWindow).NavigationService; mainNavigation.Navigate( - new Session(_settings.focusPeriod, - _settings.breakPeriod, - _settings.cycles, - _settings.distractionMode, - _settings.mascotVisible, - _settings.wordsOfAffirmation, + new Session(_settings.focusPeriod, + _settings.breakPeriod, + _settings.cycles, + _settings.distractionMode, + _settings.mascotVisible, + _settings.wordsOfAffirmation, _settings.insultingWords)); }); } @@ -162,7 +176,18 @@ namespace InnoLabProjektDektopApp.Utils } else { - _countdownTimer.Stop(); + if (_processMonitor.IsBreak) + { + _processMonitor.EndBreak(); + StartCountdown(TimeSpan.FromMinutes(_settings.focusPeriod)); + RerenderContextMenu(); + } + else + { + _processMonitor.StartBreak(); + StartCountdown(TimeSpan.FromMinutes(_settings.breakPeriod)); + RerenderContextMenu(); + } } } @@ -170,7 +195,7 @@ namespace InnoLabProjektDektopApp.Utils { if (_countdownLabel != null) { - _countdownLabel.Text = $"Countdown: {_countdownTime:mm\\:ss}"; + _countdownLabel.Text = (_processMonitor.IsBreak ? "Break: " : "Focus: ") + $"{_countdownTime:mm\\:ss}" + (_processMonitor.IsPause ? " (Paused)" : ""); } }