2020-07-07 09:37:55 +12:00
// <copyright file="SettingsForm.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
2020-07-07 07:15:45 +12:00
// </copyright>
2020-05-05 05:43:47 +12:00
namespace SystemTrayMenu.UserInterface
{
2020-07-07 07:15:45 +12:00
using System ;
using System.Collections.Generic ;
using System.Drawing ;
using System.Reflection ;
using System.Text ;
using System.Windows.Forms ;
2020-07-08 03:05:19 +12:00
using Microsoft.Win32 ;
using SystemTrayMenu.UserInterface.HotkeyTextboxControl ;
2020-07-07 07:15:45 +12:00
using SystemTrayMenu.Utilities ;
2020-07-08 03:05:19 +12:00
using static SystemTrayMenu . UserInterface . HotkeyTextboxControl . HotkeyControl ;
2020-07-07 07:15:45 +12:00
2020-05-05 05:43:47 +12:00
public partial class SettingsForm : Form
{
2020-05-14 05:14:51 +12:00
private readonly string newHotKey = string . Empty ;
2020-07-07 09:37:55 +12:00
private bool inHotkey = false ;
2020-05-05 05:43:47 +12:00
public SettingsForm ( )
{
InitializeComponent ( ) ;
Translate ( ) ;
void Translate ( )
{
Text = Translator . GetText ( "Settings" ) ;
tabPageGeneral . Text = Translator . GetText ( "General" ) ;
2020-08-06 01:33:45 +12:00
tabPageExpert . Text = Translator . GetText ( "Expert" ) ;
2020-05-05 05:43:47 +12:00
labelFolder . Text = Translator . GetText ( "Folder" ) ;
labelAutostart . Text = Translator . GetText ( "Autostart" ) ;
checkBoxAutostart . Text = Translator . GetText ( "Launch on startup" ) ;
labelHotkey . Text = Translator . GetText ( "Hotkey" ) ;
labelLanguage . Text = Translator . GetText ( "Language" ) ;
2020-08-06 01:33:45 +12:00
checkBoxHideTaskbarForm . Text = Translator . GetText ( "Hide Taskbar Icon" ) ;
checkBoxOpenItemWithOneClick . Text = Translator . GetText ( "Single click to start item" ) ;
2020-08-08 01:24:25 +12:00
checkBoxDarkModeAlwaysOn . Text = Translator . GetText ( "Dark Mode always active" ) ;
2020-05-05 05:43:47 +12:00
buttonOk . Text = Translator . GetText ( "buttonOk" ) ;
buttonCancel . Text = Translator . GetText ( "buttonCancel" ) ;
}
InitializeFolder ( ) ;
void InitializeFolder ( )
{
textBoxFolder . Text = Config . Path ;
}
InitializeAutostart ( ) ;
void InitializeAutostart ( )
{
2020-05-14 05:14:51 +12:00
checkBoxAutostart . Checked =
2020-05-05 05:43:47 +12:00
Properties . Settings . Default . IsAutostartActivated ;
}
InitializeHotkey ( ) ;
void InitializeHotkey ( )
{
textBoxHotkey . SetHotkey ( Properties . Settings . Default . HotKey ) ;
}
InitializeLanguage ( ) ;
void InitializeLanguage ( )
{
2020-05-14 05:14:51 +12:00
List < Language > dataSource = new List < Language >
{
new Language ( ) { Name = "English" , Value = "en" } ,
2020-07-07 07:15:45 +12:00
new Language ( ) { Name = "Deutsch" , Value = "de" } ,
2020-05-14 05:14:51 +12:00
} ;
2020-05-05 05:43:47 +12:00
comboBoxLanguage . DataSource = dataSource ;
comboBoxLanguage . DisplayMember = "Name" ;
comboBoxLanguage . ValueMember = "Value" ;
2020-05-14 05:14:51 +12:00
comboBoxLanguage . SelectedValue =
2020-05-05 05:43:47 +12:00
Properties . Settings . Default . CurrentCultureInfoName ;
if ( comboBoxLanguage . SelectedValue = = null )
{
comboBoxLanguage . SelectedValue = "en" ;
}
}
2020-08-06 01:33:45 +12:00
2020-08-08 01:24:25 +12:00
checkBoxHideTaskbarForm . Checked = Properties . Settings . Default . HideTaskbarForm ;
checkBoxOpenItemWithOneClick . Checked = Properties . Settings . Default . OpenItemWithOneClick ;
checkBoxDarkModeAlwaysOn . Checked = Properties . Settings . Default . IsDarkModeAlwaysOn ;
2020-05-05 05:43:47 +12:00
}
2020-07-08 03:05:19 +12:00
public string NewHotKey = > newHotKey ;
2020-05-05 05:43:47 +12:00
2020-07-08 03:05:19 +12:00
/// <summary>
/// Registers all hotkeys as configured, displaying a dialog in case of hotkey conflicts with other tools.
/// </summary>
/// <returns>Whether the hotkeys could be registered to the users content. This also applies if conflicts arise and the user decides to ignore these (i.e. not to register the conflicting hotkey).</returns>
public static bool RegisterHotkeys ( )
2020-05-05 05:43:47 +12:00
{
2020-07-08 03:05:19 +12:00
return RegisterHotkeys ( false ) ;
2020-05-05 05:43:47 +12:00
}
protected override bool ProcessCmdKey ( ref Message msg , Keys keyData )
{
switch ( keyData )
{
case Keys . Escape :
2020-07-07 09:37:55 +12:00
if ( ! inHotkey )
2020-05-05 05:43:47 +12:00
{
DialogResult = DialogResult . Cancel ;
}
else
{
return base . ProcessCmdKey ( ref msg , keyData ) ;
}
2020-07-07 07:15:45 +12:00
2020-05-05 05:43:47 +12:00
break ;
default :
return base . ProcessCmdKey ( ref msg , keyData ) ;
}
2020-07-07 07:15:45 +12:00
2020-05-05 05:43:47 +12:00
return true ;
}
/// <summary>
2020-07-07 09:37:55 +12:00
/// Helper method to cleanly register a hotkey.
2020-05-05 05:43:47 +12:00
/// </summary>
2020-07-07 09:37:55 +12:00
/// <param name="failedKeys">failedKeys.</param>
/// <param name="hotkeyString">hotkeyString.</param>
/// <param name="handler">handler.</param>
/// <returns>bool success.</returns>
2020-05-05 05:43:47 +12:00
private static bool RegisterHotkey ( StringBuilder failedKeys , string hotkeyString , HotKeyHandler handler )
{
Keys modifierKeyCode = HotkeyControl . HotkeyModifiersFromString ( hotkeyString ) ;
Keys virtualKeyCode = HotkeyControl . HotkeyFromString ( hotkeyString ) ;
if ( ! Keys . None . Equals ( virtualKeyCode ) )
{
if ( HotkeyControl . RegisterHotKey ( modifierKeyCode , virtualKeyCode , handler ) < 0 )
{
if ( failedKeys . Length > 0 )
{
failedKeys . Append ( ", " ) ;
}
2020-07-07 07:15:45 +12:00
2020-05-05 05:43:47 +12:00
failedKeys . Append ( hotkeyString ) ;
return false ;
}
}
2020-07-07 07:15:45 +12:00
2020-05-05 05:43:47 +12:00
return true ;
}
2020-07-04 08:02:16 +12:00
private static bool RegisterWrapper ( StringBuilder failedKeys , HotKeyHandler handler )
2020-05-05 05:43:47 +12:00
{
2020-07-07 09:37:55 +12:00
bool success = RegisterHotkey (
failedKeys ,
2020-07-07 02:46:47 +12:00
Properties . Settings . Default . HotKey ,
handler ) ;
return success ;
2020-05-05 05:43:47 +12:00
}
/// <summary>
/// Registers all hotkeys as configured, displaying a dialog in case of hotkey conflicts with other tools.
/// </summary>
2020-07-07 09:37:55 +12:00
/// <param name="ignoreFailedRegistration">if true, a failed hotkey registration will not be reported to the user - the hotkey will simply not be registered.</param>
2020-05-05 05:43:47 +12:00
/// <returns>Whether the hotkeys could be registered to the users content. This also applies if conflicts arise and the user decides to ignore these (i.e. not to register the conflicting hotkey).</returns>
private static bool RegisterHotkeys ( bool ignoreFailedRegistration )
{
2020-07-07 09:37:55 +12:00
// if (_instance == null)
// {
2020-05-05 05:43:47 +12:00
// return false;
2020-07-07 09:37:55 +12:00
// }
2020-05-05 05:43:47 +12:00
bool success = true ;
StringBuilder failedKeys = new StringBuilder ( ) ;
2020-07-07 02:46:47 +12:00
if ( ! RegisterWrapper ( failedKeys , Handler ) )
2020-05-05 05:43:47 +12:00
{
success = false ;
}
if ( ! success )
{
if ( ! ignoreFailedRegistration )
{
success = HandleFailedHotkeyRegistration ( failedKeys . ToString ( ) ) ;
}
}
2020-07-07 07:15:45 +12:00
2020-05-05 05:43:47 +12:00
return success | | ignoreFailedRegistration ;
}
2020-07-07 02:46:47 +12:00
private static void Handler ( )
2020-05-14 05:14:51 +12:00
{
}
2020-05-05 05:43:47 +12:00
///// <summary>
///// Check if OneDrive is blocking hotkeys
///// </summary>
///// <returns>true if onedrive has hotkeys turned on</returns>
2020-07-07 09:37:55 +12:00
// private static bool IsOneDriveBlockingHotkey()
// {
2020-05-05 05:43:47 +12:00
// if (!Environment.OSVersion.IsWindows10())
// {
// return false;
// }
// var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
// var oneDriveSettingsPath = Path.Combine(localAppData, @"Microsoft\OneDrive\settings\Personal");
// if (!Directory.Exists(oneDriveSettingsPath))
// {
// return false;
// }
// var oneDriveSettingsFile = Directory.GetFiles(oneDriveSettingsPath, "*_screenshot.dat").FirstOrDefault();
// if (!File.Exists(oneDriveSettingsFile))
// {
// return false;
// }
// var screenshotSetting = File.ReadAllLines(oneDriveSettingsFile).Skip(1).Take(1).First();
// return "2".Equals(screenshotSetting);
2020-07-07 09:37:55 +12:00
// }
2020-05-05 05:43:47 +12:00
/// <summary>
2020-07-07 07:15:45 +12:00
/// Displays a dialog for the user to choose how to handle hotkey registration failures:
2020-05-05 05:43:47 +12:00
/// retry (allowing to shut down the conflicting application before),
/// ignore (not registering the conflicting hotkey and resetting the respective config to "None", i.e. not trying to register it again on next startup)
2020-07-07 09:37:55 +12:00
/// abort (do nothing about it).
2020-05-05 05:43:47 +12:00
/// </summary>
2020-07-07 09:37:55 +12:00
/// <param name="failedKeys">comma separated list of the hotkeys that could not be registered, for display in dialog text.</param>
/// <returns>bool success.</returns>
2020-05-05 05:43:47 +12:00
private static bool HandleFailedHotkeyRegistration ( string failedKeys )
{
bool success = false ;
2020-07-04 08:02:16 +12:00
string warningTitle = Translator . GetText ( "Warning" ) ;
string message = Translator . GetText ( "Could not register the hot key." ) + failedKeys ;
2020-05-05 05:43:47 +12:00
DialogResult dr = MessageBox . Show ( message , warningTitle , MessageBoxButtons . AbortRetryIgnore , MessageBoxIcon . Exclamation ) ;
if ( dr = = DialogResult . Retry )
{
2020-07-07 09:37:55 +12:00
// LOG.DebugFormat("Re-trying to register hotkeys");
2020-05-05 05:43:47 +12:00
HotkeyControl . UnregisterHotkeys ( ) ;
success = RegisterHotkeys ( false ) ;
}
else if ( dr = = DialogResult . Ignore )
{
2020-07-07 09:37:55 +12:00
// LOG.DebugFormat("Ignoring failed hotkey registration");
2020-05-05 05:43:47 +12:00
HotkeyControl . UnregisterHotkeys ( ) ;
success = RegisterHotkeys ( true ) ;
}
2020-07-07 07:15:45 +12:00
2020-05-05 05:43:47 +12:00
return success ;
}
2020-07-08 03:05:19 +12:00
private void SettingsForm_Load ( object sender , EventArgs e )
{
2020-08-06 01:33:45 +12:00
tabControlExpert . Size = new Size (
2020-07-08 03:05:19 +12:00
tableLayoutPanelGeneral . Size . Width ,
tableLayoutPanelGeneral . Size . Height ) ;
}
private void ButtonOk_Click ( object sender , EventArgs e )
{
SetAutostart ( ) ;
SetHotkey ( ) ;
SetLanguage ( ) ;
2020-08-06 01:33:45 +12:00
SetExpertOptions ( ) ;
2020-07-08 03:05:19 +12:00
Properties . Settings . Default . Save ( ) ;
DialogResult = DialogResult . OK ;
Close ( ) ;
}
private void SetAutostart ( )
{
if ( checkBoxAutostart . Checked )
{
RegistryKey key = Registry . CurrentUser . OpenSubKey (
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" , true ) ;
key . SetValue (
Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ,
System . Diagnostics . Process . GetCurrentProcess ( ) . MainModule . FileName ) ;
Properties . Settings . Default . IsAutostartActivated = true ;
}
else
{
RegistryKey key = Registry . CurrentUser . OpenSubKey (
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" , true ) ;
key . DeleteValue ( "SystemTrayMenu" , false ) ;
Properties . Settings . Default . IsAutostartActivated = false ;
}
}
private void SetHotkey ( )
{
Properties . Settings . Default . HotKey =
new KeysConverter ( ) . ConvertToInvariantString (
textBoxHotkey . Hotkey | textBoxHotkey . HotkeyModifiers ) ;
}
private void SetLanguage ( )
{
Properties . Settings . Default . CurrentCultureInfoName =
comboBoxLanguage . SelectedValue . ToString ( ) ;
}
2020-07-07 09:37:55 +12:00
2020-08-06 01:33:45 +12:00
private void SetExpertOptions ( )
{
Properties . Settings . Default . HideTaskbarForm = checkBoxHideTaskbarForm . Checked ;
Properties . Settings . Default . OpenItemWithOneClick = checkBoxOpenItemWithOneClick . Checked ;
2020-08-08 01:24:25 +12:00
Properties . Settings . Default . IsDarkModeAlwaysOn = checkBoxDarkModeAlwaysOn . Checked ;
2020-08-06 01:33:45 +12:00
}
2020-07-08 03:05:19 +12:00
private void ButtonCancel_Click ( object sender , EventArgs e )
{
Properties . Settings . Default . Reload ( ) ;
DialogResult = DialogResult . Cancel ;
Close ( ) ;
}
private void ButtonChange_Click ( object sender , EventArgs e )
{
Config . SetFolderByUser ( false ) ;
textBoxFolder . Text = Config . Path ;
}
private void TextBoxHotkeyEnter ( object sender , EventArgs e )
{
HotkeyControl . UnregisterHotkeys ( ) ;
inHotkey = true ;
}
private void TextBoxHotkey_Leave ( object sender , EventArgs e )
{
Properties . Settings . Default . HotKey =
new KeysConverter ( ) . ConvertToInvariantString (
textBoxHotkey . Hotkey | textBoxHotkey . HotkeyModifiers ) ;
RegisterHotkeys ( ) ;
inHotkey = false ;
}
2020-05-05 05:43:47 +12:00
}
}