feat.启动器图片自定义

This commit is contained in:
lincube
2026-06-05 23:38:32 +08:00
parent eae3e67238
commit 8df0271032
13 changed files with 806 additions and 176 deletions

View File

@@ -3,6 +3,7 @@ using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Platform.Storage;
using LanMountainDesktop.Launcher.Resources;
using LanMountainDesktop.Launcher.Shell;
namespace LanMountainDesktop.Launcher.Views;
@@ -46,6 +47,7 @@ public partial class ErrorDebugWindow : Window
}
UpdatePathDisplay(_selectedHostPath);
RefreshBackgroundImageDisplay();
}
private void InitializeComponents()
@@ -63,6 +65,16 @@ public partial class ErrorDebugWindow : Window
browseButton.Click += OnBrowseClick;
}
if (this.FindControl<Button>("BrowseImageButton") is { } browseImageButton)
{
browseImageButton.Click += OnBrowseImageClick;
}
if (this.FindControl<Button>("ClearImageButton") is { } clearImageButton)
{
clearImageButton.Click += OnClearImageClick;
}
if (this.FindControl<Button>("OkButton") is { } okButton)
{
okButton.Click += (_, _) =>
@@ -111,6 +123,56 @@ public partial class ErrorDebugWindow : Window
UpdatePathDisplay(_selectedHostPath);
}
private async void OnBrowseImageClick(object? sender, RoutedEventArgs e)
{
var storageProvider = StorageProvider;
if (storageProvider is null)
{
return;
}
var patterns = LauncherBackgroundService
.GetSupportedExtensions()
.Select(extension => "*" + extension)
.ToArray();
var options = new FilePickerOpenOptions
{
Title = Strings.DebugDebug_SelectImageDialog,
AllowMultiple = false,
FileTypeFilter =
[
new FilePickerFileType(Strings.DebugDebug_ImageFiles)
{
Patterns = patterns
}
]
};
var result = await storageProvider.OpenFilePickerAsync(options);
if (result.Count <= 0)
{
return;
}
var saveResult = LauncherBackgroundService.SaveBackgroundImage(result[0].Path.LocalPath);
var status = saveResult.IsSuccess
? Strings.DebugDebug_BackgroundImageSaved
: string.Format(Strings.DebugDebug_BackgroundImageSaveFailedFormat, saveResult.ErrorMessage ?? string.Empty);
RefreshBackgroundImageDisplay(status);
}
private void OnClearImageClick(object? sender, RoutedEventArgs e)
{
var clearResult = LauncherBackgroundService.ClearBackgroundImage();
var status = clearResult.IsSuccess
? Strings.DebugDebug_BackgroundImageCleared
: string.Format(Strings.DebugDebug_BackgroundImageSaveFailedFormat, clearResult.ErrorMessage ?? string.Empty);
RefreshBackgroundImageDisplay(status);
}
private void UpdatePathDisplay(string? path)
{
if (this.FindControl<TextBlock>("PathTextBlock") is { } pathTextBlock)
@@ -118,4 +180,46 @@ public partial class ErrorDebugWindow : Window
pathTextBlock.Text = string.IsNullOrEmpty(path) ? Strings.DebugDebug_NotSelected : path;
}
}
private void RefreshBackgroundImageDisplay(string? statusOverride = null)
{
var imageInfo = LauncherBackgroundService.LoadBackgroundImage();
if (this.FindControl<TextBlock>("BackgroundImagePathTextBlock") is { } pathTextBlock)
{
pathTextBlock.Text = imageInfo.Exists && !string.IsNullOrWhiteSpace(imageInfo.FilePath)
? imageInfo.FilePath
: Strings.DebugDebug_BackgroundImageNotSet;
}
if (this.FindControl<TextBlock>("BackgroundImageStatusTextBlock") is { } statusTextBlock)
{
statusTextBlock.Text = statusOverride ?? ResolveBackgroundImageStatus(imageInfo);
}
if (this.FindControl<Button>("ClearImageButton") is { } clearButton)
{
clearButton.IsEnabled = imageInfo.Exists;
}
}
private static string ResolveBackgroundImageStatus(LauncherBackgroundService.BackgroundImageInfo imageInfo)
{
if (imageInfo.IsValid)
{
return string.Format(
Strings.DebugDebug_BackgroundImageReadyFormat,
imageInfo.Width,
imageInfo.Height);
}
if (imageInfo.Exists)
{
return string.Format(
Strings.DebugDebug_BackgroundImageInvalidFormat,
imageInfo.ErrorMessage ?? string.Empty);
}
return Strings.DebugDebug_BackgroundImageNotSet;
}
}