/// <summary> /// Closes the application. /// </summary> private void ExitApplication() { MainWindow.Close(); NotifyIcon.Dispose(); NotifyIcon = null; HotkeyListener.Dispose(); HotkeyListener = null; Shutdown(1); }
/// <summary> /// Called when the machine state changes. /// Needed to reattach the hotkey listener after a machine went to sleep and woke up again. /// </summary> /// <param name="sender">The class that triggered the event</param> /// <param name="e">The event</param> private void ListenerEvent(object sender, CustomEventArgs e) { if ((bool)e.Value) // True if the event is a log in/wake up { HotkeyListener?.Dispose(); HotkeyListener = new HotkeyListener(); ASDL.ReattachHotkeyListener(HotkeyListener); } }
/// <summary> /// Sets up the base of the application. /// Everything is coordinated from here. /// </summary> /// <param name="e">The startup event</param> protected override void OnStartup(StartupEventArgs e) { bool createdNew; _instanceMutex = new Mutex(true, "1ca95cf6-561b-4dc0-acd5-d83b3e4030b4", out createdNew); if (!createdNew) { _instanceMutex = null; System.Windows.Application.Current.Shutdown(); return; } // Set up app to run in the background base.OnStartup(e); // Sets up the main window MainWindow = new MainWindow(); // Sets up the taskbar icon and the menu that show if you left-click on it NotifyIcon = new System.Windows.Forms.NotifyIcon(); NotifyIcon.Icon = new Icon(TimeTracker.Properties.Resources.icon, SystemInformation.SmallIconSize); NotifyIcon.Visible = true; CreateContextMenu(); // Creates classes needed to track activities and windows StorageHandler = new StorageHandler(); AppStateTracker = new AppStateTracker(StorageHandler); ProgramSwitchListener = new ProgramSwitchListener(); MachineStateListener = new MachineStateListener(); HotkeyListener = new HotkeyListener(); ASDL = new ASDL(AppStateTracker, ProgramSwitchListener, MachineStateListener, HotkeyListener); // Attaches listeners NotifyIcon.DoubleClick += (s, args) => ASDL.ChangeActivity(); MachineStateListener.StateChanged += ListenerEvent; AppStateTracker.ChangeContextMenu += (s, args) => NotifyIcon.ContextMenuStrip.Items[2].Text = ((bool)args.Value) ? "Unpause" : "Pause"; ASDL.ShowActivityDialog += CreateActivityDialog; ASDL.ShowAwayFromPCDialog += CreateAwayFromPCDialog; ShowTutorialIfNeeded(); CheckForUpdates(); }
/// <summary> /// Reattaches the hotkey listener. /// When the device is locked, the old listener no longer works. /// Therefore, it has to be reattached every time the machine is unlocked. /// </summary> /// <param name="hotkeyListener"></param> public void ReattachHotkeyListener(HotkeyListener hotkeyListener) { hotkeyListener.KeyCombinationPressed += ListenerEvent; }
/// <summary> /// Decides if the activity/window has truely changed. /// If so, triggers the activity dialog. /// </summary> /// <param name="appStateTracker">The state tracker for this app</param> /// <param name="programSwitchListener">A listener to determine if the current window has changed</param> /// <param name="machineStateListener">A listener to determine if the machine state has changed</param> /// <param name="hotkeyListener">A listener to detemrine if the hotkey has been pressed</param> public ASDL(AppStateTracker appStateTracker, ProgramSwitchListener programSwitchListener, MachineStateListener machineStateListener, HotkeyListener hotkeyListener) { AppStateTracker = appStateTracker; programSwitchListener.ProgramChanged += ListenerEvent; machineStateListener.StateChanged += ListenerEvent; hotkeyListener.KeyCombinationPressed += ListenerEvent; }