Skip to content

Commit

Permalink
Merge pull request #5 from Clrs17/main
Browse files Browse the repository at this point in the history
Fix CoreWindow creation
  • Loading branch information
wherewhere authored Nov 7, 2023
2 parents 06f8284 + 3e0da70 commit c0f85ff
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
3 changes: 2 additions & 1 deletion CoreAppUWP/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CoreAppUWP.Pages;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
using System;
using Windows.ApplicationModel.Core;
Expand Down Expand Up @@ -90,7 +91,7 @@ private void EnsureWindow(LaunchActivatedEventArgs e)
// 当导航堆栈尚未还原时,导航到第一页,
// 并通过将所需信息作为导航参数传入来配置
// 参数
rootFrame.Navigate(typeof(MainPage), e);
rootFrame.Navigate(typeof(MainPage), e, new DrillInNavigationTransitionInfo());
BackdropHelper.SetBackdrop(window, SettingsHelper.Get<BackdropType>(SettingsHelper.SelectedBackdrop));
}

Expand Down
6 changes: 5 additions & 1 deletion CoreAppUWP/Controls/DesktopWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ public static Task<DesktopWindow> CreateAsync(DispatcherQueue dispatcherQueue, A
/// <summary>
/// Attempts to activate the application window by bringing it to the foreground and setting the input focus to it.
/// </summary>
public void Activate() => AppWindow.Show();
public void Activate()
{
AppWindow.Show();
AppWindow.MoveInZOrderAtTop();
}

/// <summary>
/// Closes the application window.
Expand Down
4 changes: 2 additions & 2 deletions CoreAppUWP/Helpers/BackdropHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ public static void SetAllBackdrop(BackdropType type)
{
ActiveWindows.Values.ForEach(async x =>
{
if (x.window.Dispatcher?.HasThreadAccess == false)
if (x.window.DispatcherQueue?.HasThreadAccess == false)
{
await x.window.Dispatcher.ResumeForegroundAsync();
await x.window.DispatcherQueue.ResumeForegroundAsync();
}
x.SetBackdrop(type);
});
Expand Down
59 changes: 30 additions & 29 deletions CoreAppUWP/Helpers/ThemeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using CoreAppUWP.Common;
using CommunityToolkit.WinUI;
using CoreAppUWP.Common;
using CoreAppUWP.Controls;
using Microsoft.UI;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.ViewManagement;

namespace CoreAppUWP.Helpers
Expand All @@ -20,7 +21,8 @@ public static class ThemeHelper
private static Window CurrentApplicationWindow;

// Keep reference so it does not get optimized/garbage collected
public static UISettings UISettings { get; private set; }
public static UISettings UISettings { get; } = new UISettings();
public static AccessibilitySettings AccessibilitySettings { get; } = new AccessibilitySettings();

public static WeakEvent<bool> UISettingChanged { get; } = [];

Expand All @@ -38,13 +40,13 @@ public static ElementTheme GetActualTheme() =>
public static ElementTheme GetActualTheme(Window window) =>
window == null
? SettingsHelper.Get<ElementTheme>(SettingsHelper.SelectedAppTheme)
: window.Dispatcher?.HasThreadAccess == false
? window.Dispatcher?.AwaitableRunAsync(() =>
: window.DispatcherQueue?.HasThreadAccess == false
? window.DispatcherQueue?.EnqueueAsync(() =>
window.Content is FrameworkElement _rootElement
&& _rootElement.RequestedTheme != ElementTheme.Default
? _rootElement.RequestedTheme
: SettingsHelper.Get<ElementTheme>(SettingsHelper.SelectedAppTheme),
CoreDispatcherPriority.High)?.AwaitByTaskCompleteSource()
DispatcherQueuePriority.High)?.AwaitByTaskCompleteSource()
?? SettingsHelper.Get<ElementTheme>(SettingsHelper.SelectedAppTheme)
: window.Content is FrameworkElement rootElement
&& rootElement.RequestedTheme != ElementTheme.Default
Expand All @@ -57,13 +59,13 @@ public static Task<ElementTheme> GetActualThemeAsync() =>
public static async Task<ElementTheme> GetActualThemeAsync(Window window) =>
window == null
? SettingsHelper.Get<ElementTheme>(SettingsHelper.SelectedAppTheme)
: window.Dispatcher?.HasThreadAccess == false
? await window.Dispatcher?.AwaitableRunAsync(() =>
: window.DispatcherQueue?.HasThreadAccess == false
? await window.DispatcherQueue?.EnqueueAsync(() =>
window.Content is FrameworkElement _rootElement
&& _rootElement.RequestedTheme != ElementTheme.Default
? _rootElement.RequestedTheme
: SettingsHelper.Get<ElementTheme>(SettingsHelper.SelectedAppTheme),
CoreDispatcherPriority.High)
DispatcherQueuePriority.High)
: window.Content is FrameworkElement rootElement
&& rootElement.RequestedTheme != ElementTheme.Default
? rootElement.RequestedTheme
Expand All @@ -88,39 +90,39 @@ public static ElementTheme GetRootTheme() =>
public static ElementTheme GetRootTheme(Window window) =>
window == null
? ElementTheme.Default
: window.Dispatcher.HasThreadAccess
: window.DispatcherQueue.HasThreadAccess
? window.Content is FrameworkElement rootElement
? rootElement.RequestedTheme
: ElementTheme.Default
: window.Dispatcher.AwaitableRunAsync(() =>
: window.DispatcherQueue.EnqueueAsync(() =>
window.Content is FrameworkElement _rootElement
? _rootElement.RequestedTheme
: ElementTheme.Default,
CoreDispatcherPriority.High).AwaitByTaskCompleteSource();
DispatcherQueuePriority.High).AwaitByTaskCompleteSource();

public static Task<ElementTheme> GetRootThemeAsync() =>
GetRootThemeAsync(Window.Current ?? CurrentApplicationWindow);

public static async Task<ElementTheme> GetRootThemeAsync(Window window) =>
window == null
? ElementTheme.Default
: window.Dispatcher.HasThreadAccess
: window.DispatcherQueue.HasThreadAccess
? window.Content is FrameworkElement rootElement
? rootElement.RequestedTheme
: ElementTheme.Default
: await window.Dispatcher.AwaitableRunAsync(() =>
: await window.DispatcherQueue.EnqueueAsync(() =>
window.Content is FrameworkElement _rootElement
? _rootElement.RequestedTheme
: ElementTheme.Default,
CoreDispatcherPriority.High);
DispatcherQueuePriority.High);

public static async void SetRootTheme(ElementTheme value)
{
WindowHelper.ActiveWindows.Values.ForEach(async window =>
{
if (!window.Dispatcher.HasThreadAccess)
if (window.DispatcherQueue?.HasThreadAccess == false)
{
await window.Dispatcher.ResumeForegroundAsync();
await window.DispatcherQueue.ResumeForegroundAsync();
}
if (window.Content is FrameworkElement rootElement)
Expand Down Expand Up @@ -151,9 +153,9 @@ public static async Task SetRootThemeAsync(ElementTheme value)
{
await Task.WhenAll(WindowHelper.ActiveWindows.Values.Select(async window =>
{
if (!window.Dispatcher.HasThreadAccess)
if (window.DispatcherQueue?.HasThreadAccess == false)
{
await window.Dispatcher.ResumeForegroundAsync();
await window.DispatcherQueue.ResumeForegroundAsync();
}
if (window.Content is FrameworkElement rootElement)
Expand Down Expand Up @@ -189,7 +191,6 @@ public static void Initialize()
RootTheme = SettingsHelper.Get<ElementTheme>(SettingsHelper.SelectedAppTheme);

// Registering to color changes, thus we notice when user changes theme system wide
UISettings = new UISettings();
UISettings.ColorValuesChanged += UISettings_ColorValuesChanged;
}

Expand Down Expand Up @@ -256,9 +257,9 @@ public static void UpdateExtendViewIntoTitleBar(bool IsExtendsTitleBar)
{
WindowHelper.ActiveWindows.Values.ForEach(async window =>
{
if (window.Dispatcher?.HasThreadAccess == false)
if (window.DispatcherQueue?.HasThreadAccess == false)
{
await window.Dispatcher.ResumeForegroundAsync();
await window.DispatcherQueue.ResumeForegroundAsync();
}
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = IsExtendsTitleBar;
});
Expand All @@ -278,16 +279,16 @@ public static void UpdateExtendViewIntoTitleBar(bool IsExtendsTitleBar)
public static async void UpdateSystemCaptionButtonColors()
{
bool IsDark = await IsDarkThemeAsync();
bool IsHighContrast = new AccessibilitySettings().HighContrast;
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);

WindowHelper.ActiveWindows.Values.ForEach(async window =>
{
if (window.Dispatcher?.HasThreadAccess == false)
if (window.DispatcherQueue?.HasThreadAccess == false)
{
await window.Dispatcher.ResumeForegroundAsync();
await window.DispatcherQueue.ResumeForegroundAsync();
}
bool ExtendViewIntoTitleBar = CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar;
Expand Down Expand Up @@ -316,13 +317,13 @@ public static async void UpdateSystemCaptionButtonColors()

public static async void UpdateSystemCaptionButtonColors(Window window)
{
if (window.Dispatcher?.HasThreadAccess == false)
if (window.DispatcherQueue?.HasThreadAccess == false)
{
await window.Dispatcher.ResumeForegroundAsync();
await window.DispatcherQueue.ResumeForegroundAsync();
}

bool IsDark = window?.Content is FrameworkElement rootElement ? rootElement.RequestedTheme.IsDarkTheme() : await IsDarkThemeAsync();
bool IsHighContrast = new AccessibilitySettings().HighContrast;
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);
Expand All @@ -345,7 +346,7 @@ public static async void UpdateSystemCaptionButtonColors(DesktopWindow window)
}

bool IsDark = window?.Content is FrameworkElement rootElement ? rootElement.RequestedTheme.IsDarkTheme() : await IsDarkThemeAsync();
bool IsHighContrast = new AccessibilitySettings().HighContrast;
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);
Expand Down
3 changes: 2 additions & 1 deletion CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
Description="Create a new CoreWindow."
Header="CoreWindow"
IsClickEnabled="True"
IsEnabled="{x:Bind IsCoreWindow}"
Style="{StaticResource ClickableSettingExpanderItemStyle}"
Tag="NewWindow" />
<controls:Setting
Expand All @@ -139,6 +138,7 @@
Description="Show settings charm pane."
Header="Open Settings"
IsClickEnabled="True"
IsEnabled="{x:Bind IsCoreWindow}"
Tag="SettingsFlyout">
<controls:Setting.Icon>
<AnimatedIcon>
Expand All @@ -158,6 +158,7 @@
Description="Show search charm pane."
Header="Open Search"
IsClickEnabled="True"
IsEnabled="{x:Bind IsCoreWindow}"
Tag="SearchFlyout">
<controls:Setting.Icon>
<AnimatedIcon>
Expand Down
7 changes: 4 additions & 3 deletions CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,16 @@ private async void Button_Click(object sender, RoutedEventArgs e)
else if (this.GetWindowForElement() is DesktopWindow desktopWindow)
{ desktopWindow.AppWindow.SetPresenter(AppWindowPresenterKind.CompactOverlay); }
break;
case "NewWindow" when IsCoreWindow:
case "NewWindow":
_ = await WindowHelper.CreateWindowAsync(window =>
{
if (SettingsHelper.Get<bool>(SettingsHelper.IsExtendsTitleBar))
{ CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true; }
Frame _frame = new();
window.Content = _frame;
ThemeHelper.Initialize(window);
_ = _frame.Navigate(typeof(MainPage), null, new DrillInNavigationTransitionInfo());
try { _ = _frame.Navigate(typeof(MainPage), null, new DrillInNavigationTransitionInfo()); }
catch { _ = _frame.Navigate(typeof(MainPage)); }
BackdropHelper.SetBackdrop(window, SettingsHelper.Get<BackdropType>(SettingsHelper.SelectedBackdrop));
});
break;
Expand All @@ -118,7 +119,7 @@ private async void Button_Click(object sender, RoutedEventArgs e)
AppWindow appWindow = window.AppWindow;
appWindow.Title = Package.Current.DisplayName;
appWindow.SetIcon("favicon.ico");
appWindow.Show();
window.Activate();
break;
case "SearchFlyout" when SettingsPaneRegister.IsSearchPaneSupported:
SearchPane.GetForCurrentView().Show();
Expand Down

0 comments on commit c0f85ff

Please sign in to comment.