Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
Merge branch 'dev-3' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtYurchenko committed Dec 8, 2020
2 parents 0d7ea6a + 391e378 commit 0b40fc2
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 14 deletions.
48 changes: 39 additions & 9 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<Grid>
<ToggleButton Name="WmrStatusToggle" Content="Disable WMR device" FontSize="24" Width="256" Height="124" Checked="DisableWmrDeviceAction" Unchecked="EnableWmrDeviceAction" HorizontalAlignment="Center"/>
<CheckBox Name="AutostartCheckbox" Click="SwitchAutostartStatus" Content="Add to autostart" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="50">
<CheckBox.LayoutTransform>
<ScaleTransform ScaleX="2" ScaleY="2" />
</CheckBox.LayoutTransform>
</CheckBox>
<TabControl>
<TabItem Header="WMR status" FontSize="24">
<TabItem.Content>
<StackPanel Background="#FF5B5B5B">
<ToggleButton Name="WmrStatusToggle" Content="Disable WMR device" FontSize="24" Width="256" Height="124" Margin="50" Checked="DisableWmrDeviceAction"
Unchecked="EnableWmrDeviceAction" HorizontalAlignment="Center" />

<CheckBox Name="AutostartCheckbox" Click="SwitchAutostartStatus" Content="Add to autostart" FontSize="14" Margin="50" HorizontalAlignment="Center">
<CheckBox.LayoutTransform>
<ScaleTransform ScaleX="2" ScaleY="2" />
</CheckBox.LayoutTransform>
</CheckBox>
</StackPanel>
</TabItem.Content>
</TabItem>

<TabItem Header="WMR sleep mode" FontSize="24">
<TabItem.Content>
<StackPanel Background="#FF5B5B5B">
<Label Content="Current sleep delay in minutes" Margin="0, 15, 0 , 5" HorizontalAlignment="Center"></Label>
<Label Name="SleepDelayValue" HorizontalAlignment="Center"></Label>
<Label Content="Set new sleep delay value in minutes" Margin="0, 10, 0 , 5" HorizontalAlignment="Center"></Label>
<TextBox Name="SleepDelayInputField" Width="300" Height="35" KeyUp="TrySetNewSleepDelayValue" PreviewTextInput="NumberValidationTextBox" HorizontalAlignment="Center"></TextBox>

<Label Content="Status of screensaver mode" Margin="0, 15, 0 ,5" HorizontalAlignment="Center"></Label>
<Label Name="ScreensaverModeStatus" HorizontalAlignment="Center"></Label>
<CheckBox Name="ScreensaverModeStatusCheckbox" Checked="EnableScreensaverMode" Unchecked="DisableScreensaverMode" Content="Enable screensaver mode" FontSize="14" HorizontalAlignment="Center">
<CheckBox.LayoutTransform>
<ScaleTransform ScaleX="2" ScaleY="2" />
</CheckBox.LayoutTransform>
</CheckBox>

<Button Content="Reset sleep mode changes" Width="300" Height="50" Margin="25" Click="ResetSleepModeValues"></Button>
</StackPanel>
</TabItem.Content>
</TabItem>
</TabControl>
</Grid>
</Window>
</Window>
43 changes: 42 additions & 1 deletion MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -15,14 +18,16 @@ public partial class MainWindow

private AutostartManager _autostartManager;
private TrayIconManager _trayIconManager;
private SleepModeManager _sleepModeManager;

public MainWindow()
{
InitializeComponent();

SetupTrayIconManager();
SetupAutostartManager();

SetupSleepModeManager();

_usbDevicesManager.Initialize();

DisableWmrDeviceOnStartup();
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
}
}
4 changes: 4 additions & 0 deletions WMR USB Controller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="YUART\Autostart\AutostartManager.cs" />
<Compile Include="YUART\Sleep Mode\SleepModeManager.cs" />
<Compile Include="YUART\Tray Icon\TrayIconManager.cs" />
<Compile Include="YUART\USB\UsbDevicesManager.cs" />
<Compile Include="YUART\Utilities\IntBoolConverter.cs" />
<Compile Include="YUART\Utilities\RegkeyChecker.cs" />
<Compile Include="YUART\Utilities\TimeConverter.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
7 changes: 3 additions & 4 deletions YUART/Autostart/AutostartManager.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -41,17 +42,15 @@ private void SetAutostartCheckboxValue()
{
if (_autostartCheckbox.IsChecked == null) return;

var startupAutostartValue = _autostartRegKey.GetValue(_appName);

_autostartCheckbox.IsChecked = startupAutostartValue != null;
_autostartCheckbox.IsChecked = _autostartRegKey.IsExists(_appName);
}

/// <summary>
/// Sets/removes app from autostart (depends from the value of autostart checkbox).
/// </summary>
public void SetToAutostart()
{
if (_autostartCheckbox.IsChecked == null || _autostartRegKey == null) return;
if (_autostartCheckbox.IsChecked == null) return;

if (_autostartCheckbox.IsChecked.Value)
{
Expand Down
106 changes: 106 additions & 0 deletions YUART/Sleep Mode/SleepModeManager.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Class, that provide controls for sleep mode of WMR.
/// </summary>
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;
}

/// <summary>
/// Initialize class and set startup UI values
/// </summary>
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();
}

/// <summary>
/// Set new value to regkey of sleep delay for WMR.
/// </summary>
/// <param name="newDelayInMinutes">New value of the sleep delay in minutes.</param>
public void SetNewSleepDelay(int newDelayInMinutes)
{
_hololensRegKey.SetValue(SleepDelayRegkeyName, TimeConverter.ConvertMinutesIntoMilliseconds(newDelayInMinutes));

SetCurrentSleepDelayValue();
}

/// <summary>
/// Set new value for screensaver regkey.
/// </summary>
/// <param name="newStatus">New status (on/off)</param>
public void SetScreensaverModeStatus(bool newStatus)
{
_hololensRegKey.SetValue(ScreensaverModeRegkeyName, newStatus.ConvertBoolToInt());

SetCurrentScreensaverModeStatus();
}

/// <summary>
/// Reset sleep delay value and screensaver mode value (this function will delete keys from the registry)
/// </summary>
public void ResetSleepModeValues()
{
_hololensRegKey.DeleteValue(SleepDelayRegkeyName);
_hololensRegKey.DeleteValue(ScreensaverModeRegkeyName);

UpdateUiValues();
}
}
}
18 changes: 18 additions & 0 deletions YUART/Utilities/IntBoolConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace WMR_USB_Controller.YUART.Utilities
{
/// <summary>
/// Class, that provides tools for int-bool conversion.
/// </summary>
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;
}
}
}
15 changes: 15 additions & 0 deletions YUART/Utilities/RegkeyChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.Win32;

namespace WMR_USB_Controller.YUART.Utilities
{
/// <summary>
/// Class, that provides method to check regkeys.
/// </summary>
public static class RegkeyChecker
{
public static bool IsExists(this RegistryKey regkey, string keyName)
{
return regkey.GetValue(keyName) != null;
}
}
}
22 changes: 22 additions & 0 deletions YUART/Utilities/TimeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace WMR_USB_Controller.YUART.Utilities
{
/// <summary>
/// Class, that handles methods for time conversion.
/// </summary>
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);
}
}
}

0 comments on commit 0b40fc2

Please sign in to comment.