From c9c455b64af8548550f33868a280aaad55b64de3 Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Wed, 9 Dec 2020 11:59:51 +0100 Subject: [PATCH 1/3] HolographicManager base class was created --- MainWindow.xaml | 8 +++++-- MainWindow.xaml.cs | 2 +- WMR USB Controller.csproj | 3 ++- YUART/Holographic/HolographicManager.cs | 13 ++++++++++ .../Sleep Mode/SleepModeManager.cs | 24 ++++++++----------- 5 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 YUART/Holographic/HolographicManager.cs rename YUART/{ => Holographic}/Sleep Mode/SleepModeManager.cs (71%) diff --git a/MainWindow.xaml b/MainWindow.xaml index 6ffb200..6f21180 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -16,8 +16,12 @@ - - + + + + + + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index a0a6ff3..08dddf2 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -3,7 +3,7 @@ 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.Holographic.Sleep_Mode; using WMR_USB_Controller.YUART.Tray_Icon; using WMR_USB_Controller.YUART.USB; diff --git a/WMR USB Controller.csproj b/WMR USB Controller.csproj index c02ee1a..e5649ee 100644 --- a/WMR USB Controller.csproj +++ b/WMR USB Controller.csproj @@ -61,7 +61,8 @@ Designer - + + diff --git a/YUART/Holographic/HolographicManager.cs b/YUART/Holographic/HolographicManager.cs new file mode 100644 index 0000000..d0011a9 --- /dev/null +++ b/YUART/Holographic/HolographicManager.cs @@ -0,0 +1,13 @@ +using Microsoft.Win32; + +namespace WMR_USB_Controller.YUART.Holographic +{ + /// + /// Base class, that provides base data and behaviour for holographic managers. + /// + public class HolographicManager + { + private const string PathToHololensRegKeys = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Holographic"; + protected readonly RegistryKey HololensRegKey = Registry.CurrentUser.OpenSubKey(PathToHololensRegKeys, true); + } +} \ No newline at end of file diff --git a/YUART/Sleep Mode/SleepModeManager.cs b/YUART/Holographic/Sleep Mode/SleepModeManager.cs similarity index 71% rename from YUART/Sleep Mode/SleepModeManager.cs rename to YUART/Holographic/Sleep Mode/SleepModeManager.cs index c0f7fed..ca9327e 100644 --- a/YUART/Sleep Mode/SleepModeManager.cs +++ b/YUART/Holographic/Sleep Mode/SleepModeManager.cs @@ -1,21 +1,17 @@ -using System; -using System.Windows.Controls; -using Microsoft.Win32; +using System.Windows.Controls; using WMR_USB_Controller.YUART.Utilities; -namespace WMR_USB_Controller.YUART.Sleep_Mode +namespace WMR_USB_Controller.YUART.Holographic.Sleep_Mode { /// /// Class, that provide controls for sleep mode of WMR. /// - public sealed class SleepModeManager + public sealed class SleepModeManager : HolographicManager { - 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; @@ -50,7 +46,7 @@ private void UpdateLabelsValues() private void SetCurrentSleepDelayValue() { - _sleepDelayValueLabel.Content = $"{(_hololensRegKey.IsExists(SleepDelayRegkeyName) ? TimeConverter.ConvertMillisecondsIntoMinutes((int) _hololensRegKey.GetValue(SleepDelayRegkeyName)) : DefaultSleepDelay).ToString()} minutes"; + _sleepDelayValueLabel.Content = $"{(HololensRegKey.IsExists(SleepDelayRegkeyName) ? TimeConverter.ConvertMillisecondsIntoMinutes((int) HololensRegKey.GetValue(SleepDelayRegkeyName)) : DefaultSleepDelay).ToString()} minutes"; } private void SetCurrentScreensaverModeStatus() @@ -67,7 +63,7 @@ private void SetScreensaverModeCheckboxValue() private bool GetCurrentScreensaverModeStatusFromRegistry() { - return _hololensRegKey.IsExists(ScreensaverModeRegkeyName) && ((int) _hololensRegKey.GetValue(ScreensaverModeRegkeyName)).ConvertIntToBool(); + return HololensRegKey.IsExists(ScreensaverModeRegkeyName) && ((int) HololensRegKey.GetValue(ScreensaverModeRegkeyName)).ConvertIntToBool(); } /// @@ -76,7 +72,7 @@ private bool GetCurrentScreensaverModeStatusFromRegistry() /// New value of the sleep delay in minutes. public void SetNewSleepDelay(int newDelayInMinutes) { - _hololensRegKey.SetValue(SleepDelayRegkeyName, TimeConverter.ConvertMinutesIntoMilliseconds(newDelayInMinutes)); + HololensRegKey.SetValue(SleepDelayRegkeyName, TimeConverter.ConvertMinutesIntoMilliseconds(newDelayInMinutes)); SetCurrentSleepDelayValue(); } @@ -87,7 +83,7 @@ public void SetNewSleepDelay(int newDelayInMinutes) /// New status (on/off) public void SetScreensaverModeStatus(bool newStatus) { - _hololensRegKey.SetValue(ScreensaverModeRegkeyName, newStatus.ConvertBoolToInt()); + HololensRegKey.SetValue(ScreensaverModeRegkeyName, newStatus.ConvertBoolToInt()); SetCurrentScreensaverModeStatus(); } @@ -97,8 +93,8 @@ public void SetScreensaverModeStatus(bool newStatus) /// public void ResetSleepModeValues() { - _hololensRegKey.DeleteValue(SleepDelayRegkeyName); - _hololensRegKey.DeleteValue(ScreensaverModeRegkeyName); + HololensRegKey.DeleteValue(SleepDelayRegkeyName); + HololensRegKey.DeleteValue(ScreensaverModeRegkeyName); UpdateUiValues(); } From 3cc4d23218dc7d31026a8113290c6abca11213e6 Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Wed, 9 Dec 2020 12:53:04 +0100 Subject: [PATCH 2/3] VirtualScreen toggle behaviour was implemented --- MainWindow.xaml | 2 +- MainWindow.xaml.cs | 19 +++++++ WMR USB Controller.csproj | 13 +++++ .../VirtualScreens/VirtualScreensManager.cs | 49 +++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 YUART/Holographic/VirtualScreens/VirtualScreensManager.cs diff --git a/MainWindow.xaml b/MainWindow.xaml index 6f21180..5f82ebd 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -16,7 +16,7 @@ - + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 08dddf2..b69ef46 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -4,6 +4,7 @@ using System.Windows.Input; using WMR_USB_Controller.YUART.Autostart; using WMR_USB_Controller.YUART.Holographic.Sleep_Mode; +using WMR_USB_Controller.YUART.Holographic.VirtualScreens; using WMR_USB_Controller.YUART.Tray_Icon; using WMR_USB_Controller.YUART.USB; @@ -19,6 +20,7 @@ public partial class MainWindow private AutostartManager _autostartManager; private TrayIconManager _trayIconManager; private SleepModeManager _sleepModeManager; + private VirtualScreensManager _virtualScreensManager; public MainWindow() { @@ -27,6 +29,7 @@ public MainWindow() SetupTrayIconManager(); SetupAutostartManager(); SetupSleepModeManager(); + SetupVirtualScreensManager(); _usbDevicesManager.Initialize(); @@ -51,6 +54,12 @@ private void SetupSleepModeManager() _sleepModeManager.Initialize(); } + private void SetupVirtualScreensManager() + { + _virtualScreensManager = new VirtualScreensManager(VirtualScreensCheckbox); + _virtualScreensManager.Initialize(); + } + private void DisableWmrDeviceOnStartup() { ChangeWmrDeviceState(false); @@ -122,5 +131,15 @@ private void ResetSleepModeValues(object sender, RoutedEventArgs e) { _sleepModeManager.ResetSleepModeValues(); } + + private void EnableVirtualScreens(object sender, RoutedEventArgs e) + { + _virtualScreensManager.SetVirtualScreensStatus(true); + } + + private void DisableVirtualScreens(object sender, RoutedEventArgs e) + { + _virtualScreensManager.SetVirtualScreensStatus(false); + } } } diff --git a/WMR USB Controller.csproj b/WMR USB Controller.csproj index e5649ee..5f09cc1 100644 --- a/WMR USB Controller.csproj +++ b/WMR USB Controller.csproj @@ -43,6 +43,7 @@ app.manifest + @@ -63,6 +64,7 @@ + @@ -111,5 +113,16 @@ + + + {F935DC20-1CF0-11D0-ADB9-00C04FD58A0B} + 1 + 0 + 0 + tlbimp + False + True + + \ No newline at end of file diff --git a/YUART/Holographic/VirtualScreens/VirtualScreensManager.cs b/YUART/Holographic/VirtualScreens/VirtualScreensManager.cs new file mode 100644 index 0000000..11ffb9b --- /dev/null +++ b/YUART/Holographic/VirtualScreens/VirtualScreensManager.cs @@ -0,0 +1,49 @@ +using System.Windows.Controls; +using WMR_USB_Controller.YUART.Utilities; + +namespace WMR_USB_Controller.YUART.Holographic.VirtualScreens +{ + /// + /// Class, that provide methods to manage Virtual Screens Allocation option. + /// + public sealed class VirtualScreensManager : HolographicManager + { + private const string VirtualScreensRegkey = "PreallocateVirtualMonitors"; + + private readonly CheckBox _virtualScreensCheckbox; + + public VirtualScreensManager(CheckBox virtualScreensCheckbox) + { + _virtualScreensCheckbox = virtualScreensCheckbox; + } + + /// + /// Initialize class and set startup UI values + /// + public void Initialize() + { + SetVirtualMonitorsCheckboxValue(); + } + + private void SetVirtualMonitorsCheckboxValue() + { + if (_virtualScreensCheckbox.IsChecked == null) return; + + _virtualScreensCheckbox.IsChecked = !GetCurrentVirtualScreensStatusFromRegistry(); + } + + private bool GetCurrentVirtualScreensStatusFromRegistry() + { + return HololensRegKey.IsExists(VirtualScreensRegkey) && ((int) HololensRegKey.GetValue(VirtualScreensRegkey)).ConvertIntToBool(); + } + + /// + /// Set new status for Virtual Screens option. + /// + /// Turn on/off virtual screens + public void SetVirtualScreensStatus(bool newStatus) + { + HololensRegKey.SetValue(VirtualScreensRegkey, newStatus.ConvertBoolToInt()); + } + } +} \ No newline at end of file From bb8e55d79ad8b310f5fee40af4df0613bc741d31 Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Wed, 9 Dec 2020 13:19:04 +0100 Subject: [PATCH 3/3] Autostart code was fully reimplemented --- YUART/Autostart/AutostartManager.cs | 48 +++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/YUART/Autostart/AutostartManager.cs b/YUART/Autostart/AutostartManager.cs index 8c2aa98..dd8dad6 100644 --- a/YUART/Autostart/AutostartManager.cs +++ b/YUART/Autostart/AutostartManager.cs @@ -1,7 +1,8 @@ -using System.Windows.Controls; -using Microsoft.Win32; -using WMR_USB_Controller.YUART.Utilities; +using System; +using System.Windows.Controls; +using IWshRuntimeLibrary; using Application = System.Windows.Application; +using File = System.IO.File; namespace WMR_USB_Controller.YUART.Autostart { @@ -10,13 +11,16 @@ namespace WMR_USB_Controller.YUART.Autostart /// public sealed class AutostartManager { - private const string PathToAutostartRegKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; - - private readonly RegistryKey _autostartRegKey = Registry.CurrentUser.OpenSubKey(PathToAutostartRegKey, true); - private readonly string _appExecutionPath = System.Reflection.Assembly.GetExecutingAssembly().Location; + private const string PathToTrayIcon = "WMR USB Controller Main Icon.ico"; + + private readonly WshShell _wshShell = new WshShell(); + private readonly string _workingDirectory = AppDomain.CurrentDomain.BaseDirectory; + private readonly string _assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location; private readonly CheckBox _autostartCheckbox; private string _appName; + private string _autostartShortcutPath; + private string _iconLocation; public AutostartManager(CheckBox autostartCheckbox) { @@ -30,6 +34,10 @@ public void Initialize() { SetApplicationName(); + SetAutostartShortcutPath(); + + SetIconLocation(); + SetAutostartCheckboxValue(); } @@ -38,11 +46,21 @@ private void SetApplicationName() _appName = Application.Current.MainWindow?.Title; } + private void SetAutostartShortcutPath() + { + _autostartShortcutPath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.Startup)}\{_appName}.lnk"; + } + + private void SetIconLocation() + { + _iconLocation = $"{_workingDirectory}{PathToTrayIcon}"; + } + private void SetAutostartCheckboxValue() { if (_autostartCheckbox.IsChecked == null) return; - _autostartCheckbox.IsChecked = _autostartRegKey.IsExists(_appName); + _autostartCheckbox.IsChecked = File.Exists(_autostartShortcutPath); } /// @@ -54,12 +72,22 @@ public void SetToAutostart() if (_autostartCheckbox.IsChecked.Value) { - _autostartRegKey.SetValue(_appName, _appExecutionPath); + CreateNewAutostartShortcut(); } else { - _autostartRegKey.DeleteValue(_appName, false); + File.Delete(_autostartShortcutPath); } } + + private void CreateNewAutostartShortcut() + { + var shortcut = (IWshShortcut) _wshShell.CreateShortcut(_autostartShortcutPath); + shortcut.Description = _appName; + shortcut.WorkingDirectory = _workingDirectory; + shortcut.TargetPath = _assemblyLocation; + shortcut.IconLocation = _iconLocation; + shortcut.Save(); + } } } \ No newline at end of file