feat: 使用 HotkeyManger::HotkeyPressed 事件监听热键

This commit is contained in:
刘旭 2022-06-20 09:21:50 +08:00
commit ef44ed34b0
10 changed files with 54 additions and 19 deletions

View file

@ -50,11 +50,12 @@ void App::OnClose() {
_settings.Save();
}
bool App::Initialize(Magpie::App::Settings const& settings, uint64_t hwndHost) {
bool App::Initialize(Magpie::App::Settings const& settings, Magpie::App::HotkeyManager const& hotkeyManager, uint64_t hwndHost) {
_hwndHost = hwndHost;
_settings = settings;
// HotkeyManager 中的回调总是最先调用
_hotkeyManager = Magpie::App::HotkeyManager();
_hotkeyManager = hotkeyManager;
_hotkeyPressedRevoker = hotkeyManager.HotkeyPressed(auto_revoke, { this, &App::_HotkeyManger_HotkeyPressed });
return true;
}
@ -75,4 +76,8 @@ void App::OnHostWndFocusChanged(bool isFocused) {
_hostWndFocusChangedEvent(*this, isFocused);
}
void App::_HotkeyManger_HotkeyPressed(IInspectable const&, HotkeyAction action) {
OutputDebugString(L"hotkey\n");
}
}

View file

@ -15,7 +15,7 @@ public:
void OnClose();
bool Initialize(Magpie::App::Settings const& settings, uint64_t hwndHost);
bool Initialize(Magpie::App::Settings const& settings, Magpie::App::HotkeyManager const& hotkeyManager, uint64_t hwndHost);
uint64_t HwndHost() const {
return _hwndHost;
@ -38,12 +38,16 @@ public:
}
private:
void _HotkeyManger_HotkeyPressed(IInspectable const&, HotkeyAction action);
Magpie::App::Settings _settings{ nullptr };
Magpie::App::HotkeyManager _hotkeyManager{ nullptr };
uint64_t _hwndHost{};
event<EventHandler<bool>> _hostWndFocusChangedEvent;
bool _isHostWndFocused = false;
Magpie::App::HotkeyManager::HotkeyPressed_revoker _hotkeyPressedRevoker;
};
}

View file

@ -21,7 +21,7 @@ namespace Magpie.App {
void OnClose();
Boolean Initialize(Settings settings, UInt64 hwndHost);
Boolean Initialize(Settings settings, HotkeyManager hotkeyManager, UInt64 hwndHost);
Settings Settings { get; };
HotkeyManager HotkeyManager { get; };

View file

@ -8,12 +8,11 @@
namespace winrt::Magpie::App::implementation {
HotkeyManager::HotkeyManager() {
App app = Application::Current().as<App>();
_settings = app.Settings();
_hwndHost = (HWND)app.HwndHost();
HotkeyManager::HotkeyManager(Magpie::App::Settings settings, uint64_t hwndHost) {
_settings = settings;
_hwndHost = (HWND)hwndHost;
_hotkeyChangedToken = _settings.HotkeyChanged(
_hotkeyChangedRevoker = settings.HotkeyChanged(
auto_revoke, { this,&HotkeyManager::_Settings_OnHotkeyChanged });
_RegisterHotkey(HotkeyAction::Scale);
@ -32,6 +31,18 @@ bool HotkeyManager::IsError(HotkeyAction action) {
return _errors[(size_t)action];
}
event_token HotkeyManager::HotkeyPressed(EventHandler<HotkeyAction> const& handler) {
return _hotkeyPressedEvent.add(handler);
}
void HotkeyManager::HotkeyPressed(event_token const& token) {
_hotkeyPressedEvent.remove(token);
}
void HotkeyManager::OnHotkeyPressed(HotkeyAction action) {
_hotkeyPressedEvent(*this, action);
}
void HotkeyManager::_Settings_OnHotkeyChanged(IInspectable const&, HotkeyAction action) {
_RegisterHotkey(action);
}

View file

@ -6,20 +6,27 @@
namespace winrt::Magpie::App::implementation {
struct HotkeyManager : HotkeyManagerT<HotkeyManager> {
HotkeyManager();
HotkeyManager(Magpie::App::Settings settings, uint64_t hwndHost);
~HotkeyManager();
bool IsError(HotkeyAction action);
event_token HotkeyPressed(EventHandler<HotkeyAction> const& handler);
void HotkeyPressed(event_token const& token);
void OnHotkeyPressed(HotkeyAction action);
private:
void _Settings_OnHotkeyChanged(IInspectable const&, HotkeyAction action);
void _RegisterHotkey(HotkeyAction action);
Magpie::App::Settings::HotkeyChanged_revoker _hotkeyChangedToken;
Magpie::App::Settings::HotkeyChanged_revoker _hotkeyChangedRevoker;
std::array<bool, (size_t)HotkeyAction::COUNT_OR_NONE> _errors;
event<EventHandler<HotkeyAction>> _hotkeyPressedEvent;
Magpie::App::Settings _settings{ nullptr };
HWND _hwndHost = NULL;
};

View file

@ -3,8 +3,11 @@ namespace Magpie.App
[default_interface]
runtimeclass HotkeyManager
{
HotkeyManager();
HotkeyManager(Settings settings, UInt64 hwndHost);
Boolean IsError(HotkeyAction action);
event Windows.Foundation.EventHandler<HotkeyAction> HotkeyPressed;
void OnHotkeyPressed(HotkeyAction action);
}
}

View file

@ -34,8 +34,8 @@ ShortcutControl::ShortcutControl() {
_settings = app.Settings();
_hotkeyManager = app.HotkeyManager();
_hotkeyChangedToken = _settings.HotkeyChanged(
winrt::auto_revoke, { this,&ShortcutControl::_Settings_OnHotkeyChanged });
_hotkeyChangedRevoker = _settings.HotkeyChanged(
auto_revoke, { this,&ShortcutControl::_Settings_OnHotkeyChanged });
_shortcutDialog.Title(box_value(L"激活快捷键"));
_shortcutDialog.Content(_shortcutDialogContent);

View file

@ -43,7 +43,7 @@ private:
event<Data::PropertyChangedEventHandler> _propertyChangedEvent;
Magpie::App::Settings::HotkeyChanged_revoker _hotkeyChangedToken;
Magpie::App::Settings::HotkeyChanged_revoker _hotkeyChangedRevoker;
Magpie::App::HotkeySettings _hotkey;
Controls::ContentDialog _shortcutDialog;

View file

@ -119,6 +119,9 @@ bool XamlApp::Initialize(HINSTANCE hInstance) {
ShowWindow(_hwndXamlHost, _settings.IsWindowMaximized() ? SW_MAXIMIZE : SW_SHOW);
}
// HotkeyManager 中的回调总是最先调用
_hotkeyManager = winrt::Magpie::App::HotkeyManager(_settings, (uint64_t)_hwndXamlHost);
// 初始化 UWP 应用和 XAML Islands
_uwpApp = winrt::Magpie::App::App();
if (!isWin11) {
@ -131,7 +134,7 @@ bool XamlApp::Initialize(HINSTANCE hInstance) {
}
}
_uwpApp.Initialize(_settings, (uint64_t)_hwndXamlHost);
_uwpApp.Initialize(_settings, _hotkeyManager, (uint64_t)_hwndXamlHost);
// 未显示窗口时视为位于前台,否则显示窗口的动画有小瑕疵
_uwpApp.OnHostWndFocusChanged(true);
@ -324,7 +327,9 @@ LRESULT XamlApp::_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
switch (message) {
case WM_HOTKEY:
{
if (message >= 0) {
using winrt::Magpie::App::HotkeyAction;
if (wParam >= 0 && wParam < (UINT)HotkeyAction::COUNT_OR_NONE) {
_hotkeyManager.OnHotkeyPressed((HotkeyAction)wParam);
return 0;
}

View file

@ -39,7 +39,7 @@ private:
Win32Utils::ScopedHandle _hMutex;
winrt::Magpie::App::Settings _settings{ nullptr };
winrt::Magpie::App::HotkeyManager _hotkeyManager{ nullptr };
winrt::Magpie::App::App _uwpApp{ nullptr };
winrt::Magpie::App::MainPage _mainPage{ nullptr };
HWND _hwndXamlHost = NULL;