Skip to content

Commit

Permalink
fix: UI 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Feb 14, 2024
1 parent 4d56536 commit ba3f5d4
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
45 changes: 45 additions & 0 deletions src/Shared/XamlHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "pch.h"
#include "XamlHelper.h"
#include "Win32Helper.h"
#include <winrt/Windows.UI.Xaml.Media.h>
#include <vector>

namespace winrt {
using namespace Windows::UI::Xaml;
Expand Down Expand Up @@ -84,4 +86,47 @@ void XamlHelper::CloseComboBoxPopup(winrt::XamlRoot const& root) {
}
}

void XamlHelper::UpdateThemeOfXamlPopups(winrt::XamlRoot const& root, winrt::ElementTheme theme) {
for (const auto& popup : winrt::VisualTreeHelper::GetOpenPopupsForXamlRoot(root)) {
winrt::FrameworkElement child = popup.Child().as<winrt::FrameworkElement>();
child.RequestedTheme(theme);
UpdateThemeOfTooltips(child, theme);
}
}

void XamlHelper::UpdateThemeOfTooltips(winrt::DependencyObject const& root, winrt::ElementTheme theme) {
if (Win32Helper::GetOSVersion().IsWin11()) {
// Win11 中 Tooltip 自动适应主题
return;
}

// 遍历 XAML 树
std::vector<winrt::DependencyObject> elems{ root };
do {
std::vector<winrt::DependencyObject> temp;

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

if (winrt::IInspectable tooltipContent = winrt::ToolTipService::GetToolTip(current)) {
if (winrt::ToolTip tooltip = tooltipContent.try_as<winrt::ToolTip>()) {
tooltip.RequestedTheme(theme);
} else {
winrt::ToolTip themedTooltip;
themedTooltip.Content(tooltipContent);
themedTooltip.RequestedTheme(theme);
winrt::ToolTipService::SetToolTip(current, themedTooltip);
}
}

temp.emplace_back(std::move(current));
}
}

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

}
11 changes: 11 additions & 0 deletions src/Shared/XamlHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@ namespace XamlIslandsCpp {

struct XamlHelper {
static void RepositionXamlPopups(winrt::Windows::UI::Xaml::XamlRoot const& root, bool closeFlyoutPresenter);

static void CloseComboBoxPopup(winrt::Windows::UI::Xaml::XamlRoot const& root);

static void UpdateThemeOfXamlPopups(
const winrt::Windows::UI::Xaml::XamlRoot& root,
winrt::Windows::UI::Xaml::ElementTheme theme
);

static void UpdateThemeOfTooltips(
const winrt::Windows::UI::Xaml::DependencyObject& root,
winrt::Windows::UI::Xaml::ElementTheme theme
);
};

}
7 changes: 7 additions & 0 deletions src/XamlIslandsCpp.App/RootPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif
#include "Win32Helper.h"
#include "CommonSharedConstants.h"
#include "XamlHelper.h"

using namespace XamlIslandsCpp;

Expand Down Expand Up @@ -43,6 +44,12 @@ void RootPage::Theme(int value) {
_propertyChangedEvent(*this, PropertyChangedEventArgs(L"Theme"));
}

void RootPage::ComboBox_DropDownOpened(IInspectable const&, IInspectable const&) const {
// 修复下拉框不适配主题的问题
// https://github.com/microsoft/microsoft-ui-xaml/issues/6622
XamlHelper::UpdateThemeOfXamlPopups(XamlRoot(), ActualTheme());
}

static Color Win32ColorToWinRTColor(COLORREF color) {
return { 255, GetRValue(color), GetGValue(color), GetBValue(color) };
}
Expand Down
2 changes: 2 additions & 0 deletions src/XamlIslandsCpp.App/RootPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct RootPage : RootPageT<RootPage> {
int Theme() const noexcept;
void Theme(int value);

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

private:
void _SetTheme(AppTheme theme);

Expand Down
2 changes: 2 additions & 0 deletions src/XamlIslandsCpp.App/RootPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<ComboBox MinWidth="120"
HorizontalAlignment="Right"
VerticalAlignment="Center"
DropDownOpened="ComboBox_DropDownOpened"
SelectedIndex="{x:Bind Theme, Mode=TwoWay}">
<ComboBoxItem x:Uid="Theme_Light" />
<ComboBoxItem x:Uid="Theme_Dark" />
Expand All @@ -59,6 +60,7 @@
<ComboBox MinWidth="120"
HorizontalAlignment="Right"
VerticalAlignment="Center"
DropDownOpened="ComboBox_DropDownOpened"
SelectedIndex="0">
<ComboBoxItem x:Uid="Background_Solid" />
<ComboBoxItem>Acrylic</ComboBoxItem>
Expand Down
1 change: 1 addition & 0 deletions src/XamlIslandsCpp.App/TitleBarControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace winrt::XamlIslandsCpp::App::implementation {

void TitleBarControl::IsWindowActive(bool value) {
VisualStateManager::GoToState(*this, value ? L"Active" : L"NotActive", false);
CaptionButtons().IsWindowActive(value);
}

}
5 changes: 1 addition & 4 deletions src/XamlIslandsCpp/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ LRESULT MainWindow::_MessageHandler(UINT msg, WPARAM wParam, LPARAM lParam) noex
}
case WM_ACTIVATE:
{
if (_isCustomTitleBarEnabled) {
_content.TitleBar().IsWindowActive(LOWORD(wParam) != WA_INACTIVE);
}

_content.TitleBar().IsWindowActive(LOWORD(wParam) != WA_INACTIVE);
break;
}
case WM_DESTROY:
Expand Down

0 comments on commit ba3f5d4

Please sign in to comment.