Skip to content

Commit

Permalink
perf: 更多地方直接使用实现类型
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Dec 18, 2024
1 parent 1e6bba0 commit 2c1d4f4
Show file tree
Hide file tree
Showing 41 changed files with 124 additions and 169 deletions.
1 change: 0 additions & 1 deletion src/Magpie/AboutViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "IconHelper.h"
#include "CommonSharedConstants.h"

using namespace ::Magpie;
using namespace ::Magpie;
using namespace winrt;
using namespace Windows::UI::Xaml::Media::Imaging;
Expand Down
1 change: 0 additions & 1 deletion src/Magpie/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "IsNullStateTrigger.h"
#include "TextBlockHelper.h"

using namespace ::Magpie;
using namespace ::Magpie;
using namespace winrt;
using namespace Windows::UI::ViewManagement;
Expand Down
2 changes: 1 addition & 1 deletion src/Magpie/CandidateWindowItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fire_and_forget CandidateWindowItem::_ResolveWindow(bool resolveIcon, bool resol
co_return;
}

App::Get().Dispatcher().TryRunAsync(
App::Get().Dispatcher().RunAsync(
CoreDispatcherPriority::Normal,
[this, defaultProfileName(std::move(defaultProfileName)), aumid(reader.AUMID())]() {
if (!defaultProfileName.empty()) {
Expand Down
6 changes: 6 additions & 0 deletions src/Magpie/CaptionButtonsControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

namespace winrt::Magpie::implementation {

enum class CaptionButton {
Minimize = HTMINBUTTON,
Maximize = HTMAXBUTTON,
Close = HTCLOSE
};

struct CaptionButtonsControl : CaptionButtonsControlT<CaptionButtonsControl> {
Size CaptionButtonSize() const;

Expand Down
18 changes: 1 addition & 17 deletions src/Magpie/CaptionButtonsControl.idl
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
namespace Magpie {
enum CaptionButton {
Minimize = 8, // HTMINBUTTON
Maximize = 9, // HTMAXBUTTON
Close = 20 // HTCLOSE
};

[default_interface]
runtimeclass CaptionButtonsControl : Windows.UI.Xaml.Controls.UserControl {
CaptionButtonsControl();

Windows.Foundation.Size CaptionButtonSize { get; };

void HoverButton(CaptionButton button);
void PressButton(CaptionButton button);
void ReleaseButton(CaptionButton button);
void ReleaseButtons();
void LeaveButtons();

void IsWindowMaximized(Boolean value);
void IsWindowActive(Boolean value);
}
}
4 changes: 2 additions & 2 deletions src/Magpie/EffectParametersViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "ScalingMode.h"
#include "EffectsService.h"
#include "EffectDesc.h"
#include "App.h"

using namespace Magpie;

Expand All @@ -35,7 +36,6 @@ static fire_and_forget LazySaveAppSettings() {
}

sleeping = true;
CoreDispatcher dispatcher = CoreWindow::GetForCurrentThread().Dispatcher();

co_await 1s;

Expand All @@ -50,7 +50,7 @@ static fire_and_forget LazySaveAppSettings() {
}

// 回到主线程
co_await dispatcher;
co_await App::Get().Dispatcher();

sleeping = false;
AppSettings::Get().SaveAsync();
Expand Down
4 changes: 2 additions & 2 deletions src/Magpie/FileDialogHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "App.h"

using namespace ::Magpie;
using namespace winrt::Magpie;
using namespace winrt::Magpie::implementation;
using namespace winrt;

namespace Magpie {
Expand All @@ -15,7 +15,7 @@ std::optional<std::wstring> FileDialogHelper::OpenFileDialog(IFileDialog* fileDi
fileDialog->GetOptions(&options1);
fileDialog->SetOptions(options1 | options | FOS_FORCEFILESYSTEM);

if (fileDialog->Show(implementation::App::Get().MainWindow().Handle()) != S_OK) {
if (fileDialog->Show(App::Get().MainWindow().Handle()) != S_OK) {
// 被用户取消
return std::wstring();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Magpie/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif
#include "XamlHelper.h"
#include "ComboBoxHelper.h"
#include "App.h"

using namespace ::Magpie;

Expand All @@ -27,7 +28,7 @@ void HomePage::SimulateExclusiveFullscreenToggleSwitch_Toggled(IInspectable cons
}

// 这个回调被触发时 UI 还没有更新,需要异步处理
Dispatcher().RunAsync(CoreDispatcherPriority::Low, [weakThis(get_weak())]() {
App::Get().Dispatcher().RunAsync(CoreDispatcherPriority::Low, [weakThis(get_weak())]() {
auto strongThis = weakThis.get();
if (!strongThis) {
return;
Expand Down
3 changes: 1 addition & 2 deletions src/Magpie/HomeViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,14 @@ fire_and_forget HomeViewModel::IsTouchSupportEnabled(bool value) {
}

auto weakThis = get_weak();
CoreDispatcher dispatcher = CoreWindow::GetForCurrentThread().Dispatcher();

// UAC 可能导致 XAML Islands 崩溃,因此不能在主线程上执行 ShellExecute,
// 见 https://github.com/microsoft/microsoft-ui-xaml/issues/4952
co_await resume_background();

TouchHelper::IsTouchSupportEnabled(value);

co_await dispatcher;
co_await App::Get().Dispatcher();

if (weakThis.get()) {
RaisePropertyChanged(L"IsTouchSupportEnabled");
Expand Down
10 changes: 5 additions & 5 deletions src/Magpie/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lPar
[[fallthrough]];
case WM_NCMOUSEMOVE:
{
auto captionButtons = Content()->TitleBar().CaptionButtons();
CaptionButtonsControl& captionButtons = Content()->TitleBar().CaptionButtons();

// 将 hover 状态通知 CaptionButtons。标题栏窗口拦截了 XAML Islands 中的标题栏
// 控件的鼠标消息,标题栏按钮的状态由我们手动控制。
Expand All @@ -444,7 +444,7 @@ LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lPar
case HTMINBUTTON:
case HTMAXBUTTON:
case HTCLOSE:
captionButtons.HoverButton((winrt::Magpie::CaptionButton)wParam);
captionButtons.HoverButton((CaptionButton)wParam);

// 追踪鼠标以确保鼠标离开标题栏时我们能收到 WM_NCMOUSELEAVE 消息,否则无法
// 可靠的收到这个消息,尤其是在用户快速移动鼠标的时候。
Expand Down Expand Up @@ -502,7 +502,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((CaptionButton)wParam);
// 在标题栏按钮上按下左键后我们便捕获光标,这样才能在释放时得到通知。注意捕获光标后
// 便不会再收到 NC 族消息,这就是为什么我们要处理 WM_MOUSEMOVE 和 WM_LBUTTONUP
SetCapture(_hwndTitleBar);
Expand Down Expand Up @@ -535,7 +535,7 @@ LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lPar
case HTMAXBUTTON:
case HTCLOSE:
// 在标题栏按钮上释放左键
Content()->TitleBar().CaptionButtons().ReleaseButton((winrt::Magpie::CaptionButton)wParam);
Content()->TitleBar().CaptionButtons().ReleaseButton((CaptionButton)wParam);
break;
default:
Content()->TitleBar().CaptionButtons().ReleaseButtons();
Expand All @@ -558,7 +558,7 @@ void MainWindow::_ResizeTitleBarWindow() noexcept {
return;
}

auto titleBar = Content()->TitleBar();
TitleBarControl& titleBar = Content()->TitleBar();

// 获取标题栏的边框矩形
winrt::Rect rect{0.0f, 0.0f, (float)titleBar.ActualWidth(), (float)titleBar.ActualHeight()};
Expand Down
4 changes: 1 addition & 3 deletions src/Magpie/ProfileViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "CommonSharedConstants.h"
#include "App.h"

using namespace ::Magpie;
using namespace ::Magpie;
using namespace winrt;
using namespace Windows::Graphics::Display;
Expand Down Expand Up @@ -758,7 +757,6 @@ fire_and_forget ProfileViewModel::_LoadIcon() {
const bool preferLightTheme = App::Get().IsLightTheme();
const bool isPackaged = _data->isPackaged;
const std::wstring path = _data->pathRule;
CoreDispatcher dispatcher = App::Get().Dispatcher();
const uint32_t iconSize =
(uint32_t)std::lroundf(32.0f * App::Get().MainWindow().CurrentDpi() / USER_DEFAULT_SCREEN_DPI);

Expand All @@ -780,7 +778,7 @@ fire_and_forget ProfileViewModel::_LoadIcon() {
iconBitmap = IconHelper::ExtractIconFromExe(path.c_str(), iconSize);
}

co_await dispatcher;
co_await App::Get().Dispatcher();
if (!weakThis.get()) {
co_return;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Magpie/RootPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "LocalizationService.h"
#include "App.h"

using namespace ::Magpie;
using namespace ::Magpie;
using namespace winrt;
using namespace Windows::Graphics::Display;
Expand Down Expand Up @@ -241,7 +240,6 @@ fire_and_forget RootPage::_LoadIcon(MUXC::NavigationViewItem const& item, const
bool preferLightTheme = App::Get().IsLightTheme();
bool isPackaged = profile.isPackaged;
std::wstring path = profile.pathRule;
CoreDispatcher dispatcher = App::Get().Dispatcher();
const uint32_t iconSize = (uint32_t)std::lroundf(
16.0f * App::Get().MainWindow().CurrentDpi() / USER_DEFAULT_SCREEN_DPI);

Expand All @@ -265,7 +263,7 @@ fire_and_forget RootPage::_LoadIcon(MUXC::NavigationViewItem const& item, const
iconBitmap = IconHelper::ExtractIconFromExe(path.c_str(), iconSize);
}

co_await dispatcher;
co_await App::Get().Dispatcher();

auto strongRef = weakRef.get();
if (!strongRef) {
Expand Down
5 changes: 5 additions & 0 deletions src/Magpie/RootPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "RootPage.g.h"
#include "Event.h"
#include "NewProfileViewModel.h"
#include "TitleBarControl.h"

namespace Magpie {
struct Profile;
Expand Down Expand Up @@ -39,6 +40,10 @@ struct RootPage : RootPageT<RootPage> {

void NavigateToAboutPage();

TitleBarControl& TitleBar() {
return *get_self<TitleBarControl>(RootPageT::TitleBar());
}

private:
void _UpdateTheme(bool updateIcons);

Expand Down
17 changes: 10 additions & 7 deletions src/Magpie/ScalingModeEffectItem.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "pch.h"
#include "ScalingModeEffectItem.h"
#if __has_include("ScalingTypeItem.g.cpp")
#include "ScalingTypeItem.g.cpp"
#endif
#if __has_include("ScalingModeEffectItem.g.cpp")
#include "ScalingModeEffectItem.g.cpp"
#endif
Expand Down Expand Up @@ -69,19 +72,19 @@ IVector<IInspectable> ScalingModeEffectItem::ScalingTypes() noexcept {
ResourceLoader::GetForCurrentView(CommonSharedConstants::APP_RESOURCE_MAP_ID);

return single_threaded_vector(std::vector<IInspectable>{
Magpie::ScalingType(
make<ScalingTypeItem>(
resourceLoader.GetString(L"ScalingModes_ScaleFlyout_Type_Factor"),
resourceLoader.GetString(L"ScalingModes_ScaleFlyout_Type_Factor_Description")
),
Magpie::ScalingType(
make<ScalingTypeItem>(
resourceLoader.GetString(L"ScalingModes_ScaleFlyout_Type_Fit"),
resourceLoader.GetString(L"ScalingModes_ScaleFlyout_Type_Fit_Description")
),
Magpie::ScalingType(
make<ScalingTypeItem>(
resourceLoader.GetString(L"ScalingModes_ScaleFlyout_Type_Absolute"),
resourceLoader.GetString(L"ScalingModes_ScaleFlyout_Type_Absolute_Description")
),
Magpie::ScalingType(
make<ScalingTypeItem>(
resourceLoader.GetString(L"ScalingModes_ScaleFlyout_Type_Fill"),
resourceLoader.GetString(L"ScalingModes_ScaleFlyout_Type_Fill_Description")
),
Expand Down Expand Up @@ -227,7 +230,7 @@ void ScalingModeEffectItem::ScalingPixelsY(double value) {
}

void ScalingModeEffectItem::Remove() {
Removed.invoke(*this, _effectIdx);
Removed.Invoke(_effectIdx);
}

bool ScalingModeEffectItem::CanMove() const noexcept {
Expand All @@ -245,11 +248,11 @@ bool ScalingModeEffectItem::CanMoveDown() const noexcept {
}

void ScalingModeEffectItem::MoveUp() noexcept {
Moved.invoke(*this, true);
Moved.Invoke(*this, true);
}

void ScalingModeEffectItem::MoveDown() noexcept {
Moved.invoke(*this, false);
Moved.Invoke(*this, false);
}

void ScalingModeEffectItem::RefreshMoveState() {
Expand Down
22 changes: 16 additions & 6 deletions src/Magpie/ScalingModeEffectItem.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
#pragma once
#include "ScalingTypeItem.g.h"
#include "ScalingModeEffectItem.g.h"
#include "EffectParametersViewModel.h"
#include "Event.h"

namespace Magpie {
struct EffectOption;
}

namespace Magpie {
struct EffectInfo;
}

namespace winrt::Magpie::implementation {

struct ScalingTypeItem : ScalingTypeItemT<ScalingTypeItem> {
ScalingTypeItem(hstring name, hstring desc) : _name(std::move(name)), _desc(std::move(desc)) {}

hstring Name() const noexcept { return _name; }

hstring Desc() const noexcept { return _desc; }

private:
hstring _name;
hstring _desc;
};

struct ScalingModeEffectItem : ScalingModeEffectItemT<ScalingModeEffectItem>,
wil::notify_property_changed_base<ScalingModeEffectItem> {
ScalingModeEffectItem(uint32_t scalingModeIdx, uint32_t effectIdx);
Expand Down Expand Up @@ -71,8 +81,8 @@ struct ScalingModeEffectItem : ScalingModeEffectItemT<ScalingModeEffectItem>,
void RefreshMoveState();

// 上移为 true,下移为 false
wil::typed_event<Magpie::ScalingModeEffectItem, bool> Moved;
wil::untyped_event<uint32_t> Removed;
::Magpie::Event<ScalingModeEffectItem&, bool> Moved;
::Magpie::Event<uint32_t> Removed;

private:
::Magpie::EffectOption& _Data() noexcept;
Expand Down
7 changes: 5 additions & 2 deletions src/Magpie/ScalingModeEffectItem.idl
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
namespace Magpie {
runtimeclass ScalingTypeItem {
String Name { get; };
String Desc { get; };
}

runtimeclass ScalingModeEffectItem : Windows.UI.Xaml.Data.INotifyPropertyChanged {
UInt32 ScalingModeIdx;
UInt32 EffectIdx;
Expand All @@ -19,14 +24,12 @@ namespace Magpie {
EffectParametersViewModel Parameters { get; };

void Remove();
event Windows.Foundation.EventHandler<UInt32> Removed;

Boolean CanMove { get; };
Boolean CanMoveUp { get; };
Boolean CanMoveDown { get; };
void MoveUp();
void MoveDown();
event Windows.Foundation.TypedEventHandler<ScalingModeEffectItem, Boolean> Moved;

void RefreshMoveState();
}
Expand Down
Loading

0 comments on commit 2c1d4f4

Please sign in to comment.