From 4cfb58111cdf9022cfa0bac606db6b4300cf7134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=97=AD?= Date: Thu, 11 Apr 2024 16:37:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=20wil::notify=5F?= =?UTF-8?q?property=5Fchanged=5Fbase=20=E7=AE=80=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Magpie.App/AboutViewModel.cpp | 46 ++++++------- src/Magpie.App/AboutViewModel.h | 12 +--- src/Magpie.App/CandidateWindowItem.cpp | 4 +- src/Magpie.App/CandidateWindowItem.h | 13 +--- src/Magpie.App/EffectParametersViewModel.h | 33 ++-------- src/Magpie.App/HomeViewModel.cpp | 30 ++++----- src/Magpie.App/HomeViewModel.h | 12 +--- src/Magpie.App/NewProfileViewModel.cpp | 12 ++-- src/Magpie.App/NewProfileViewModel.h | 17 ++--- src/Magpie.App/PageFrame.cpp | 8 +-- src/Magpie.App/PageFrame.h | 12 +--- src/Magpie.App/ProfileViewModel.cpp | 64 +++++++++---------- src/Magpie.App/ProfileViewModel.h | 13 +--- .../ScalingConfigurationViewModel.cpp | 10 +-- .../ScalingConfigurationViewModel.h | 17 ++--- src/Magpie.App/ScalingModeEffectItem.cpp | 28 ++++---- src/Magpie.App/ScalingModeEffectItem.h | 13 +--- src/Magpie.App/ScalingModeItem.cpp | 28 ++++---- src/Magpie.App/ScalingModeItem.h | 13 +--- src/Magpie.App/SettingsViewModel.cpp | 42 ++++++------ src/Magpie.App/SettingsViewModel.h | 13 +--- src/Magpie.App/TitlebarControl.cpp | 2 +- src/Magpie.App/TitlebarControl.h | 12 +--- src/Magpie.App/pch.h | 2 + src/Magpie/MainWindow.cpp | 10 ++- src/Magpie/XamlApp.cpp | 22 +++---- src/Magpie/XamlWindow.h | 8 ++- 27 files changed, 190 insertions(+), 306 deletions(-) diff --git a/src/Magpie.App/AboutViewModel.cpp b/src/Magpie.App/AboutViewModel.cpp index bf444bc04..42ffdb6ad 100644 --- a/src/Magpie.App/AboutViewModel.cpp +++ b/src/Magpie.App/AboutViewModel.cpp @@ -50,7 +50,7 @@ AboutViewModel::AboutViewModel() { } that->_logo = std::move(bitmap); - that->_propertyChangedEvent(*that, PropertyChangedEventArgs(L"Logo")); + that->RaisePropertyChanged(L"Logo"); })(this); } @@ -99,7 +99,7 @@ bool AboutViewModel::IsCheckForPreviewUpdates() const noexcept { void AboutViewModel::IsCheckForPreviewUpdates(bool value) { AppSettings::Get().IsCheckForPreviewUpdates(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsCheckForPreviewUpdates")); + RaisePropertyChanged(L"IsCheckForPreviewUpdates"); } bool AboutViewModel::IsCheckForUpdatesButtonEnabled() const noexcept { @@ -117,7 +117,7 @@ bool AboutViewModel::IsAutoCheckForUpdates() const noexcept { void AboutViewModel::IsAutoCheckForUpdates(bool value) { AppSettings::Get().IsAutoCheckForUpdates(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsAutoCheckForUpdates")); + RaisePropertyChanged(L"IsAutoCheckForUpdates"); } bool AboutViewModel::IsAnyUpdateStatus() const noexcept { @@ -140,7 +140,7 @@ void AboutViewModel::IsErrorWhileChecking(bool value) { } } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsErrorWhileChecking")); + RaisePropertyChanged(L"IsErrorWhileChecking"); } bool AboutViewModel::IsNoUpdate() const noexcept { @@ -189,7 +189,7 @@ void AboutViewModel::IsUpdateCardOpen(bool value) { } } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsUpdateCardOpen")); + RaisePropertyChanged(L"IsUpdateCardOpen"); } bool AboutViewModel::IsUpdateCardClosable() const noexcept { @@ -259,23 +259,23 @@ void AboutViewModel::Retry() { } void AboutViewModel::_UpdateService_StatusChanged(UpdateStatus status) { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsCheckingForUpdates")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsCheckForUpdatesButtonEnabled")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsAnyUpdateStatus")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsErrorWhileChecking")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsNoUpdate")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsAvailable")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsDownloading")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsErrorWhileDownloading")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsInstalling")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsDownloadingOrLater")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsUpdateCardOpen")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsUpdateCardClosable")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsCancelButtonVisible")); + RaisePropertyChanged(L"IsCheckingForUpdates"); + RaisePropertyChanged(L"IsCheckForUpdatesButtonEnabled"); + RaisePropertyChanged(L"IsAnyUpdateStatus"); + RaisePropertyChanged(L"IsErrorWhileChecking"); + RaisePropertyChanged(L"IsNoUpdate"); + RaisePropertyChanged(L"IsAvailable"); + RaisePropertyChanged(L"IsDownloading"); + RaisePropertyChanged(L"IsErrorWhileDownloading"); + RaisePropertyChanged(L"IsInstalling"); + RaisePropertyChanged(L"IsDownloadingOrLater"); + RaisePropertyChanged(L"IsUpdateCardOpen"); + RaisePropertyChanged(L"IsUpdateCardClosable"); + RaisePropertyChanged(L"IsCancelButtonVisible"); if (status >= UpdateStatus::Available) { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"UpdateCardTitle")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"UpdateReleaseNotesLink")); + RaisePropertyChanged(L"UpdateCardTitle"); + RaisePropertyChanged(L"UpdateReleaseNotesLink"); if (status == UpdateStatus::Downloading) { _downloadProgressChangedRevoker = UpdateService::Get().DownloadProgressChanged( @@ -286,15 +286,15 @@ void AboutViewModel::_UpdateService_StatusChanged(UpdateStatus status) { _downloadProgressChangedRevoker.Revoke(); if (status >= UpdateStatus::ErrorWhileDownloading) { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsNoDownloadProgress")); + RaisePropertyChanged(L"IsNoDownloadProgress"); } } } } void AboutViewModel::_UpdateService_DownloadProgressChanged(double) { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsNoDownloadProgress")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"DownloadProgress")); + RaisePropertyChanged(L"IsNoDownloadProgress"); + RaisePropertyChanged(L"DownloadProgress"); } } diff --git a/src/Magpie.App/AboutViewModel.h b/src/Magpie.App/AboutViewModel.h index 21de65c1b..a8680995f 100644 --- a/src/Magpie.App/AboutViewModel.h +++ b/src/Magpie.App/AboutViewModel.h @@ -4,17 +4,10 @@ namespace winrt::Magpie::App::implementation { -struct AboutViewModel : AboutViewModelT { +struct AboutViewModel : AboutViewModelT, + wil::notify_property_changed_base { AboutViewModel(); - event_token PropertyChanged(PropertyChangedEventHandler const& handler) { - return _propertyChangedEvent.add(handler); - } - - void PropertyChanged(event_token const& token) noexcept { - _propertyChangedEvent.remove(token); - } - Imaging::SoftwareBitmapSource Logo() const noexcept { return _logo; } @@ -73,7 +66,6 @@ struct AboutViewModel : AboutViewModelT { void _UpdateService_StatusChanged(UpdateStatus status); void _UpdateService_DownloadProgressChanged(double); - event _propertyChangedEvent; WinRTUtils::EventRevoker _updateStatusChangedRevoker; WinRTUtils::EventRevoker _downloadProgressChangedRevoker; WinRTUtils::EventRevoker _showOnHomePageChangedRevoker; diff --git a/src/Magpie.App/CandidateWindowItem.cpp b/src/Magpie.App/CandidateWindowItem.cpp index 82a72c383..16cfcf8f7 100644 --- a/src/Magpie.App/CandidateWindowItem.cpp +++ b/src/Magpie.App/CandidateWindowItem.cpp @@ -121,7 +121,7 @@ fire_and_forget CandidateWindowItem::_ResolveWindow(bool resolveIcon, bool resol } // 即使 defaultProfileName 为空也通知 DefaultProfileName 已更改 // 这是为了正确设置 CandidateWindowIndex - that->_propertyChangedEvent(*that, PropertyChangedEventArgs(L"DefaultProfileName")); + that->RaisePropertyChanged(L"DefaultProfileName"); that->_aumid = aumid; } @@ -167,7 +167,7 @@ fire_and_forget CandidateWindowItem::_ResolveWindow(bool resolveIcon, bool resol strongThis->_icon = std::move(fontIcon); } - strongThis->_propertyChangedEvent(*this, PropertyChangedEventArgs(L"Icon")); + strongThis->RaisePropertyChanged(L"Icon"); } } diff --git a/src/Magpie.App/CandidateWindowItem.h b/src/Magpie.App/CandidateWindowItem.h index d0b51eb6a..e873d3645 100644 --- a/src/Magpie.App/CandidateWindowItem.h +++ b/src/Magpie.App/CandidateWindowItem.h @@ -3,17 +3,10 @@ namespace winrt::Magpie::App::implementation { -struct CandidateWindowItem : CandidateWindowItemT { +struct CandidateWindowItem : CandidateWindowItemT, + wil::notify_property_changed_base { CandidateWindowItem(uint64_t hWnd, uint32_t dpi, bool isLightTheme, CoreDispatcher const& dispatcher); - event_token PropertyChanged(PropertyChangedEventHandler const& handler) { - return _propertyChangedEvent.add(handler); - } - - void PropertyChanged(event_token const& token) noexcept { - _propertyChangedEvent.remove(token); - } - hstring Title() const noexcept { return _title; } @@ -39,8 +32,6 @@ struct CandidateWindowItem : CandidateWindowItemT { private: fire_and_forget _ResolveWindow(bool resolveIcon, bool resolveName, HWND hWnd, bool isLightTheme, uint32_t dpi, CoreDispatcher dispatcher); - event _propertyChangedEvent; - hstring _title; Controls::IconElement _icon{ nullptr }; hstring _defaultProfileName; diff --git a/src/Magpie.App/EffectParametersViewModel.h b/src/Magpie.App/EffectParametersViewModel.h index 7f69b9d91..7e568be74 100644 --- a/src/Magpie.App/EffectParametersViewModel.h +++ b/src/Magpie.App/EffectParametersViewModel.h @@ -10,19 +10,11 @@ struct EffectInfo; namespace winrt::Magpie::App::implementation { -struct ScalingModeBoolParameter : ScalingModeBoolParameterT { +struct ScalingModeBoolParameter : ScalingModeBoolParameterT, + wil::notify_property_changed_base { ScalingModeBoolParameter(uint32_t index, const hstring& label, bool initValue) : _index(index), _label(box_value(label)), _value(initValue) { } - - event_token PropertyChanged(PropertyChangedEventHandler const& handler) { - return _propertyChangedEvent.add(handler); - } - - void PropertyChanged(event_token const& token) noexcept { - _propertyChangedEvent.remove(token); - } - uint32_t Index() const noexcept { return _index; } @@ -33,7 +25,7 @@ struct ScalingModeBoolParameter : ScalingModeBoolParameterT _propertyChangedEvent; - const uint32_t _index; IInspectable _label; bool _value; }; -struct ScalingModeFloatParameter : ScalingModeFloatParameterT { +struct ScalingModeFloatParameter : ScalingModeFloatParameterT, + wil::notify_property_changed_base { ScalingModeFloatParameter(uint32_t index, const hstring& label, float initValue, float minimum, float maximum, float step) : _index(index), _label(label), _value(initValue), _minimum(minimum), _maximum(maximum), _step(step) { } - event_token PropertyChanged(PropertyChangedEventHandler const& handler) { - return _propertyChangedEvent.add(handler); - } - - void PropertyChanged(event_token const& token) noexcept { - _propertyChangedEvent.remove(token); - } - uint32_t Index() const noexcept { return _index; } @@ -71,8 +54,8 @@ struct ScalingModeFloatParameter : ScalingModeFloatParameterT _propertyChangedEvent; - const uint32_t _index; const hstring _label; const double _minimum; diff --git a/src/Magpie.App/HomeViewModel.cpp b/src/Magpie.App/HomeViewModel.cpp index da2559fff..6e70e7d96 100644 --- a/src/Magpie.App/HomeViewModel.cpp +++ b/src/Magpie.App/HomeViewModel.cpp @@ -84,8 +84,8 @@ uint32_t HomeViewModel::Delay() const noexcept { void HomeViewModel::Delay(uint32_t value) { AppSettings::Get().CountdownSeconds(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"Delay")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"TimerButtonText")); + RaisePropertyChanged(L"Delay"); + RaisePropertyChanged(L"TimerButtonText"); } bool HomeViewModel::IsAutoRestore() const noexcept { @@ -100,7 +100,7 @@ void HomeViewModel::IsAutoRestore(bool value) { } settings.IsAutoRestore(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsAutoRestore")); + RaisePropertyChanged(L"IsAutoRestore"); } bool HomeViewModel::IsWndToRestore() const noexcept { @@ -144,8 +144,8 @@ inline void HomeViewModel::ShowUpdateCard(bool value) noexcept { UpdateService::Get().IsShowOnHomePage(false); } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ShowUpdateCard")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"UpdateCardTitle")); + RaisePropertyChanged(L"ShowUpdateCard"); + RaisePropertyChanged(L"UpdateCardTitle"); } hstring HomeViewModel::UpdateCardTitle() const noexcept { @@ -187,27 +187,27 @@ void HomeViewModel::RemindMeLater() { void HomeViewModel::_ScalingService_IsTimerOnChanged(bool value) { if (!value) { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"TimerProgressRingValue")); + RaisePropertyChanged(L"TimerProgressRingValue"); } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"TimerProgressRingValue")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"TimerLabelText")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"TimerButtonText")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsTimerOn")); + RaisePropertyChanged(L"TimerProgressRingValue"); + RaisePropertyChanged(L"TimerLabelText"); + RaisePropertyChanged(L"TimerButtonText"); + RaisePropertyChanged(L"IsTimerOn"); } void HomeViewModel::_ScalingService_TimerTick(double) { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"TimerProgressRingValue")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"TimerLabelText")); + RaisePropertyChanged(L"TimerProgressRingValue"); + RaisePropertyChanged(L"TimerLabelText"); } void HomeViewModel::_ScalingService_IsRunningChanged(bool) { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsNotRunning")); + RaisePropertyChanged(L"IsNotRunning"); } void HomeViewModel::_ScalingService_WndToRestoreChanged(HWND) { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsWndToRestore")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"RestoreWndDesc")); + RaisePropertyChanged(L"IsWndToRestore"); + RaisePropertyChanged(L"RestoreWndDesc"); } } diff --git a/src/Magpie.App/HomeViewModel.h b/src/Magpie.App/HomeViewModel.h index b4efdbddb..5cbe2ed56 100644 --- a/src/Magpie.App/HomeViewModel.h +++ b/src/Magpie.App/HomeViewModel.h @@ -4,17 +4,9 @@ namespace winrt::Magpie::App::implementation { -struct HomeViewModel : HomeViewModelT { +struct HomeViewModel : HomeViewModelT, wil::notify_property_changed_base { HomeViewModel(); - event_token PropertyChanged(PropertyChangedEventHandler const& handler) { - return _propertyChangedEvent.add(handler); - } - - void PropertyChanged(event_token const& token) noexcept { - _propertyChangedEvent.remove(token); - } - bool IsTimerOn() const noexcept; double TimerProgressRingValue() const noexcept; @@ -66,8 +58,6 @@ struct HomeViewModel : HomeViewModelT { void _ScalingService_WndToRestoreChanged(HWND); - event _propertyChangedEvent; - WinRTUtils::EventRevoker _isTimerOnRevoker; WinRTUtils::EventRevoker _timerTickRevoker; WinRTUtils::EventRevoker _isRunningChangedRevoker; diff --git a/src/Magpie.App/NewProfileViewModel.cpp b/src/Magpie.App/NewProfileViewModel.cpp index d4cf009dd..7c93cee3d 100644 --- a/src/Magpie.App/NewProfileViewModel.cpp +++ b/src/Magpie.App/NewProfileViewModel.cpp @@ -114,9 +114,9 @@ void NewProfileViewModel::PrepareForOpen(uint32_t dpi, bool isLightTheme, CoreDi items.reserve(candidateWindows.size()); std::copy(candidateWindows.begin(), candidateWindows.end(), std::insert_iterator(items, items.begin())); _candidateWindows = single_threaded_vector(std::move(items)); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CandidateWindows")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsNoCandidateWindow")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsAnyCandidateWindow")); + RaisePropertyChanged(L"CandidateWindows"); + RaisePropertyChanged(L"IsNoCandidateWindow"); + RaisePropertyChanged(L"IsAnyCandidateWindow"); CandidateWindowIndex(-1); if (_candidateWindows.Size() == 1) { @@ -138,14 +138,14 @@ void NewProfileViewModel::PrepareForOpen(uint32_t dpi, bool isLightTheme, CoreDi } _profiles = single_threaded_vector(std::move(profiles)); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"Profiles")); + RaisePropertyChanged(L"Profiles"); ProfileIndex(0); } void NewProfileViewModel::CandidateWindowIndex(int value) { _candidateWindowIndex = value; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CandidateWindowIndex")); + RaisePropertyChanged(L"CandidateWindowIndex"); if (value >= 0) { Name(_candidateWindows.GetAt(value).as().DefaultProfileName()); @@ -156,7 +156,7 @@ void NewProfileViewModel::CandidateWindowIndex(int value) { void NewProfileViewModel::Name(const hstring& value) noexcept { _name = value; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"Name")); + RaisePropertyChanged(L"Name"); _IsConfirmButtonEnabled(!value.empty() && _candidateWindowIndex >= 0); } diff --git a/src/Magpie.App/NewProfileViewModel.h b/src/Magpie.App/NewProfileViewModel.h index 6ee57aabd..3e87bfddd 100644 --- a/src/Magpie.App/NewProfileViewModel.h +++ b/src/Magpie.App/NewProfileViewModel.h @@ -3,17 +3,10 @@ namespace winrt::Magpie::App::implementation { -struct NewProfileViewModel : NewProfileViewModelT { +struct NewProfileViewModel : NewProfileViewModelT, + wil::notify_property_changed_base { NewProfileViewModel() = default; - event_token PropertyChanged(PropertyChangedEventHandler const& handler) { - return _propertyChangedEvent.add(handler); - } - - void PropertyChanged(event_token const& token) noexcept { - _propertyChangedEvent.remove(token); - } - void PrepareForOpen(uint32_t dpi, bool isLightTheme, CoreDispatcher const& dispatcher); IVector CandidateWindows() const noexcept { @@ -42,7 +35,7 @@ struct NewProfileViewModel : NewProfileViewModelT { void ProfileIndex(int value) { _profileIndex = value; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ProfileIndex")); + RaisePropertyChanged(L"ProfileIndex"); } bool IsConfirmButtonEnabled() const noexcept { @@ -66,11 +59,9 @@ struct NewProfileViewModel : NewProfileViewModelT { } _isConfirmButtonEnabled = value; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsConfirmButtonEnabled")); + RaisePropertyChanged(L"IsConfirmButtonEnabled"); } - event _propertyChangedEvent; - IVector _candidateWindows{ nullptr }; int _candidateWindowIndex = -1; hstring _name; diff --git a/src/Magpie.App/PageFrame.cpp b/src/Magpie.App/PageFrame.cpp index 26cb034e4..a0389d60f 100644 --- a/src/Magpie.App/PageFrame.cpp +++ b/src/Magpie.App/PageFrame.cpp @@ -109,24 +109,24 @@ void PageFrame::_Update() { void PageFrame::_OnTitleChanged(DependencyObject const& sender, DependencyPropertyChangedEventArgs const&) { PageFrame* that = get_self(sender.as()); that->_Update(); - that->_propertyChangedEvent(*that, PropertyChangedEventArgs{ L"Title" }); + that->RaisePropertyChanged(L"Title"); } void PageFrame::_OnIconChanged(DependencyObject const& sender, DependencyPropertyChangedEventArgs const&) { PageFrame* that = get_self(sender.as()); that->_Update(); - that->_propertyChangedEvent(*that, PropertyChangedEventArgs{ L"Icon" }); + that->RaisePropertyChanged(L"Icon"); } void PageFrame::_OnHeaderActionChanged(DependencyObject const& sender, DependencyPropertyChangedEventArgs const&) { PageFrame* that = get_self(sender.as()); that->_Update(); - that->_propertyChangedEvent(*that, PropertyChangedEventArgs{ L"HeaderAction" }); + that->RaisePropertyChanged(L"HeaderAction"); } void PageFrame::_OnMainContentChanged(DependencyObject const& sender, DependencyPropertyChangedEventArgs const&) { PageFrame* that = get_self(sender.as()); - that->_propertyChangedEvent(*that, PropertyChangedEventArgs{ L"MainContent" }); + that->RaisePropertyChanged(L"MainContent"); } } diff --git a/src/Magpie.App/PageFrame.h b/src/Magpie.App/PageFrame.h index b46128f86..cac49e163 100644 --- a/src/Magpie.App/PageFrame.h +++ b/src/Magpie.App/PageFrame.h @@ -3,7 +3,7 @@ namespace winrt::Magpie::App::implementation { -struct PageFrame : PageFrameT { +struct PageFrame : PageFrameT, wil::notify_property_changed_base { void Title(const hstring& value) { SetValue(TitleProperty, box_value(value)); } @@ -46,14 +46,6 @@ struct PageFrame : PageFrameT { void ScrollViewer_ViewChanging(IInspectable const&, Controls::ScrollViewerViewChangingEventArgs const&); void ScrollViewer_KeyDown(IInspectable const& sender, Input::KeyRoutedEventArgs const& args); - event_token PropertyChanged(PropertyChangedEventHandler const& value) { - return _propertyChangedEvent.add(value); - } - - void PropertyChanged(event_token const& token) { - _propertyChangedEvent.remove(token); - } - static const DependencyProperty TitleProperty; static const DependencyProperty IconProperty; static const DependencyProperty HeaderActionProperty; @@ -66,8 +58,6 @@ struct PageFrame : PageFrameT { static void _OnMainContentChanged(DependencyObject const& sender, DependencyPropertyChangedEventArgs const&); void _Update(); - - event _propertyChangedEvent; }; } diff --git a/src/Magpie.App/ProfileViewModel.cpp b/src/Magpie.App/ProfileViewModel.cpp index 8c52a4792..2a15fb084 100644 --- a/src/Magpie.App/ProfileViewModel.cpp +++ b/src/Magpie.App/ProfileViewModel.cpp @@ -343,14 +343,14 @@ void ProfileViewModel::Launch() const noexcept { void ProfileViewModel::RenameText(const hstring& value) { _renameText = value; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"RenameText")); + RaisePropertyChanged(L"RenameText"); _trimedRenameText = value; StrUtils::Trim(_trimedRenameText); bool newEnabled = !_trimedRenameText.empty() && _trimedRenameText != _data->name; if (_isRenameConfirmButtonEnabled != newEnabled) { _isRenameConfirmButtonEnabled = newEnabled; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsRenameConfirmButtonEnabled")); + RaisePropertyChanged(L"IsRenameConfirmButtonEnabled"); } } @@ -360,7 +360,7 @@ void ProfileViewModel::Rename() { } ProfileService::Get().RenameProfile(_index, _trimedRenameText); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"Name")); + RaisePropertyChanged(L"Name"); } bool ProfileViewModel::CanMoveUp() const noexcept { @@ -384,8 +384,8 @@ void ProfileViewModel::MoveUp() { --_index; _data = &profileService.GetProfile(_index); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMoveUp")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMoveDown")); + RaisePropertyChanged(L"CanMoveUp"); + RaisePropertyChanged(L"CanMoveDown"); } void ProfileViewModel::MoveDown() { @@ -401,8 +401,8 @@ void ProfileViewModel::MoveDown() { ++_index; _data = &profileService.GetProfile(_index); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMoveUp")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMoveDown")); + RaisePropertyChanged(L"CanMoveUp"); + RaisePropertyChanged(L"CanMoveDown"); } void ProfileViewModel::Delete() { @@ -422,7 +422,7 @@ void ProfileViewModel::ScalingMode(int value) { _data->scalingMode = value - 1; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ScalingMode")); + RaisePropertyChanged(L"ScalingMode"); } int ProfileViewModel::CaptureMethod() const noexcept { @@ -448,8 +448,8 @@ void ProfileViewModel::CaptureMethod(int value) { } _data->captureMethod = captureMethod; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CaptureMethod")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanCaptureTitleBar")); + RaisePropertyChanged(L"CaptureMethod"); + RaisePropertyChanged(L"CanCaptureTitleBar"); AppSettings::Get().SaveAsync(); } @@ -466,7 +466,7 @@ void ProfileViewModel::IsAutoScale(bool value) { _data->isAutoScale = value; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsAutoScale")); + RaisePropertyChanged(L"IsAutoScale"); if (value) { // 立即检查前台窗口是否应自动缩放 @@ -486,7 +486,7 @@ void ProfileViewModel::Is3DGameMode(bool value) { _data->Is3DGameMode(value); AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"Is3DGameMode")); + RaisePropertyChanged(L"Is3DGameMode"); } bool ProfileViewModel::HasMultipleMonitors() const noexcept { @@ -510,7 +510,7 @@ void ProfileViewModel::MultiMonitorUsage(int value) { _data->multiMonitorUsage = multiMonitorUsage; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"MultiMonitorUsage")); + RaisePropertyChanged(L"MultiMonitorUsage"); } IVector ProfileViewModel::GraphicsCards() const noexcept { @@ -546,7 +546,7 @@ void ProfileViewModel::GraphicsCard(int value) { _data->graphicsCard = value; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"GraphicsCard")); + RaisePropertyChanged(L"GraphicsCard"); } bool ProfileViewModel::IsShowGraphicsCardSettingsCard() const noexcept { @@ -565,7 +565,7 @@ void ProfileViewModel::IsFrameRateLimiterEnabled(bool value) { _data->isFrameRateLimiterEnabled = value; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsFrameRateLimiterEnabled")); + RaisePropertyChanged(L"IsFrameRateLimiterEnabled"); } double ProfileViewModel::MaxFrameRate() const noexcept { @@ -581,7 +581,7 @@ void ProfileViewModel::MaxFrameRate(double value) { _data->maxFrameRate = std::isnan(value) ? 60.0f : (float)value; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"MaxFrameRate")); + RaisePropertyChanged(L"MaxFrameRate"); } bool ProfileViewModel::IsShowFPS() const noexcept { @@ -596,7 +596,7 @@ void ProfileViewModel::IsShowFPS(bool value) { _data->IsShowFPS(value); AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsShowFPS")); + RaisePropertyChanged(L"IsShowFPS"); } bool ProfileViewModel::IsWindowResizingDisabled() const noexcept { @@ -611,7 +611,7 @@ void ProfileViewModel::IsWindowResizingDisabled(bool value) { _data->IsWindowResizingDisabled(value); AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsWindowResizingDisabled")); + RaisePropertyChanged(L"IsWindowResizingDisabled"); } bool ProfileViewModel::IsCaptureTitleBar() const noexcept { @@ -626,7 +626,7 @@ void ProfileViewModel::IsCaptureTitleBar(bool value) { _data->IsCaptureTitleBar(value); AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsCaptureTitleBar")); + RaisePropertyChanged(L"IsCaptureTitleBar"); } bool ProfileViewModel::CanCaptureTitleBar() const noexcept { @@ -646,7 +646,7 @@ void ProfileViewModel::IsCroppingEnabled(bool value) { _data->isCroppingEnabled = value; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsCroppingEnabled")); + RaisePropertyChanged(L"IsCroppingEnabled"); } double ProfileViewModel::CroppingLeft() const noexcept { @@ -661,7 +661,7 @@ void ProfileViewModel::CroppingLeft(double value) { _data->cropping.Left = std::isnan(value) ? 0.0f : (float)value; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CroppingLeft")); + RaisePropertyChanged(L"CroppingLeft"); } double ProfileViewModel::CroppingTop() const noexcept { @@ -677,7 +677,7 @@ void ProfileViewModel::CroppingTop(double value) { _data->cropping.Top = std::isnan(value) ? 0.0f : (float)value; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CroppingTop")); + RaisePropertyChanged(L"CroppingTop"); } double ProfileViewModel::CroppingRight() const noexcept { @@ -692,7 +692,7 @@ void ProfileViewModel::CroppingRight(double value) { _data->cropping.Right = std::isnan(value) ? 0.0f : (float)value; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CroppingRight")); + RaisePropertyChanged(L"CroppingRight"); } double ProfileViewModel::CroppingBottom() const noexcept { @@ -707,7 +707,7 @@ void ProfileViewModel::CroppingBottom(double value) { _data->cropping.Bottom = std::isnan(value) ? 0.0f : (float)value; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CroppingBottom")); + RaisePropertyChanged(L"CroppingBottom"); } bool ProfileViewModel::IsAdjustCursorSpeed() const noexcept { @@ -722,7 +722,7 @@ void ProfileViewModel::IsAdjustCursorSpeed(bool value) { _data->IsAdjustCursorSpeed(value); AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsAdjustCursorSpeed")); + RaisePropertyChanged(L"IsAdjustCursorSpeed"); } bool ProfileViewModel::IsDrawCursor() const noexcept { @@ -737,7 +737,7 @@ void ProfileViewModel::IsDrawCursor(bool value) { _data->IsDrawCursor(value); AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsDrawCursor")); + RaisePropertyChanged(L"IsDrawCursor"); } int ProfileViewModel::CursorScaling() const noexcept { @@ -757,7 +757,7 @@ void ProfileViewModel::CursorScaling(int value) { _data->cursorScaling = cursorScaling; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CursorScaling")); + RaisePropertyChanged(L"CursorScaling"); } double ProfileViewModel::CustomCursorScaling() const noexcept { @@ -772,7 +772,7 @@ void ProfileViewModel::CustomCursorScaling(double value) { _data->customCursorScaling = std::isnan(value) ? 1.0f : (float)value; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CustomCursorScaling")); + RaisePropertyChanged(L"CustomCursorScaling"); } int ProfileViewModel::CursorInterpolationMode() const noexcept { @@ -792,7 +792,7 @@ void ProfileViewModel::CursorInterpolationMode(int value) { _data->cursorInterpolationMode = cursorInterpolationMode; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CursorInterpolationMode")); + RaisePropertyChanged(L"CursorInterpolationMode"); } hstring ProfileViewModel::LaunchParameters() const noexcept { @@ -805,7 +805,7 @@ void ProfileViewModel::LaunchParameters(const hstring& value) { _data->launchParameters = trimmed; AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"LaunchParameters")); + RaisePropertyChanged(L"LaunchParameters"); } bool ProfileViewModel::IsDirectFlipDisabled() const noexcept { @@ -820,7 +820,7 @@ void ProfileViewModel::IsDirectFlipDisabled(bool value) { _data->IsDirectFlipDisabled(value); AppSettings::Get().SaveAsync(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsDirectFlipDisabled")); + RaisePropertyChanged(L"IsDirectFlipDisabled"); } fire_and_forget ProfileViewModel::_LoadIcon(FrameworkElement const& rootPage) { @@ -879,7 +879,7 @@ fire_and_forget ProfileViewModel::_LoadIcon(FrameworkElement const& rootPage) { _icon = nullptr; } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"Icon")); + RaisePropertyChanged(L"Icon"); } } diff --git a/src/Magpie.App/ProfileViewModel.h b/src/Magpie.App/ProfileViewModel.h index 78ce07879..ad2bfc973 100644 --- a/src/Magpie.App/ProfileViewModel.h +++ b/src/Magpie.App/ProfileViewModel.h @@ -8,18 +8,11 @@ struct Profile; namespace winrt::Magpie::App::implementation { -struct ProfileViewModel : ProfileViewModelT { +struct ProfileViewModel : ProfileViewModelT, + wil::notify_property_changed_base { ProfileViewModel(int profileIdx); ~ProfileViewModel(); - event_token PropertyChanged(PropertyChangedEventHandler const& handler) { - return _propertyChangedEvent.add(handler); - } - - void PropertyChanged(event_token const& token) noexcept { - _propertyChangedEvent.remove(token); - } - Controls::IconElement Icon() const noexcept { return _icon; } @@ -159,8 +152,6 @@ struct ProfileViewModel : ProfileViewModelT { IVector _captureMethods{ nullptr }; SmallVector _graphicsCards; - event _propertyChangedEvent; - uint32_t _index = 0; // 可以保存此指针的原因是:用户停留在此页面时不会有缩放配置被创建或删除 Profile* _data = nullptr; diff --git a/src/Magpie.App/ScalingConfigurationViewModel.cpp b/src/Magpie.App/ScalingConfigurationViewModel.cpp index 2051e7837..9be27572d 100644 --- a/src/Magpie.App/ScalingConfigurationViewModel.cpp +++ b/src/Magpie.App/ScalingConfigurationViewModel.cpp @@ -130,19 +130,19 @@ void ScalingConfigurationViewModel::PrepareForAdd() { copyFromList.push_back(box_value(scalingMode.name)); } _newScalingModeCopyFromList = single_threaded_vector(std::move(copyFromList)); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"NewScalingModeCopyFromList")); + RaisePropertyChanged(L"NewScalingModeCopyFromList"); _newScalingModeName.clear(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"NewScalingModeName")); + RaisePropertyChanged(L"NewScalingModeName"); _newScalingModeCopyFrom = 0; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"NewScalingModeCopyFrom")); + RaisePropertyChanged(L"NewScalingModeCopyFrom"); } void ScalingConfigurationViewModel::NewScalingModeName(const hstring& value) noexcept { _newScalingModeName = value; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"NewScalingModeName")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsAddButtonEnabled")); + RaisePropertyChanged(L"NewScalingModeName"); + RaisePropertyChanged(L"IsAddButtonEnabled"); } void ScalingConfigurationViewModel::AddScalingMode() { diff --git a/src/Magpie.App/ScalingConfigurationViewModel.h b/src/Magpie.App/ScalingConfigurationViewModel.h index 7b0e4989d..e41976837 100644 --- a/src/Magpie.App/ScalingConfigurationViewModel.h +++ b/src/Magpie.App/ScalingConfigurationViewModel.h @@ -5,17 +5,10 @@ namespace winrt::Magpie::App::implementation { -struct ScalingConfigurationViewModel : ScalingConfigurationViewModelT { +struct ScalingConfigurationViewModel : ScalingConfigurationViewModelT, + wil::notify_property_changed_base { ScalingConfigurationViewModel(); - event_token PropertyChanged(PropertyChangedEventHandler const& handler) { - return _propertyChangedEvent.add(handler); - } - - void PropertyChanged(event_token const& token) noexcept { - _propertyChangedEvent.remove(token); - } - void Export() const noexcept; void Import() { @@ -32,7 +25,7 @@ struct ScalingConfigurationViewModel : ScalingConfigurationViewModelT ScalingModes() const noexcept { @@ -57,7 +50,7 @@ struct ScalingConfigurationViewModel : ScalingConfigurationViewModelT _propertyChangedEvent; - IObservableVector _scalingModes = single_threaded_observable_vector(); WinRTUtils::EventRevoker _scalingModeAddedRevoker; diff --git a/src/Magpie.App/ScalingModeEffectItem.cpp b/src/Magpie.App/ScalingModeEffectItem.cpp index d5b3ec13e..4b93fc936 100644 --- a/src/Magpie.App/ScalingModeEffectItem.cpp +++ b/src/Magpie.App/ScalingModeEffectItem.cpp @@ -128,20 +128,20 @@ void ScalingModeEffectItem::ScalingType(int value) { if (data.scalingType == MagpieCore::ScalingType::Absolute) { data.scale = { 1.0f,1.0f }; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ScalingFactorX")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ScalingFactorY")); + RaisePropertyChanged(L"ScalingFactorX"); + RaisePropertyChanged(L"ScalingFactorY"); } else if (scalingType == MagpieCore::ScalingType::Absolute) { SIZE monitorSize = GetMonitorSize(); data.scale = { (float)monitorSize.cx,(float)monitorSize.cy }; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ScalingPixelsX")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ScalingPixelsY")); + RaisePropertyChanged(L"ScalingPixelsX"); + RaisePropertyChanged(L"ScalingPixelsY"); } data.scalingType = scalingType; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ScalingType")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsShowScalingFactors")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsShowScalingPixels")); + RaisePropertyChanged(L"ScalingType"); + RaisePropertyChanged(L"IsShowScalingFactors"); + RaisePropertyChanged(L"IsShowScalingPixels"); AppSettings::Get().SaveAsync(); } @@ -170,7 +170,7 @@ void ScalingModeEffectItem::ScalingFactorX(double value) { data.scale.first = (float)value; } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ScalingFactorX")); + RaisePropertyChanged(L"ScalingFactorX"); AppSettings::Get().SaveAsync(); } @@ -188,7 +188,7 @@ void ScalingModeEffectItem::ScalingFactorY(double value) { data.scale.second = (float)value; } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ScalingFactorY")); + RaisePropertyChanged(L"ScalingFactorY"); AppSettings::Get().SaveAsync(); } @@ -206,7 +206,7 @@ void ScalingModeEffectItem::ScalingPixelsX(double value) { data.scale.first = (float)std::round(value); } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ScalingPixelsX")); + RaisePropertyChanged(L"ScalingPixelsX"); AppSettings::Get().SaveAsync(); } @@ -224,7 +224,7 @@ void ScalingModeEffectItem::ScalingPixelsY(double value) { data.scale.second = (float)std::round(value); } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"ScalingPixelsY")); + RaisePropertyChanged(L"ScalingPixelsY"); AppSettings::Get().SaveAsync(); } @@ -255,9 +255,9 @@ void ScalingModeEffectItem::MoveDown() noexcept { } void ScalingModeEffectItem::RefreshMoveState() { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMove")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMoveUp")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMoveDown")); + RaisePropertyChanged(L"CanMove"); + RaisePropertyChanged(L"CanMoveUp"); + RaisePropertyChanged(L"CanMoveDown"); } EffectOption& ScalingModeEffectItem::_Data() noexcept { diff --git a/src/Magpie.App/ScalingModeEffectItem.h b/src/Magpie.App/ScalingModeEffectItem.h index aa7b8ef8c..907987665 100644 --- a/src/Magpie.App/ScalingModeEffectItem.h +++ b/src/Magpie.App/ScalingModeEffectItem.h @@ -11,17 +11,10 @@ struct EffectInfo; namespace winrt::Magpie::App::implementation { -struct ScalingModeEffectItem : ScalingModeEffectItemT { +struct ScalingModeEffectItem : ScalingModeEffectItemT, + wil::notify_property_changed_base { ScalingModeEffectItem(uint32_t scalingModeIdx, uint32_t effectIdx); - event_token PropertyChanged(PropertyChangedEventHandler const& handler) { - return _propertyChangedEvent.add(handler); - } - - void PropertyChanged(event_token const& token) noexcept { - _propertyChangedEvent.remove(token); - } - hstring Name() const noexcept { return _name; } @@ -97,8 +90,6 @@ struct ScalingModeEffectItem : ScalingModeEffectItemT { ::Magpie::Core::EffectOption& _Data() noexcept; const ::Magpie::Core::EffectOption& _Data() const noexcept; - event _propertyChangedEvent; - uint32_t _scalingModeIdx = 0; uint32_t _effectIdx = 0; hstring _name; diff --git a/src/Magpie.App/ScalingModeItem.cpp b/src/Magpie.App/ScalingModeItem.cpp index 3b8c68cd6..dc07a6809 100644 --- a/src/Magpie.App/ScalingModeItem.cpp +++ b/src/Magpie.App/ScalingModeItem.cpp @@ -58,13 +58,13 @@ void ScalingModeItem::_Index(uint32_t value) noexcept { for (const IInspectable& item : _effects) { item.as().ScalingModeIdx(value); } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMoveUp")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMoveDown")); + RaisePropertyChanged(L"CanMoveUp"); + RaisePropertyChanged(L"CanMoveDown"); } void ScalingModeItem::_ScalingModesService_Added(EffectAddedWay) { if (_index + 2 == ScalingModesService::Get().GetScalingModeCount()) { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMoveDown")); + RaisePropertyChanged(L"CanMoveDown"); } } @@ -81,16 +81,16 @@ void ScalingModeItem::_ScalingModesService_Removed(uint32_t index) { if (_index > index) { _Index(_index - 1); } else { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMoveUp")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanMoveDown")); + RaisePropertyChanged(L"CanMoveUp"); + RaisePropertyChanged(L"CanMoveDown"); } } void ScalingModeItem::_Effects_VectorChanged(IObservableVector const&, IVectorChangedEventArgs const& args) { if (!_isMovingEffects) { - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"Description")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"CanReorderEffects")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsShowMoveButtons")); + RaisePropertyChanged(L"Description"); + RaisePropertyChanged(L"CanReorderEffects"); + RaisePropertyChanged(L"IsShowMoveButtons"); return; } @@ -121,7 +121,7 @@ void ScalingModeItem::_Effects_VectorChanged(IObservableVector con _effects.GetAt(i).as().EffectIdx(i); } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"Description")); + RaisePropertyChanged(L"Description"); AppSettings::Get().SaveAsync(); } @@ -144,7 +144,7 @@ void ScalingModeItem::_ScalingModeEffectItem_Removed(IInspectable const&, uint32 _effects.GetAt(index).as().RefreshMoveState(); } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"HasUnkownEffects")); + RaisePropertyChanged(L"HasUnkownEffects"); AppSettings::Get().SaveAsync(); } @@ -252,20 +252,20 @@ bool ScalingModeItem::HasUnkownEffects() const noexcept { void ScalingModeItem::RenameText(const hstring& value) noexcept { _renameText = value; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"RenameText")); + RaisePropertyChanged(L"RenameText"); _trimedRenameText = value; StrUtils::Trim(_trimedRenameText); bool newEnabled = !_trimedRenameText.empty() && _trimedRenameText != _Data().name; if (_isRenameButtonEnabled != newEnabled) { _isRenameButtonEnabled = newEnabled; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsRenameButtonEnabled")); + RaisePropertyChanged(L"IsRenameButtonEnabled"); } } void ScalingModeItem::RenameFlyout_Opening() { RenameText(hstring(_Data().name)); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"RenameTextBoxSelectionStart")); + RaisePropertyChanged(L"RenameTextBoxSelectionStart"); } void ScalingModeItem::RenameTextBox_KeyDown(IInspectable const&, Input::KeyRoutedEventArgs const& args) { @@ -283,7 +283,7 @@ void ScalingModeItem::RenameButton_Click() { XamlUtils::ClosePopups(Application::Current().as().RootPage().XamlRoot()); _Data().name = _trimedRenameText; - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"Name")); + RaisePropertyChanged(L"Name"); AppSettings::Get().SaveAsync(); } diff --git a/src/Magpie.App/ScalingModeItem.h b/src/Magpie.App/ScalingModeItem.h index 2f782b5eb..a773c9e13 100644 --- a/src/Magpie.App/ScalingModeItem.h +++ b/src/Magpie.App/ScalingModeItem.h @@ -9,17 +9,10 @@ struct ScalingMode; namespace winrt::Magpie::App::implementation { -struct ScalingModeItem : ScalingModeItemT { +struct ScalingModeItem : ScalingModeItemT, + wil::notify_property_changed_base { ScalingModeItem(uint32_t index, bool isInitialExpanded); - event_token PropertyChanged(PropertyChangedEventHandler const& handler) { - return _propertyChangedEvent.add(handler); - } - - void PropertyChanged(event_token const& token) noexcept { - _propertyChangedEvent.remove(token); - } - void AddEffect(const hstring& fullName); bool IsInitialExpanded() const noexcept { @@ -100,8 +93,6 @@ struct ScalingModeItem : ScalingModeItemT { ScalingMode& _Data() noexcept; const ScalingMode& _Data() const noexcept; - event _propertyChangedEvent; - uint32_t _index = 0; IObservableVector _effects{ nullptr }; diff --git a/src/Magpie.App/SettingsViewModel.cpp b/src/Magpie.App/SettingsViewModel.cpp index a7d11402a..e68822e7b 100644 --- a/src/Magpie.App/SettingsViewModel.cpp +++ b/src/Magpie.App/SettingsViewModel.cpp @@ -42,8 +42,8 @@ void SettingsViewModel::Language(int value) { } AppSettings::Get().Language(value - 1); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"Language")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"RequireRestart")); + RaisePropertyChanged(L"Language"); + RaisePropertyChanged(L"RequireRestart"); } bool SettingsViewModel::RequireRestart() const noexcept { @@ -87,7 +87,7 @@ void SettingsViewModel::Theme(int value) { } AppSettings::Get().Theme(theme); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"Theme")); + RaisePropertyChanged(L"Theme"); } void SettingsViewModel::IsRunAtStartup(bool value) { @@ -102,7 +102,7 @@ void SettingsViewModel::IsRunAtStartup(bool value) { _UpdateStartupOptions(); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsMinimizeAtStartupEnabled")); + RaisePropertyChanged(L"IsMinimizeAtStartupEnabled"); } void SettingsViewModel::IsMinimizeAtStartup(bool value) { @@ -134,7 +134,7 @@ void SettingsViewModel::IsPortableMode(bool value) { } settings.IsPortableMode(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsPortableMode")); + RaisePropertyChanged(L"IsPortableMode"); } fire_and_forget SettingsViewModel::OpenConfigLocation() const noexcept { @@ -149,14 +149,14 @@ bool SettingsViewModel::IsShowNotifyIcon() const noexcept { void SettingsViewModel::IsShowNotifyIcon(bool value) { AppSettings::Get().IsShowNotifyIcon(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsShowNotifyIcon")); + RaisePropertyChanged(L"IsShowNotifyIcon"); if (_isRunAtStartup) { AutoStartHelper::EnableAutoStart(AppSettings::Get().IsAlwaysRunAsAdmin(), nullptr); _UpdateStartupOptions(); } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsMinimizeAtStartupEnabled")); + RaisePropertyChanged(L"IsMinimizeAtStartupEnabled"); } bool SettingsViewModel::IsProcessElevated() const noexcept { @@ -195,7 +195,7 @@ void SettingsViewModel::IsSimulateExclusiveFullscreen(bool value) { } settings.IsSimulateExclusiveFullscreen(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsSimulateExclusiveFullscreen")); + RaisePropertyChanged(L"IsSimulateExclusiveFullscreen"); } bool SettingsViewModel::IsInlineParams() const noexcept { @@ -210,7 +210,7 @@ void SettingsViewModel::IsInlineParams(bool value) { } settings.IsInlineParams(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsInlineParams")); + RaisePropertyChanged(L"IsInlineParams"); } bool SettingsViewModel::IsDeveloperMode() const noexcept { @@ -225,7 +225,7 @@ void SettingsViewModel::IsDeveloperMode(bool value) { } settings.IsDeveloperMode(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsDeveloperMode")); + RaisePropertyChanged(L"IsDeveloperMode"); } bool SettingsViewModel::IsDebugMode() const noexcept { @@ -240,7 +240,7 @@ void SettingsViewModel::IsDebugMode(bool value) { } settings.IsDebugMode(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsDebugMode")); + RaisePropertyChanged(L"IsDebugMode"); } bool SettingsViewModel::IsEffectCacheDisabled() const noexcept { @@ -255,7 +255,7 @@ void SettingsViewModel::IsEffectCacheDisabled(bool value) { } settings.IsEffectCacheDisabled(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsEffectCacheDisabled")); + RaisePropertyChanged(L"IsEffectCacheDisabled"); } bool SettingsViewModel::IsFontCacheDisabled() const noexcept { @@ -270,7 +270,7 @@ void SettingsViewModel::IsFontCacheDisabled(bool value) { } settings.IsFontCacheDisabled(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsFontCacheDisabled")); + RaisePropertyChanged(L"IsFontCacheDisabled"); } bool SettingsViewModel::IsSaveEffectSources() const noexcept { @@ -285,7 +285,7 @@ void SettingsViewModel::IsSaveEffectSources(bool value) { } settings.IsSaveEffectSources(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsSaveEffectSources")); + RaisePropertyChanged(L"IsSaveEffectSources"); } bool SettingsViewModel::IsWarningsAreErrors() const noexcept { @@ -300,7 +300,7 @@ void SettingsViewModel::IsWarningsAreErrors(bool value) { } settings.IsWarningsAreErrors(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsWarningsAreErrors")); + RaisePropertyChanged(L"IsWarningsAreErrors"); } int SettingsViewModel::DuplicateFrameDetectionMode() const noexcept { @@ -321,12 +321,12 @@ void SettingsViewModel::DuplicateFrameDetectionMode(int value) { settings.DuplicateFrameDetectionMode(mode); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"DuplicateFrameDetectionMode")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsDynamicDection")); + RaisePropertyChanged(L"DuplicateFrameDetectionMode"); + RaisePropertyChanged(L"IsDynamicDection"); if (mode != ::Magpie::Core::DuplicateFrameDetectionMode::Dynamic) { settings.IsStatisticsForDynamicDetectionEnabled(false); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsStatisticsForDynamicDetectionEnabled")); + RaisePropertyChanged(L"IsStatisticsForDynamicDetectionEnabled"); } } @@ -346,7 +346,7 @@ void SettingsViewModel::IsStatisticsForDynamicDetectionEnabled(bool value) { } settings.IsStatisticsForDynamicDetectionEnabled(value); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsStatisticsForDynamicDetectionEnabled")); + RaisePropertyChanged(L"IsStatisticsForDynamicDetectionEnabled"); } void SettingsViewModel::_UpdateStartupOptions() { @@ -358,8 +358,8 @@ void SettingsViewModel::_UpdateStartupOptions() { _isMinimizeAtStartup = false; } - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsRunAtStartup")); - _propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsMinimizeAtStartup")); + RaisePropertyChanged(L"IsRunAtStartup"); + RaisePropertyChanged(L"IsMinimizeAtStartup"); } } diff --git a/src/Magpie.App/SettingsViewModel.h b/src/Magpie.App/SettingsViewModel.h index f3556f31a..971847e2e 100644 --- a/src/Magpie.App/SettingsViewModel.h +++ b/src/Magpie.App/SettingsViewModel.h @@ -3,7 +3,8 @@ namespace winrt::Magpie::App::implementation { -struct SettingsViewModel : SettingsViewModelT { +struct SettingsViewModel : SettingsViewModelT, + wil::notify_property_changed_base { SettingsViewModel(); IVector Languages() const; @@ -79,19 +80,9 @@ struct SettingsViewModel : SettingsViewModelT { bool IsStatisticsForDynamicDetectionEnabled() const noexcept; void IsStatisticsForDynamicDetectionEnabled(bool value); - event_token PropertyChanged(PropertyChangedEventHandler const& handler) { - return _propertyChangedEvent.add(handler); - } - - void PropertyChanged(event_token const& token) { - _propertyChangedEvent.remove(token); - } - private: void _UpdateStartupOptions(); - event _propertyChangedEvent; - bool _isRunAtStartup = false; bool _isMinimizeAtStartup = false; }; diff --git a/src/Magpie.App/TitlebarControl.cpp b/src/Magpie.App/TitlebarControl.cpp index b1f91bf82..f1eb28d6f 100644 --- a/src/Magpie.App/TitlebarControl.cpp +++ b/src/Magpie.App/TitlebarControl.cpp @@ -25,7 +25,7 @@ TitleBarControl::TitleBarControl() { } that->_logo = std::move(bitmap); - that->_propertyChangedEvent(*that, PropertyChangedEventArgs(L"Logo")); + that->RaisePropertyChanged(L"Logo"); }(this); } diff --git a/src/Magpie.App/TitlebarControl.h b/src/Magpie.App/TitlebarControl.h index fe7e63a93..ac922ad68 100644 --- a/src/Magpie.App/TitlebarControl.h +++ b/src/Magpie.App/TitlebarControl.h @@ -2,7 +2,8 @@ #include "TitleBarControl.g.h" namespace winrt::Magpie::App::implementation { -struct TitleBarControl : TitleBarControlT { +struct TitleBarControl : TitleBarControlT, + wil::notify_property_changed_base { TitleBarControl(); void Loading(FrameworkElement const&, IInspectable const&); @@ -11,19 +12,10 @@ struct TitleBarControl : TitleBarControlT { return _logo; } - event_token PropertyChanged(PropertyChangedEventHandler const& value) { - return _propertyChangedEvent.add(value); - } - - void PropertyChanged(event_token const& token) { - _propertyChangedEvent.remove(token); - } - void IsWindowActive(bool value); private: Imaging::SoftwareBitmapSource _logo{ nullptr }; - event _propertyChangedEvent; }; } diff --git a/src/Magpie.App/pch.h b/src/Magpie.App/pch.h index e6ce05738..ca4e99c1c 100644 --- a/src/Magpie.App/pch.h +++ b/src/Magpie.App/pch.h @@ -35,6 +35,8 @@ #include #include +#include + namespace winrt { using namespace Windows::UI::Core; using namespace Windows::UI::Xaml::Data; diff --git a/src/Magpie/MainWindow.cpp b/src/Magpie/MainWindow.cpp index 8a6b2a431..0974af491 100644 --- a/src/Magpie/MainWindow.cpp +++ b/src/Magpie/MainWindow.cpp @@ -64,14 +64,12 @@ bool MainWindow::Create(HINSTANCE hInstance, winrt::Point windowCenter, winrt::S // 在此过程中,_isMaximized 始终是 true。 // 保存原始窗口化位置 - WINDOWPLACEMENT wp{}; - wp.length = sizeof(wp); + WINDOWPLACEMENT wp{ .length = sizeof(wp) }; GetWindowPlacement(Handle(), &wp); // 查询最大化窗口位置 if (HMONITOR hMon = MonitorFromWindow(Handle(), MONITOR_DEFAULTTONEAREST)) { - MONITORINFO mi{}; - mi.cbSize = sizeof(mi); + MONITORINFO mi{ .cbSize = sizeof(mi) }; GetMonitorInfo(hMon, &mi); // 播放窗口显示动画 @@ -257,7 +255,7 @@ std::pair MainWindow::_CreateWindow(HINSTANCE hInstance, winrt::Poi windowSize.cx = std::lroundf(windowSizeInPixels.Width); windowSize.cy = std::lroundf(windowSizeInPixels.Height); - MONITORINFO mi{ sizeof(mi) }; + MONITORINFO mi{ .cbSize = sizeof(mi) }; GetMonitorInfo(hMon, &mi); // 确保启动位置在屏幕工作区内。不允许启动时跨越多个屏幕 @@ -295,7 +293,7 @@ std::pair MainWindow::_CreateWindow(HINSTANCE hInstance, winrt::Poi if (windowSize.cx == 0) { const HMONITOR hMon = MonitorFromWindow(Handle(), MONITOR_DEFAULTTONEAREST); - MONITORINFO mi{ sizeof(mi) }; + MONITORINFO mi{ .cbSize = sizeof(mi) }; GetMonitorInfo(hMon, &mi); const float dpiFactor = _CurrentDpi() / float(USER_DEFAULT_SCREEN_DPI); diff --git a/src/Magpie/XamlApp.cpp b/src/Magpie/XamlApp.cpp index 30f44d8e7..620cbdc0b 100644 --- a/src/Magpie/XamlApp.cpp +++ b/src/Magpie/XamlApp.cpp @@ -16,8 +16,8 @@ static const UINT WM_MAGPIE_QUIT = RegisterWindowMessage(CommonSharedConstants:: // 来自 https://github.com/CommunityToolkit/Microsoft.Toolkit.Win32/blob/6fb2c3e00803ea563af20f6bc9363091b685d81f/Microsoft.Toolkit.Win32.UI.XamlApplication/XamlApplication.cpp#L140 // 参见:https://github.com/microsoft/microsoft-ui-xaml/issues/7260#issuecomment-1231314776 static void FixThreadPoolCrash() noexcept { - LoadLibraryEx(L"twinapi.appcore.dll", nullptr, 0); - LoadLibraryEx(L"threadpoolwinrt.dll", nullptr, 0); + LoadLibraryEx(L"twinapi.appcore.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + LoadLibraryEx(L"threadpoolwinrt.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); } bool XamlApp::Initialize(HINSTANCE hInstance, const wchar_t* arguments) { @@ -113,14 +113,15 @@ void XamlApp::Restart(bool asElevated, const wchar_t* arguments) noexcept { // 提前释放锁 _ReleaseMutexes(); - SHELLEXECUTEINFO execInfo{}; - execInfo.cbSize = sizeof(execInfo); - execInfo.lpFile = L"Magpie.exe"; - execInfo.lpParameters = arguments; - execInfo.lpVerb = asElevated ? L"runas" : L"open"; // 调用 ShellExecuteEx 后立即退出,因此应该指定 SEE_MASK_NOASYNC - execInfo.fMask = SEE_MASK_NOASYNC; - execInfo.nShow = SW_SHOWNORMAL; + SHELLEXECUTEINFO execInfo = { + .cbSize = sizeof(execInfo), + .fMask = SEE_MASK_NOASYNC, + .lpVerb = asElevated ? L"runas" : L"open", + .lpFile = Win32Utils::GetExePath().c_str(), + .lpParameters = arguments, + .nShow = SW_SHOWNORMAL + }; if (!ShellExecuteEx(&execInfo)) { Logger::Get().Win32Error("ShellExecuteEx 失败"); @@ -130,8 +131,7 @@ void XamlApp::Restart(bool asElevated, const wchar_t* arguments) noexcept { void XamlApp::SaveSettings() { if (_mainWindow && NotifyIconService::Get().IsShow()) { - WINDOWPLACEMENT wp{}; - wp.length = sizeof(wp); + WINDOWPLACEMENT wp{ .length = sizeof(wp) }; if (GetWindowPlacement(_mainWindow.Handle(), &wp)) { _mainWindowCenter = { (wp.rcNormalPosition.left + wp.rcNormalPosition.right) / 2.0f, diff --git a/src/Magpie/XamlWindow.h b/src/Magpie/XamlWindow.h index d1db58fc9..11c282d99 100644 --- a/src/Magpie/XamlWindow.h +++ b/src/Magpie/XamlWindow.h @@ -202,8 +202,7 @@ class XamlWindowT { // 如果有自动隐藏的任务栏,我们在它的方向稍微减小客户区,这样用户就可以用鼠标呼出任务栏 if (HMONITOR hMon = MonitorFromWindow(_hWnd, MONITOR_DEFAULTTONEAREST)) { - MONITORINFO monInfo{}; - monInfo.cbSize = sizeof(MONITORINFO); + MONITORINFO monInfo{ .cbSize = sizeof(MONITORINFO) }; GetMonitorInfo(hMon, &monInfo); // 检查是否有自动隐藏的任务栏 @@ -307,7 +306,10 @@ class XamlWindowT { // 这里我们想要黑色背景而不是原始边框 // hack 来自 https://github.com/microsoft/terminal/blob/0ee2c74cd432eda153f3f3e77588164cde95044f/src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp#L1030-L1047 HDC opaqueDc; - BP_PAINTPARAMS params = { sizeof(params), BPPF_NOCLIP | BPPF_ERASE }; + BP_PAINTPARAMS params = { + .cbSize = sizeof(params), + .dwFlags = BPPF_NOCLIP | BPPF_ERASE + }; HPAINTBUFFER buf = BeginBufferedPaint(hdc, &rcRest, BPBF_TOPDOWNDIB, ¶ms, &opaqueDc); if (buf && opaqueDc) { FillRect(opaqueDc, &rcRest, backgroundBrush);