Skip to content

Commit

Permalink
perf: 尝试优化性能
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Dec 25, 2024
1 parent b26ad25 commit 2d64597
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/Magpie/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ bool MainWindow::Create() noexcept {
GetMonitorInfo(hMon, &mi);

// 播放窗口显示动画
SetWindowPos(
/*SetWindowPos(
Handle(),
NULL,
mi.rcWork.left,
mi.rcWork.top,
mi.rcMonitor.right - mi.rcMonitor.left,
mi.rcMonitor.bottom - mi.rcMonitor.top,
SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW
);
);*/
}

// 将状态设为最大化,也还原了原始的窗口化位置
Expand Down
59 changes: 48 additions & 11 deletions src/Magpie/RootPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,19 @@ void RootPage::InitializeComponent() {
}
}

void RootPage::RootPage_Loaded(IInspectable const&, RoutedEventArgs const&) {
// 消除焦点框
IsTabStop(true);
Focus(FocusState::Programmatic);
IsTabStop(false);
template<typename Fn>
static int Measure1(const Fn& func) noexcept {
using namespace std::chrono;

// 设置 NavigationView 内的 Tooltip 的主题
XamlHelper::UpdateThemeOfTooltips(RootNavigationView(), ActualTheme());
auto t = steady_clock::now();
func();
auto dura = duration_cast<microseconds>(steady_clock::now() - t);

// 启动时跳过所有动画,比如 ToggleSwitch 和 NavigationView 的动画
std::vector<DependencyObject> elems{ *this };
return int(dura.count());
}

static void SkipAnimations(const DependencyObject& root) {
std::vector<DependencyObject> elems{ root };
do {
std::vector<DependencyObject> temp;

Expand All @@ -112,11 +114,11 @@ void RootPage::RootPage_Loaded(IInspectable const&, RoutedEventArgs const&) {
}
}

for (VisualTransition transition : group.Transitions()) {
/*for (VisualTransition transition : group.Transitions()) {
if (auto storyboard = transition.Storyboard()) {
storyboard.SkipToFill();
}
}
}*/
}
}

Expand All @@ -128,6 +130,41 @@ void RootPage::RootPage_Loaded(IInspectable const&, RoutedEventArgs const&) {
} while (!elems.empty());
}

void RootPage::RootPage_Loaded(IInspectable const&, RoutedEventArgs const&) {
// 消除焦点框
IsTabStop(true);
Focus(FocusState::Programmatic);
IsTabStop(false);

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

// 启动时跳过所有动画,比如 ToggleSwitch 和 NavigationView 的动画
int t = Measure1([&]() {
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) {
auto current = VisualTreeHelper::GetChild(elem, i);

hstring className = get_class_name(elem);
if (className == name_of<ToggleSwitch>() || className == name_of<SettingsExpander>()) {
SkipAnimations(elem);
} else {
temp.emplace_back(std::move(current));
}
}
}

elems = std::move(temp);
} while (!elems.empty());
});
OutputDebugString(fmt::format(L"{}\n", t).c_str());
}

void RootPage::NavigationView_SelectionChanged(
MUXC::NavigationView const&,
MUXC::NavigationViewSelectionChangedEventArgs const& args
Expand Down
3 changes: 2 additions & 1 deletion src/Magpie/TitlebarControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ void TitleBarControl::TitleBarControl_Loading(FrameworkElement const&, IInspecta
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);
VisualStateManager::GoToState(
*this, expanded ? L"Expanded" : L"Compact", App::Get().RootPage()->IsLoaded());
});
}

Expand Down

0 comments on commit 2d64597

Please sign in to comment.