diff --git a/Directory.Build.props b/Directory.Build.props index 5785c823..7c95851f 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -11,9 +11,9 @@ - 2.4.2 - 2.4.2.0 - 2.4.2.0 + 2.5.0 + 2.5.0.0 + 2.5.0.0 latest-recommended true diff --git a/Interop/RepoAnnouncementsNative.cs b/Interop/RepoAnnouncementsNative.cs new file mode 100644 index 00000000..3b1a206e --- /dev/null +++ b/Interop/RepoAnnouncementsNative.cs @@ -0,0 +1,138 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace VNotch.Interop; + +internal sealed record PinnedAnnouncementDto( + [property: JsonPropertyName("id")] string Id, + [property: JsonPropertyName("number")] ulong Number, + [property: JsonPropertyName("title")] string Title, + [property: JsonPropertyName("summary")] string Summary, + [property: JsonPropertyName("url")] string Url, + [property: JsonPropertyName("category")] string Category); + +internal sealed record FetchAnnouncementsResponseDto( + [property: JsonPropertyName("announcements")] List? Announcements, + [property: JsonPropertyName("totalPinned")] int TotalPinned, + [property: JsonPropertyName("fromCache")] bool FromCache); + +/// +/// Thin ABI 2.2 P/Invoke wrapper for pinned repository announcements in vnotch_rust.dll. +/// +internal static partial class RepoAnnouncementsNative +{ + private static readonly JsonSerializerOptions JsonOptions = new() + { + PropertyNameCaseInsensitive = true + }; + + [LibraryImport(NativeAbi.RustLibraryName)] + private static unsafe partial int vn_announcements_fetch_unseen_utf8( + byte* reqJson, + uint reqLen, + out VnOwnedBytes outJson); + + [LibraryImport(NativeAbi.RustLibraryName)] + private static unsafe partial int vn_announcements_mark_seen_utf8( + byte* reqJson, + uint reqLen, + out uint outCount); + + [LibraryImport(NativeAbi.RustLibraryName)] + private static unsafe partial int vn_announcements_parse_pinned_utf8( + byte* htmlUtf8, + uint htmlLen, + out VnOwnedBytes outJson); + + internal static unsafe FetchAnnouncementsResponseDto FetchUnseen( + string cachePath, + string? url = null, + bool forceRefresh = false) + { + byte[] reqBytes = JsonSerializer.SerializeToUtf8Bytes(new + { + cachePath, + url, + forceRefresh + }); + + VnOwnedBytes owned = default; + try + { + int status; + fixed (byte* ptr = reqBytes) + { + status = vn_announcements_fetch_unseen_utf8(ptr, checked((uint)reqBytes.Length), out owned); + } + + NativeResult.RequireSuccess(status, nameof(vn_announcements_fetch_unseen_utf8)); + ReadOnlySpan span = owned.AsSpan(); + if (span.IsEmpty) + { + return new FetchAnnouncementsResponseDto([], 0, false); + } + + return JsonSerializer.Deserialize(span, JsonOptions) + ?? new FetchAnnouncementsResponseDto([], 0, false); + } + finally + { + owned.Free(); + } + } + + internal static unsafe uint MarkSeen(string cachePath, IReadOnlyList ids) + { + if (ids.Count == 0) + { + return 0; + } + + byte[] reqBytes = JsonSerializer.SerializeToUtf8Bytes(new + { + cachePath, + ids + }); + + int status; + uint count; + fixed (byte* ptr = reqBytes) + { + status = vn_announcements_mark_seen_utf8(ptr, checked((uint)reqBytes.Length), out count); + } + + NativeResult.RequireSuccess(status, nameof(vn_announcements_mark_seen_utf8)); + return count; + } + + internal static unsafe IReadOnlyList ParsePinnedHtml(string html) + { + byte[] htmlBytes = Encoding.UTF8.GetBytes(html ?? string.Empty); + VnOwnedBytes owned = default; + try + { + int status; + fixed (byte* ptr = htmlBytes) + { + status = vn_announcements_parse_pinned_utf8(ptr, checked((uint)htmlBytes.Length), out owned); + } + + NativeResult.RequireSuccess(status, nameof(vn_announcements_parse_pinned_utf8)); + ReadOnlySpan span = owned.AsSpan(); + if (span.IsEmpty) + { + return []; + } + + return JsonSerializer.Deserialize>(span, JsonOptions) ?? []; + } + finally + { + owned.Free(); + } + } +} diff --git a/Services/RepoAnnouncementService.cs b/Services/RepoAnnouncementService.cs new file mode 100644 index 00000000..c49cdcff --- /dev/null +++ b/Services/RepoAnnouncementService.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using VNotch.Interop; + +namespace VNotch.Services; + +/// +/// Thin async facade over Rust pinned GitHub Discussions announcements fetcher and local seen-state. +/// +internal static class RepoAnnouncementService +{ + private const string CacheFileName = "announcements-cache.json"; + + internal static string DefaultCachePath => Path.Combine(AppPaths.LocalAppDataRoot, CacheFileName); + + internal static Task> FetchUnseenAsync( + bool forceRefresh = false, + CancellationToken cancellationToken = default) + { + return Task.Run>(() => + { + cancellationToken.ThrowIfCancellationRequested(); + var response = RepoAnnouncementsNative.FetchUnseen(DefaultCachePath, url: null, forceRefresh: forceRefresh); + return response.Announcements ?? []; + }, cancellationToken); + } + + internal static void MarkSeen(string announcementId) + { + if (string.IsNullOrWhiteSpace(announcementId)) + { + return; + } + + try + { + _ = RepoAnnouncementsNative.MarkSeen(DefaultCachePath, [announcementId]); + } + catch (Exception ex) + { + RuntimeLog.Warn("ANNOUNCEMENT", $"Failed to record seen announcement '{announcementId}': {ex.Message}"); + } + } +} diff --git a/Tests/packages.lock.json b/Tests/packages.lock.json index 5825e79a..c74afebd 100644 --- a/Tests/packages.lock.json +++ b/Tests/packages.lock.json @@ -198,15 +198,15 @@ "Microsoft.Extensions.DependencyInjection": "[8.0.1, )", "Microsoft.ML.OnnxRuntime.DirectML": "[1.24.4, )", "Microsoft.Web.WebView2": "[1.0.4078.44, )", - "VNotch.Application": "[2.4.2, )", - "VNotch.Core": "[2.4.2, )", + "VNotch.Application": "[2.5.0, )", + "VNotch.Core": "[2.5.0, )", "Vortice.Direct3D9": "[3.8.3, )" } }, "vnotch.application": { "type": "Project", "dependencies": { - "VNotch.Core": "[2.4.2, )" + "VNotch.Core": "[2.5.0, )" } }, "vnotch.core": { diff --git a/Tools/Assert-Coverage.ps1 b/Tools/Assert-Coverage.ps1 index a4f86b8d..84b62098 100644 --- a/Tools/Assert-Coverage.ps1 +++ b/Tools/Assert-Coverage.ps1 @@ -8,7 +8,7 @@ param( $ErrorActionPreference = 'Stop' if ($AllProductionAssemblies) { - foreach ($assembly in 'V-Notch', 'VNotch.Core', 'VNotch.Application', 'VNotch.Infrastructure.Windows') { + foreach ($assembly in 'V-Notch', 'VNotch.Core', 'VNotch.Application') { & $PSCommandPath -ResultsDirectory $ResultsDirectory -MinimumPercent $MinimumPercent -AssemblyName $assembly } return diff --git a/V-Notch.csproj b/V-Notch.csproj index f109200d..13466ccf 100644 --- a/V-Notch.csproj +++ b/V-Notch.csproj @@ -42,9 +42,9 @@ reflection, COM interop or CPU/GPU translation backends. --> true false - 2.4.2 - 2.4.2.0 - 2.4.2.0 + 2.5.0 + 2.5.0.0 + 2.5.0.0 w0otdev, viper1aa V-Notch V-Notch diff --git a/Windows/SettingsWindow.Announcements.cs b/Windows/SettingsWindow.Announcements.cs new file mode 100644 index 00000000..5dc71c0b --- /dev/null +++ b/Windows/SettingsWindow.Announcements.cs @@ -0,0 +1,195 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media; +using System.Windows.Media.Animation; +using System.Windows.Threading; +using VNotch.Interop; +using VNotch.Services; + +namespace VNotch; + +public partial class SettingsWindow +{ + private readonly Queue _repoAnnouncementsQueue = new(); + private CancellationTokenSource? _repoAnnouncementsCts; + private PinnedAnnouncementDto? _activeRepoAnnouncement; + private bool _isRepoAnnouncementBannerVisible; + private int _repoAnnouncementTotalCount; + private int _repoAnnouncementShownIndex; + + private async Task LoadRepoAnnouncementsAsync() + { + if (_isClosing || _settings.EnableLocalOnlyMode || RepoAnnouncementBanner == null) + { + return; + } + + CancelRepoAnnouncementsFetch(); + var cts = new CancellationTokenSource(); + _repoAnnouncementsCts = cts; + + try + { + IReadOnlyList unseen = + await RepoAnnouncementService.FetchUnseenAsync(forceRefresh: false, cts.Token); + if (_isClosing || cts.IsCancellationRequested || unseen.Count == 0) + { + return; + } + + await Dispatcher.InvokeAsync(() => + { + if (_isClosing || cts.IsCancellationRequested) return; + _repoAnnouncementsQueue.Clear(); + foreach (var item in unseen) + { + if (!string.IsNullOrWhiteSpace(item.Id) && !string.IsNullOrWhiteSpace(item.Title)) + { + _repoAnnouncementsQueue.Enqueue(item); + } + } + + _repoAnnouncementTotalCount = _repoAnnouncementsQueue.Count; + _repoAnnouncementShownIndex = 0; + ShowNextRepoAnnouncement(); + }, DispatcherPriority.Normal); + } + catch (OperationCanceledException) + { + } + catch (Exception ex) + { + RuntimeLog.Warn(LogCategory, $"Failed loading pinned announcements: {ex.Message}"); + } + } + + private void ShowNextRepoAnnouncement() + { + if (RepoAnnouncementBanner == null || _isClosing) + { + return; + } + + if (_repoAnnouncementsQueue.Count == 0) + { + _activeRepoAnnouncement = null; + HideRepoAnnouncementBanner(); + return; + } + + PinnedAnnouncementDto next = _repoAnnouncementsQueue.Dequeue(); + _activeRepoAnnouncement = next; + _repoAnnouncementShownIndex++; + + RepoAnnouncementTitle.Text = next.Title; + RepoAnnouncementMessage.Text = !string.IsNullOrWhiteSpace(next.Summary) + ? next.Summary + : "Pinned announcement on GitHub Discussions."; + RepoAnnouncementDetailsButton.Content = Loc.Get("settings.cpp26Banner.details"); + RepoAnnouncementDismissButton.Content = Loc.Get("settings.cpp26Banner.dismiss"); + + if (_repoAnnouncementTotalCount > 1) + { + RepoAnnouncementBadgeText.Text = $"{_repoAnnouncementShownIndex}/{_repoAnnouncementTotalCount}"; + RepoAnnouncementBadge.Visibility = Visibility.Visible; + } + else + { + RepoAnnouncementBadge.Visibility = Visibility.Collapsed; + } + + if (_isRepoAnnouncementBannerVisible && RepoAnnouncementBanner.Visibility == Visibility.Visible) + { + return; + } + + _isRepoAnnouncementBannerVisible = true; + RepoAnnouncementBanner.Visibility = Visibility.Visible; + + int fps = AnimationConfig.TargetFps; + var ease = new ExponentialEase { EasingMode = EasingMode.EaseOut, Exponent = 6 }; + + RepoAnnouncementBanner.BeginAnimation(OpacityProperty, null); + RepoAnnouncementTranslate.BeginAnimation(TranslateTransform.YProperty, null); + + var fade = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(350)) { EasingFunction = ease }; + var slide = new DoubleAnimation(20, 0, TimeSpan.FromMilliseconds(350)) { EasingFunction = ease }; + + Timeline.SetDesiredFrameRate(fade, fps); + Timeline.SetDesiredFrameRate(slide, fps); + + RepoAnnouncementBanner.BeginAnimation(OpacityProperty, fade); + RepoAnnouncementTranslate.BeginAnimation(TranslateTransform.YProperty, slide); + } + + private void HideRepoAnnouncementBanner() + { + if (RepoAnnouncementBanner == null || !_isRepoAnnouncementBannerVisible) + { + return; + } + + _isRepoAnnouncementBannerVisible = false; + int fps = AnimationConfig.TargetFps; + var ease = new ExponentialEase { EasingMode = EasingMode.EaseIn, Exponent = 5 }; + + var fade = new DoubleAnimation(RepoAnnouncementBanner.Opacity, 0, TimeSpan.FromMilliseconds(250)) { EasingFunction = ease }; + var slide = new DoubleAnimation(RepoAnnouncementTranslate.Y, 20, TimeSpan.FromMilliseconds(250)) { EasingFunction = ease }; + + Timeline.SetDesiredFrameRate(fade, fps); + Timeline.SetDesiredFrameRate(slide, fps); + + fade.Completed += (_, _) => + { + if (!_isRepoAnnouncementBannerVisible) + { + RepoAnnouncementBanner.BeginAnimation(OpacityProperty, null); + RepoAnnouncementTranslate.BeginAnimation(TranslateTransform.YProperty, null); + RepoAnnouncementBanner.Opacity = 0; + RepoAnnouncementTranslate.Y = 20; + RepoAnnouncementBanner.Visibility = Visibility.Collapsed; + } + }; + + RepoAnnouncementBanner.BeginAnimation(OpacityProperty, fade); + RepoAnnouncementTranslate.BeginAnimation(TranslateTransform.YProperty, slide); + } + + private void RepoAnnouncementDetails_Click(object sender, RoutedEventArgs e) + { + var current = _activeRepoAnnouncement; + if (current != null) + { + string seenId = current.Id; + _ = Task.Run(() => RepoAnnouncementService.MarkSeen(seenId)); + SafeLauncher.TryOpenUrl(current.Url); + } + + ShowNextRepoAnnouncement(); + } + + private void RepoAnnouncementDismiss_Click(object sender, RoutedEventArgs e) + { + var current = _activeRepoAnnouncement; + if (current != null) + { + string seenId = current.Id; + _ = Task.Run(() => RepoAnnouncementService.MarkSeen(seenId)); + } + + ShowNextRepoAnnouncement(); + } + + private void CancelRepoAnnouncementsFetch() + { + var cts = Interlocked.Exchange(ref _repoAnnouncementsCts, null); + if (cts != null) + { + try { cts.Cancel(); } catch { } + cts.Dispose(); + } + } +} diff --git a/Windows/SettingsWindow.FirstOpen.cs b/Windows/SettingsWindow.FirstOpen.cs index 67585494..8d01c680 100644 --- a/Windows/SettingsWindow.FirstOpen.cs +++ b/Windows/SettingsWindow.FirstOpen.cs @@ -151,5 +151,6 @@ private void EnsureBackgroundInitializationStarted() { _updatePresenter.CheckForUpdatesAsync().SafeFireAndForget("SETTINGS-UPDATE-CHECK"); } + LoadRepoAnnouncementsAsync().SafeFireAndForget("SETTINGS-ANNOUNCEMENTS"); } } diff --git a/Windows/SettingsWindow.GlassSurface.cs b/Windows/SettingsWindow.GlassSurface.cs index a38e7705..a86ca1de 100644 --- a/Windows/SettingsWindow.GlassSurface.cs +++ b/Windows/SettingsWindow.GlassSurface.cs @@ -142,6 +142,7 @@ protected override void OnClosed(EventArgs e) StopAndDisposeTagTimer(ImportSettingsButton); StopAndDisposeTagTimer(CheckUpdateButton); _updatePresenter.Dispose(); + CancelRepoAnnouncementsFetch(); Loc.ReleaseTranslationIndex(); _monitorChoicesClosed = true; UnsubscribeDisplaySettingsChanged(); diff --git a/Windows/SettingsWindow.xaml b/Windows/SettingsWindow.xaml index 807374b6..0a9f0bae 100644 --- a/Windows/SettingsWindow.xaml +++ b/Windows/SettingsWindow.xaml @@ -4161,6 +4161,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +