Skip to content

Commit

Permalink
添加 .NET Core App 8.0 支持
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Sep 5, 2024
1 parent 87feae3 commit 23220b2
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 110 deletions.
108 changes: 49 additions & 59 deletions MicaDemo/Helpers/ThemeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,86 +114,76 @@ private static async void UISettings_ColorValuesChanged(UISettings sender, objec
InvokeUISettingChanged(await IsDarkThemeAsync());
}

public static async Task<bool> IsDarkThemeAsync()
{
ElementTheme ActualTheme = await GetRootThemeAsync();
return Window.Current != null
? ActualTheme == ElementTheme.Default
? Application.Current.RequestedTheme == ApplicationTheme.Dark
: ActualTheme == ElementTheme.Dark
: ActualTheme == ElementTheme.Default
? UISettings?.GetColorValue(UIColorType.Foreground).IsColorLight() == true
: ActualTheme == ElementTheme.Dark;
}
public static Task<bool> IsDarkThemeAsync() => GetRootThemeAsync().ContinueWith(x => IsDarkTheme(x.Result));

public static bool IsDarkTheme(this ElementTheme ActualTheme)
public static bool IsDarkTheme(ElementTheme actualTheme)
{
return Window.Current != null
? ActualTheme == ElementTheme.Default
? actualTheme == ElementTheme.Default
? Application.Current.RequestedTheme == ApplicationTheme.Dark
: ActualTheme == ElementTheme.Dark
: ActualTheme == ElementTheme.Default
: actualTheme == ElementTheme.Dark
: actualTheme == ElementTheme.Default
? UISettings?.GetColorValue(UIColorType.Foreground).IsColorLight() == true
: ActualTheme == ElementTheme.Dark;
: actualTheme == ElementTheme.Dark;
}

public static bool IsColorLight(this Color color) => ((5 * color.G) + (2 * color.R) + color.B) > (8 * 128);

public static void UpdateExtendViewIntoTitleBar(bool IsExtendsTitleBar)
public static void UpdateExtendViewIntoTitleBar(bool isExtendsTitleBar)
{
WindowHelper.ActiveWindows.Values.ForEach(async window =>
{
await window.Dispatcher.ResumeForegroundAsync();

CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = IsExtendsTitleBar;
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = isExtendsTitleBar;

if (WindowHelper.IsAppWindowSupported && WindowHelper.ActiveAppWindows.TryGetValue(window.Dispatcher, out Dictionary<XamlRoot, AppWindow> appWindows))
{
foreach (AppWindow appWindow in appWindows.Values)
{
appWindow.TitleBar.ExtendsContentIntoTitleBar = IsExtendsTitleBar;
appWindow.TitleBar.ExtendsContentIntoTitleBar = isExtendsTitleBar;
}
}
});
}

public static async void UpdateSystemCaptionButtonColors()
{
bool IsDark = await IsDarkThemeAsync();
bool IsHighContrast = AccessibilitySettings.HighContrast;
bool isDark = await IsDarkThemeAsync();
bool isHighContrast = AccessibilitySettings.HighContrast;

Color ForegroundColor = IsDark || IsHighContrast ? Colors.White : Colors.Black;
Color BackgroundColor = IsHighContrast ? Color.FromArgb(255, 0, 0, 0) : IsDark ? Color.FromArgb(255, 32, 32, 32) : Color.FromArgb(255, 243, 243, 243);
Color foregroundColor = isDark || isHighContrast ? Colors.White : Colors.Black;
Color backgroundColor = isHighContrast ? Color.FromArgb(255, 0, 0, 0) : isDark ? Color.FromArgb(255, 32, 32, 32) : Color.FromArgb(255, 243, 243, 243);

WindowHelper.ActiveWindows.Values.ForEach(async window =>
{
await window.Dispatcher.ResumeForegroundAsync();

if (IsStatusBarSupported)
{
StatusBar StatusBar = StatusBar.GetForCurrentView();
StatusBar.ForegroundColor = ForegroundColor;
StatusBar.BackgroundColor = BackgroundColor;
StatusBar.BackgroundOpacity = 0; // 透明度
StatusBar statusBar = StatusBar.GetForCurrentView();
statusBar.ForegroundColor = foregroundColor;
statusBar.BackgroundColor = backgroundColor;
statusBar.BackgroundOpacity = 0; // 透明度
}
else
{
bool ExtendViewIntoTitleBar = CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar;
ApplicationViewTitleBar TitleBar = ApplicationView.GetForCurrentView().TitleBar;
TitleBar.ForegroundColor = TitleBar.ButtonForegroundColor = ForegroundColor;
TitleBar.BackgroundColor = TitleBar.InactiveBackgroundColor = BackgroundColor;
TitleBar.ButtonBackgroundColor = TitleBar.ButtonInactiveBackgroundColor = ExtendViewIntoTitleBar ? Colors.Transparent : BackgroundColor;
bool extendViewIntoTitleBar = CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar;
ApplicationViewTitleBar titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.ForegroundColor = titleBar.ButtonForegroundColor = foregroundColor;
titleBar.BackgroundColor = titleBar.InactiveBackgroundColor = backgroundColor;
titleBar.ButtonBackgroundColor = titleBar.ButtonInactiveBackgroundColor = extendViewIntoTitleBar ? Colors.Transparent : backgroundColor;
}

if (WindowHelper.IsAppWindowSupported && WindowHelper.ActiveAppWindows.TryGetValue(window.Dispatcher, out Dictionary<XamlRoot, AppWindow> appWindows))
{
foreach (AppWindow appWindow in appWindows.Values)
{
bool ExtendViewIntoTitleBar = appWindow.TitleBar.ExtendsContentIntoTitleBar;
AppWindowTitleBar TitleBar = appWindow.TitleBar;
TitleBar.ForegroundColor = TitleBar.ButtonForegroundColor = ForegroundColor;
TitleBar.BackgroundColor = TitleBar.InactiveBackgroundColor = BackgroundColor;
TitleBar.ButtonBackgroundColor = TitleBar.ButtonInactiveBackgroundColor = ExtendViewIntoTitleBar ? Colors.Transparent : BackgroundColor;
bool extendViewIntoTitleBar = appWindow.TitleBar.ExtendsContentIntoTitleBar;
AppWindowTitleBar titleBar = appWindow.TitleBar;
titleBar.ForegroundColor = titleBar.ButtonForegroundColor = foregroundColor;
titleBar.BackgroundColor = titleBar.InactiveBackgroundColor = backgroundColor;
titleBar.ButtonBackgroundColor = titleBar.ButtonInactiveBackgroundColor = extendViewIntoTitleBar ? Colors.Transparent : backgroundColor;
}
}
});
Expand All @@ -203,44 +193,44 @@ public static async void UpdateSystemCaptionButtonColors(Window window)
{
await window.Dispatcher.ResumeForegroundAsync();

bool IsDark = window?.Content is FrameworkElement rootElement ? IsDarkTheme(rootElement.RequestedTheme) : await IsDarkThemeAsync();
bool IsHighContrast = AccessibilitySettings.HighContrast;
bool isDark = window?.Content is FrameworkElement rootElement ? IsDarkTheme(rootElement.RequestedTheme) : await IsDarkThemeAsync();
bool isHighContrast = AccessibilitySettings.HighContrast;

Color ForegroundColor = IsDark || IsHighContrast ? Colors.White : Colors.Black;
Color BackgroundColor = IsHighContrast ? Color.FromArgb(255, 0, 0, 0) : IsDark ? Color.FromArgb(255, 32, 32, 32) : Color.FromArgb(255, 243, 243, 243);
Color foregroundColor = isDark || isHighContrast ? Colors.White : Colors.Black;
Color backgroundColor = isHighContrast ? Color.FromArgb(255, 0, 0, 0) : isDark ? Color.FromArgb(255, 32, 32, 32) : Color.FromArgb(255, 243, 243, 243);

if (IsStatusBarSupported)
{
StatusBar StatusBar = StatusBar.GetForCurrentView();
StatusBar.ForegroundColor = ForegroundColor;
StatusBar.BackgroundColor = BackgroundColor;
StatusBar.BackgroundOpacity = 0; // 透明度
StatusBar statusBar = StatusBar.GetForCurrentView();
statusBar.ForegroundColor = foregroundColor;
statusBar.BackgroundColor = backgroundColor;
statusBar.BackgroundOpacity = 0; // 透明度
}
else
{
bool ExtendViewIntoTitleBar = CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar;
ApplicationViewTitleBar TitleBar = ApplicationView.GetForCurrentView().TitleBar;
TitleBar.ForegroundColor = TitleBar.ButtonForegroundColor = ForegroundColor;
TitleBar.BackgroundColor = TitleBar.InactiveBackgroundColor = BackgroundColor;
TitleBar.ButtonBackgroundColor = TitleBar.ButtonInactiveBackgroundColor = ExtendViewIntoTitleBar ? Colors.Transparent : BackgroundColor;
bool extendViewIntoTitleBar = CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar;
ApplicationViewTitleBar titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.ForegroundColor = titleBar.ButtonForegroundColor = foregroundColor;
titleBar.BackgroundColor = titleBar.InactiveBackgroundColor = backgroundColor;
titleBar.ButtonBackgroundColor = titleBar.ButtonInactiveBackgroundColor = extendViewIntoTitleBar ? Colors.Transparent : backgroundColor;
}
}

public static async void UpdateSystemCaptionButtonColors(AppWindow window)
{
await window.DispatcherQueue.ResumeForegroundAsync();

bool IsDark = window.GetXamlRootForWindow() is FrameworkElement rootElement ? IsDarkTheme(rootElement.RequestedTheme) : await IsDarkThemeAsync();
bool IsHighContrast = AccessibilitySettings.HighContrast;
bool isDark = window.GetXamlRootForWindow() is FrameworkElement rootElement ? IsDarkTheme(rootElement.RequestedTheme) : await IsDarkThemeAsync();
bool isHighContrast = AccessibilitySettings.HighContrast;

Color ForegroundColor = IsDark || IsHighContrast ? Colors.White : Colors.Black;
Color BackgroundColor = IsHighContrast ? Color.FromArgb(255, 0, 0, 0) : IsDark ? Color.FromArgb(255, 32, 32, 32) : Color.FromArgb(255, 243, 243, 243);
Color foregroundColor = isDark || isHighContrast ? Colors.White : Colors.Black;
Color backgroundColor = isHighContrast ? Color.FromArgb(255, 0, 0, 0) : isDark ? Color.FromArgb(255, 32, 32, 32) : Color.FromArgb(255, 243, 243, 243);

bool ExtendViewIntoTitleBar = window.TitleBar.ExtendsContentIntoTitleBar;
AppWindowTitleBar TitleBar = window.TitleBar;
TitleBar.ForegroundColor = TitleBar.ButtonForegroundColor = ForegroundColor;
TitleBar.BackgroundColor = TitleBar.InactiveBackgroundColor = BackgroundColor;
TitleBar.ButtonBackgroundColor = TitleBar.ButtonInactiveBackgroundColor = ExtendViewIntoTitleBar ? Colors.Transparent : BackgroundColor;
bool extendViewIntoTitleBar = window.TitleBar.ExtendsContentIntoTitleBar;
AppWindowTitleBar titleBar = window.TitleBar;
titleBar.ForegroundColor = titleBar.ButtonForegroundColor = foregroundColor;
titleBar.BackgroundColor = titleBar.InactiveBackgroundColor = backgroundColor;
titleBar.ButtonBackgroundColor = titleBar.ButtonInactiveBackgroundColor = extendViewIntoTitleBar ? Colors.Transparent : backgroundColor;
}
}
}
14 changes: 7 additions & 7 deletions MicaDemo/Helpers/UIElementHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static void OnContextFlyoutChanged(DependencyObject d, DependencyPropert
UIElement element = (UIElement)d;
if (ApiInformation.IsPropertyPresent("Windows.UI.Xaml.UIElement", "ContextFlyout"))
{
element.ContextFlyout = GetContextFlyout(element);
element.ContextFlyout = e.NewValue as FlyoutBase;
}
else if (element is FrameworkElement frameworkElement)
{
Expand Down Expand Up @@ -143,23 +143,23 @@ private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedE
{
if (d is MenuFlyoutItem item)
{
if (e.NewValue is IconElement IconElement && ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.MenuFlyoutItem", "Icon"))
if (ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.MenuFlyoutItem", "Icon"))
{
item.Icon = IconElement;
item.Icon = e.NewValue as IconElement;
}
}
else if (d is MenuFlyoutSubItem subitem)
{
if (e.NewValue is IconElement IconElement && ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.MenuFlyoutSubItem", "Icon"))
if (ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.MenuFlyoutSubItem", "Icon"))
{
subitem.Icon = IconElement;
subitem.Icon = e.NewValue as IconElement;
}
}
else if (d is ToggleMenuFlyoutItem toggle)
{
if (e.NewValue is IconElement IconElement && ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.ToggleMenuFlyoutItem", "Icon"))
if (ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Controls.ToggleMenuFlyoutItem", "Icon"))
{
toggle.Icon = IconElement;
toggle.Icon = e.NewValue as IconElement;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions MicaDemo/Helpers/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public static async Task<bool> CreateWindowAsync(Action<Window> launched)
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Window newWindow = Window.Current;
launched(newWindow);
TrackWindow(newWindow);
Window.Current.Activate();
Window window = Window.Current;
TrackWindow(window);
launched(window);
window.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
return await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
Expand Down
2 changes: 1 addition & 1 deletion MicaDemo/Pages/BlurPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected override void OnNavigatedFrom(NavigationEventArgs e)

private void Page_Loaded(object sender, RoutedEventArgs e)
{
_ = ThemeHelper.GetRootThemeAsync().ContinueWith(x => x.Result.IsDarkTheme()).ContinueWith(x => isDark = x.Result);
_ = ThemeHelper.GetRootThemeAsync().ContinueWith(x => ThemeHelper.IsDarkTheme(x.Result)).ContinueWith(x => isDark = x.Result);
Provider.CompactOverlay = this.IsAppWindow() ? (ICompactOverlay)new AppWindowCompactOverlay(this.GetWindowForElement()) : new CoreWindowCompactOverlay();
}

Expand Down
2 changes: 1 addition & 1 deletion MicaDemo/Pages/MicaPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected override void OnNavigatedFrom(NavigationEventArgs e)

private void Page_Loaded(object sender, RoutedEventArgs e)
{
_ = ThemeHelper.GetRootThemeAsync().ContinueWith(x => x.Result.IsDarkTheme()).ContinueWith(x => isDark = x.Result);
_ = ThemeHelper.GetRootThemeAsync().ContinueWith(x => ThemeHelper.IsDarkTheme(x.Result)).ContinueWith(x => isDark = x.Result);
Provider.CompactOverlay = this.IsAppWindow() ? (ICompactOverlay)new AppWindowCompactOverlay(this.GetWindowForElement()) : new CoreWindowCompactOverlay();
}

Expand Down
61 changes: 61 additions & 0 deletions MicaForUWP/Helpers/ApiInfoHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Versioning;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Metadata;

namespace MicaForUWP.Helpers
{
/// <summary>
/// Gets information about the availability of Windows Runtime APIs.
/// </summary>
#if NET
[SupportedOSPlatform("Windows10.0.10240.0")]
#endif
[ContractVersion(typeof(FoundationContract), 0x10000u)]
public static class ApiInfoHelper
{
#region Properties

/// <summary>
/// Gets is <see cref="Windows.UI.Core.CoreWindow.ActivationMode"/> supported.
/// </summary>
#if NET
[SupportedOSPlatformGuard("Windows10.0.16299.0")]
#endif
public static bool IsActivationModeSupported { get; } = ApiInformation.IsPropertyPresent("Windows.UI.Core.CoreWindow", "ActivationMode");

#endregion

#region Methods

/// <summary>
/// Gets is <see cref="Windows.UI.Composition.Compositor.CreateBackdropBrush"/> supported.
/// </summary>
#if NET
[SupportedOSPlatformGuard("Windows10.0.14393.0")]
#endif
public static bool IsCreateBackdropBrushSupported { get; } = ApiInformation.IsMethodPresent("Windows.UI.Composition.Compositor", "CreateBackdropBrush");

/// <summary>
/// Gets is <see cref="Windows.UI.Composition.Compositor.CreateHostBackdropBrush"/> supported.
/// </summary>
#if NET
[SupportedOSPlatformGuard("Windows10.0.15063.0")]
#endif
public static bool IsCreateHostBackdropBrushSupported { get; } = ApiInformation.IsMethodPresent("Windows.UI.Composition.Compositor", "CreateHostBackdropBrush");

/// <summary>
/// Gets is <see cref="Windows.UI.Composition.Compositor.TryCreateBlurredWallpaperBackdropBrush"/> supported.
/// </summary>
#if NET
[SupportedOSPlatformGuard("Windows10.0.22000.0")]
#endif
public static bool IsTryCreateBlurredWallpaperBackdropBrushSupported { get; } = ApiInformation.IsMethodPresent("Windows.UI.Composition.Compositor", "TryCreateBlurredWallpaperBackdropBrush");

#endregion
}
}
Loading

0 comments on commit 23220b2

Please sign in to comment.