-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
274 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
....Hutao/Snap.Hutao/Control/Behavior/PeriodicInvokeCommandOrOnActualThemeChangedBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using CommunityToolkit.WinUI.Behaviors; | ||
using Microsoft.UI.Xaml; | ||
|
||
namespace Snap.Hutao.Control.Behavior; | ||
|
||
[DependencyProperty("Period", typeof(TimeSpan))] | ||
[DependencyProperty("Command", typeof(ICommand))] | ||
[DependencyProperty("CommandParameter", typeof(object))] | ||
internal sealed partial class PeriodicInvokeCommandOrOnActualThemeChangedBehavior : BehaviorBase<FrameworkElement>, IDisposable | ||
{ | ||
private TaskCompletionSource acutalThemeChangedTaskCompletionSource = new(); | ||
private CancellationTokenSource periodicTimerCancellationTokenSource = new(); | ||
|
||
public void Dispose() | ||
{ | ||
periodicTimerCancellationTokenSource.Dispose(); | ||
} | ||
|
||
protected override bool Initialize() | ||
{ | ||
AssociatedObject.ActualThemeChanged += OnActualThemeChanged; | ||
return true; | ||
} | ||
|
||
protected override void OnAssociatedObjectLoaded() | ||
{ | ||
RunCoreAsync().SafeForget(); | ||
} | ||
|
||
protected override bool Uninitialize() | ||
{ | ||
AssociatedObject.ActualThemeChanged -= OnActualThemeChanged; | ||
return true; | ||
} | ||
|
||
private void OnActualThemeChanged(FrameworkElement sender, object args) | ||
{ | ||
acutalThemeChangedTaskCompletionSource.TrySetResult(); | ||
periodicTimerCancellationTokenSource.Cancel(); | ||
} | ||
|
||
private void TryExecuteCommand() | ||
{ | ||
if (AssociatedObject is null) | ||
{ | ||
return; | ||
} | ||
|
||
if (Command is not null && Command.CanExecute(CommandParameter)) | ||
{ | ||
Command.Execute(CommandParameter); | ||
} | ||
} | ||
|
||
private async ValueTask RunCoreAsync() | ||
{ | ||
using (PeriodicTimer timer = new(Period)) | ||
{ | ||
do | ||
{ | ||
if (!IsAttached) | ||
{ | ||
break; | ||
} | ||
|
||
TryExecuteCommand(); | ||
|
||
try | ||
{ | ||
Task nextTickTask = timer.WaitForNextTickAsync(periodicTimerCancellationTokenSource.Token).AsTask(); | ||
await Task.WhenAny(nextTickTask, acutalThemeChangedTaskCompletionSource.Task).ConfigureAwait(false); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
} | ||
|
||
acutalThemeChangedTaskCompletionSource = new(); | ||
periodicTimerCancellationTokenSource = new(); | ||
} | ||
while (true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.