diff --git a/MainWindow.xaml b/MainWindow.xaml index 6ed68ae..6ffb200 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -8,14 +8,44 @@ Title="WMR USB Controller" Height="512" Width="512" - Icon="WMR USB Controller Main Icon.ico" - Background="#FF5B5B5B"> + Icon="WMR USB Controller Main Icon.ico"> - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + \ No newline at end of file diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 8c226cb..a0a6ff3 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -1,6 +1,9 @@ using System; +using System.Text.RegularExpressions; using System.Windows; +using System.Windows.Input; using WMR_USB_Controller.YUART.Autostart; +using WMR_USB_Controller.YUART.Sleep_Mode; using WMR_USB_Controller.YUART.Tray_Icon; using WMR_USB_Controller.YUART.USB; @@ -15,6 +18,7 @@ public partial class MainWindow private AutostartManager _autostartManager; private TrayIconManager _trayIconManager; + private SleepModeManager _sleepModeManager; public MainWindow() { @@ -22,7 +26,8 @@ public MainWindow() SetupTrayIconManager(); SetupAutostartManager(); - + SetupSleepModeManager(); + _usbDevicesManager.Initialize(); DisableWmrDeviceOnStartup(); @@ -40,6 +45,12 @@ private void SetupAutostartManager() _autostartManager.Initialize(); } + private void SetupSleepModeManager() + { + _sleepModeManager = new SleepModeManager(SleepDelayValue, ScreensaverModeStatus, ScreensaverModeStatusCheckbox); + _sleepModeManager.Initialize(); + } + private void DisableWmrDeviceOnStartup() { ChangeWmrDeviceState(false); @@ -81,5 +92,35 @@ private void SwitchAutostartStatus(object sender, RoutedEventArgs e) { _autostartManager.SetToAutostart(); } + + private void NumberValidationTextBox(object sender, TextCompositionEventArgs e) + { + var regex = new Regex("[^0-9]+"); + e.Handled = regex.IsMatch(e.Text); + } + + private void TrySetNewSleepDelayValue(object sender, KeyEventArgs e) + { + if (e.Key != Key.Enter) return; + + _sleepModeManager.SetNewSleepDelay(Int32.Parse(SleepDelayInputField.Text)); + + SleepDelayInputField.Text = String.Empty; + } + + private void EnableScreensaverMode(object sender, RoutedEventArgs e) + { + _sleepModeManager.SetScreensaverModeStatus(true); + } + + private void DisableScreensaverMode(object sender, RoutedEventArgs e) + { + _sleepModeManager.SetScreensaverModeStatus(false); + } + + private void ResetSleepModeValues(object sender, RoutedEventArgs e) + { + _sleepModeManager.ResetSleepModeValues(); + } } } diff --git a/WMR USB Controller.csproj b/WMR USB Controller.csproj index 55ffc87..c02ee1a 100644 --- a/WMR USB Controller.csproj +++ b/WMR USB Controller.csproj @@ -61,8 +61,12 @@ Designer + + + + MSBuild:Compile Designer diff --git a/YUART/Autostart/AutostartManager.cs b/YUART/Autostart/AutostartManager.cs index 45d5dea..8c2aa98 100644 --- a/YUART/Autostart/AutostartManager.cs +++ b/YUART/Autostart/AutostartManager.cs @@ -1,5 +1,6 @@ using System.Windows.Controls; using Microsoft.Win32; +using WMR_USB_Controller.YUART.Utilities; using Application = System.Windows.Application; namespace WMR_USB_Controller.YUART.Autostart @@ -41,9 +42,7 @@ private void SetAutostartCheckboxValue() { if (_autostartCheckbox.IsChecked == null) return; - var startupAutostartValue = _autostartRegKey.GetValue(_appName); - - _autostartCheckbox.IsChecked = startupAutostartValue != null; + _autostartCheckbox.IsChecked = _autostartRegKey.IsExists(_appName); } /// @@ -51,7 +50,7 @@ private void SetAutostartCheckboxValue() /// public void SetToAutostart() { - if (_autostartCheckbox.IsChecked == null || _autostartRegKey == null) return; + if (_autostartCheckbox.IsChecked == null) return; if (_autostartCheckbox.IsChecked.Value) { diff --git a/YUART/Sleep Mode/SleepModeManager.cs b/YUART/Sleep Mode/SleepModeManager.cs new file mode 100644 index 0000000..c0f7fed --- /dev/null +++ b/YUART/Sleep Mode/SleepModeManager.cs @@ -0,0 +1,106 @@ +using System; +using System.Windows.Controls; +using Microsoft.Win32; +using WMR_USB_Controller.YUART.Utilities; + +namespace WMR_USB_Controller.YUART.Sleep_Mode +{ + /// + /// Class, that provide controls for sleep mode of WMR. + /// + public sealed class SleepModeManager + { + private const string PathToHololensRegKeys = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Holographic"; + private const string SleepDelayRegkeyName = "IdleTimerDuration"; + private const string ScreensaverModeRegkeyName = "ScreensaverModeEnabled"; + private const int DefaultSleepDelay = 15; + + private readonly RegistryKey _hololensRegKey = Registry.CurrentUser.OpenSubKey(PathToHololensRegKeys, true); + private readonly Label _sleepDelayValueLabel; + private readonly Label _screensaverModeStatusLabel; + private readonly CheckBox _screensaverModeCheckbox; + + public SleepModeManager(Label sleepDelayValueLabel, Label screensaverModeStatusLabel, CheckBox screensaverModeCheckbox) + { + _sleepDelayValueLabel = sleepDelayValueLabel; + _screensaverModeStatusLabel = screensaverModeStatusLabel; + _screensaverModeCheckbox = screensaverModeCheckbox; + } + + /// + /// Initialize class and set startup UI values + /// + public void Initialize() + { + UpdateUiValues(); + } + + private void UpdateUiValues() + { + UpdateLabelsValues(); + + SetScreensaverModeCheckboxValue(); + } + + private void UpdateLabelsValues() + { + SetCurrentSleepDelayValue(); + SetCurrentScreensaverModeStatus(); + } + + private void SetCurrentSleepDelayValue() + { + _sleepDelayValueLabel.Content = $"{(_hololensRegKey.IsExists(SleepDelayRegkeyName) ? TimeConverter.ConvertMillisecondsIntoMinutes((int) _hololensRegKey.GetValue(SleepDelayRegkeyName)) : DefaultSleepDelay).ToString()} minutes"; + } + + private void SetCurrentScreensaverModeStatus() + { + _screensaverModeStatusLabel.Content = GetCurrentScreensaverModeStatusFromRegistry(); + } + + private void SetScreensaverModeCheckboxValue() + { + if (_screensaverModeCheckbox.IsChecked == null) return; + + _screensaverModeCheckbox.IsChecked = GetCurrentScreensaverModeStatusFromRegistry(); + } + + private bool GetCurrentScreensaverModeStatusFromRegistry() + { + return _hololensRegKey.IsExists(ScreensaverModeRegkeyName) && ((int) _hololensRegKey.GetValue(ScreensaverModeRegkeyName)).ConvertIntToBool(); + } + + /// + /// Set new value to regkey of sleep delay for WMR. + /// + /// New value of the sleep delay in minutes. + public void SetNewSleepDelay(int newDelayInMinutes) + { + _hololensRegKey.SetValue(SleepDelayRegkeyName, TimeConverter.ConvertMinutesIntoMilliseconds(newDelayInMinutes)); + + SetCurrentSleepDelayValue(); + } + + /// + /// Set new value for screensaver regkey. + /// + /// New status (on/off) + public void SetScreensaverModeStatus(bool newStatus) + { + _hololensRegKey.SetValue(ScreensaverModeRegkeyName, newStatus.ConvertBoolToInt()); + + SetCurrentScreensaverModeStatus(); + } + + /// + /// Reset sleep delay value and screensaver mode value (this function will delete keys from the registry) + /// + public void ResetSleepModeValues() + { + _hololensRegKey.DeleteValue(SleepDelayRegkeyName); + _hololensRegKey.DeleteValue(ScreensaverModeRegkeyName); + + UpdateUiValues(); + } + } +} \ No newline at end of file diff --git a/YUART/Utilities/IntBoolConverter.cs b/YUART/Utilities/IntBoolConverter.cs new file mode 100644 index 0000000..8f0f392 --- /dev/null +++ b/YUART/Utilities/IntBoolConverter.cs @@ -0,0 +1,18 @@ +namespace WMR_USB_Controller.YUART.Utilities +{ + /// + /// Class, that provides tools for int-bool conversion. + /// + public static class IntBoolConverter + { + public static bool ConvertIntToBool(this int value) + { + return value == 1; + } + + public static int ConvertBoolToInt(this bool value) + { + return value ? 1 : 0; + } + } +} \ No newline at end of file diff --git a/YUART/Utilities/RegkeyChecker.cs b/YUART/Utilities/RegkeyChecker.cs new file mode 100644 index 0000000..3e5d7fa --- /dev/null +++ b/YUART/Utilities/RegkeyChecker.cs @@ -0,0 +1,15 @@ +using Microsoft.Win32; + +namespace WMR_USB_Controller.YUART.Utilities +{ + /// + /// Class, that provides method to check regkeys. + /// + public static class RegkeyChecker + { + public static bool IsExists(this RegistryKey regkey, string keyName) + { + return regkey.GetValue(keyName) != null; + } + } +} \ No newline at end of file diff --git a/YUART/Utilities/TimeConverter.cs b/YUART/Utilities/TimeConverter.cs new file mode 100644 index 0000000..7d2b713 --- /dev/null +++ b/YUART/Utilities/TimeConverter.cs @@ -0,0 +1,22 @@ +using System; + +namespace WMR_USB_Controller.YUART.Utilities +{ + /// + /// Class, that handles methods for time conversion. + /// + public static class TimeConverter + { + private const double MillisecondsInMinute = 60000d; + + public static int ConvertMillisecondsIntoMinutes(int milliseconds) + { + return Convert.ToInt32(Math.Round(milliseconds / MillisecondsInMinute)); + } + + public static int ConvertMinutesIntoMilliseconds(int minutes) + { + return Convert.ToInt32(minutes * MillisecondsInMinute); + } + } +} \ No newline at end of file