Skip to content

Commit

Permalink
全局消息弹窗 (#1022)
Browse files Browse the repository at this point in the history
* feat: 消息窗口创建在单独的线程中
一个线程内不能创建多个 XAML Islands 窗口

* refactor: 删除 XamlHostingHelper

* feat: 实现显示消息

* feat: 使消息窗口显示在源窗口上方

* feat: 初步实现弹窗随源窗口移动

* feat: 使弹窗的移动更平滑

* fix: 优化 toast 尺寸

* fix: 修复闪烁

* refactor: 操纵 toast 的代码集中在 ToastPage 中

* fix: 修复无限循环

* feat: toast 上展示 logo

* feat: 优化 toast 位置

* refactor: 添加注释

* perf: 优化关于页面加载 logo 的性能

* feat: 应用内消息改为使用 ToastService 实现

* UI: 删除消息的句号

* fix: 如果更改所有者失败则定期将弹窗置顶

* Revert "UI: 删除消息的句号"

This reverts commit ea207e5.

* feat: toast 配置主题变化

* feat: 更多错误消息

* feat: 更多错误消息

* feat: 优化消息弹窗

* chore: 优化措辞

* feat: 如果窗口没有重定向表面,GDI 和 DwmSharedSurface 捕获直接失败

* feat: 捕获失败则退出缩放

* perf: 优化渲染后端线程的响应性

* feat: 支持缩放中途出现的捕获错误

* chore: 修改措辞
  • Loading branch information
Blinue authored Dec 4, 2024
1 parent c4cff28 commit 2c59c6c
Show file tree
Hide file tree
Showing 43 changed files with 1,017 additions and 311 deletions.
3 changes: 2 additions & 1 deletion src/Magpie.App/AboutPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif
#include "Win32Utils.h"
#include "CommonSharedConstants.h"
#include "ToastService.h"

namespace winrt::Magpie::App::implementation {

Expand All @@ -15,7 +16,7 @@ void AboutPage::VersionTextBlock_DoubleTapped(IInspectable const&, Input::Double

const hstring message = ResourceLoader::GetForCurrentView(CommonSharedConstants::APP_RESOURCE_MAP_ID)
.GetString(L"About_DeveloperModeEnabled");
Application::Current().as<App>().RootPage().ShowToast(message);
ToastService::Get().ShowMessageInApp(L"", message);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Magpie.App/AboutViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ AboutViewModel::AboutViewModel() {
([](AboutViewModel* that)->fire_and_forget {
auto weakThis = that->get_weak();
SoftwareBitmapSource bitmap;
co_await bitmap.SetBitmapAsync(
IconHelper::ExtractIconFromExe(Win32Utils::GetExePath().c_str(), 256));
co_await bitmap.SetBitmapAsync(IconHelper::ExtractAppIcon(256));

if (!weakThis.get()) {
co_return;
Expand Down
11 changes: 10 additions & 1 deletion src/Magpie.App/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "EffectsService.h"
#include "UpdateService.h"
#include "LocalizationService.h"
#include "Logger.h"
#include "ToastService.h"

namespace winrt::Magpie::App::implementation {

Expand Down Expand Up @@ -76,6 +76,13 @@ void App::Close() {
_windowsXamlManager = nullptr;

Exit();

// 做最后的清理,见
// https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.hosting.windowsxamlmanager.close
MSG msg;
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
DispatchMessage(&msg);
}
}

void App::SaveSettings() {
Expand All @@ -98,6 +105,7 @@ StartUpOptions App::Initialize(int) {
result.IsNeedElevated = settings.IsAlwaysRunAsAdmin();

LocalizationService::Get().Initialize();
ToastService::Get().Initialize();
ShortcutService::Get().Initialize();
ScalingService::Get().Initialize();
UpdateService::Get().Initialize();
Expand All @@ -110,6 +118,7 @@ void App::Uninitialize() {
// 不显示托盘图标的情况下关闭主窗口仍会在后台驻留数秒,推测和 XAML Islands 有关
// 这里提前取消热键注册,这样关闭 Magpie 后立即重新打开不会注册热键失败
ShortcutService::Get().Uninitialize();
ToastService::Get().Uninitialize();
}

bool App::IsShowNotifyIcon() const noexcept {
Expand Down
5 changes: 5 additions & 0 deletions src/Magpie.App/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
Color="#FFFFFF" />
<SolidColorBrush x:Key="DeleteButtonForegroundPressedBrush"
Color="#B3FFFFFF" />

<!-- TeachingTip -->
<!-- TeachingTip 查找资源的方式很奇怪,似乎只能全局覆盖 -->
<x:Double x:Key="TeachingTipMinWidth">20</x:Double>
<x:Double x:Key="TeachingTipMaxWidth">400</x:Double>
</ResourceDictionary>
</Application.Resources>
</Application>
2 changes: 1 addition & 1 deletion src/Magpie.App/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void HomePage::SimulateExclusiveFullscreenToggleSwitch_Toggled(IInspectable cons
}

// 这个回调被触发时 UI 还没有更新,需要异步处理
Dispatcher().TryRunAsync(CoreDispatcherPriority::Low, [weakThis(get_weak())]() {
Dispatcher().RunAsync(CoreDispatcherPriority::Low, [weakThis(get_weak())]() {
auto strongThis = weakThis.get();
if (!strongThis) {
return;
Expand Down
9 changes: 7 additions & 2 deletions src/Magpie.App/IconHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,14 @@ SoftwareBitmap IconHelper::ExtractIconFromExe(const wchar_t* fileName, uint32_t
return bitmap;
}

SoftwareBitmap IconHelper::ExtractAppSmallIcon() {
// 小图标在多处使用,应该缓存
static SoftwareBitmap result = ExtractAppIcon(40);
return result;
}

SoftwareBitmap IconHelper::ExtractAppIcon(uint32_t preferredSize) {
// 作为性能优化,使用 LoadImage 而不是 SHDefExtractIcon 加载程序图标。
// 经测试,LoadImage 快两倍左右。
/// LoadImage 比 SHDefExtractIcon 快两倍左右
wil::unique_hicon hIcon((HICON)LoadImage(
GetModuleHandle(nullptr),
MAKEINTRESOURCE(CommonSharedConstants::IDI_APP),
Expand Down
1 change: 1 addition & 0 deletions src/Magpie.App/IconHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace winrt::Magpie::App {
struct IconHelper {
static Windows::Graphics::Imaging::SoftwareBitmap ExtractIconFormWnd(HWND hWnd, uint32_t preferredSize);
static Windows::Graphics::Imaging::SoftwareBitmap ExtractIconFromExe(const wchar_t* fileName, uint32_t preferredSize);
static Windows::Graphics::Imaging::SoftwareBitmap ExtractAppSmallIcon();
static Windows::Graphics::Imaging::SoftwareBitmap ExtractAppIcon(uint32_t preferredSize);
};

Expand Down
17 changes: 17 additions & 0 deletions src/Magpie.App/Magpie.App.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@
<DependentUpon>TitleBarControl.xaml</DependentUpon>
<SubType>Code</SubType>
</ClInclude>
<ClInclude Include="ToastPage.h">
<DependentUpon>ToastPage.xaml</DependentUpon>
<SubType>Code</SubType>
</ClInclude>
<ClInclude Include="ToastService.h" />
<ClInclude Include="TouchHelper.h" />
<ClInclude Include="UpdateService.h" />
<ClInclude Include="WrapPanel.h">
Expand Down Expand Up @@ -403,6 +408,11 @@
<DependentUpon>TitleBarControl.xaml</DependentUpon>
<SubType>Code</SubType>
</ClCompile>
<ClCompile Include="ToastPage.cpp">
<DependentUpon>ToastPage.xaml</DependentUpon>
<SubType>Code</SubType>
</ClCompile>
<ClCompile Include="ToastService.cpp" />
<ClCompile Include="TouchHelper.cpp" />
<ClCompile Include="UpdateService.cpp" />
<ClCompile Include="WrapPanel.cpp">
Expand All @@ -411,6 +421,10 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<Midl Include="ToastPage.idl">
<DependentUpon>ToastPage.xaml</DependentUpon>
<SubType>Code</SubType>
</Midl>
<None Include="SettingsExpanderCornerRadiusConverter.idl">
<SubType>Designer</SubType>
</None>
Expand Down Expand Up @@ -591,6 +605,9 @@
<Page Include="TitleBarControl.xaml">
<SubType>Designer</SubType>
</Page>
<Page Include="ToastPage.xaml">
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Magpie.App.rc" />
Expand Down
7 changes: 7 additions & 0 deletions src/Magpie.App/Magpie.App.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
<ClCompile Include="TouchHelper.cpp">
<Filter>Helpers</Filter>
</ClCompile>
<ClCompile Include="ToastService.cpp">
<Filter>Services</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
Expand Down Expand Up @@ -123,6 +126,9 @@
<ClInclude Include="TouchHelper.h">
<Filter>Helpers</Filter>
</ClInclude>
<ClInclude Include="ToastService.h">
<Filter>Services</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Pages">
Expand Down Expand Up @@ -282,6 +288,7 @@
<Page Include="BlueInfoBar.xaml">
<Filter>Styles</Filter>
</Page>
<Page Include="ToastPage.xaml" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Magpie.App.rc" />
Expand Down
27 changes: 27 additions & 0 deletions src/Magpie.App/Resources.language-en-US.resw
Original file line number Diff line number Diff line change
Expand Up @@ -838,4 +838,31 @@
<data name="Home_Advanced_SimulateExclusiveFullscreen_InfoBar.Title" xml:space="preserve">
<value>This option is not compatible with some older games. Please use it with caution.</value>
</data>
<data name="Message_InvalidScalingMode" xml:space="preserve">
<value>Scaling mode is invalid.</value>
</data>
<data name="Message_TouchSupport" xml:space="preserve">
<value>Failed to enable touch support.</value>
</data>
<data name="Message_InvalidSourceWindow" xml:space="preserve">
<value>Scaling of this window is not supported.</value>
</data>
<data name="Message_Maximized" xml:space="preserve">
<value>Scaling of maximized or fullscreen windows is disabled. You can change this setting on the Home page.</value>
</data>
<data name="Message_LowIntegrityLevel" xml:space="preserve">
<value>Magpie needs to be run as an administrator to scale this window.</value>
</data>
<data name="Message_ScalingFailedGeneral" xml:space="preserve">
<value>Please refer to the logs for more details.</value>
</data>
<data name="Message_CaptureFailed" xml:space="preserve">
<value>An error occurred while capturing this window. Please try using a different capture method.</value>
</data>
<data name="Message_CreateFenceFailed" xml:space="preserve">
<value>The current graphics card does not support ID3D11Device5::CreateFence. Please try switching the graphics card or updating the drivers.</value>
</data>
<data name="Message_ScalingFailed" xml:space="preserve">
<value>Scaling failed</value>
</data>
</root>
27 changes: 27 additions & 0 deletions src/Magpie.App/Resources.language-zh-Hans.resw
Original file line number Diff line number Diff line change
Expand Up @@ -838,4 +838,31 @@
<data name="Home_Advanced_SimulateExclusiveFullscreen_InfoBar.Title" xml:space="preserve">
<value>此选项和一些旧游戏不兼容,请谨慎使用。</value>
</data>
<data name="Message_InvalidScalingMode" xml:space="preserve">
<value>缩放模式无效。</value>
</data>
<data name="Message_TouchSupport" xml:space="preserve">
<value>启用触控支持失败。</value>
</data>
<data name="Message_InvalidSourceWindow" xml:space="preserve">
<value>不支持缩放这个窗口。</value>
</data>
<data name="Message_Maximized" xml:space="preserve">
<value>已禁止缩放最大化或全屏的窗口。你可以在主页里更改这个行为。</value>
</data>
<data name="Message_LowIntegrityLevel" xml:space="preserve">
<value>Magpie 需要以管理员身份运行才能缩放这个窗口。</value>
</data>
<data name="Message_ScalingFailedGeneral" xml:space="preserve">
<value>详情请参阅日志。</value>
</data>
<data name="Message_CaptureFailed" xml:space="preserve">
<value>无法捕获这个窗口,请尝试切换捕获方式。</value>
</data>
<data name="Message_CreateFenceFailed" xml:space="preserve">
<value>当前显卡不支持 ID3D11Device5::CreateFence,请尝试切换显卡或更新驱动。</value>
</data>
<data name="Message_ScalingFailed" xml:space="preserve">
<value>缩放时出错</value>
</data>
</root>
Loading

0 comments on commit 2c59c6c

Please sign in to comment.