Skip to content

Commit

Permalink
feat: 支持缩放中途出现的捕获错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Nov 5, 2024
1 parent dcab625 commit e635693
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 18 deletions.
16 changes: 7 additions & 9 deletions src/Magpie.App/ScalingService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ void ScalingService::Initialize() {
AppSettings::Get().IsAutoRestoreChanged({ this, &ScalingService::_Settings_IsAutoRestoreChanged });
_scalingRuntime = std::make_unique<ScalingRuntime>();
_scalingRuntime->IsRunningChanged({ this, &ScalingService::_ScalingRuntime_IsRunningChanged });
_scalingRuntime->ScalingFailed({ this, &ScalingService::_ScalingRuntime_ScalingFailed });

ShortcutService::Get().ShortcutActivated(
{ this, &ScalingService::_ShortcutService_ShortcutPressed }
Expand Down Expand Up @@ -234,15 +233,20 @@ void ScalingService::_Settings_IsAutoRestoreChanged(bool value) {
}
}

void ScalingService::_ScalingRuntime_IsRunningChanged(bool isRunning) {
_dispatcher.RunAsync(CoreDispatcherPriority::Normal, [this, isRunning]() {
void ScalingService::_ScalingRuntime_IsRunningChanged(bool isRunning, ScalingError error) {
_dispatcher.RunAsync(CoreDispatcherPriority::Normal, [this, isRunning, error]() {
if (isRunning) {
StopTimer();

if (AppSettings::Get().IsAutoRestore()) {
_WndToRestore(NULL);
}
} else {
if (error != ScalingError::NoError && IsWindowVisible(_hwndCurSrc)) {
// 缩放初始化时或缩放中途出错
ShowError(_hwndCurSrc, error);
}

if (GetForegroundWindow() == _hwndCurSrc) {
// 退出全屏后如果前台窗口不变视为通过热键退出
_hwndChecked = _hwndCurSrc;
Expand All @@ -263,12 +267,6 @@ void ScalingService::_ScalingRuntime_IsRunningChanged(bool isRunning) {
});
}

void ScalingService::_ScalingRuntime_ScalingFailed(HWND hWnd, ScalingError error) {
_dispatcher.RunAsync(CoreDispatcherPriority::Normal, [hWnd, error]() {
ShowError(hWnd, error);
});
}

void ScalingService::_StartScale(HWND hWnd, const Profile& profile) {
if (profile.scalingMode < 0) {
ShowError(hWnd, ScalingError::InvalidScalingMode);
Expand Down
3 changes: 1 addition & 2 deletions src/Magpie.App/ScalingService.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ class ScalingService {

void _Settings_IsAutoRestoreChanged(bool value);

void _ScalingRuntime_IsRunningChanged(bool isRunning);
void _ScalingRuntime_ScalingFailed(HWND hWnd, ScalingError error);
void _ScalingRuntime_IsRunningChanged(bool isRunning, ScalingError error);

void _StartScale(HWND hWnd, const Profile& profile);

Expand Down
1 change: 1 addition & 0 deletions src/Magpie.Core/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ void Renderer::_BackendThreadProc() noexcept {
{
// 捕获出错,退出缩放
ScalingWindow::Get().Dispatcher().TryEnqueue([]() {
ScalingWindow::Get().RuntimeError(ScalingError::CaptureFailed);
ScalingWindow::Get().Destroy();
});

Expand Down
9 changes: 4 additions & 5 deletions src/Magpie.Core/ScalingRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void ScalingRuntime::Start(HWND hwndSrc, ScalingOptions&& options) {
return;
}

IsRunningChanged.Invoke(true);
IsRunningChanged.Invoke(true, ScalingError::NoError);

_Dispatcher().TryEnqueue([this, dispatcher(_Dispatcher()), hwndSrc, options(std::move(options))]() mutable {
ScalingError error = ScalingWindow::Get().Create(dispatcher, hwndSrc, std::move(options));
Expand All @@ -63,8 +63,7 @@ void ScalingRuntime::Start(HWND hwndSrc, ScalingOptions&& options) {
} else {
// 缩放失败
_state.store(_State::Idle, std::memory_order_relaxed);
IsRunningChanged.Invoke(false);
ScalingFailed.Invoke(hwndSrc, error);
IsRunningChanged.Invoke(false, error);
}
});
}
Expand Down Expand Up @@ -160,7 +159,7 @@ void ScalingRuntime::_ScalingThreadProc() noexcept {
scalingWindow.Destroy();

if (_state.exchange(_State::Idle, std::memory_order_relaxed) != _State::Idle) {
IsRunningChanged.Invoke(false);
IsRunningChanged.Invoke(false, ScalingError::NoError);
}

return;
Expand Down Expand Up @@ -195,7 +194,7 @@ void ScalingRuntime::_ScalingThreadProc() noexcept {
} else {
// 缩放结束
_state.store(_State::Idle, std::memory_order_relaxed);
IsRunningChanged.Invoke(false);
IsRunningChanged.Invoke(false, scalingWindow.RuntimeError());
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Magpie.Core/ScalingRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class ScalingRuntime {
}

// 调用者应处理线程同步
WinRTUtils::Event<winrt::delegate<bool>> IsRunningChanged;
WinRTUtils::Event<winrt::delegate<HWND, ScalingError>> ScalingFailed;
WinRTUtils::Event<winrt::delegate<bool, ScalingError>> IsRunningChanged;

private:
void _ScalingThreadProc() noexcept;
Expand Down
1 change: 1 addition & 0 deletions src/Magpie.Core/ScalingWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ ScalingError ScalingWindow::Create(
_dispatcher = dispatcher;

_isSrcRepositioning = false;
_runtimeError = ScalingError::NoError;

if (FindWindow(CommonSharedConstants::SCALING_WINDOW_CLASS_NAME, nullptr)) {
Logger::Get().Error("已存在缩放窗口");
Expand Down
11 changes: 11 additions & 0 deletions src/Magpie.Core/ScalingWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ class ScalingWindow : public WindowBase<ScalingWindow> {

void CleanAfterSrcRepositioned() noexcept;

// 缩放过程中出现的错误
ScalingError RuntimeError() const noexcept {
return _runtimeError;
}

void RuntimeError(ScalingError value) noexcept {
_runtimeError = value;
}

protected:
LRESULT _MessageHandler(UINT msg, WPARAM wParam, LPARAM lParam) noexcept;

Expand Down Expand Up @@ -94,6 +103,8 @@ class ScalingWindow : public WindowBase<ScalingWindow> {

std::array<wil::unique_hwnd, 4> _hwndTouchHoles{};

ScalingError _runtimeError = ScalingError::NoError;

bool _isSrcRepositioning = false;
bool _isDDFWindowShown = false;
};
Expand Down

0 comments on commit e635693

Please sign in to comment.