-Komplette view überarbeitet für Thunderstore mit mods suche, vorschau, markdown (readme) etc
todo: wenn neuer mod aufgerufen wird, soll die zurückfunktion deaktviert sein oder so das sie den mod mit aktualisiert zu dem der davor ausgewählt war (:
This commit is contained in:
@@ -6,8 +6,8 @@
|
||||
mc:Ignorable="d" d:DesignWidth="1424" d:DesignHeight="714"
|
||||
x:Class="Valheim_Dashboard.Views.MainWindow"
|
||||
x:DataType="vm:MainWindowViewModel" CanResize="false"
|
||||
Icon="/Assets/avalonia-logo.ico"
|
||||
Title="Valheim_Dashboard"
|
||||
Icon="/Assets/vd.png"
|
||||
Title="Valheim Dashboard by tbS"
|
||||
>
|
||||
|
||||
<Design.DataContext>
|
||||
|
||||
@@ -52,7 +52,7 @@
|
||||
<TextBlock Text="Mods" FontSize="28" FontWeight="Bold" Grid.Row="0"/>
|
||||
|
||||
<!-- ListBox füllt jetzt automatisch den Rest -->
|
||||
<StackPanel IsVisible="{Binding !IsModsLoading}">
|
||||
<StackPanel IsVisible="{Binding !IsModsLoading}" Grid.Row="1">
|
||||
<ListBox ItemsSource="{Binding Mods}" SelectedItem="{Binding SelectedMod}">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
|
||||
@@ -152,7 +152,9 @@ public partial class ServerSettingsView : UserControl
|
||||
}
|
||||
else
|
||||
{
|
||||
mainWindowViewModel.SwitchToFTP(ftpClient);
|
||||
// Übergabe des Clients an ViewModel
|
||||
mainWindowViewModel.SwitchToFTP(FtpViewModel._ftpClient);
|
||||
//mainWindowViewModel.SwitchToFTP(ftpClient);
|
||||
}
|
||||
|
||||
|
||||
|
||||
23
Valheim Dashboard/Views/StoreView.axaml
Normal file
23
Valheim Dashboard/Views/StoreView.axaml
Normal file
@@ -0,0 +1,23 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d" d:DesignWidth="1280" d:DesignHeight="800"
|
||||
x:Class="Valheim_Dashboard.Views.StoreView"
|
||||
xmlns:vm="clr-namespace:Valheim_Dashboard.ViewModels"
|
||||
x:DataType="vm:StoreViewModel"
|
||||
>
|
||||
|
||||
<DockPanel Margin="20">
|
||||
|
||||
<!-- Navigation -->
|
||||
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Spacing="10">
|
||||
<Button Content="← Zurück" Command="{Binding BackCommand}" />
|
||||
<Button Content="Thunderstore" Command="{Binding ShowThunderstoreCommand}" />
|
||||
<Button Content="NexusMods" Command="{Binding ShowNexusCommand}" />
|
||||
</StackPanel>
|
||||
|
||||
<!-- Innerer Bereich -->
|
||||
<ContentControl Content="{Binding CurrentStoreView}" />
|
||||
</DockPanel>
|
||||
</UserControl>
|
||||
33
Valheim Dashboard/Views/StoreView.axaml.cs
Normal file
33
Valheim Dashboard/Views/StoreView.axaml.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Reactive.Linq;
|
||||
using Avalonia.Animation;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.ReactiveUI;
|
||||
using Avalonia.Styling;
|
||||
using ReactiveUI;
|
||||
using Valheim_Dashboard.ViewModels;
|
||||
using static Valheim_Dashboard.Animationen;
|
||||
using Valheim_Dashboard.Views;
|
||||
|
||||
namespace Valheim_Dashboard.Views
|
||||
{
|
||||
public partial class StoreView : ReactiveUserControl<StoreViewModel>
|
||||
{
|
||||
|
||||
|
||||
|
||||
public StoreView()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
195
Valheim Dashboard/Views/ThunderstoreView.axaml
Normal file
195
Valheim Dashboard/Views/ThunderstoreView.axaml
Normal file
@@ -0,0 +1,195 @@
|
||||
<UserControl
|
||||
xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vm="clr-namespace:Valheim_Dashboard.ViewModels"
|
||||
xmlns:models="clr-namespace:Valheim_Dashboard.Models"
|
||||
xmlns:li="using:LoadingIndicators.Avalonia"
|
||||
xmlns:conv="clr-namespace:Valheim_Dashboard.Converters"
|
||||
xmlns:md="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
|
||||
xmlns:ctxt="clr-namespace:ColorTextBlock.Avalonia;assembly=ColorTextBlock.Avalonia"
|
||||
xmlns:wv="using:AvaloniaWebView"
|
||||
mc:Ignorable="d"
|
||||
x:Class="Valheim_Dashboard.Views.ThunderstoreView"
|
||||
x:DataType="vm:ThunderstoreViewModel">
|
||||
|
||||
<UserControl.Resources>
|
||||
<conv:BoolToColSpanConverter x:Key="ColSpanConverter"/>
|
||||
</UserControl.Resources>
|
||||
|
||||
<Grid ColumnDefinitions="*,750" RowDefinitions="Auto,*" Margin="16">
|
||||
<!-- Header -->
|
||||
<StackPanel Grid.ColumnSpan="2" Orientation="Horizontal" Spacing="12" Margin="0,0,0,12">
|
||||
<TextBlock Text="Thunderstore" FontSize="28" FontWeight="Bold"/>
|
||||
<StackPanel Orientation="Horizontal" Spacing="6" Margin="12,0,0,0" VerticalAlignment="Center">
|
||||
<TextBox Width="260"
|
||||
Watermark="Suchen…"
|
||||
Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
<Button Content="Suchen" Command="{Binding RunSearchCommand}"/>
|
||||
<Button Content="✕" Command="{Binding ClearSearchCommand}" ToolTip.Tip="Suche leeren"/>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
|
||||
<!-- Linke Seite -->
|
||||
<Border Grid.Row="1"
|
||||
Grid.ColumnSpan="{Binding HasSelection, Converter={StaticResource ColSpanConverter}}"
|
||||
Margin="0,0,8,0"
|
||||
BorderBrush="#333" BorderThickness="1"
|
||||
CornerRadius="8" Padding="8">
|
||||
<ListBox x:Name="ModsList"
|
||||
ItemsSource="{Binding ModsWithFooter}"
|
||||
SelectedItem="{Binding SelectedMod, Mode=TwoWay}"
|
||||
SelectionMode="Single">
|
||||
<ListBox.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<VirtualizingStackPanel/>
|
||||
</ItemsPanelTemplate>
|
||||
</ListBox.ItemsPanel>
|
||||
|
||||
<ListBox.DataTemplates>
|
||||
<!-- Mod-Kachel -->
|
||||
<DataTemplate x:DataType="models:StoreModInfo">
|
||||
<Border Margin="0,6" Padding="10" Background="#1D1D1D" CornerRadius="10">
|
||||
<Grid ColumnDefinitions="Auto,*,Auto" VerticalAlignment="Center">
|
||||
<Image Width="56" Height="56" Source="{Binding Icon}"
|
||||
Stretch="UniformToFill" ClipToBounds="True"/>
|
||||
<StackPanel Grid.Column="1" Margin="12,0,12,0">
|
||||
<TextBlock Text="{Binding Name}" FontWeight="SemiBold" FontSize="16"/>
|
||||
<TextBlock Text="{Binding Description}" FontSize="12" Foreground="#AAAAAA"
|
||||
TextWrapping="Wrap" MaxLines="2" TextTrimming="CharacterEllipsis"/>
|
||||
</StackPanel>
|
||||
<Button Grid.Column="2" MinWidth="120" Height="36" VerticalAlignment="Center"
|
||||
Content="Installieren"
|
||||
Command="{Binding $parent[UserControl].DataContext.InstallModCommand}"
|
||||
CommandParameter="{Binding}"/>
|
||||
</Grid>
|
||||
</Border>
|
||||
</DataTemplate>
|
||||
|
||||
<!-- Footer -->
|
||||
<DataTemplate x:DataType="models:ListFooterItem">
|
||||
<StackPanel Margin="0,10,0,6"
|
||||
HorizontalAlignment="Center"
|
||||
IsHitTestVisible="False"
|
||||
DataContext="{Binding $parent[UserControl].DataContext}">
|
||||
<StackPanel Orientation="Horizontal" Spacing="10" HorizontalAlignment="Center">
|
||||
<li:LoadingIndicator Width="22" Height="22"
|
||||
IsActive="{Binding IsLoading}"
|
||||
IsVisible="{Binding IsFooterVisible}"
|
||||
Mode="Arcs" SpeedRatio="1.2"/>
|
||||
<TextBlock Text="– Ende erreicht –"
|
||||
Foreground="#BBBBBB"
|
||||
IsVisible="{Binding IsListEndVisible}"/>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</DataTemplate>
|
||||
</ListBox.DataTemplates>
|
||||
</ListBox>
|
||||
</Border>
|
||||
|
||||
<!-- Rechte Seite: nur wenn ausgewählt -->
|
||||
<Border Grid.Column="1" Grid.Row="1"
|
||||
Margin="8,0,0,0"
|
||||
BorderBrush="#333" BorderThickness="1"
|
||||
CornerRadius="8" Padding="16"
|
||||
IsVisible="{Binding HasSelection}">
|
||||
<!-- WICHTIG: an ModDetails binden, nicht an SelectedMod -->
|
||||
<ContentControl Content="{Binding ModDetails}">
|
||||
<ContentControl.ContentTemplate>
|
||||
<DataTemplate x:DataType="vm:ModDetailsViewModel">
|
||||
<Grid RowDefinitions="Auto,*">
|
||||
<StackPanel Orientation="Horizontal" Spacing="12">
|
||||
<Image Source="{Binding IconBitmap}" Width="64" Height="64"/>
|
||||
<StackPanel>
|
||||
<TextBlock Text="{Binding Package.FullName}" FontSize="20" FontWeight="Bold"/>
|
||||
<StackPanel Orientation="Horizontal" Spacing="8">
|
||||
<Border CornerRadius="6" Padding="6,2" Background="#223344">
|
||||
<TextBlock Text="{Binding DisplayTotalDownloads}"/>
|
||||
</Border>
|
||||
<Border CornerRadius="6" Padding="6,2" Background="#224422">
|
||||
<TextBlock Text="{Binding DisplayRating}"/>
|
||||
</Border>
|
||||
<Border CornerRadius="6" Padding="6,2" Background="#664400"
|
||||
IsVisible="{Binding Package.IsDeprecated}">
|
||||
<TextBlock Text="Deprecated"/>
|
||||
</Border>
|
||||
</StackPanel>
|
||||
<TextBlock Text="{Binding Package.Latest.VersionNumber}" Opacity="0.7"/>
|
||||
</StackPanel>
|
||||
<Button Content="Webseite"
|
||||
HorizontalAlignment="Right"
|
||||
Command="{Binding OpenUrlCommand}"
|
||||
CommandParameter="{Binding Package.Latest.WebsiteUrl}"/>
|
||||
</StackPanel>
|
||||
|
||||
<TabControl Grid.Row="1" Margin="0,12,0,0">
|
||||
<TabItem Header="Übersicht">
|
||||
<ScrollViewer>
|
||||
<StackPanel Spacing="8">
|
||||
<TextBlock Text="{Binding Package.Latest.Description}" TextWrapping="Wrap"/>
|
||||
|
||||
<UniformGrid Columns="2" Margin="0,8,0,0">
|
||||
<StackPanel>
|
||||
<TextBlock Text="Owner" FontWeight="Bold"/>
|
||||
<TextBlock Text="{Binding Package.Owner}"/>
|
||||
</StackPanel>
|
||||
<StackPanel>
|
||||
<TextBlock Text="Created" FontWeight="Bold"/>
|
||||
<TextBlock Text="{Binding Package.DateCreated, StringFormat={}{0:yyyy-MM-dd}}"/>
|
||||
</StackPanel>
|
||||
<StackPanel>
|
||||
<TextBlock Text="Updated" FontWeight="Bold"/>
|
||||
<TextBlock Text="{Binding Package.DateUpdated, StringFormat={}{0:yyyy-MM-dd}}"/>
|
||||
</StackPanel>
|
||||
<StackPanel>
|
||||
<TextBlock Text="Version Downloads" FontWeight="Bold"/>
|
||||
<TextBlock Text="{Binding Package.Latest.Downloads, StringFormat={}{0:N0}}"/>
|
||||
</StackPanel>
|
||||
</UniformGrid>
|
||||
|
||||
<StackPanel>
|
||||
<TextBlock Text="Dependencies" FontWeight="Bold" Margin="0,8,0,0"/>
|
||||
<TextBlock Text="{Binding DependenciesJoined}" TextWrapping="Wrap"/>
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel>
|
||||
<TextBlock Text="Kategorien" FontWeight="Bold" Margin="0,8,0,0"/>
|
||||
<TextBlock Text="{Binding CategoriesJoined}" TextWrapping="Wrap"/>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
</TabItem>
|
||||
|
||||
<TabItem Header="Readme">
|
||||
<Grid RowDefinitions="Auto,*">
|
||||
<StackPanel Orientation="Horizontal" Spacing="6" Margin="0,0,0,8" Grid.Row="0">
|
||||
<Button Content="◀" Click="OnBackClick"/>
|
||||
<Button Content="▶" Click="OnForwardClick"/>
|
||||
<Button Content="↺" Click="OnHomeClick"/>
|
||||
</StackPanel>
|
||||
|
||||
<wv:WebView x:Name="ReadmeWeb"
|
||||
AttachedToVisualTree="ReadmeWeb_OnAttached"
|
||||
DetachedFromVisualTree="ReadmeWeb_OnDetached"
|
||||
Grid.Row="1"/>
|
||||
|
||||
<li:LoadingIndicator IsActive="{Binding IsLoading}"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
Width="28" Height="28"
|
||||
Mode="Arcs" />
|
||||
<TextBlock Text="{Binding Error}" Foreground="Tomato"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
IsVisible="{Binding HasError}"/>
|
||||
</Grid>
|
||||
</TabItem>
|
||||
</TabControl>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ContentControl.ContentTemplate>
|
||||
</ContentControl>
|
||||
</Border>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
522
Valheim Dashboard/Views/ThunderstoreView.axaml.cs
Normal file
522
Valheim Dashboard/Views/ThunderstoreView.axaml.cs
Normal file
@@ -0,0 +1,522 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Presenters;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
using Avalonia.WebView;
|
||||
using AvaloniaWebView;
|
||||
using ExCSS;
|
||||
using Markdown.Avalonia;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Valheim_Dashboard.ViewModels;
|
||||
using WebViewCore.Events;
|
||||
|
||||
namespace Valheim_Dashboard.Views
|
||||
{
|
||||
public partial class ThunderstoreView : UserControl
|
||||
{
|
||||
private ScrollViewer? _scroll;
|
||||
private bool _resetting;
|
||||
private WebView? _readmeWeb;
|
||||
private DispatcherTimer? _bridgeTimer;
|
||||
private int _externalDepth;
|
||||
|
||||
// Bridge / Navigation
|
||||
private string? _lastDrained; // entprellt JS-Queue
|
||||
private string? _lastHtmlKey; // zuletzt geladener HTML-Hash (aus Dateiname)
|
||||
private string? _currentFileUri; // aktuell geladene file://-URI (für Dedupl.)
|
||||
|
||||
// file://pfad -> (ns,name), damit Back/Forward des WebViews linke Auswahl synchronisiert
|
||||
private readonly Dictionary<string, (string ns, string name)> _fileToMod = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
// eigene Mod-History (für Back, wenn WebView selbst kein Back hat)
|
||||
private readonly Stack<(string ns, string name)> _navStack = new();
|
||||
private bool _suppressPush; // verhindert „Push“ beim Back-Open
|
||||
|
||||
// Sauberes (De-)Abonnieren des Details-VM Handlers
|
||||
private ModDetailsViewModel? _lastDetailsVm;
|
||||
|
||||
public ThunderstoreView()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
this.AttachedToVisualTree += OnAttachedToVisualTree;
|
||||
this.DetachedFromVisualTree += OnDetachedFromVisualTree;
|
||||
ModsList.TemplateApplied += ModsList_TemplateApplied;
|
||||
|
||||
// Bei DataContext-Änderung erneut Hooken (idempotent)
|
||||
this.DataContextChanged += (_, __) => HookAnchor();
|
||||
|
||||
// ModDetails.PropertyChanged idempotent abonnieren
|
||||
this.DataContextChanged += (_, __) =>
|
||||
{
|
||||
if (_lastDetailsVm != null)
|
||||
_lastDetailsVm.PropertyChanged -= ModDetailsOnPropertyChanged;
|
||||
|
||||
if (DataContext is ThunderstoreViewModel vm)
|
||||
{
|
||||
_lastDetailsVm = vm.ModDetails;
|
||||
_lastDetailsVm.PropertyChanged += ModDetailsOnPropertyChanged;
|
||||
}
|
||||
else
|
||||
{
|
||||
_lastDetailsVm = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private async void ModDetailsOnPropertyChanged(object? _, System.ComponentModel.PropertyChangedEventArgs e)
|
||||
{
|
||||
if (e.PropertyName == nameof(ModDetailsViewModel.ReadmeHtml) &&
|
||||
DataContext is ThunderstoreViewModel vm)
|
||||
{
|
||||
await SetHtmlForCurrentAsync(vm);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAttachedToVisualTree(object? s, VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
HookScroll();
|
||||
HookAnchor();
|
||||
base.OnAttachedToVisualTree(e);
|
||||
}
|
||||
|
||||
private void OnDetachedFromVisualTree(object? s, VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
if (_scroll != null) _scroll.ScrollChanged -= OnScrollChanged;
|
||||
_scroll = null;
|
||||
|
||||
if (_lastDetailsVm != null)
|
||||
{
|
||||
_lastDetailsVm.PropertyChanged -= ModDetailsOnPropertyChanged;
|
||||
_lastDetailsVm = null;
|
||||
}
|
||||
|
||||
base.OnDetachedFromVisualTree(e);
|
||||
}
|
||||
|
||||
// ----------- XAML Events -----------
|
||||
|
||||
private async void ReadmeWeb_OnAttached(object? sender, VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
_readmeWeb = sender as WebView;
|
||||
if (_readmeWeb is null)
|
||||
{
|
||||
Debug.WriteLine("[WV] not found at attach.");
|
||||
return;
|
||||
}
|
||||
|
||||
_readmeWeb.PropertyChanged += OnWebPropertyChanged;
|
||||
|
||||
// JS-Bridge regelmäßig leeren
|
||||
_bridgeTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(150) };
|
||||
_bridgeTimer.Tick += BridgeTimer_Tick;
|
||||
_bridgeTimer.Start();
|
||||
|
||||
// initial füllen
|
||||
if (DataContext is ThunderstoreViewModel vm)
|
||||
await SetHtmlForCurrentAsync(vm);
|
||||
}
|
||||
|
||||
private void ReadmeWeb_OnDetached(object? sender, VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
if (_readmeWeb != null)
|
||||
_readmeWeb.PropertyChanged -= OnWebPropertyChanged;
|
||||
|
||||
if (_bridgeTimer != null)
|
||||
{
|
||||
_bridgeTimer.Tick -= BridgeTimer_Tick;
|
||||
_bridgeTimer.Stop();
|
||||
}
|
||||
|
||||
_readmeWeb = null;
|
||||
_bridgeTimer = null;
|
||||
_lastDrained = null;
|
||||
_lastHtmlKey = null;
|
||||
_currentFileUri = null;
|
||||
_externalDepth = 0;
|
||||
_fileToMod.Clear();
|
||||
_navStack.Clear();
|
||||
_suppressPush = false;
|
||||
}
|
||||
|
||||
// ----------- Öffentlicher Back-Handler -----------
|
||||
|
||||
|
||||
private async void OnBackClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
// Wenn wir seit der aktuellen Readme nach extern navigiert sind → erst WebView zurück
|
||||
if (_externalDepth > 0 && _readmeWeb?.IsCanGoBack == true)
|
||||
{
|
||||
_externalDepth--; // wir kommen einen Schritt aus dem externen Verlauf zurück
|
||||
_readmeWeb.GoBack();
|
||||
return;
|
||||
}
|
||||
|
||||
// sonst: Mod-History benutzen
|
||||
if (_navStack.TryPop(out var prev))
|
||||
{
|
||||
_suppressPush = true;
|
||||
try { await OpenModAsync(prev.ns, prev.name); } // lädt Readme + setzt Selektion
|
||||
finally { _suppressPush = false; }
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback
|
||||
if (_readmeWeb?.IsCanGoBack == true)
|
||||
_readmeWeb.GoBack();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private async Task SyncSelectionWithCurrentFileAsync()
|
||||
{
|
||||
if (_readmeWeb is null || DataContext is not ThunderstoreViewModel vm)
|
||||
return;
|
||||
|
||||
var url = _readmeWeb.Url?.ToString() ?? "";
|
||||
if (string.IsNullOrWhiteSpace(url)) return;
|
||||
|
||||
// Fragment abwerfen
|
||||
var noFrag = url.Split('#')[0];
|
||||
|
||||
// Robust normalisieren
|
||||
if (!Uri.TryCreate(noFrag, UriKind.Absolute, out var u)) return;
|
||||
if (!u.IsFile) return;
|
||||
|
||||
// internen Dedupe-Status aus der URL aktualisieren (wichtig nach Back/Forward!)
|
||||
_currentFileUri = u.AbsoluteUri;
|
||||
try
|
||||
{
|
||||
var file = Path.GetFileName(u.LocalPath);
|
||||
var m = Regex.Match(file, @"vd_readme_([0-9A-F]+)\.html", RegexOptions.IgnoreCase);
|
||||
if (m.Success) _lastHtmlKey = m.Groups[1].Value.ToUpperInvariant();
|
||||
}
|
||||
catch { /* egal */ }
|
||||
|
||||
// Schlüssel wie in SetHtmlForModAsync (slashes normalisieren)
|
||||
var key = u.LocalPath.Replace('\\', '/');
|
||||
|
||||
Debug.WriteLine($"[WV SYNC] url={url} | key={key}");
|
||||
|
||||
if (!_fileToMod.TryGetValue(key, out var mod))
|
||||
{
|
||||
Debug.WriteLine("[WV SYNC] no mapping for key");
|
||||
return;
|
||||
}
|
||||
|
||||
// Linke Auswahl stellen, wenn abweichend
|
||||
var hit = vm.Mods.FirstOrDefault(m =>
|
||||
string.Equals(m.Namespace, mod.ns, StringComparison.OrdinalIgnoreCase) &&
|
||||
string.Equals(m.PackageName, mod.name, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (hit != null && !ReferenceEquals(hit, vm.SelectedMod))
|
||||
{
|
||||
await Dispatcher.UIThread.InvokeAsync(() =>
|
||||
{
|
||||
Debug.WriteLine($"[WV SYNC] select {mod.ns}/{mod.name}");
|
||||
vm.SelectedMod = hit;
|
||||
ModsList?.ScrollIntoView(hit);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ----------- VM → HTML ins WebView -----------
|
||||
|
||||
private async Task SetHtmlForCurrentAsync(ThunderstoreViewModel vm)
|
||||
{
|
||||
var html = vm.ModDetails.ReadmeHtml ?? "<p>(empty)</p>";
|
||||
|
||||
// Ziel-Mod (für History/Sync)
|
||||
var ns =
|
||||
vm.SelectedMod?.Namespace
|
||||
?? vm.ModDetails.Package?.Latest?.Namespace
|
||||
?? vm.ModDetails.Package?.Namespace
|
||||
?? "";
|
||||
|
||||
var name =
|
||||
vm.SelectedMod?.PackageName
|
||||
?? vm.ModDetails.Package?.Latest?.Name
|
||||
?? vm.ModDetails.Package?.Name
|
||||
?? "";
|
||||
|
||||
await SetHtmlForModAsync(ns, name, html);
|
||||
}
|
||||
|
||||
private async Task SetHtmlForModAsync(string ns, string name, string html)
|
||||
{
|
||||
if (_readmeWeb is null) return;
|
||||
|
||||
// stabile Temp-Datei pro Content-Hash → verhindert Spam
|
||||
using var sha = SHA256.Create();
|
||||
var key = Convert.ToHexString(sha.ComputeHash(Encoding.UTF8.GetBytes(html))).Substring(0, 16);
|
||||
|
||||
if (key == _lastHtmlKey && _currentFileUri is not null)
|
||||
{
|
||||
// gleicher Inhalt schon angezeigt → nichts tun
|
||||
return;
|
||||
}
|
||||
|
||||
var path = Path.Combine(Path.GetTempPath(), $"vd_readme_{key}.html");
|
||||
if (!File.Exists(path))
|
||||
await File.WriteAllTextAsync(path, html, Encoding.UTF8);
|
||||
|
||||
var fileUri = new Uri(path).AbsoluteUri;
|
||||
|
||||
// bereits diese Datei geladen? (Doppelnavigation vermeiden)
|
||||
if (string.Equals(_currentFileUri, fileUri, StringComparison.OrdinalIgnoreCase))
|
||||
return;
|
||||
|
||||
// Map für Back/Forward-Sync (LocalPath-Key)
|
||||
var localKey = path.Replace('\\', '/');
|
||||
_fileToMod[localKey] = (ns, name);
|
||||
|
||||
_lastHtmlKey = key;
|
||||
_currentFileUri = fileUri;
|
||||
|
||||
_readmeWeb.Url = new Uri(fileUri);
|
||||
_externalDepth = 0;
|
||||
Debug.WriteLine("[WV] navigate to " + fileUri);
|
||||
}
|
||||
|
||||
// ----------- JS-Bridge (Queue) -----------
|
||||
|
||||
private async void BridgeTimer_Tick(object? sender, EventArgs e)
|
||||
{
|
||||
if (_readmeWeb is null) return;
|
||||
|
||||
try
|
||||
{
|
||||
var js = @"(function(){
|
||||
try{
|
||||
if(!window.__VD_QUEUE) return '[]';
|
||||
var out = window.__VD_QUEUE.splice(0, window.__VD_QUEUE.length);
|
||||
return JSON.stringify(out);
|
||||
}catch(e){ return '[]'; }
|
||||
})()";
|
||||
|
||||
var result = await _readmeWeb.ExecuteScriptAsync(js);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(result) || result == "null" || result == "undefined")
|
||||
return;
|
||||
|
||||
// mache Engines liefern "\"[...]\"" → einmal decodieren
|
||||
if (result.Length > 1 && result[0] == '"' && result[^1] == '"')
|
||||
{
|
||||
try
|
||||
{
|
||||
var inner = JsonSerializer.Deserialize<string>(result);
|
||||
if (!string.IsNullOrEmpty(inner)) result = inner;
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
if (result == _lastDrained) return;
|
||||
_lastDrained = result;
|
||||
|
||||
using var doc = JsonDocument.Parse(result);
|
||||
if (doc.RootElement.ValueKind != JsonValueKind.Array) return;
|
||||
|
||||
foreach (var item in doc.RootElement.EnumerateArray())
|
||||
{
|
||||
var payload = item.ValueKind == JsonValueKind.String ? (item.GetString() ?? "") : item.GetRawText();
|
||||
if (!string.IsNullOrWhiteSpace(payload))
|
||||
HandleBridgeMessage(payload);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("[WV] Bridge exec err: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleBridgeMessage(string json)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
var root = doc.RootElement;
|
||||
var type = root.TryGetProperty("type", out var t) ? t.GetString() : null;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case "log":
|
||||
{
|
||||
var msg = root.TryGetProperty("msg", out var mEl) ? mEl.GetString() : null;
|
||||
Debug.WriteLine("[WV LOG] " + (msg ?? ""));
|
||||
if (!string.IsNullOrWhiteSpace(msg) && msg.Equals("dom-ready", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Falls Url-PropertyChanged nicht feuert: hier nachziehen
|
||||
_ = SyncSelectionWithCurrentFileAsync();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case "external-nav":
|
||||
{
|
||||
_externalDepth++;
|
||||
var u = root.TryGetProperty("url", out var uEl) ? uEl.GetString() : "";
|
||||
Debug.WriteLine("[WV NAV] external-nav " + u);
|
||||
break;
|
||||
}
|
||||
|
||||
case "open-package":
|
||||
{
|
||||
var ns = root.TryGetProperty("ns", out var nsEl) ? (nsEl.GetString() ?? "") : "";
|
||||
var name = root.TryGetProperty("name", out var nEl) ? (nEl.GetString() ?? "") : "";
|
||||
Debug.WriteLine($"[WV] open-package {ns}/{name}");
|
||||
|
||||
// aktuellen Mod in unsere History pushen (nur, wenn nicht gerade Back)
|
||||
if (!_suppressPush && DataContext is ThunderstoreViewModel vm && vm.SelectedMod is not null)
|
||||
{
|
||||
_navStack.Push((vm.SelectedMod.Namespace, vm.SelectedMod.PackageName));
|
||||
}
|
||||
|
||||
_ = OpenModAsync(ns, name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("[WV HOST ERR] " + ex.Message + " | raw=" + json);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------- WebView-URL-Änderung → linke Auswahl synchronisieren -----------
|
||||
|
||||
private void OnWebPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
|
||||
{
|
||||
if (_readmeWeb is null) return;
|
||||
if (e.Property?.Name != nameof(WebView.Url)) return;
|
||||
|
||||
_ = SyncSelectionWithCurrentFileAsync(); // fire-and-forget
|
||||
}
|
||||
|
||||
// ----------- Mod intern öffnen -----------
|
||||
|
||||
private async Task OpenModAsync(string ns, string name)
|
||||
{
|
||||
if (DataContext is not ThunderstoreViewModel vm) return;
|
||||
|
||||
// Zentrale Dedup-Logik im VM verwenden
|
||||
var hit = vm.GetOrCreateTempMod(ns, name, $"https://thunderstore.io/c/valheim/p/{ns}/{name}/");
|
||||
vm.SelectedMod = hit;
|
||||
|
||||
await vm.ModDetails.LoadAsync(ns, name);
|
||||
|
||||
// Icon + Beschreibung nachziehen
|
||||
var pkg = vm.ModDetails.Package;
|
||||
var iconUrl = pkg?.Latest?.Icon;
|
||||
if (!string.IsNullOrWhiteSpace(iconUrl))
|
||||
_ = vm.LoadIconThrottledAsync(hit, iconUrl!);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(pkg?.Latest?.Description))
|
||||
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
|
||||
{
|
||||
hit.Description = pkg!.Latest!.Description;
|
||||
hit.IsTemporary = false;
|
||||
});
|
||||
|
||||
await SetHtmlForModAsync(ns, name, vm.ModDetails.ReadmeHtml ?? "<p>(empty)</p>");
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void OnForwardClick(object? s, Avalonia.Interactivity.RoutedEventArgs e)
|
||||
=> _readmeWeb?.GoForward();
|
||||
|
||||
private async void OnHomeClick(object? s, Avalonia.Interactivity.RoutedEventArgs e)
|
||||
{
|
||||
if (_readmeWeb is null) return;
|
||||
if (DataContext is ThunderstoreViewModel vm)
|
||||
{
|
||||
_resetting = true;
|
||||
try { await SetHtmlForCurrentAsync(vm); }
|
||||
finally { _resetting = false; }
|
||||
}
|
||||
}
|
||||
|
||||
private void HookAnchor()
|
||||
{
|
||||
if (DataContext is not Valheim_Dashboard.ViewModels.ThunderstoreViewModel vm) return;
|
||||
|
||||
vm.ModDetails.OpenPackageRequested -= OnOpenPackageRequested;
|
||||
vm.ModDetails.OpenPackageRequested += OnOpenPackageRequested;
|
||||
}
|
||||
|
||||
private async void OnOpenPackageRequested(string ns, string name, string? url)
|
||||
{
|
||||
if (DataContext is not ThunderstoreViewModel vm) return;
|
||||
|
||||
if (!_suppressPush && vm.SelectedMod is not null)
|
||||
_navStack.Push((vm.SelectedMod.Namespace, vm.SelectedMod.PackageName));
|
||||
|
||||
var hit = vm.GetOrCreateTempMod(ns, name, url);
|
||||
|
||||
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
|
||||
{
|
||||
vm.SelectedMod = hit;
|
||||
ModsList?.ScrollIntoView(hit);
|
||||
});
|
||||
|
||||
await vm.ModDetails.LoadAsync(ns, name);
|
||||
|
||||
var pkg = vm.ModDetails.Package;
|
||||
var iconUrl = pkg?.Latest?.Icon;
|
||||
if (!string.IsNullOrWhiteSpace(iconUrl))
|
||||
_ = vm.LoadIconThrottledAsync(hit, iconUrl!);
|
||||
|
||||
var desc = pkg?.Latest?.Description;
|
||||
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(desc))
|
||||
hit.Description = desc!;
|
||||
hit.IsTemporary = false;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void ModsList_TemplateApplied(object? s, TemplateAppliedEventArgs e) => HookScroll();
|
||||
|
||||
private void HookScroll()
|
||||
{
|
||||
if (_scroll != null) _scroll.ScrollChanged -= OnScrollChanged;
|
||||
|
||||
_scroll = ModsList?
|
||||
.GetVisualDescendants()
|
||||
.OfType<ScrollViewer>()
|
||||
.FirstOrDefault();
|
||||
|
||||
if (_scroll != null)
|
||||
_scroll.ScrollChanged += OnScrollChanged;
|
||||
}
|
||||
|
||||
private async void OnScrollChanged(object? sender, ScrollChangedEventArgs e)
|
||||
{
|
||||
if (DataContext is not ThunderstoreViewModel vm || _scroll is null) return;
|
||||
|
||||
var extentH = _scroll.Extent.Height;
|
||||
var viewH = _scroll.Viewport.Height;
|
||||
var offsetY = _scroll.Offset.Y;
|
||||
|
||||
if (extentH <= 0 || viewH <= 0) return;
|
||||
|
||||
var nearEnd = offsetY + viewH >= extentH - 400;
|
||||
if (nearEnd)
|
||||
await vm.LoadNextPageAsync(prefetchNext: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user