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

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

View File

@@ -287,16 +287,8 @@ namespace EveOPreview.View
this._hotkeyHandler = new HotkeyHandler(this.Handle, hotkey); this._hotkeyHandler = new HotkeyHandler(this.Handle, hotkey);
this._hotkeyHandler.Pressed += HotkeyPressed_Handler; this._hotkeyHandler.Pressed += HotkeyPressed_Handler;
try
{
this._hotkeyHandler.Register(); 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
}
}
public void UnregisterHotkey() public void UnregisterHotkey()
{ {