138: Simplify Hotkey management code
This commit is contained in:
@@ -29,28 +29,14 @@ 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)
|
this.Unregister();
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
this.Unregister();
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
// Please no exceptions in the finalizer thread
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsRegistered { get; private set; }
|
public bool IsRegistered { get; private set; }
|
||||||
@@ -61,22 +47,12 @@ namespace EveOPreview.UI.Hotkeys
|
|||||||
|
|
||||||
public bool CanRegister()
|
public bool CanRegister()
|
||||||
{
|
{
|
||||||
// Any exception means "no, you can't register"
|
// Attempt to register
|
||||||
try
|
if (this.Register())
|
||||||
{
|
|
||||||
// Attempt to register
|
|
||||||
if (this.Register())
|
|
||||||
{
|
|
||||||
// Unregister and say we managed it
|
|
||||||
this.Unregister();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Win32Exception)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
catch (NotSupportedException)
|
|
||||||
{
|
{
|
||||||
|
// Unregister and say we managed it
|
||||||
|
this.Unregister();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@@ -287,15 +287,7 @@ 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()
|
||||||
|
Reference in New Issue
Block a user