Skip to content

Commit

Permalink
刚显示窗口时跳过动画 (#1040)
Browse files Browse the repository at this point in the history
* feat: 初步实现启动时跳过动画

* perf: 尝试优化性能

* ui: 优化 SettingsExpander 箭头动画

* perf: 优化启动性能

* fix: 优化最大化显示窗口逻辑

* chore: 优化注释

* chore
  • Loading branch information
Blinue authored Dec 26, 2024
1 parent 6fd97a4 commit ed61b66
Show file tree
Hide file tree
Showing 28 changed files with 161 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/Magpie/CandidateWindowItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct CandidateWindowItem : CandidateWindowItemT<CandidateWindowItem>,
return _title;
}

Controls::IconElement Icon() const noexcept;
IconElement Icon() const noexcept;

hstring DefaultProfileName() const noexcept {
return _defaultProfileName;
Expand All @@ -33,7 +33,7 @@ struct CandidateWindowItem : CandidateWindowItemT<CandidateWindowItem>,
fire_and_forget _ResolveWindow(bool resolveIcon, bool resolveName, HWND hWnd);

hstring _title;
Controls::IconElement _icon{ nullptr };
IconElement _icon{ nullptr };
hstring _defaultProfileName;

hstring _aumid;
Expand Down
2 changes: 1 addition & 1 deletion src/Magpie/ContentDialogHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Magpie {

struct ContentDialogHelper {
static winrt::IAsyncOperation<winrt::Controls::ContentDialogResult> ShowAsync(winrt::Controls::ContentDialog dialog);
static winrt::IAsyncOperation<winrt::ContentDialogResult> ShowAsync(winrt::ContentDialog dialog);
static bool IsAnyDialogOpen() noexcept;
static void CloseActiveDialog();
};
Expand Down
4 changes: 2 additions & 2 deletions src/Magpie/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace winrt::Magpie::implementation {

void HomePage::TimerSlider_Loaded(IInspectable const& sender, RoutedEventArgs const&) const {
// 修正 Slider 中 Tooltip 的主题
XamlHelper::UpdateThemeOfTooltips(sender.as<Controls::Slider>(), ActualTheme());
XamlHelper::UpdateThemeOfTooltips(sender.as<Slider>(), ActualTheme());
}

void HomePage::ComboBox_DropDownOpened(IInspectable const& sender, IInspectable const&) const {
Expand All @@ -23,7 +23,7 @@ void HomePage::ComboBox_DropDownOpened(IInspectable const& sender, IInspectable
void HomePage::SimulateExclusiveFullscreenToggleSwitch_Toggled(IInspectable const& sender, RoutedEventArgs const&) {
// 如果没有启用开发者模式,模拟独占全屏选项位于页面底部,用户可能注意不到警告。
// 为了解决这个问题,打开模拟独占全屏选项时自动滚动页面。
if (_viewModel->IsDeveloperMode() || !sender.as<Controls::ToggleSwitch>().IsOn() || !IsLoaded()) {
if (_viewModel->IsDeveloperMode() || !sender.as<ToggleSwitch>().IsOn() || !IsLoaded()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Magpie/KeyVisual.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct KeyVisual : KeyVisualT<KeyVisual>, wil::notify_property_changed_base<KeyV
winrt::Magpie::VisualType _visualType = winrt::Magpie::VisualType::Small;
bool _isError = false;

Controls::ContentPresenter _keyPresenter{ nullptr };
ContentPresenter _keyPresenter{ nullptr };
IsEnabledChanged_revoker _isEnabledChangedRevoker;
};

Expand Down
13 changes: 6 additions & 7 deletions src/Magpie/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ bool MainWindow::Create() noexcept {
_appThemeChangedRevoker = App::Get().ThemeChanged(winrt::auto_revoke, [this](bool) { _UpdateTheme(); });
_UpdateTheme();

const bool isMaximized = AppSettings::Get().IsMainWindowMaximized();
if (isMaximized) {
_SetInitialMaximized();
}
_SetInitialMaximized(AppSettings::Get().IsMainWindowMaximized());

// 1. 设置初始 XAML Islands 窗口的尺寸
// 2. 刷新窗口边框
Expand All @@ -62,8 +59,8 @@ 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&) {
if (isMaximized) {
Content()->Loaded([this](winrt::IInspectable const&, winrt::RoutedEventArgs const&) {
if (AppSettings::Get().IsMainWindowMaximized()) {
// ShowWindow(Handle(), SW_SHOWMAXIMIZED) 会显示错误的动画。因此我们以窗口化显示,
// 但位置和大小都和最大化相同,显示完毕后将状态设为最大化。
//
Expand All @@ -86,13 +83,15 @@ bool MainWindow::Create() noexcept {
mi.rcWork.top,
mi.rcMonitor.right - mi.rcMonitor.left,
mi.rcMonitor.bottom - mi.rcMonitor.top,
SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW
SWP_SHOWWINDOW
);
}

// 将状态设为最大化,也还原了原始的窗口化位置
wp.showCmd = SW_SHOWMAXIMIZED;
SetWindowPlacement(Handle(), &wp);

_SetInitialMaximized(false);
} else {
ShowWindow(Handle(), SW_SHOWNORMAL);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Magpie/PageFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

using namespace ::Magpie;
using namespace winrt;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Text;
Expand All @@ -31,7 +30,7 @@ void PageFrame::Title(hstring value) {
RaisePropertyChanged(L"Title");
}

void PageFrame::Icon(Controls::IconElement value) {
void PageFrame::Icon(IconElement value) {
if (_icon == value) {
return;
}
Expand Down Expand Up @@ -101,7 +100,7 @@ void PageFrame::ScrollViewer_ViewChanging(IInspectable const&, ScrollViewerViewC
}

void PageFrame::ScrollViewer_KeyDown(IInspectable const& sender, KeyRoutedEventArgs const& args) {
auto scrollViewer = sender.as<Controls::ScrollViewer>();
auto scrollViewer = sender.as<struct ScrollViewer>();
switch (args.Key()) {
case VirtualKey::Up:
scrollViewer.ChangeView(scrollViewer.HorizontalOffset(), scrollViewer.VerticalOffset() - 100, 1);
Expand Down
8 changes: 4 additions & 4 deletions src/Magpie/PageFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ struct PageFrame : PageFrameT<PageFrame>, wil::notify_property_changed_base<Page
hstring Title() const noexcept { return _title; }
void Title(hstring value);

Controls::IconElement Icon() const { return _icon; }
void Icon(Controls::IconElement value);
IconElement Icon() const { return _icon; }
void Icon(IconElement value);

FrameworkElement HeaderAction() const { return _headerAction; }
void HeaderAction(FrameworkElement value);
Expand All @@ -23,15 +23,15 @@ struct PageFrame : PageFrameT<PageFrame>, wil::notify_property_changed_base<Page
void SizeChanged(IInspectable const&, SizeChangedEventArgs const& e);

void ScrollViewer_PointerPressed(IInspectable const&, Input::PointerRoutedEventArgs const&);
void ScrollViewer_ViewChanging(IInspectable const&, Controls::ScrollViewerViewChangingEventArgs const&);
void ScrollViewer_ViewChanging(IInspectable const&, ScrollViewerViewChangingEventArgs const&);
void ScrollViewer_KeyDown(IInspectable const& sender, Input::KeyRoutedEventArgs const& args);

private:
void _UpdateIconContainer();
void _UpdateHeaderActionPresenter();

hstring _title;
Controls::IconElement _icon{ nullptr };
IconElement _icon{ nullptr };
FrameworkElement _headerAction{ nullptr };
IInspectable _mainContent{ nullptr };
};
Expand Down
1 change: 0 additions & 1 deletion src/Magpie/ProfilePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using namespace Magpie;
using namespace winrt;
using namespace Windows::Globalization::NumberFormatting;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Input;

Expand Down
2 changes: 1 addition & 1 deletion src/Magpie/ProfilePage.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct ProfilePage : ProfilePageT<ProfilePage> {

void ComboBox_DropDownOpened(IInspectable const& sender, IInspectable const&);

void CursorScalingComboBox_SelectionChanged(IInspectable const&, Controls::SelectionChangedEventArgs const&);
void CursorScalingComboBox_SelectionChanged(IInspectable const&, SelectionChangedEventArgs const&);

static Windows::Globalization::NumberFormatting::INumberFormatter2 NumberFormatter() noexcept;

Expand Down
4 changes: 2 additions & 2 deletions src/Magpie/ProfileViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct ProfileViewModel : ProfileViewModelT<ProfileViewModel>,
ProfileViewModel(int profileIdx);
~ProfileViewModel();

Controls::IconElement Icon() const noexcept {
IconElement Icon() const noexcept {
return _icon;
}

Expand Down Expand Up @@ -164,7 +164,7 @@ struct ProfileViewModel : ProfileViewModelT<ProfileViewModel>,
::Magpie::Event<uint32_t>::EventRevoker _dpiChangedRevoker;
::Magpie::Event<>::EventRevoker _adaptersChangedRevoker;

Controls::IconElement _icon{ nullptr };
IconElement _icon{ nullptr };

const bool _isDefaultProfile = true;
bool _isRenameConfirmButtonEnabled = false;
Expand Down
35 changes: 34 additions & 1 deletion src/Magpie/RootPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ using namespace Windows::Graphics::Display;
using namespace Windows::Graphics::Imaging;
using namespace Windows::UI::ViewManagement;
using namespace Windows::UI;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media::Animation;
using namespace Windows::UI::Xaml::Media::Imaging;

namespace winrt::Magpie::implementation {
Expand Down Expand Up @@ -86,6 +86,18 @@ void RootPage::InitializeComponent() {
}
}

static void SkipToggleSwitchAnimations(const DependencyObject& elem) {
FrameworkElement rootGrid = VisualTreeHelper::GetChild(elem, 0).try_as<FrameworkElement>();

for (VisualStateGroup group : VisualStateManager::GetVisualStateGroups(rootGrid)) {
for (VisualState state : group.States()) {
if (Storyboard storyboard = state.Storyboard()) {
storyboard.SkipToFill();
}
}
}
}

void RootPage::RootPage_Loaded(IInspectable const&, RoutedEventArgs const&) {
// 消除焦点框
IsTabStop(true);
Expand All @@ -94,6 +106,27 @@ void RootPage::RootPage_Loaded(IInspectable const&, RoutedEventArgs const&) {

// 设置 NavigationView 内的 Tooltip 的主题
XamlHelper::UpdateThemeOfTooltips(RootNavigationView(), ActualTheme());

// 启动时跳过 ToggleSwitch 的动画
std::vector<DependencyObject> elems{ *this };
do {
std::vector<DependencyObject> temp;

for (const DependencyObject& elem : elems) {
const int count = VisualTreeHelper::GetChildrenCount(elem);
for (int i = 0; i < count; ++i) {
DependencyObject current = VisualTreeHelper::GetChild(elem, i);

if (get_class_name(current) == name_of<ToggleSwitch>()) {
SkipToggleSwitchAnimations(current);
} else {
temp.emplace_back(std::move(current));
}
}
}

elems = std::move(temp);
} while (!elems.empty());
}

void RootPage::NavigationView_SelectionChanged(
Expand Down
1 change: 0 additions & 1 deletion src/Magpie/ScalingModesPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using namespace ::Magpie;
using namespace winrt;
using namespace Windows::Globalization::NumberFormatting;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Input;

Expand Down
2 changes: 1 addition & 1 deletion src/Magpie/ScalingModesPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct ScalingModesPage : ScalingModesPageT<ScalingModesPage> {

IInspectable _moreOptionsButton{ nullptr };

Controls::MenuFlyout _addEffectMenuFlyout;
MenuFlyout _addEffectMenuFlyout;
com_ptr<ScalingModesViewModel> _viewModel = make_self<ScalingModesViewModel>();
ScalingModeItem* _curScalingMode = nullptr;
};
Expand Down
8 changes: 4 additions & 4 deletions src/Magpie/SettingsCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ struct SettingsCard : SettingsCardT<SettingsCard> {
IInspectable Description() const { return GetValue(_descriptionProperty); }
void Description(IInspectable const& value) const { SetValue(_descriptionProperty, value); }

Controls::IconElement HeaderIcon() const { return GetValue(_headerIconProperty).as<Controls::IconElement>(); }
void HeaderIcon(Controls::IconElement const& value) const { SetValue(_headerIconProperty, value); }
IconElement HeaderIcon() const { return GetValue(_headerIconProperty).as<IconElement>(); }
void HeaderIcon(IconElement const& value) const { SetValue(_headerIconProperty, value); }

Controls::IconElement ActionIcon() const { return GetValue(_actionIconProperty).as<Controls::IconElement>(); }
void ActionIcon(Controls::IconElement const& value) const { SetValue(_actionIconProperty, value); }
IconElement ActionIcon() const { return GetValue(_actionIconProperty).as<IconElement>(); }
void ActionIcon(IconElement const& value) const { SetValue(_actionIconProperty, value); }

hstring ActionIconToolTip() const { return GetValue(_actionIconToolTipProperty).as<hstring>(); }
void ActionIconToolTip(const hstring& value) const { SetValue(_actionIconToolTipProperty, box_value(value)); }
Expand Down
Loading

0 comments on commit ed61b66

Please sign in to comment.