138: Simplify Hotkey management code

This commit is contained in:
Anton Kasyanov
2019-02-25 21:33:49 +02:00
parent f229681f54
commit ff36d15b75
2 changed files with 14 additions and 46 deletions

View File

@@ -29,28 +29,14 @@ namespace EveOPreview.UI.Hotkeys
public void Dispose()
{
if (this.IsRegistered)
{
this.Unregister();
}
this.Unregister();
GC.SuppressFinalize(this);
}
~HotkeyHandler()
{
// Unregister the hotkey if necessary
if (this.IsRegistered)
{
try
{
this.Unregister();
}
catch (Exception)
{
// Please no exceptions in the finalizer thread
}
}
this.Unregister();
}
public bool IsRegistered { get; private set; }
@@ -61,22 +47,12 @@ namespace EveOPreview.UI.Hotkeys
public bool CanRegister()
{
// Any exception means "no, you can't register"
try
{
// Attempt to register
if (this.Register())
{
// Unregister and say we managed it
this.Unregister();
return true;
}
}
catch (Win32Exception)
{
}
catch (NotSupportedException)
// Attempt to register
if (this.Register())
{
// Unregister and say we managed it
this.Unregister();
return true;
}
return false;
@@ -87,12 +63,12 @@ namespace EveOPreview.UI.Hotkeys
// Check that we have not registered
if (this.IsRegistered)
{
throw new NotSupportedException("This hotkey is already registered");
return false;
}
if (this.KeyCode == Keys.None)
{
throw new NotSupportedException("Cannot register an empty hotkey");
return false;
}
// Remove all modifiers from the 'main' hotkey
@@ -122,18 +98,18 @@ namespace EveOPreview.UI.Hotkeys
// Check that we have registered
if (!this.IsRegistered)
{
throw new NotSupportedException("This hotkey was not registered");
return;
}
this.IsRegistered = false;
Application.RemoveMessageFilter(this);
// Clean up after ourselves
if (!HotkeyHandlerNativeMethods.UnregisterHotKey(this._hotkeyTarget, this._hotkeyId))
{
throw new Win32Exception();
return;
}
this.IsRegistered = false;
}
#region IMessageFilter

View File

@@ -287,15 +287,7 @@ namespace EveOPreview.View
this._hotkeyHandler = new HotkeyHandler(this.Handle, hotkey);
this._hotkeyHandler.Pressed += HotkeyPressed_Handler;
try
{
this._hotkeyHandler.Register();
}
catch (Exception)
{
// There can be a lot of possible exception reasons here
// In case of any of them the hotkey setting is silently ignored
}
this._hotkeyHandler.Register();
}
public void UnregisterHotkey()