private ToolStripMenuItem HotkeyMenuItem(Icon icon, string id) { var hotkey = SettingsFiles.ApplicationSettings.FindHotkey(id); return(new ToolStripMenuItem(SystemCommands.GetTitle(id), icon.ToBitmap(), (_, __) => SetHotkey(id)) { ShortcutKeys = hotkey, ShowShortcutKeys = hotkey != Keys.None, }); }
private void SetHotkey(string id) { ExecuteUiAction(() => { var result = SetHotkeyDialog.ExecuteDialog(SystemCommands.GetTitle(id), SettingsFiles.ApplicationSettings.FindHotkey(id)); if (result == null) { return; } SettingsFiles.ApplicationSettings.SetHotkey(id, result.Value); SettingsFiles.SaveApplicationSettings(); }, "Could not set hotkey for " + SystemCommands.GetTitle(id)); }
private void RefreshHotkeys() { // Unregister all hotkeys. foreach (var hotkey in _hotkeys) { hotkey.Dispose(); } _hotkeys.Clear(); // Remove any hotkeys for profiles that no longer exist (but keep hotkeys for system commands). if (SettingsFiles.ApplicationSettings.Hotkeys.RemoveAll(x => !SystemCommands.IsSystemCommand(x.Id) && !_profiles.ContainsKey(x.Id)) != 0) { SettingsFiles.SaveApplicationSettings(); } // Register hotkeys. foreach (var hotkeySetting in SettingsFiles.ApplicationSettings.Hotkeys) { var id = hotkeySetting.Id; try { _hotkeys.Add(new WinFormsHotkey(hotkeySetting.Hotkey, true, () => ExecuteSystemCommandOrLoadProfile(hotkeySetting.Id))); } catch (Exception ex) { BalloonTip("Could not bind hotkey " + WinFormsHotkey.HotkeyString(hotkeySetting.Hotkey) + " to " + SystemCommands.GetTitle(id), ex.Message, ToolTipIcon.Warning); } } }
private void BuildTrayMenu() { contextMenuStrip.Items.Clear(); // Add to load menu foreach (var kvp in _profiles) { var name = kvp.Key; var profile = kvp.Value; var item = new ToolStripMenuItem(name, Resources.Profile.ToBitmap(), (_, __) => LoadProfile(name)); var ex = profile.Exception ?? profile.Value.Validate(); if (ex != null) { item.Image = Resources.Warning.ToBitmap(); var message = ex.Message; var extraMessage = profile.TryValue?.MissingAdaptersMessage(); if (!string.IsNullOrEmpty(extraMessage)) { message += "\r\n" + extraMessage; } item.ToolTipText = message; } contextMenuStrip.Items.Add(item); } contextMenuStrip.Items.Add(new ToolStripMenuItem(SystemCommands.GetTitle(SystemCommands.MonitorsOffCommand), Resources.MainIcon.ToBitmap(), (_, __) => ExecuteSystemCommand(SystemCommands.MonitorsOffCommand))); contextMenuStrip.Items.Add(new ToolStripSeparator()); // Menu for saving items var newProfileMenuItem = new ToolStripMenuItem("New Profile...", Resources.NewProfile.ToBitmap(), OnMenuSaveAs); if (_profiles.Count != 0) { var saveMenu = new ToolStripMenuItem("Save Profile", Resources.SaveProfile.ToBitmap()) { DropDown = new ToolStripDropDownMenu() }; saveMenu.DropDownItems.Add(newProfileMenuItem); saveMenu.DropDownItems.Add(new ToolStripSeparator()); foreach (var profile in _profiles.Keys) { saveMenu.DropDownItems.Add(new ToolStripMenuItem(profile, Resources.SaveProfile.ToBitmap(), (_, __) => SaveProfile(profile))); } contextMenuStrip.Items.Add(saveMenu); } else { contextMenuStrip.Items.Add(newProfileMenuItem); } // Menu for deleting items if (_profiles.Count != 0) { var deleteMenu = new ToolStripMenuItem("Delete Profile", Resources.DeleteProfile.ToBitmap()) { DropDown = new ToolStripDropDownMenu() }; foreach (var profile in _profiles.Keys) { deleteMenu.DropDownItems.Add(new ToolStripMenuItem(profile, Resources.DeleteProfile.ToBitmap(), (_, __) => DeleteProfile(profile))); } contextMenuStrip.Items.Add(deleteMenu); } // Menu for hotkeys if (_profiles.Count != 0) { var hotkeyMenu = new ToolStripMenuItem("Hotkeys", Resources.Hotkey.ToBitmap()) { DropDown = new ToolStripDropDownMenu() }; foreach (var profile in _profiles.Keys) { hotkeyMenu.DropDownItems.Add(HotkeyMenuItem(Resources.Profile, profile)); } hotkeyMenu.DropDownItems.Add(HotkeyMenuItem(Resources.MainIcon, SystemCommands.MonitorsOffCommand)); contextMenuStrip.Items.Add(hotkeyMenu); } contextMenuStrip.Items.Add(new ToolStripSeparator()); contextMenuStrip.Items.Add(new ToolStripMenuItem("About", Resources.About.ToBitmap(), OnAbout)); contextMenuStrip.Items.Add(new ToolStripMenuItem("Exit", null, (_, __) => Application.Exit())); }