Skip to content

Commit

Permalink
preview: Add theme combobox #11
Browse files Browse the repository at this point in the history
  • Loading branch information
roxk committed Apr 12, 2024
1 parent ca1d698 commit 8e3153c
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 4 deletions.
1 change: 1 addition & 0 deletions WinUI3XamlPreview/WinUI3XamlPreview/MainPage.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> Resolutions();
}
}
24 changes: 22 additions & 2 deletions WinUI3XamlPreview/WinUI3XamlPreview/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,30 @@
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="*" />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Grid>
<ScrollView>
<Grid Padding="16,0,16,0">
<ComboBox
x:Name="themeComboBox"
MinWidth="96"
VerticalAlignment="Center"
SelectedIndex="0"
SelectionChanged="themeComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="x:String">
<TextBlock Text="{x:Bind local:MainPage.ThemeDisplay((x:String))}" />
</DataTemplate>
</ComboBox.ItemTemplate>
<x:String>System</x:String>
<x:String>Light</x:String>
<x:String>Dark</x:String>
</ComboBox>
</Grid>
</ScrollView>
<Grid Grid.Row="1">
<ScrollView
x:Name="viewportScrollView"
Background="Gray"
Expand All @@ -30,7 +50,7 @@
VerticalAlignment="Bottom" />
</Grid>
<ScrollView
Grid.Row="1"
Grid.Row="2"
BorderBrush="{ThemeResource ControlStrokeColorDefaultBrush}"
BorderThickness="0,1,0,0"
CornerRadius="0"
Expand Down
59 changes: 59 additions & 0 deletions WinUI3XamlPreview/WinUI3XamlPreview/MainPage.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ namespace winrt::WinUI3XamlPreview::implementation
{
Preview::InstanceInternal()->FilePathChanged(_filePathChangedToken);
}
void MainPage::Window(mux::Window const& window)
{
_window = window;
}
void MainPage::OnFilePathChanged(IInspectable const& sender, winrt::hstring const& e)
{
OpenFileAndRead(e);
Expand Down Expand Up @@ -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<IInspectable> MainPage::Resolutions()
{
static auto resolutions = winrt::single_threaded_vector<wf::IInspectable>({
Expand All @@ -164,6 +173,31 @@ namespace winrt::WinUI3XamlPreview::implementation
auto resolution = winrt::unbox_value<wf::Numerics::float2>(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));
Expand All @@ -182,6 +216,24 @@ namespace winrt::WinUI3XamlPreview::implementation
auto resolutionFloat2 = unbox_value<wf::Numerics::float2>(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())
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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<winrt::hstring>(themeComboBox().SelectedItem()));
}
7 changes: 7 additions & 0 deletions WinUI3XamlPreview/WinUI3XamlPreview/MainPage.xaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<wf::IInspectable> 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<MainPage>;
Expand All @@ -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();
Expand All @@ -36,6 +42,7 @@ namespace winrt::WinUI3XamlPreview::implementation
template<typename T, typename D>
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);
};
}

Expand Down
2 changes: 1 addition & 1 deletion WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.idl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace WinUI3XamlPreview
{
[default_interface]
unsealed runtimeclass MainWindow : Microsoft.UI.Xaml.Window
{
MainWindow();
void InitializeComponent();
}
}
2 changes: 1 addition & 1 deletion WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<local:MainPage />
<local:MainPage x:Name="mainPage" />
</Window>
7 changes: 7 additions & 0 deletions WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,4 +13,10 @@ using namespace Microsoft::UI::Xaml;

namespace winrt::WinUI3XamlPreview::implementation
{
void MainWindow::InitializeComponent()
{
MainWindowT::InitializeComponent();
ExtendsContentIntoTitleBar(true);
mainPage().as<MainPage>()->Window(*this);
}
}
1 change: 1 addition & 0 deletions WinUI3XamlPreview/WinUI3XamlPreview/MainWindow.xaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@
<data name="FileNotFoundTitle" xml:space="preserve">
<value>File Not Found</value>
</data>
<data name="ThemeDisplay_Dark" xml:space="preserve">
<value>Dark</value>
</data>
<data name="ThemeDisplay_Light" xml:space="preserve">
<value>Light</value>
</data>
<data name="ThemeDisplay_System" xml:space="preserve">
<value>System Theme</value>
</data>
<data name="XamlLoadErrorMessage" xml:space="preserve">
<value>Cannot load XAML. If you added new user/custom control, build the project first. If your app is packaged, deploy it first.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@
<data name="FileNotFoundTitle" xml:space="preserve">
<value>找不到檔案</value>
</data>
<data name="ThemeDisplay_Dark" xml:space="preserve">
<value>深色</value>
</data>
<data name="ThemeDisplay_Light" xml:space="preserve">
<value>淺色</value>
</data>
<data name="ThemeDisplay_System" xml:space="preserve">
<value>系統主題</value>
</data>
<data name="XamlLoadErrorMessage" xml:space="preserve">
<value>無法讀取 XAML。如果你新增了 User/Custom Control,請先構建專案。如果你的應用是已包裝的,請先安裝。</value>
</data>
Expand Down
6 changes: 6 additions & 0 deletions WinUI3XamlPreview/WinUI3XamlPreview/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
#undef GetCurrentTime

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Graphics.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.ApplicationModel.Activation.h>
#include <winrt/Windows.Storage.h>
#include <winrt/Microsoft.UI.Composition.h>
#include <winrt/Microsoft.UI.Input.h>
#include <winrt/Microsoft.UI.Windowing.h>
#include <winrt/Microsoft.Windows.ApplicationModel.Resources.h>
#include <winrt/Microsoft.UI.Xaml.h>
#include <winrt/Microsoft.UI.Xaml.Controls.h>
Expand All @@ -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;
Expand All @@ -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;
}

0 comments on commit 8e3153c

Please sign in to comment.