Skip to content

Commit

Permalink
同步新代码
Browse files Browse the repository at this point in the history
更新 Readme
  • Loading branch information
wherewhere committed Apr 21, 2024
1 parent b292bea commit 9e520c7
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 25 deletions.
92 changes: 77 additions & 15 deletions MicaDemo/Common/ThreadSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,33 @@ public interface IThreadSwitcher : INotifyCompletion
IThreadSwitcher GetAwaiter();
}

/// <summary>
/// The interface of helper type for switch thread.
/// </summary>
/// <typeparam name="T">The type of the result of <see cref="GetAwaiter"/>.</typeparam>
public interface IThreadSwitcher<out T> : IThreadSwitcher
{
/// <summary>
/// Gets an awaiter used to await <typeparamref name="T"/>.
/// </summary>
/// <returns>A <typeparamref name="T"/> awaiter instance.</returns>
new T GetAwaiter();
}

/// <summary>
/// A helper type for switch thread by <see cref="CoreDispatcher"/>. This type is not intended to be used directly from your code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public readonly struct CoreDispatcherThreadSwitcher : IThreadSwitcher
public readonly struct CoreDispatcherThreadSwitcher : IThreadSwitcher<CoreDispatcherThreadSwitcher>
{
/// <summary>
/// A <see cref="CoreDispatcher"/> whose foreground thread to switch execution to.
/// </summary>
private readonly CoreDispatcher dispatcher;

/// <summary>
/// Specifies the priority for event dispatch.
/// </summary>
private readonly CoreDispatcherPriority priority;

/// <summary>
Expand All @@ -57,10 +77,7 @@ public CoreDispatcherThreadSwitcher(CoreDispatcher dispatcher, CoreDispatcherPri
/// <inheritdoc/>
public void GetResult() { }

/// <summary>
/// Gets an awaiter used to await this <see cref="CoreDispatcherThreadSwitcher"/>.
/// </summary>
/// <returns>An awaiter instance.</returns>
/// <inheritdoc/>
public CoreDispatcherThreadSwitcher GetAwaiter() => this;

/// <inheritdoc/>
Expand All @@ -74,9 +91,16 @@ public void GetResult() { }
/// A helper type for switch thread by <see cref="DispatcherQueue"/>. This type is not intended to be used directly from your code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public readonly struct DispatcherQueueThreadSwitcher : IThreadSwitcher
public readonly struct DispatcherQueueThreadSwitcher : IThreadSwitcher<DispatcherQueueThreadSwitcher>
{
/// <summary>
/// A <see cref="DispatcherQueue"/> whose foreground thread to switch execution to.
/// </summary>
private readonly DispatcherQueue dispatcher;

/// <summary>
/// Specifies the priority for event dispatch.
/// </summary>
private readonly DispatcherQueuePriority priority;

/// <summary>
Expand All @@ -97,10 +121,7 @@ public DispatcherQueueThreadSwitcher(DispatcherQueue dispatcher, DispatcherQueue
/// <inheritdoc/>
public void GetResult() { }

/// <summary>
/// Gets an awaiter used to await this <see cref="DispatcherQueueThreadSwitcher"/>.
/// </summary>
/// <returns>An awaiter instance.</returns>
/// <inheritdoc/>
public DispatcherQueueThreadSwitcher GetAwaiter() => this;

/// <inheritdoc/>
Expand All @@ -110,12 +131,49 @@ public void GetResult() { }
public void OnCompleted(Action continuation) => _ = dispatcher.TryEnqueue(priority, () => continuation());
}

/// <summary>
/// A helper type for switch thread by <see cref="SynchronizationContext"/>. This type is not intended to be used directly from your code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public readonly struct SynchronizationContextThreadSwitcher : IThreadSwitcher<SynchronizationContextThreadSwitcher>
{
/// <summary>
/// Specifies the priority for event dispatch.
/// </summary>
private readonly SynchronizationContext context;

/// <summary>
/// Initializes a new instance of the <see cref="SynchronizationContextThreadSwitcher"/> struct.
/// </summary>
/// <param name="context">A <see cref="SynchronizationContext"/> whose foreground thread to switch execution to.</param>
public SynchronizationContextThreadSwitcher(SynchronizationContext context) => this.context = context;

/// <inheritdoc/>
public bool IsCompleted => !(this.context is SynchronizationContext context)
|| SynchronizationContext.Current == context;

/// <inheritdoc/>
public void GetResult() { }

/// <inheritdoc/>
public SynchronizationContextThreadSwitcher GetAwaiter() => this;

/// <inheritdoc/>
IThreadSwitcher IThreadSwitcher.GetAwaiter() => this;

/// <inheritdoc/>
public void OnCompleted(Action continuation) => context.Post(_ => continuation(), null);
}

/// <summary>
/// A helper type for switch thread by <see cref="ThreadPool"/>. This type is not intended to be used directly from your code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public readonly struct ThreadPoolThreadSwitcher : IThreadSwitcher
public readonly struct ThreadPoolThreadSwitcher : IThreadSwitcher<ThreadPoolThreadSwitcher>
{
/// <summary>
/// Specifies the priority for event dispatch.
/// </summary>
private readonly WorkItemPriority priority;

/// <summary>
Expand All @@ -130,10 +188,7 @@ public void GetResult() { }
/// <inheritdoc/>
public void GetResult() { }

/// <summary>
/// Gets an awaiter used to await this <see cref="ThreadPoolThreadSwitcher"/>.
/// </summary>
/// <returns>An awaiter instance.</returns>
/// <inheritdoc/>
public ThreadPoolThreadSwitcher GetAwaiter() => this;

/// <inheritdoc/>
Expand Down Expand Up @@ -169,6 +224,13 @@ public static class ThreadSwitcher
/// <returns>An object that you can <see langword="await"/>.</returns>
public static CoreDispatcherThreadSwitcher ResumeForegroundAsync(this CoreDispatcher dispatcher, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) => new CoreDispatcherThreadSwitcher(dispatcher, priority);

/// <summary>
/// A helper function—for use within a coroutine—that you can <see langword="await"/> to switch execution to a specific foreground thread.
/// </summary>
/// <param name="context">A <see cref="SynchronizationContext"/> whose foreground thread to switch execution to.</param>
/// <returns>An object that you can <see langword="await"/>.</returns>
public static SynchronizationContextThreadSwitcher ResumeForegroundAsync(this SynchronizationContext context) => new SynchronizationContextThreadSwitcher(context);

/// <summary>
/// A helper function—for use within a coroutine—that returns control to the caller, and then immediately resumes execution on a thread pool thread.
/// </summary>
Expand Down
19 changes: 16 additions & 3 deletions MicaDemo/Helpers/ThemeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MicaDemo.Common;
using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
Expand All @@ -19,8 +20,20 @@ public static class ThemeHelper
// Keep reference so it does not get optimized/garbage collected
public static UISettings UISettings { get; } = new UISettings();
public static AccessibilitySettings AccessibilitySettings { get; } = new AccessibilitySettings();

#region UISettingChanged

public static WeakEvent<bool> UISettingChanged { get; } = new WeakEvent<bool>();
private static readonly WeakEvent<bool> actions = new WeakEvent<bool>();

public static event Action<bool> UISettingChanged
{
add => actions.Add(value);
remove => actions.Remove(value);
}

public static void InvokeUISettingChanged(bool value) => actions.Invoke(value);

#endregion

#region RootTheme

Expand Down Expand Up @@ -62,7 +75,7 @@ await Task.WhenAll(WindowHelper.ActiveWindows.Values.Select(async window =>
}));

UpdateSystemCaptionButtonColors();
UISettingChanged.Invoke(await IsDarkThemeAsync());
InvokeUISettingChanged(await IsDarkThemeAsync());
}

#endregion
Expand Down Expand Up @@ -96,7 +109,7 @@ public static async void Initialize(AppWindow window)
private static async void UISettings_ColorValuesChanged(UISettings sender, object args)
{
UpdateSystemCaptionButtonColors();
UISettingChanged.Invoke(await IsDarkThemeAsync());
InvokeUISettingChanged(await IsDarkThemeAsync());
}

public static async Task<bool> IsDarkThemeAsync()
Expand Down
2 changes: 1 addition & 1 deletion MicaDemo/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MicaDemo")]
[assembly: AssemblyCopyright("Copyright © 2017 - 2023 wherewhere. All Rights Reserved.")]
[assembly: AssemblyCopyright("Copyright © 2017 - 2024 wherewhere. All Rights Reserved.")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down
2 changes: 1 addition & 1 deletion MicaForUWP/MicaForUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Authors>wherewhere</Authors>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<Copyright>Copyright © 2017 - 2023 wherewhere. All Rights Reserved.</Copyright>
<Copyright>Copyright © 2017 - 2024 wherewhere. All Rights Reserved.</Copyright>
<CsWinRTIncludes>Microsoft.Graphics.Canvas</CsWinRTIncludes>
<Deterministic>true</Deterministic>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<img alt="Mica For UWP LOGO" src="logo.png" width="200px"/>

# Mica For UWP
一个无需WinUI便可实现的Mica笔刷库

[![Microsoft Store](https://img.shields.io/badge/download-Demo-magenta.svg?label=Microsoft%20Store&logo=Microsoft&style=flat-square&color=11a2f8)](https://www.microsoft.com/store/apps/9NK6JSM7MDNX "Demo")
一个无需 WinUI 便可实现的 Mica 笔刷

[![LICENSE](https://img.shields.io/github/license/wherewhere/Mica-For-UWP.svg?label=License&style=flat-square)](https://github.com/wherewhere/Mica-For-UWP/blob/master/LICENSE "LICENSE")
[![Issues](https://img.shields.io/github/issues/wherewhere/Mica-For-UWP.svg?label=Issues&style=flat-square)](https://github.com/wherewhere/Mica-For-UWP/issues "Issues")
[![Stargazers](https://img.shields.io/github/stars/wherewhere/Mica-For-UWP.svg?label=Stars&style=flat-square)](https://github.com/wherewhere/Mica-For-UWP/stargazers "Stargazers")

[![GitHub All Releases](https://img.shields.io/github/downloads/wherewhere/Mica-For-UWP/total.svg?label=DOWNLOAD&logo=github&style=for-the-badge)](https://github.com/wherewhere/Mica-For-UWP/releases/latest "GitHub All Releases")
[![Microsoft Store](https://img.shields.io/badge/download-Demo-magenta.svg?label=Microsoft%20Store&logo=Microsoft&style=for-the-badge&color=11a2f8)](https://www.microsoft.com/store/apps/9NK6JSM7MDNX "Demo")
[![NuGet](https://img.shields.io/nuget/dt/MicaForUWP.svg?logo=NuGet&style=for-the-badge)](https://www.nuget.org/packages/MicaForUWP "NuGet")

## 目录
- [Mica For UWP](#mica-for-uwp)
- [目录](#目录)
- [如何使用](#如何使用)
- [使用到的模块](#使用到的模块)
- [参与人员](#参与人员)

## 如何使用
和普通的笔刷一样
```
```xml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -41,3 +41,9 @@
TintColor="{ThemeResource SolidBackgroundFillColorBase}" />
</ResourceDictionary>
```

## 使用到的模块
- [Win2D](https://github.com/Microsoft/Win2D "Win2D")

## 参与人员
[![Contributors](https://contrib.rocks/image?repo=wherewhere/Mica-For-UWP)](https://github.com/wherewhere/Mica-For-UWP/graphs/contributors "Contributors")

0 comments on commit 9e520c7

Please sign in to comment.