Skip to content

Commit

Permalink
perf: 直接使用 RootPage 的实现类型
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Dec 16, 2024
1 parent 380bed2 commit e9baffe
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 58 deletions.
1 change: 1 addition & 0 deletions src/Common.Pre.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
<IsDebug>$(Configuration.StartsWith('Debug'))</IsDebug>
<IsPackaged>$(Configuration.EndsWith('Packaged'))</IsPackaged>
<DefaultLanguage>en-US</DefaultLanguage>
<PreferredToolArchitecture>x64</PreferredToolArchitecture>
</PropertyGroup>
</Project>
4 changes: 1 addition & 3 deletions src/Magpie/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void App::Restart(bool asElevated, const wchar_t* arguments) noexcept {
}
}

const Magpie::RootPage& App::RootPage() const noexcept {
const com_ptr<RootPage>& App::RootPage() const noexcept {
assert(_mainWindow);
return _mainWindow.Content();
}
Expand Down Expand Up @@ -353,8 +353,6 @@ void App::_QuitWithoutMainWindow() {
}

void App::_MainWindow_Destoryed() {
UpdateService::Get().ClosingMainWindow();

if (!NotifyIconService::Get().IsShow()) {
_QuitWithoutMainWindow();
}
Expand Down
5 changes: 2 additions & 3 deletions src/Magpie/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "MainWindow.h"
#include "AppSettings.h"
#include "Event.h"
#include "RootPage.h"

namespace winrt::Magpie::implementation {

Expand All @@ -29,9 +30,7 @@ class App : public App_base<App, Markup::IXamlMetadataProvider> {

void Restart(bool asElevated = false, const wchar_t* arguments = nullptr) noexcept;

// 在由外部源引发的回调中可能返回 nullptr
// 这是因为用户关闭主窗口后 RootPage 不会立刻析构
const Magpie::RootPage& RootPage() const noexcept;
const com_ptr<RootPage>& RootPage() const noexcept;

const ::Magpie::MainWindow& MainWindow() const noexcept {
return _mainWindow;
Expand Down
2 changes: 1 addition & 1 deletion src/Magpie/HomeViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void HomeViewModel::IsAutoCheckForUpdates(bool value) noexcept {

void HomeViewModel::DownloadAndInstall() {
UpdateService::Get().DownloadAndInstall();
App::Get().RootPage().NavigateToAboutPage();
App::Get().RootPage()->NavigateToAboutPage();
}

void HomeViewModel::ReleaseNotes() {
Expand Down
32 changes: 16 additions & 16 deletions src/Magpie/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool MainWindow::Create() noexcept {
return false;
}

_Content(winrt::Magpie::RootPage());
_Content(winrt::make_self<winrt::Magpie::implementation::RootPage>());

_appThemeChangedRevoker = App::Get().ThemeChanged(winrt::auto_revoke, [this](bool) { _UpdateTheme(); });
_UpdateTheme();
Expand All @@ -61,7 +61,7 @@ bool MainWindow::Create() noexcept {
(sizeToSet.cx == 0 ? (SWP_NOMOVE | SWP_NOSIZE) : 0) | SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOCOPYBITS);

// Xaml 控件加载完成后显示主窗口
Content().Loaded([this, isMaximized](winrt::IInspectable const&, winrt::RoutedEventArgs const&) {
((RootPage::base_type&)*Content()).Loaded([this, isMaximized](winrt::IInspectable const&, winrt::RoutedEventArgs const&) {
if (isMaximized) {
// ShowWindow(Handle(), SW_SHOWMAXIMIZED) 会显示错误的动画。因此我们以窗口化显示,
// 但位置和大小都和最大化相同,显示完毕后将状态设为最大化。
Expand Down Expand Up @@ -136,7 +136,7 @@ bool MainWindow::Create() noexcept {
);
}

Content().TitleBar().SizeChanged([this](winrt::IInspectable const&, winrt::SizeChangedEventArgs const&) {
Content()->TitleBar().SizeChanged([this](winrt::IInspectable const&, winrt::SizeChangedEventArgs const&) {
_ResizeTitleBarWindow();
});

Expand All @@ -157,7 +157,7 @@ LRESULT MainWindow::_MessageHandler(UINT msg, WPARAM wParam, LPARAM lParam) noex
{
LRESULT ret = base_type::_MessageHandler(WM_SIZE, wParam, lParam);
_ResizeTitleBarWindow();
Content().TitleBar().CaptionButtons().IsWindowMaximized(_IsMaximized());
Content()->TitleBar().CaptionButtons().IsWindowMaximized(_IsMaximized());
return ret;
}
case WM_GETMINMAXINFO:
Expand Down Expand Up @@ -213,7 +213,7 @@ LRESULT MainWindow::_MessageHandler(UINT msg, WPARAM wParam, LPARAM lParam) noex
case WM_ACTIVATE:
{
if (Content()) {
Content().TitleBar().IsWindowActive(LOWORD(wParam) != WA_INACTIVE);
Content()->TitleBar().IsWindowActive(LOWORD(wParam) != WA_INACTIVE);
}
break;
}
Expand Down Expand Up @@ -343,7 +343,7 @@ std::pair<POINT, SIZE> MainWindow::_CreateWindow() noexcept {
}

void MainWindow::_UpdateTheme() noexcept {
XamlWindowT::_SetTheme(Content().ActualTheme() == winrt::ElementTheme::Dark);
XamlWindowT::_SetTheme(Content()->ActualTheme() == winrt::ElementTheme::Dark);
}

LRESULT MainWindow::_TitleBarWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept {
Expand Down Expand Up @@ -385,7 +385,7 @@ LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lPar
}

static const winrt::Size buttonSizeInDips = [this]() {
return Content().TitleBar().CaptionButtons().CaptionButtonSize();
return Content()->TitleBar().CaptionButtons().CaptionButtonSize();
}();

const float buttonWidthInPixels = buttonSizeInDips.Width * _CurrentDpi() / USER_DEFAULT_SCREEN_DPI;
Expand Down Expand Up @@ -420,7 +420,7 @@ LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lPar
[[fallthrough]];
case WM_NCMOUSEMOVE:
{
auto captionButtons = Content().TitleBar().CaptionButtons();
auto captionButtons = Content()->TitleBar().CaptionButtons();

// 将 hover 状态通知 CaptionButtons。标题栏窗口拦截了 XAML Islands 中的标题栏
// 控件的鼠标消息,标题栏按钮的状态由我们手动控制。
Expand Down Expand Up @@ -467,12 +467,12 @@ LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lPar
// 先检查鼠标是否在主窗口上,如果正在显示文字提示,会返回 _hwndTitleBar
HWND hwndUnderCursor = WindowFromPoint(cursorPos);
if (hwndUnderCursor != Handle() && hwndUnderCursor != _hwndTitleBar) {
Content().TitleBar().CaptionButtons().LeaveButtons();
Content()->TitleBar().CaptionButtons().LeaveButtons();
} else {
// 然后检查鼠标在标题栏上的位置
LRESULT hit = SendMessage(_hwndTitleBar, WM_NCHITTEST, 0, MAKELPARAM(cursorPos.x, cursorPos.y));
if (hit != HTMINBUTTON && hit != HTMAXBUTTON && hit != HTCLOSE) {
Content().TitleBar().CaptionButtons().LeaveButtons();
Content()->TitleBar().CaptionButtons().LeaveButtons();
}
}

Expand All @@ -494,7 +494,7 @@ LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lPar
case HTMINBUTTON:
case HTMAXBUTTON:
case HTCLOSE:
Content().TitleBar().CaptionButtons().PressButton((winrt::Magpie::CaptionButton)wParam);
Content()->TitleBar().CaptionButtons().PressButton((winrt::Magpie::CaptionButton)wParam);
// 在标题栏按钮上按下左键后我们便捕获光标,这样才能在释放时得到通知。注意捕获光标后
// 便不会再收到 NC 族消息,这就是为什么我们要处理 WM_MOUSEMOVE 和 WM_LBUTTONUP
SetCapture(_hwndTitleBar);
Expand All @@ -520,17 +520,17 @@ LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lPar
case HTCAPTION:
{
// 在可拖拽区域或上边框释放左键,将此消息传递给主窗口
Content().TitleBar().CaptionButtons().ReleaseButtons();
Content()->TitleBar().CaptionButtons().ReleaseButtons();
return SendMessage(Handle(), msg, wParam, lParam);
}
case HTMINBUTTON:
case HTMAXBUTTON:
case HTCLOSE:
// 在标题栏按钮上释放左键
Content().TitleBar().CaptionButtons().ReleaseButton((winrt::Magpie::CaptionButton)wParam);
Content()->TitleBar().CaptionButtons().ReleaseButton((winrt::Magpie::CaptionButton)wParam);
break;
default:
Content().TitleBar().CaptionButtons().ReleaseButtons();
Content()->TitleBar().CaptionButtons().ReleaseButtons();
}

return 0;
Expand All @@ -550,11 +550,11 @@ void MainWindow::_ResizeTitleBarWindow() noexcept {
return;
}

auto titleBar = Content().TitleBar();
auto titleBar = Content()->TitleBar();

// 获取标题栏的边框矩形
winrt::Rect rect{0.0f, 0.0f, (float)titleBar.ActualWidth(), (float)titleBar.ActualHeight()};
rect = titleBar.TransformToVisual(Content()).TransformBounds(rect);
rect = titleBar.TransformToVisual(*Content()).TransformBounds(rect);

const float dpiScale = _CurrentDpi() / float(USER_DEFAULT_SCREEN_DPI);

Expand Down
5 changes: 3 additions & 2 deletions src/Magpie/MainWindow.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once
#include "XamlWindow.h"
#include <winrt/Magpie.h>
#include "RootPage.h"

namespace Magpie {

class MainWindow : public XamlWindowT<MainWindow, winrt::Magpie::RootPage> {
using base_type = XamlWindowT<MainWindow, winrt::Magpie::RootPage>;
class MainWindow : public XamlWindowT<MainWindow, winrt::com_ptr<winrt::Magpie::implementation::RootPage>> {
using base_type = XamlWindowT<MainWindow, winrt::com_ptr<winrt::Magpie::implementation::RootPage>>;
friend Core::WindowBaseT<MainWindow>;
public:
bool Create() noexcept;
Expand Down
2 changes: 1 addition & 1 deletion src/Magpie/PageFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void PageFrame::MainContent(IInspectable value) {

void PageFrame::Loaded(IInspectable const&, RoutedEventArgs const&) {
// Win10 中更新 ToolTip 的主题
XamlHelper::UpdateThemeOfTooltips(*this, App::Get().RootPage().ActualTheme());
XamlHelper::UpdateThemeOfTooltips(*this, App::Get().IsLightTheme() ? ElementTheme::Light : ElementTheme::Dark);
}

void PageFrame::SizeChanged(IInspectable const&, SizeChangedEventArgs const& e) {
Expand Down
22 changes: 8 additions & 14 deletions src/Magpie/ProfileViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include "CommonSharedConstants.h"
#include "App.h"

using namespace Magpie;
using namespace Magpie::Core;
using namespace ::Magpie;
using namespace ::Magpie::Core;
using namespace winrt;
using namespace Windows::Graphics::Display;
using namespace Windows::Graphics::Imaging;
Expand Down Expand Up @@ -66,20 +66,14 @@ ProfileViewModel::ProfileViewModel(int profileIdx) : _isDefaultProfile(profileId
// 占位
_icon = FontIcon();

RootPage rootPage = App::Get().RootPage();
_themeChangedRevoker = rootPage.ActualThemeChanged(
auto_revoke,
[this](FrameworkElement const& sender, IInspectable const&) {
_LoadIcon(sender);
}
);
_appThemeChangedRevoker = App::Get().ThemeChanged(auto_revoke, [this](bool) { _LoadIcon(); });

_displayInformation = DisplayInformation::GetForCurrentView();
_dpiChangedRevoker = _displayInformation.DpiChanged(
auto_revoke,
[this](DisplayInformation const&, IInspectable const&) {
if (App::Get().MainWindow()) {
_LoadIcon(App::Get().RootPage());
_LoadIcon();
}
}
);
Expand All @@ -91,7 +85,7 @@ ProfileViewModel::ProfileViewModel(int profileIdx) : _isDefaultProfile(profileId
_isProgramExist = Win32Helper::FileExists(_data->pathRule.c_str());
}

_LoadIcon(rootPage);
_LoadIcon();
}

ResourceLoader resourceLoader =
Expand Down Expand Up @@ -762,17 +756,17 @@ void ProfileViewModel::IsDirectFlipDisabled(bool value) {
RaisePropertyChanged(L"IsDirectFlipDisabled");
}

fire_and_forget ProfileViewModel::_LoadIcon(FrameworkElement const& rootPage) {
fire_and_forget ProfileViewModel::_LoadIcon() {
std::wstring iconPath;
SoftwareBitmap iconBitmap{ nullptr };

auto weakThis = get_weak();

if (_isProgramExist) {
const bool preferLightTheme = rootPage.ActualTheme() == ElementTheme::Light;
const bool preferLightTheme = App::Get().IsLightTheme();
const bool isPackaged = _data->isPackaged;
const std::wstring path = _data->pathRule;
CoreDispatcher dispatcher = rootPage.Dispatcher();
CoreDispatcher dispatcher = App::Get().Dispatcher();
const uint32_t iconSize = (uint32_t)std::lroundf(32 * _displayInformation.LogicalDpi() / USER_DEFAULT_SCREEN_DPI);

co_await resume_background();
Expand Down
5 changes: 3 additions & 2 deletions src/Magpie/ProfileViewModel.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "ProfileViewModel.g.h"
#include "SmallVector.h"
#include "Event.h"

namespace Magpie {
struct Profile;
Expand Down Expand Up @@ -141,7 +142,7 @@ struct ProfileViewModel : ProfileViewModelT<ProfileViewModel>,
void IsDirectFlipDisabled(bool value);

private:
fire_and_forget _LoadIcon(FrameworkElement const& rootPage);
fire_and_forget _LoadIcon();

bool _isProgramExist = true;

Expand All @@ -156,7 +157,7 @@ struct ProfileViewModel : ProfileViewModelT<ProfileViewModel>,
// 可以保存此指针的原因是: 用户停留在此页面时不会有缩放配置被创建或删除
::Magpie::Profile* _data = nullptr;

RootPage::ActualThemeChanged_revoker _themeChangedRevoker;
::Magpie::Core::EventRevoker _appThemeChangedRevoker;
Windows::Graphics::Display::DisplayInformation _displayInformation{ nullptr };
Windows::Graphics::Display::DisplayInformation::DpiChanged_revoker _dpiChangedRevoker;

Expand Down
1 change: 0 additions & 1 deletion src/Magpie/RootPage.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "RootPage.g.h"
#include "Event.h"
#include "AppSettings.h"

namespace Magpie {
struct Profile;
Expand Down
5 changes: 0 additions & 5 deletions src/Magpie/RootPage.idl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ namespace Magpie {
runtimeclass RootPage : Windows.UI.Xaml.Controls.Page {
RootPage();

Microsoft.UI.Xaml.Controls.NavigationView RootNavigationView { get; };
TitleBarControl TitleBar { get; };

NewProfileViewModel NewProfileViewModel { get; };

void NavigateToAboutPage();
}
}
2 changes: 1 addition & 1 deletion src/Magpie/ScalingModeItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void ScalingModeItem::RenameButton_Click() {
}

// Flyout 没有 IsOpen 可供绑定,只能用变通方法关闭
XamlHelper::ClosePopups(App::Get().RootPage().XamlRoot());
XamlHelper::ClosePopups(App::Get().RootPage()->XamlRoot());

_Data().name = _trimedRenameText;
RaisePropertyChanged(L"Name");
Expand Down
2 changes: 1 addition & 1 deletion src/Magpie/TitlebarControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TitleBarControl::TitleBarControl() {
}

void TitleBarControl::Loading(FrameworkElement const&, IInspectable const&) {
MUXC::NavigationView rootNavigationView = App::Get().RootPage().RootNavigationView();
MUXC::NavigationView rootNavigationView = App::Get().RootPage()->RootNavigationView();
rootNavigationView.DisplayModeChanged([this](const auto&, const auto& args) {
bool expanded = args.DisplayMode() == MUXC::NavigationViewDisplayMode::Expanded;
VisualStateManager::GoToState(*this, expanded ? L"Expanded" : L"Compact", true);
Expand Down
7 changes: 6 additions & 1 deletion src/Magpie/UpdateService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using namespace ::Magpie::Core;
using namespace winrt::Magpie;
using namespace winrt::Magpie::implementation;
using namespace winrt;
using namespace Windows::Storage::Streams;
using namespace Windows::System::Threading;
Expand Down Expand Up @@ -53,6 +54,10 @@ void UpdateService::Initialize() noexcept {
_StopTimer();
}
});

implementation::App::Get().MainWindow().Destroyed([this]() {
_MainWindow_Closed();
});
}

fire_and_forget UpdateService::CheckForUpdatesAsync(bool isAutoUpdate) {
Expand Down Expand Up @@ -456,7 +461,7 @@ void UpdateService::EnteringAboutPage() {
}
}

void UpdateService::ClosingMainWindow() {
void UpdateService::_MainWindow_Closed() {
EnteringAboutPage();

if (_status == UpdateStatus::Available) {
Expand Down
4 changes: 2 additions & 2 deletions src/Magpie/UpdateService.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class UpdateService {

void EnteringAboutPage();

void ClosingMainWindow();

void Cancel();

const std::wstring& Tag() const noexcept {
Expand Down Expand Up @@ -82,6 +80,8 @@ class UpdateService {
void _StartTimer();
void _StopTimer();

void _MainWindow_Closed();

// DispatcherTimer 在不显示主窗口时可能停滞,因此使用 ThreadPoolTimer
winrt::Threading::ThreadPoolTimer _timer{ nullptr };
winrt::CoreDispatcher _dispatcher{ nullptr };
Expand Down
Loading

0 comments on commit e9baffe

Please sign in to comment.