Skip to content

Commit

Permalink
fix: 优化初始化失败逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Dec 21, 2024
1 parent f541343 commit cfa152e
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/Magpie/AdaptersService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "pch.h"
#include "AdaptersService.h"

namespace Magpie {

bool Magpie::AdaptersService::Initialize() noexcept {
return false;
}

void Magpie::AdaptersService::Uninitialize() noexcept {
}

}
23 changes: 23 additions & 0 deletions src/Magpie/AdaptersService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

namespace Magpie {

class AdaptersService {
public:
static AdaptersService& Get() noexcept {
static AdaptersService instance;
return instance;
}

AdaptersService(const AdaptersService&) = delete;
AdaptersService(AdaptersService&&) = delete;

bool Initialize() noexcept;

void Uninitialize() noexcept;

private:
AdaptersService() = default;
};

}
13 changes: 10 additions & 3 deletions src/Magpie/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "IsNullStateTrigger.h"
#include "TextBlockHelper.h"
#include "MainWindow.h"
#include "AdaptersService.h"

using namespace ::Magpie;
using namespace winrt;
Expand Down Expand Up @@ -146,6 +147,7 @@ bool App::Initialize(const wchar_t* arguments) {

if (settings.IsAlwaysRunAsAdmin() && !Win32Helper::IsProcessElevated()) {
Restart(true, arguments);
_Uninitialize();
return false;
}

Expand All @@ -164,6 +166,10 @@ bool App::Initialize(const wchar_t* arguments) {

LocalizationService::Get().Initialize();
ToastService::Get().Initialize();
if (!AdaptersService::Get().Initialize()) {
_Uninitialize();
return false;
}
ShortcutService::Get().Initialize();
ScalingService::Get().Initialize();
UpdateService::Get().Initialize();
Expand All @@ -178,7 +184,7 @@ bool App::Initialize(const wchar_t* arguments) {
// 不显示托盘图标时忽略 -t 参数
if (!notifyIconService.IsShow() || arguments != L"-t"sv) {
if (!_mainWindow->Create()) {
Quit();
_Uninitialize();
return false;
}
}
Expand Down Expand Up @@ -249,10 +255,11 @@ const com_ptr<RootPage>& App::RootPage() const noexcept {
void App::_Uninitialize() {
NotifyIconService::Get().Uninitialize();
ScalingService::Get().Uninitialize();
// 不显示托盘图标的情况下关闭主窗口仍会在后台驻留数秒,推测和 XAML Islands 有关。
// 这里提前取消热键注册,这样关闭 Magpie 后立即重新打开不会注册热键失败。
// 提前取消热键注册,这样关闭 Magpie 后立即重新打开不会注册热键失败
ShortcutService::Get().Uninitialize();
AdaptersService::Get().Uninitialize();
ToastService::Get().Uninitialize();
EffectsService::Get().Uninitialize();

_isShowNotifyIconChangedRevoker.Revoke();
_themeChangedRevoker.Revoke();
Expand Down
5 changes: 5 additions & 0 deletions src/Magpie/EffectsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ fire_and_forget EffectsService::Initialize() {
_initialized.notify_one();
}

void EffectsService::Uninitialize() {
// 等待解析完成,防止退出时崩溃
_WaitForInitialize();
}

const std::vector<EffectInfo>& EffectsService::Effects() noexcept {
_WaitForInitialize();
return _effects;
Expand Down
2 changes: 2 additions & 0 deletions src/Magpie/EffectsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class EffectsService {

winrt::fire_and_forget Initialize();

void Uninitialize();

const std::vector<EffectInfo>& Effects() noexcept;

const EffectInfo* GetEffect(std::wstring_view name) noexcept;
Expand Down
2 changes: 2 additions & 0 deletions src/Magpie/Magpie.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<DependentUpon>AboutViewModel.idl</DependentUpon>
<SubType>Code</SubType>
</ClInclude>
<ClInclude Include="AdaptersService.h" />
<ClInclude Include="AppSettings.h" />
<ClInclude Include="AppXReader.h" />
<ClInclude Include="AutoStartHelper.h" />
Expand Down Expand Up @@ -260,6 +261,7 @@
<DependentUpon>AboutViewModel.idl</DependentUpon>
<SubType>Code</SubType>
</ClCompile>
<ClCompile Include="AdaptersService.cpp" />
<ClCompile Include="AppSettings.cpp" />
<ClCompile Include="AppXReader.cpp" />
<ClCompile Include="AutoStartHelper.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions src/Magpie/Magpie.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
<ClCompile Include="XamlHelper.cpp">
<Filter>Helpers</Filter>
</ClCompile>
<ClCompile Include="AdaptersService.cpp">
<Filter>Services</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
Expand Down Expand Up @@ -154,6 +157,9 @@
<ClInclude Include="resource.h">
<Filter>Resources</Filter>
</ClInclude>
<ClInclude Include="AdaptersService.h">
<Filter>Services</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Pages">
Expand Down
10 changes: 9 additions & 1 deletion src/Magpie/NotifyIconService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Magpie {

// 当任务栏被创建时会广播此消息。用于在资源管理器被重新启动时重新创建托盘图标
// https://learn.microsoft.com/en-us/windows/win32/shell/taskbar#taskbar-creation-notification
static UINT WM_TASKBARCREATED;
static UINT WM_TASKBARCREATED = 0;

void NotifyIconService::Initialize() noexcept {
WM_TASKBARCREATED = RegisterWindowMessage(L"TaskbarCreated");
Expand All @@ -23,7 +23,15 @@ void NotifyIconService::Initialize() noexcept {
_nid.uID = 0;
}

bool NotifyIconService::_IsInitialized() const noexcept {
return WM_TASKBARCREATED;
}

void NotifyIconService::Uninitialize() noexcept {
if (!_IsInitialized()) {
return;
}

IsShow(false);

if (_nid.hWnd) {
Expand Down
4 changes: 3 additions & 1 deletion src/Magpie/NotifyIconService.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class NotifyIconService {
void Uninitialize() noexcept;

void IsShow(bool value) noexcept;
bool IsShow() noexcept {
bool IsShow() const noexcept {
// 返回 _shouldShow 而不是 _isShow,对外接口假设总是创建成功
return _shouldShow;
}

private:
bool _IsInitialized() const noexcept;

static LRESULT _NotifyIconWndProcStatic(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
return Get()._NotifyIconWndProc(hWnd, msg, wParam, lParam);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Magpie/ScalingService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ void ScalingService::Initialize() {
_CheckForegroundTimer_Tick(nullptr);
}

bool ScalingService::_IsInitialized() const noexcept {
return (bool)_checkForegroundTimer;
}

void ScalingService::Uninitialize() {
if (!_IsInitialized()) {
return;
}

_checkForegroundTimer.Cancel();
_countDownTimer.Stop();
_scalingRuntime.reset();
Expand Down
2 changes: 2 additions & 0 deletions src/Magpie/ScalingService.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class ScalingService {
private:
ScalingService() = default;

bool _IsInitialized() const noexcept;

void _WndToRestore(HWND value);

void _ShortcutService_ShortcutPressed(winrt::Magpie::ShortcutAction action);
Expand Down
8 changes: 8 additions & 0 deletions src/Magpie/ShortcutService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ void ShortcutService::Initialize() {
}
}

bool ShortcutService::_IsInitialized() const noexcept {
return (bool)_hwndHotkey;
}

void ShortcutService::Uninitialize() {
if (!_IsInitialized()) {
return;
}

_keyboardHook.reset();

for (int i = 0; i < (int)ShortcutAction::COUNT_OR_NONE; ++i) {
Expand Down
2 changes: 2 additions & 0 deletions src/Magpie/ShortcutService.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class ShortcutService {
private:
ShortcutService() = default;

bool _IsInitialized() const noexcept;

static LRESULT _WndProcStatic(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
return Get()._WndProc(hWnd, msg, wParam, lParam);
}
Expand Down

0 comments on commit cfa152e

Please sign in to comment.