Skip to content

Commit

Permalink
feat: 背景切换 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed Feb 14, 2024
1 parent ba3f5d4 commit 1d0e255
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 101 deletions.
25 changes: 6 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,24 @@

![MainWindow.png](img/MainWindow.png)

本项目实现了一个最小的 [XAML Islands](https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/xaml-islands) (C++/WinRT) 应用。目标是:
[XAML Islands](https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/xaml-islands) (C++/WinRT) 应用示例。

1. 作为创建 XAML Islands 应用的起点。
2. 作为一个用于实验/调试的最小环境。
**实现的功能**

**已实现的功能**
:heavy_check_mark: WinUI 集成(支持正式版而不是仅限预发行版)

:heavy_check_mark: XAML Islands 集成
:heavy_check_mark: 自定义标题栏

:heavy_check_mark: WinUI 集成(支持正式版而不是仅限预发行版)
:heavy_check_mark: 明暗主题切换

:heavy_check_mark: [修剪 resources.pri](https://github.com/microsoft/microsoft-ui-xaml/pull/4400)
:heavy_check_mark: 背景切换

:heavy_check_mark: 支持打包为 MSIX

:heavy_check_mark: 支持 x64 和 ARM64 架构

:heavy_check_mark: 支持 XAML 热重载

**不会实现的功能**(这些功能超出了本项目的目标,你可在 [Magpie](https://github.com/Blinue/Magpie) 中找到它们的实现)

:x: 明暗主题切换

:x: Mica 背景

:x: 自定义标题栏

## 编译要求

1. Windows SDK 22621
Expand All @@ -37,7 +28,3 @@
## 运行要求

Windows 10 v1903+ 或 Windows 11

## 发布

执行 publish.py 将编译程序、清理文件以及修剪 resources.pri,可用于发布的程序体积约 8.3 MB (x64)。
47 changes: 32 additions & 15 deletions src/XamlIslandsCpp.App/RootPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RootPage::RootPage() {
void RootPage::InitializeComponent() {
RootPageT::InitializeComponent();

_SetTheme(_settings.Theme());
_SetTheme(_settings.Theme(), _settings.Backdrop());
}

bool RootPage::IsCustomTitleBarEnabled() const noexcept {
Expand All @@ -40,10 +40,28 @@ void RootPage::Theme(int value) {
}

_settings.Theme((AppTheme)value);
_SetTheme((AppTheme)value);
_SetTheme((AppTheme)value, _settings.Backdrop());
_propertyChangedEvent(*this, PropertyChangedEventArgs(L"Theme"));
}

bool RootPage::IsMicaAvailable() noexcept {
return Win32Helper::GetOSVersion().Is22H2OrNewer();
}

int RootPage::Backdrop() const noexcept {
return (int)_settings.Backdrop();
}

void RootPage::Backdrop(int value) {
if (value < 0) {
return;
}

_settings.Backdrop((WindowBackdrop)value);
_SetTheme(_settings.Theme(), (WindowBackdrop)value);
_propertyChangedEvent(*this, PropertyChangedEventArgs(L"Backdrop"));
}

void RootPage::ComboBox_DropDownOpened(IInspectable const&, IInspectable const&) const {
// 修复下拉框不适配主题的问题
// https://github.com/microsoft/microsoft-ui-xaml/issues/6622
Expand All @@ -54,25 +72,24 @@ static Color Win32ColorToWinRTColor(COLORREF color) {
return { 255, GetRValue(color), GetGValue(color), GetBValue(color) };
}

void RootPage::_SetTheme(AppTheme theme) {
void RootPage::_SetTheme(AppTheme theme, WindowBackdrop backdrop) {
const bool isDarkTheme = theme == AppTheme::Dark;

if (IsLoaded() && (ActualTheme() == ElementTheme::Dark) == isDarkTheme) {
// 无需切换
return;
}
ElementTheme newTheme = isDarkTheme ? ElementTheme::Dark : ElementTheme::Light;
RequestedTheme(newTheme);

if (Win32Helper::GetOSVersion().Is22H2OrNewer()) {
// Win11 22H2+ 使用系统的 Mica 背景
MUXC::BackdropMaterial::SetApplyToRootOrPageBackground(*this, true);
} else {
const Windows::UI::Color bkgColor = Win32ColorToWinRTColor(
isDarkTheme ? CommonSharedConstants::DARK_TINT_COLOR : CommonSharedConstants::LIGHT_TINT_COLOR);
Background(SolidColorBrush(bkgColor));
if (backdrop != WindowBackdrop::SolidColor) {
MUXC::BackdropMaterial::SetApplyToRootOrPageBackground(*this, true);
return;
}

MUXC::BackdropMaterial::SetApplyToRootOrPageBackground(*this, false);
}

ElementTheme newTheme = isDarkTheme ? ElementTheme::Dark : ElementTheme::Light;
RequestedTheme(newTheme);
const Windows::UI::Color bkgColor = Win32ColorToWinRTColor(
isDarkTheme ? CommonSharedConstants::DARK_TINT_COLOR : CommonSharedConstants::LIGHT_TINT_COLOR);
Background(SolidColorBrush(bkgColor));
}

}
7 changes: 6 additions & 1 deletion src/XamlIslandsCpp.App/RootPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ struct RootPage : RootPageT<RootPage> {
int Theme() const noexcept;
void Theme(int value);

static bool IsMicaAvailable() noexcept;

int Backdrop() const noexcept;
void Backdrop(int value);

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

private:
void _SetTheme(AppTheme theme);
void _SetTheme(AppTheme theme, WindowBackdrop backdrop);

event<PropertyChangedEventHandler> _propertyChangedEvent;

Expand Down
3 changes: 3 additions & 0 deletions src/XamlIslandsCpp.App/RootPage.idl
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ namespace XamlIslandsCpp.App {

Boolean IsCustomTitleBarEnabled;
Int32 Theme;

static Boolean IsMicaAvailable { get; };
Int32 Backdrop;
}
}
7 changes: 3 additions & 4 deletions src/XamlIslandsCpp.App/RootPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
xmlns:local="using:XamlIslandsCpp.App"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
muxc:BackdropMaterial.ApplyToRootOrPageBackground="True"
mc:Ignorable="d">
<Page.Resources>
<ResourceDictionary>
Expand Down Expand Up @@ -61,11 +60,11 @@
HorizontalAlignment="Right"
VerticalAlignment="Center"
DropDownOpened="ComboBox_DropDownOpened"
SelectedIndex="0">
SelectedIndex="{x:Bind Backdrop, Mode=TwoWay}">
<ComboBoxItem x:Uid="Background_Solid" />
<ComboBoxItem>Acrylic</ComboBoxItem>
<ComboBoxItem>Mica</ComboBoxItem>
<ComboBoxItem>Mica Alt</ComboBoxItem>
<ComboBoxItem IsEnabled="{x:Bind local:RootPage.IsMicaAvailable}">Mica</ComboBoxItem>
<ComboBoxItem IsEnabled="{x:Bind local:RootPage.IsMicaAvailable}">Mica Alt</ComboBoxItem>
</ComboBox>
</Grid>
</StackPanel>
Expand Down
16 changes: 15 additions & 1 deletion src/XamlIslandsCpp.App/Settings.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "pch.h"
#include "pch.h"
#include "Settings.h"
#if __has_include("Settings.g.cpp")
#include "Settings.g.cpp"
#endif
#include <winrt/Windows.UI.ViewManagement.h>
#include "Win32Helper.h"

using namespace ::XamlIslandsCpp;
using namespace winrt;
using namespace Windows::UI;
using namespace Windows::UI::ViewManagement;
Expand All @@ -19,6 +21,9 @@ static bool IsColorLight(const Color& clr) noexcept {
Settings::Settings() {
Color foregroundColor = UISettings().GetColorValue(UIColorType::Foreground);
_theme = IsColorLight(foregroundColor) ? AppTheme::Dark : AppTheme::Light;

_backdrop = Win32Helper::GetOSVersion().Is22H2OrNewer() ?
WindowBackdrop::Mica : WindowBackdrop::SolidColor;
}

void Settings::IsCustomTitleBarEnabled(bool value) {
Expand All @@ -39,4 +44,13 @@ void Settings::Theme(AppTheme value) {
_appThemeChangedEvent(*this, value);
}

void Settings::Backdrop(WindowBackdrop value) {
if (_backdrop == value) {
return;
}

_backdrop = value;
_backdropChangedEvent(*this, value);
}

}
20 changes: 18 additions & 2 deletions src/XamlIslandsCpp.App/Settings.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once
#pragma once
#include "Settings.g.h"

namespace winrt::XamlIslandsCpp::App::implementation {

struct Settings : SettingsT<Settings> {
Settings();
Settings();

bool IsCustomTitleBarEnabled() const noexcept {
return _isCustomTitleBarEnabled;
Expand Down Expand Up @@ -34,11 +34,27 @@ struct Settings : SettingsT<Settings> {
_appThemeChangedEvent.remove(token);
}

WindowBackdrop Backdrop() const noexcept {
return _backdrop;
}

void Backdrop(WindowBackdrop value);

event_token BackdropChanged(EventHandler<WindowBackdrop> const& handler) {
return _backdropChangedEvent.add(handler);
}

void BackdropChanged(event_token const& token) {
_backdropChangedEvent.remove(token);
}

private:
event<EventHandler<AppTheme>> _appThemeChangedEvent;
event<EventHandler<bool>> _isCustomTitleBarEnabledChangedEvent;
event<EventHandler<WindowBackdrop>> _backdropChangedEvent;

AppTheme _theme = AppTheme::Light;
WindowBackdrop _backdrop = WindowBackdrop::SolidColor;
bool _isCustomTitleBarEnabled = true;
};

Expand Down
34 changes: 22 additions & 12 deletions src/XamlIslandsCpp.App/Settings.idl
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
namespace XamlIslandsCpp.App {
enum AppTheme {
Light,
Dark
};
enum AppTheme {
Light,
Dark
};

runtimeclass Settings {
Settings();

Boolean IsCustomTitleBarEnabled;
event Windows.Foundation.EventHandler<Boolean> IsCustomTitleBarEnabledChanged;
enum WindowBackdrop {
SolidColor,
Acrylic,
Mica,
MicaAlt
};

AppTheme Theme;
event Windows.Foundation.EventHandler<AppTheme> ThemeChanged;
}
runtimeclass Settings {
Settings();

Boolean IsCustomTitleBarEnabled;
event Windows.Foundation.EventHandler<Boolean> IsCustomTitleBarEnabledChanged;

AppTheme Theme;
event Windows.Foundation.EventHandler<AppTheme> ThemeChanged;

WindowBackdrop Backdrop;
event Windows.Foundation.EventHandler<WindowBackdrop> BackdropChanged;
}
}
Loading

0 comments on commit 1d0e255

Please sign in to comment.