From 8e3153c56025684ba4baddedd1aeca673b721042 Mon Sep 17 00:00:00 2001 From: Roxk Date: Fri, 12 Apr 2024 23:49:03 +0800 Subject: [PATCH] preview: Add theme combobox #11 --- .../WinUI3XamlPreview/MainPage.idl | 1 + .../WinUI3XamlPreview/MainPage.xaml | 24 +++++++- .../WinUI3XamlPreview/MainPage.xaml.cpp | 59 +++++++++++++++++++ .../WinUI3XamlPreview/MainPage.xaml.h | 7 +++ .../WinUI3XamlPreview/MainWindow.idl | 2 +- .../WinUI3XamlPreview/MainWindow.xaml | 2 +- .../WinUI3XamlPreview/MainWindow.xaml.cpp | 7 +++ .../WinUI3XamlPreview/MainWindow.xaml.h | 1 + .../Strings/en-US/Resources.resw | 9 +++ .../Strings/zh-HK/Resources.resw | 9 +++ WinUI3XamlPreview/WinUI3XamlPreview/pch.h | 6 ++ 11 files changed, 123 insertions(+), 4 deletions(-) diff --git a/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.idl b/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.idl index e1ab9a4..b6f9ca9 100644 --- a/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.idl +++ b/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.idl @@ -5,6 +5,7 @@ unsealed runtimeclass MainPage : Microsoft.UI.Xaml.Controls.Page MainPage(); static String ResolutionDisplay(Object resolutionFloat2); static String ScaleDisplay(Double scalePercentage); +static String ThemeDisplay(String theme); static Windows.Foundation.Collections.IVector Resolutions(); } } diff --git a/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.xaml b/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.xaml index aed89e0..0f1e965 100644 --- a/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.xaml +++ b/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.xaml @@ -10,10 +10,30 @@ mc:Ignorable="d"> + - + + + + + + + + + System + Light + Dark + + + + FilePathChanged(_filePathChangedToken); } + void MainPage::Window(mux::Window const& window) + { + _window = window; + } void MainPage::OnFilePathChanged(IInspectable const& sender, winrt::hstring const& e) { OpenFileAndRead(e); @@ -149,6 +153,11 @@ namespace winrt::WinUI3XamlPreview::implementation { return std::to_wstring(int(scalePercentage)) + winrt::hstring(L"%"); } + winrt::hstring MainPage::ThemeDisplay(winrt::hstring const& theme) + { + mwamr::ResourceLoader res(mwamr::ResourceLoader::GetDefaultResourceFilePath(), L"WinUI3XamlPreview/Resources"); + return res.GetString(winrt::hstring(L"ThemeDisplay_") + theme); + } wfc::IVector MainPage::Resolutions() { static auto resolutions = winrt::single_threaded_vector({ @@ -164,6 +173,31 @@ namespace winrt::WinUI3XamlPreview::implementation auto resolution = winrt::unbox_value(resolutionFloat2); return ResolutionDisplay(resolution); } + void MainPage::SetRegionsForCustomTitleBar() + { + auto& window = _window; + if (window == nullptr) + { + return; + } + auto scale = Content().XamlRoot().RasterizationScale(); + auto themeCb = themeComboBox(); + auto transform = themeCb.TransformToVisual(nullptr); + auto bounds = transform.TransformBounds(wf::Rect{ 0, 0, + float(themeCb.ActualWidth()), + float(themeCb.ActualHeight()) + }); + auto themeCbRect = wg::RectInt32{ int32_t(bounds.X * scale), + int32_t(bounds.Y * scale), + int32_t(bounds.Width * scale), + int32_t(bounds.Height * scale) + }; + + wg::RectInt32 rectArray[] = { themeCbRect }; + auto nonClientInputSrc = + mui::InputNonClientPointerSource::GetForWindowId(window.AppWindow().Id()); + nonClientInputSrc.SetRegionRects(mui::NonClientRegionKind::Passthrough, rectArray); + } winrt::hstring MainPage::ResolutionDisplay(wf::Numerics::float2 resolution) { auto width = std::to_wstring(int(resolution.x)); @@ -182,6 +216,24 @@ namespace winrt::WinUI3XamlPreview::implementation auto resolutionFloat2 = unbox_value(combobBox.SelectedItem()); UpdateCurrentResolution(resolutionFloat2); } + void MainPage::UpdateCurrentTheme(winrt::hstring const& theme) + { + if (!IsLoaded()) + { + return; + } + if (_currentTheme == theme) + { + return; + } + _currentTheme = theme; + CombobBoxSelectedItem(themeComboBox(), box_value(theme), ThemeDisplay(theme)); + elementWrapper().RequestedTheme(theme == L"System" + ? mux::ElementTheme::Default + : theme == L"Dark" + ? mux::ElementTheme::Dark + : mux::ElementTheme::Light); + } void MainPage::UpdateCurrentScale(double scale) { if (!IsLoaded()) @@ -273,6 +325,7 @@ void winrt::WinUI3XamlPreview::implementation::MainPage::scaleComboBox_Selection void winrt::WinUI3XamlPreview::implementation::MainPage::Page_Loaded(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e) { + SetRegionsForCustomTitleBar(); UpdateResolutionByComboBox(); FitToPage(); @@ -323,3 +376,9 @@ void winrt::WinUI3XamlPreview::implementation::MainPage::fitPageButton_Click(win { FitToPage(); } + + +void winrt::WinUI3XamlPreview::implementation::MainPage::themeComboBox_SelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e) +{ + UpdateCurrentTheme(unbox_value(themeComboBox().SelectedItem())); +} diff --git a/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.xaml.h b/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.xaml.h index 997d310..7abd313 100644 --- a/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.xaml.h +++ b/WinUI3XamlPreview/WinUI3XamlPreview/MainPage.xaml.h @@ -8,12 +8,17 @@ namespace winrt::WinUI3XamlPreview::implementation { MainPage(); ~MainPage(); + [[idlgen::hide]] void Window(mux::Window const& window); [[idlgen::hide]] void OnFilePathChanged(wf::IInspectable const& sender, winrt::hstring const& e); static winrt::hstring ScaleDisplay(double scalePercentage); + static winrt::hstring ThemeDisplay(winrt::hstring const& theme); static wfc::IVector Resolutions(); static winrt::hstring ResolutionDisplay(wf::IInspectable const& resolutionFloat2); private: + void SetRegionsForCustomTitleBar(); static winrt::hstring ResolutionDisplay(wf::Numerics::float2 resolution); + mux::Window _window{}; + winrt::hstring _currentTheme{}; double _currentScale{}; wf::Numerics::float2 _currentResolution{}; friend struct MainPageT; @@ -26,6 +31,7 @@ namespace winrt::WinUI3XamlPreview::implementation void Page_Loaded(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e); void UpdateScaleByComboBox(); void UpdateResolutionByComboBox(); + void UpdateCurrentTheme(winrt::hstring const& theme); void UpdateCurrentScale(double scale); void UpdateCurrentResolution(wf::Numerics::float2 resolution); void FitToPage(); @@ -36,6 +42,7 @@ namespace winrt::WinUI3XamlPreview::implementation template void CombobBoxSelectedItem(muxc::ComboBox const& comboBox, T&& value, D display); void fitPageButton_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e); + void themeComboBox_SelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Controls::SelectionChangedEventArgs const& e); }; } diff --git a/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.idl b/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.idl index d7e0d25..2d4ff55 100644 --- a/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.idl +++ b/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.idl @@ -1,8 +1,8 @@ namespace WinUI3XamlPreview { -[default_interface] unsealed runtimeclass MainWindow : Microsoft.UI.Xaml.Window { MainWindow(); +void InitializeComponent(); } } diff --git a/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml b/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml index 995dc54..46ca362 100644 --- a/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml +++ b/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml @@ -8,5 +8,5 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> - + diff --git a/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml.cpp b/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml.cpp index ad27931..4012c60 100644 --- a/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml.cpp +++ b/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml.cpp @@ -3,6 +3,7 @@ #if __has_include("MainWindow.g.cpp") #include "MainWindow.g.cpp" #endif +#include "MainPage.xaml.h" using namespace winrt; using namespace Microsoft::UI::Xaml; @@ -12,4 +13,10 @@ using namespace Microsoft::UI::Xaml; namespace winrt::WinUI3XamlPreview::implementation { + void MainWindow::InitializeComponent() + { + MainWindowT::InitializeComponent(); + ExtendsContentIntoTitleBar(true); + mainPage().as()->Window(*this); + } } diff --git a/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml.h b/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml.h index c3460ec..fb4c841 100644 --- a/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml.h +++ b/WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml.h @@ -11,6 +11,7 @@ namespace winrt::WinUI3XamlPreview::implementation // Xaml objects should not call InitializeComponent during construction. // See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent } + void InitializeComponent(); }; } diff --git a/WinUI3XamlPreview/WinUI3XamlPreview/Strings/en-US/Resources.resw b/WinUI3XamlPreview/WinUI3XamlPreview/Strings/en-US/Resources.resw index 437abe4..c186a71 100644 --- a/WinUI3XamlPreview/WinUI3XamlPreview/Strings/en-US/Resources.resw +++ b/WinUI3XamlPreview/WinUI3XamlPreview/Strings/en-US/Resources.resw @@ -123,6 +123,15 @@ File Not Found + + Dark + + + Light + + + System Theme + Cannot load XAML. If you added new user/custom control, build the project first. If your app is packaged, deploy it first. diff --git a/WinUI3XamlPreview/WinUI3XamlPreview/Strings/zh-HK/Resources.resw b/WinUI3XamlPreview/WinUI3XamlPreview/Strings/zh-HK/Resources.resw index 8e09224..23f050c 100644 --- a/WinUI3XamlPreview/WinUI3XamlPreview/Strings/zh-HK/Resources.resw +++ b/WinUI3XamlPreview/WinUI3XamlPreview/Strings/zh-HK/Resources.resw @@ -123,6 +123,15 @@ 找不到檔案 + + 深色 + + + 淺色 + + + 系統主題 + 無法讀取 XAML。如果你新增了 User/Custom Control,請先構建專案。如果你的應用是已包裝的,請先安裝。 diff --git a/WinUI3XamlPreview/WinUI3XamlPreview/pch.h b/WinUI3XamlPreview/WinUI3XamlPreview/pch.h index 0de4dc2..c76a3e3 100644 --- a/WinUI3XamlPreview/WinUI3XamlPreview/pch.h +++ b/WinUI3XamlPreview/WinUI3XamlPreview/pch.h @@ -10,10 +10,13 @@ #undef GetCurrentTime #include +#include #include #include #include #include +#include +#include #include #include #include @@ -34,6 +37,7 @@ namespace winrt { namespace waa = Windows::ApplicationModel::Activation; namespace wf = Windows::Foundation; + namespace wg = Windows::Graphics; namespace wfc = Windows::Foundation::Collections; namespace ws = Windows::Storage; namespace mux = Microsoft::UI::Xaml; @@ -43,4 +47,6 @@ namespace winrt namespace mwal = Microsoft::Windows::AppLifecycle; namespace mwamr = Microsoft::Windows::ApplicationModel::Resources; namespace mud = Microsoft::UI::Dispatching; + namespace mui = Microsoft::UI::Input; + namespace muw = Microsoft::UI::Windowing; }