From ff36d15b7571b79a93ecc4d7fa5c92a1244e50c7 Mon Sep 17 00:00:00 2001 From: Anton Kasyanov Date: Mon, 25 Feb 2019 21:33:49 +0200 Subject: [PATCH] 138: Simplify Hotkey management code --- Eve-O-Preview/Hotkeys/HotkeyHandler.cs | 50 +++++-------------- .../View/Implementation/ThumbnailView.cs | 10 +--- 2 files changed, 14 insertions(+), 46 deletions(-) diff --git a/Eve-O-Preview/Hotkeys/HotkeyHandler.cs b/Eve-O-Preview/Hotkeys/HotkeyHandler.cs index 86c705a..4b95cf2 100644 --- a/Eve-O-Preview/Hotkeys/HotkeyHandler.cs +++ b/Eve-O-Preview/Hotkeys/HotkeyHandler.cs @@ -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 diff --git a/Eve-O-Preview/View/Implementation/ThumbnailView.cs b/Eve-O-Preview/View/Implementation/ThumbnailView.cs index 7199707..7c386ca 100644 --- a/Eve-O-Preview/View/Implementation/ThumbnailView.cs +++ b/Eve-O-Preview/View/Implementation/ThumbnailView.cs @@ -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()