Skip to content

Commit

Permalink
refactor: 避免使用 <filesystem>
Browse files Browse the repository at this point in the history
这使二进制文件减小了 6KB
  • Loading branch information
Blinue committed Apr 13, 2024
1 parent 00e2312 commit d3a8025
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
26 changes: 12 additions & 14 deletions src/Magpie.App/ShortcutService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ void ShortcutService::Initialize() {
};
RegisterClassEx(&wcex);

_hwndHotkey = CreateWindow(CommonSharedConstants::HOTKEY_WINDOW_CLASS_NAME, nullptr, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInst, 0);
_hwndHotkey.reset(CreateWindow(CommonSharedConstants::HOTKEY_WINDOW_CLASS_NAME,
nullptr, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInst, 0));

_RegisterShortcut(ShortcutAction::Scale);
_RegisterShortcut(ShortcutAction::Overlay);

AppSettings::Get().ShortcutChanged({ this, &ShortcutService::_AppSettings_OnShortcutChanged });

_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, _LowLevelKeyboardProc, NULL, NULL);
_keyboardHook.reset(SetWindowsHookEx(WH_KEYBOARD_LL, _LowLevelKeyboardProc, NULL, NULL));
if (!_keyboardHook) {
Logger::Get().Win32Error("SetWindowsHookEx 失败");
}
Expand All @@ -50,18 +51,15 @@ void ShortcutService::Uninitialize() {
return;
}

if (_keyboardHook) {
UnhookWindowsHookEx(_keyboardHook);
}
_keyboardHook.reset();

for (int i = 0; i < (int)ShortcutAction::COUNT_OR_NONE; ++i) {
if (!_ShortcutInfos[i].isError) {
UnregisterHotKey(_hwndHotkey, i);
if (!_shortcutInfos[i].isError) {
UnregisterHotKey(_hwndHotkey.get(), i);
}
}

DestroyWindow(_hwndHotkey);
_hwndHotkey = NULL;

_hwndHotkey.reset();
}

ShortcutService::~ShortcutService() {
Expand All @@ -87,9 +85,9 @@ void ShortcutService::_AppSettings_OnShortcutChanged(ShortcutAction action) {

void ShortcutService::_RegisterShortcut(ShortcutAction action) {
const Shortcut& shortcut = AppSettings::Get().GetShortcut(action);
bool& isError = _ShortcutInfos[(size_t)action].isError;
bool& isError = _shortcutInfos[(size_t)action].isError;

UnregisterHotKey(_hwndHotkey, (int)action);
UnregisterHotKey(_hwndHotkey.get(), (int)action);

if (shortcut.IsEmpty() || ShortcutHelper::CheckShortcut(shortcut) != ShortcutError::NoError) {
Logger::Get().Win32Error(fmt::format("注册热键 {} 失败", ShortcutHelper::ToString(action)));
Expand All @@ -112,7 +110,7 @@ void ShortcutService::_RegisterShortcut(ShortcutAction action) {
modifiers |= MOD_SHIFT;
}

isError = !RegisterHotKey(_hwndHotkey, (int)action, modifiers, shortcut.code);
isError = !RegisterHotKey(_hwndHotkey.get(), (int)action, modifiers, shortcut.code);
if (isError) {
Logger::Get().Win32Error(fmt::format("注册热键 {} 失败", ShortcutHelper::ToString(action)));
}
Expand All @@ -123,7 +121,7 @@ void ShortcutService::_FireShortcut(ShortcutAction action) {

// 限制触发频率
auto cur = steady_clock::now();
auto& lastFireTime = _ShortcutInfos[(size_t)action].lastFireTime;
auto& lastFireTime = _shortcutInfos[(size_t)action].lastFireTime;
if (duration_cast<milliseconds>(cur - lastFireTime).count() < 100) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Magpie.App/ShortcutService.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ShortcutService {
void Uninitialize();

bool IsError(ShortcutAction action) const noexcept {
return _ShortcutInfos[(size_t)action].isError;
return _shortcutInfos[(size_t)action].isError;
}

void StopKeyboardHook() noexcept {
Expand Down Expand Up @@ -53,9 +53,9 @@ class ShortcutService {
std::chrono::steady_clock::time_point lastFireTime{};
bool isError = true;
};
std::array<_ShortcutInfo, (size_t)ShortcutAction::COUNT_OR_NONE> _ShortcutInfos{};
HWND _hwndHotkey = NULL;
HHOOK _keyboardHook = NULL;
std::array<_ShortcutInfo, (size_t)ShortcutAction::COUNT_OR_NONE> _shortcutInfos{};
wil::unique_hwnd _hwndHotkey;
wil::unique_hhook _keyboardHook;

bool _isKeyboardHookActive = true;
// 用于防止长按时重复触发热键
Expand Down
4 changes: 2 additions & 2 deletions src/Magpie.App/UpdateService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "Win32Utils.h"
#include "CommonSharedConstants.h"
#include <zip/zip.h>
#include <filesystem>
#include <winrt/Windows.Security.Cryptography.Core.h>

using namespace winrt;
Expand Down Expand Up @@ -237,7 +236,8 @@ fire_and_forget UpdateService::DownloadAndInstall() {
DownloadProgressChanged.Invoke(_downloadProgress);

// 删除 update 文件夹
std::filesystem::remove_all(CommonSharedConstants::UPDATE_DIR);
wil::RemoveDirectoryRecursiveNoThrow(
CommonSharedConstants::UPDATE_DIR, wil::RemoveDirectoryOptions::KeepRootDirectory);

// 下载新版
try {
Expand Down

0 comments on commit d3a8025

Please sign in to comment.