public ApplicationContext() { var keyboardController = new KeyboardController(); if (Settings.Default.EnableAtStartup) { keyboardController.ToggleBacklight(allowInTerminalServerSession: false); } var brightMenuItem = new MenuItem("On: Bright"); var dimMenuItem = new MenuItem("On: Dim"); var timerMenuItem = new MenuItem("Timer") { Checked = Settings.Default.Timer }; var keypressMenuItem = new MenuItem("Monitor key presses") { Checked = Settings.Default.MonitorKeys }; var settingsMenuItem = new MenuItem(text: "Settings") /* { BarBreak = true }*/; _trayIcon = new NotifyIcon { Icon = Properties.Resources.TrayIcon, ContextMenu = new ContextMenu(menuItems: new[] { brightMenuItem, dimMenuItem, new MenuItem(text: "Off", onClick: (_, __) => keyboardController.ToggleBacklight(KeyboardBrightness.Off, allowInTerminalServerSession: true)), timerMenuItem, keypressMenuItem, new MenuItem("-"), // or use BarBreak instead, on the next item, to seperate vertically settingsMenuItem, new MenuItem(text: "Exit", onClick: (_, __) => Application.Exit()) }), Visible = false, Text = "Thinkpad Backlight" }; var configWindow = new Form1(brightMenuItem, dimMenuItem, timerMenuItem, keypressMenuItem, keyboardController); _trayIcon.DoubleClick += configWindow.ShowConfig; settingsMenuItem.Click += configWindow.ShowConfig; _trayIcon.Visible = true; }
private void SystemEventsOnPowerModeChanged(object sender, PowerModeChangedEventArgs e) { // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault switch (e.Mode) { case PowerModes.Suspend: timer1.Stop(); break; case PowerModes.Resume: if (Settings.Default.EnableAtStartup) { _keyboardController.ToggleBacklight(allowInTerminalServerSession: false); } timer1.Reset(); break; } }
public Form1(MenuItem brightMenuItem, MenuItem dimMenuItem, MenuItem timerMenuItem, MenuItem keypressMenuItem, KeyboardController keyboardController) { if (brightMenuItem == null) { throw new ArgumentNullException(nameof(brightMenuItem)); } if (dimMenuItem == null) { throw new ArgumentNullException(nameof(dimMenuItem)); } _timerMenuItem = timerMenuItem ?? throw new ArgumentNullException(nameof(timerMenuItem)); _keypressMenuItem = keypressMenuItem ?? throw new ArgumentNullException(nameof(keypressMenuItem)); _keyboardController = keyboardController ?? throw new ArgumentNullException(nameof(keyboardController)); InitializeComponent(); Icon = Properties.Resources.TrayIcon; brightMenuItem.Click += (sender, args) => { timer1.Reset(); _keyboardController.ToggleBacklight(KeyboardBrightness.Bright, allowInTerminalServerSession: true); }; dimMenuItem.Click += (sender, args) => { timer1.Reset(); _keyboardController.ToggleBacklight(KeyboardBrightness.Dim, allowInTerminalServerSession: true); }; timerMenuItem.Click += TimerMenuItemOnClick; keypressMenuItem.Click += KeypressMenuItemOnClick; if (Settings.Default.Seconds < 1) { throw new ConfigurationErrorsException("The seconds setting must be 1 or more"); } SystemEvents.PowerModeChanged += SystemEventsOnPowerModeChanged; if (Settings.Default.Timer) { timer1.Interval = Settings.Default.Seconds * 1000; timer1.Start(); } if (Settings.Default.MonitorKeys) { SubscribeToKeyDownEvents(); } BrightnessComboBox.SelectedIndex = Settings.Default.Bright ? 1 : 0; BrightnessComboBox.SelectedIndexChanged += (sender, args) => { Settings.Default.Bright = BrightnessComboBox.SelectedIndex == 1; Settings.Default.Save(); }; OnStartupCheckBox.Checked = Settings.Default.EnableAtStartup; OnStartupCheckBox.CheckedChanged += (sender, args) => { Settings.Default.EnableAtStartup = OnStartupCheckBox.Checked; Settings.Default.Save(); }; OnKeyPressCheckBox.Checked = Settings.Default.MonitorKeys; OnKeyPressCheckBox.CheckedChanged += KeypressMenuItemOnClick; TimerCheckBox.Checked = Settings.Default.Timer; TimerCheckBox.CheckedChanged += TimerMenuItemOnClick; SecondsNumeric.Maximum = ushort.MaxValue; SecondsNumeric.Value = Settings.Default.Seconds; SecondsDisplayLabel.Text = $"({SecondsNumeric.Value})"; SecondsNumeric.ValueChanged += (sender, args) => { ushort seconds = (ushort)SecondsNumeric.Value; SecondsDisplayLabel.Text = $"({seconds})"; timer1.Interval = seconds * 1000; Settings.Default.Seconds = seconds; Settings.Default.Save(); }; void TimerMenuItemOnClick(object sender, EventArgs args) { if (timerMenuItem.Checked) { timerMenuItem.Checked = false; Settings.Default.Timer = false; timer1.Stop(); } else { timerMenuItem.Checked = true; Settings.Default.Timer = true; timer1.Stop(); timer1.Start(); } Settings.Default.Save(); } void KeypressMenuItemOnClick(object sender, EventArgs args) { if (keypressMenuItem.Checked) { keypressMenuItem.Checked = false; Settings.Default.MonitorKeys = false; UnsubscribeFromKeyDownEvents(); } else { keypressMenuItem.Checked = true; Settings.Default.MonitorKeys = true; SubscribeToKeyDownEvents(); } Settings.Default.Save(); } }