public void OnRestartClick() { OnRestartClicked?.Invoke(); }
public ContextMenuController(IController controller) { _controller = controller; _notifyIcon = new NotifyIcon() { Text = APPLICATION_NAME, Visible = true, Icon = UACUtils.IsUserAdministrator ? ohmuaclogo : ohmlogo }; _notifyIcon.BalloonTipClosed += (s, e) => IsShowingNotification = false; _notifyIcon.BalloonTipClicked += (s, e) => IsShowingNotification = false; var contextMenuStrip = new ContextMenuStrip(); contextMenuStrip.Closing += ContextMenuStrip_Closing; #region Monitors _monitorsItem = contextMenuStrip.Items.Add(MONITORS_ICON_NAME) as ToolStripMenuItem; _monitorsItem.Name = MONITORS_ICON_NAME; _monitorsItem.Available = false; _temperatureItem = _monitorsItem.DropDown.Items.Add(TEMPERATURE_ICON_NAME); _temperatureItem.Available = false; _temperatureItem.VisibleChanged += (s, e) => InvalidateMonitorsIcon(); #endregion #region Records _recordsItem = contextMenuStrip.Items.Add(RECORD_ICON_NAME, start_record_icon) as ToolStripMenuItem; _recordsItem.DropDown.Closing += RecordsDropDown_Closing; var recordable = _recordsItem.DropDown.Items; foreach (var hwmType in Enum.GetValues(typeof(HardwareMonitorType))) { var item = new ToolStripMenuItem() { Text = hwmType.ToString(), CheckOnClick = true, Tag = hwmType }; item.MouseDown += CancelRecordsDropdownClosing; recordable.Add(item); } recordable.Add(new ToolStripSeparator()); _selectAllAction = recordable.Add("Select All", null, (s, e) => ToggleHardwareMonitorTypesSelection(true)); _selectAllAction.MouseDown += CancelRecordsDropdownClosing; _unselectAllAction = recordable.Add("Unselect All", null, (s, e) => ToggleHardwareMonitorTypesSelection(false)); _unselectAllAction.MouseDown += CancelRecordsDropdownClosing; _toggleRecordingAction = recordable.Add(START_RECORDING_ACTION_ICON_NAME, start_record_icon, (s, e) => { var recordables = _recordsItem.DropDown.Items; if (IsRecording) { if (DialogResult.Yes == MessageBox.Show("Do you want to stop recording and save the values in a log file?", APPLICATION_NAME, MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { _recorder.Save(); _recorder.Close(true); _controller.RemoveObserver(_recorder); _recorder = null; //re-enables items check and restores names for (int i = 0; i < recordables.Count - 4 /*excludes separator and functionalities*/; i++) { var menuItem = (recordables[i] as ToolStripMenuItem); menuItem.Text = menuItem.Tag.ToString(); menuItem.CheckOnClick = true; } _selectAllAction.Enabled = true; _unselectAllAction.Enabled = true; _toggleRecordingAction.Text = START_RECORDING_ACTION_ICON_NAME; _toggleRecordingAction.Image = start_record_icon; IsRecording = false; } } else { List <HardwareMonitorType> selectedTypes = new List <HardwareMonitorType>(); HardwareMonitorType hwmTypesToRecord; for (int i = 0; i < recordables.Count - 4 /*excludes separator and functionalities*/; i++) { if ((recordables[i] as ToolStripMenuItem).Checked) { selectedTypes.Add((HardwareMonitorType)recordables[i].Tag); } } if (selectedTypes.Count > 0) { StringBuilder sb = new StringBuilder(); hwmTypesToRecord = selectedTypes[0]; foreach (var type in selectedTypes) { hwmTypesToRecord |= type; sb.AppendLine(type.ToString()); } if (DialogResult.Yes == MessageBox.Show($"Start recording the following hardware sensors?\n{sb.ToString()}", APPLICATION_NAME, MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { //disables items check for (int i = 0; i < recordables.Count - 4 /*excludes separator and functionalities*/; i++) { var menuItem = (recordables[i] as ToolStripMenuItem); menuItem.Checked = false; menuItem.CheckOnClick = false; } _selectAllAction.Enabled = false; _unselectAllAction.Enabled = false; StartRecording(hwmTypesToRecord); } } } }); _toggleRecordingAction.MouseDown += CancelRecordsDropdownClosing; #endregion #region Logs _logsItem = contextMenuStrip.Items.Add(LOGS_ICON_NAME, null) as ToolStripMenuItem; contextMenuStrip.Opening += InvalidateLogsDropDown; _logsItem.MouseDown += (s, e) => { if (!_logsItem.AllowDrop) { CancelContextMenuStripClosing(s, e); } }; _logsItem.DropDown.Closing += LogsDropDown_Closing; var logs = _logsItem.DropDown.Items; //if dropdown shows log file but that file has actually been deleted, don't close the dropdown if the item gets clicked (_verboseLogsItem = logs.Add(VERBOSE_LOGS_ICON_NAME, verbose_logs_icon, (s, e) => OpenLogs(LogLevel.Verbose))).MouseDown += CancelLogsDropdownClosing; (_debugLogsItem = logs.Add(DEBUG_LOGS_ICON_NAME, debug_logs_icon, (s, e) => OpenLogs(LogLevel.Debug))).MouseDown += CancelLogsDropdownClosing; (_warningLogsItem = logs.Add(WARNING_LOGS_ICON_NAME, warning_logs_icon, (s, e) => OpenLogs(LogLevel.Warning))).MouseDown += CancelLogsDropdownClosing; (_errorLogsItem = logs.Add(ERROR_LOGS_ICON_NAME, error_logs_icon, (s, e) => OpenLogs(LogLevel.Error))).MouseDown += CancelLogsDropdownClosing; #endregion _settingsItem = contextMenuStrip.Items.Add(SETTINGS_ICON_NAME); _settingsItem.Name = SETTINGS_ICON_NAME; _settingsItem.Available = false; contextMenuStrip.Items.Add(new ToolStripSeparator()); _infoItem = contextMenuStrip.Items.Add("About", info_icon.ToBitmap(), (s, e) => { MessageBox.Show(new Form() { TopMost = true /*pretty bad, I know*/ }, "HardwareMonitor windows service and winform developed by Tommaso Bertoni, 2015.\n\nBased on the OpenHardwareMonitorLib project.", $"{APPLICATION_NAME} - {INFO_ICON_NAME}", MessageBoxButtons.OK, MessageBoxIcon.Information); }); contextMenuStrip.Items.Add("Restart", null, (s, e) => OnRestartClicked?.Invoke()); contextMenuStrip.Items.Add("Exit", null, (s, e) => OnExitClicked?.Invoke()); _notifyIcon.ContextMenuStrip = contextMenuStrip; _notifyIcon.ContextMenuStrip.Closed += (s, e) => { _infoItem.Available = true; if (_settingsItem != null) { _settingsItem.Available = true; } }; _notifyIcon.MouseClick += NotifyIcon_click; }