Files
LanMountainDesktop/LanMountainDesktop/ViewModels/WallpaperSettingsPageViewModel.cs

229 lines
7.3 KiB
C#
Raw Normal View History

2026-03-14 13:36:18 +08:00
using System;
using System.Collections.Generic;
2026-03-14 16:38:56 +08:00
using System.Collections.ObjectModel;
2026-03-14 13:36:18 +08:00
using System.Linq;
using System.Threading.Tasks;
2026-03-14 16:38:56 +08:00
using Avalonia.Media;
using Avalonia.Media.Imaging;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
2026-03-14 13:36:18 +08:00
using LanMountainDesktop.Services;
using LanMountainDesktop.Services.Settings;
namespace LanMountainDesktop.ViewModels;
public sealed partial class WallpaperSettingsPageViewModel : ViewModelBase
{
private readonly ISettingsFacadeService _settingsFacade;
private readonly LocalizationService _localizationService = new();
private readonly string _languageCode;
private bool _isInitializing;
public WallpaperSettingsPageViewModel(ISettingsFacadeService settingsFacade)
{
_settingsFacade = settingsFacade;
_languageCode = _localizationService.NormalizeLanguageCode(_settingsFacade.Region.Get().LanguageCode);
WallpaperPlacements = CreateWallpaperPlacements();
2026-03-14 16:38:56 +08:00
WallpaperTypes = CreateWallpaperTypes();
PresetColors = CreatePresetColors();
2026-03-14 13:36:18 +08:00
RefreshLocalizedText();
_isInitializing = true;
Load();
_isInitializing = false;
}
public IReadOnlyList<SelectionOption> WallpaperPlacements { get; }
2026-03-14 16:38:56 +08:00
public IReadOnlyList<SelectionOption> WallpaperTypes { get; }
public IReadOnlyList<string> PresetColors { get; }
2026-03-14 13:36:18 +08:00
2026-03-14 16:38:56 +08:00
[ObservableProperty]
2026-03-14 13:36:18 +08:00
private string _wallpaperPath = string.Empty;
2026-03-14 16:38:56 +08:00
[ObservableProperty]
private SelectionOption _selectedWallpaperType = null!;
2026-03-14 13:36:18 +08:00
2026-03-14 16:38:56 +08:00
[ObservableProperty]
private string? _selectedColor;
[ObservableProperty]
private SelectionOption _selectedWallpaperPlacement = null!;
[ObservableProperty]
2026-03-14 13:36:18 +08:00
private string _wallpaperHeader = string.Empty;
2026-03-14 16:38:56 +08:00
[ObservableProperty]
private string _wallpaperTypeLabel = string.Empty;
[ObservableProperty]
2026-03-14 13:36:18 +08:00
private string _wallpaperPathLabel = string.Empty;
2026-03-14 16:38:56 +08:00
[ObservableProperty]
private string _wallpaperColorLabel = string.Empty;
[ObservableProperty]
2026-03-14 13:36:18 +08:00
private string _wallpaperPlacementLabel = string.Empty;
2026-03-14 16:38:56 +08:00
[ObservableProperty]
2026-03-14 13:36:18 +08:00
private string _wallpaperPlacementDescription = string.Empty;
2026-03-14 16:38:56 +08:00
[ObservableProperty]
2026-03-14 13:36:18 +08:00
private string _importWallpaperButtonText = string.Empty;
2026-03-14 16:38:56 +08:00
[ObservableProperty]
2026-03-14 13:36:18 +08:00
private string _filePickerTitle = string.Empty;
2026-03-14 16:38:56 +08:00
[ObservableProperty]
private bool _isImageOrVideo;
[ObservableProperty]
private bool _isSolidColor;
[ObservableProperty]
private Bitmap? _previewImage;
2026-03-14 13:36:18 +08:00
public void Load()
{
var wallpaper = _settingsFacade.Wallpaper.Get();
WallpaperPath = wallpaper.WallpaperPath ?? string.Empty;
2026-03-14 16:38:56 +08:00
SelectedWallpaperType = WallpaperTypes.FirstOrDefault(t => t.Value == wallpaper.Type) ?? WallpaperTypes[0];
SelectedColor = wallpaper.Color ?? PresetColors[0];
2026-03-14 13:36:18 +08:00
var wallpaperPlacement = string.IsNullOrWhiteSpace(wallpaper.Placement)
? "Fill"
: wallpaper.Placement;
SelectedWallpaperPlacement = WallpaperPlacements.FirstOrDefault(option =>
string.Equals(option.Value, wallpaperPlacement, StringComparison.OrdinalIgnoreCase))
?? WallpaperPlacements[0];
2026-03-14 16:38:56 +08:00
UpdateVisibility();
UpdatePreviewImage(WallpaperPath);
}
partial void OnSelectedWallpaperTypeChanged(SelectionOption value)
{
UpdateVisibility();
if (_isInitializing) return;
SaveWallpaper();
}
private void UpdateVisibility()
{
IsImageOrVideo = SelectedWallpaperType?.Value is "Image" or "Video";
IsSolidColor = SelectedWallpaperType?.Value == "SolidColor";
}
partial void OnSelectedColorChanged(string? value)
{
if (_isInitializing) return;
SaveWallpaper();
2026-03-14 13:36:18 +08:00
}
public async Task ImportWallpaperAsync(string sourcePath)
{
var importedPath = await _settingsFacade.WallpaperMedia.ImportAssetAsync(sourcePath);
if (!string.IsNullOrWhiteSpace(importedPath))
{
WallpaperPath = importedPath;
}
}
partial void OnWallpaperPathChanged(string value)
{
2026-03-14 16:38:56 +08:00
UpdatePreviewImage(value);
if (_isInitializing) return;
2026-03-14 13:36:18 +08:00
SaveWallpaper();
}
2026-03-14 16:38:56 +08:00
private void UpdatePreviewImage(string path)
2026-03-14 13:36:18 +08:00
{
2026-03-14 16:38:56 +08:00
if (string.IsNullOrWhiteSpace(path) || !System.IO.File.Exists(path))
2026-03-14 13:36:18 +08:00
{
2026-03-14 16:38:56 +08:00
PreviewImage = null;
2026-03-14 13:36:18 +08:00
return;
}
2026-03-14 16:38:56 +08:00
try
{
using var stream = System.IO.File.OpenRead(path);
PreviewImage = new Bitmap(stream);
}
catch
{
PreviewImage = null;
}
}
partial void OnSelectedWallpaperPlacementChanged(SelectionOption value)
{
if (_isInitializing || value is null) return;
2026-03-14 13:36:18 +08:00
SaveWallpaper();
}
2026-03-14 16:38:56 +08:00
[RelayCommand]
private void SelectColor(string color)
{
SelectedColor = color;
}
2026-03-14 13:36:18 +08:00
private void SaveWallpaper()
{
2026-03-15 17:08:07 +08:00
var normalizedPath = SelectedWallpaperType?.Value == "SolidColor" || string.IsNullOrWhiteSpace(WallpaperPath)
? null
: WallpaperPath;
2026-03-14 13:36:18 +08:00
_settingsFacade.Wallpaper.Save(new WallpaperSettingsState(
2026-03-15 17:08:07 +08:00
normalizedPath,
2026-03-14 16:38:56 +08:00
SelectedWallpaperType.Value,
SelectedColor,
2026-03-14 13:36:18 +08:00
SelectedWallpaperPlacement.Value));
}
private IReadOnlyList<SelectionOption> CreateWallpaperPlacements()
{
return
[
new SelectionOption("Fill", L("settings.wallpaper.placement.fill", "Fill")),
new SelectionOption("Fit", L("settings.wallpaper.placement.fit", "Fit")),
new SelectionOption("Stretch", L("settings.wallpaper.placement.stretch", "Stretch")),
new SelectionOption("Center", L("settings.wallpaper.placement.center", "Center")),
new SelectionOption("Tile", L("settings.wallpaper.placement.tile", "Tile"))
];
}
2026-03-14 16:38:56 +08:00
private IReadOnlyList<SelectionOption> CreateWallpaperTypes()
{
return
[
new SelectionOption("Image", L("settings.wallpaper.type.image", "Image")),
new SelectionOption("Video", L("settings.wallpaper.type.video", "Video")),
new SelectionOption("SolidColor", L("settings.wallpaper.type.solid_color", "Solid Color"))
];
}
private IReadOnlyList<string> CreatePresetColors()
{
return
[
"#D8A7B1", "#B6C9BB", "#A2B5BB", "#E6E2D3",
"#B5A397", "#C5C1C0", "#D4BE8D", "#C08261",
"#8E9775", "#9FBAD3", "#E5BAA2", "#4E596F"
];
}
2026-03-14 13:36:18 +08:00
private void RefreshLocalizedText()
{
WallpaperHeader = L("settings.wallpaper.title", "Wallpaper");
2026-03-14 16:38:56 +08:00
WallpaperTypeLabel = L("settings.wallpaper.type_label", "Wallpaper Type");
2026-03-14 13:36:18 +08:00
WallpaperPathLabel = L("settings.wallpaper.current_label", "Current Wallpaper");
2026-03-14 16:38:56 +08:00
WallpaperColorLabel = L("settings.wallpaper.color_label", "Wallpaper Color");
2026-03-14 13:36:18 +08:00
WallpaperPlacementLabel = L("settings.wallpaper.placement_label", "Placement");
WallpaperPlacementDescription = L("settings.wallpaper.placement_desc", "Adjust how the image fills the desktop.");
ImportWallpaperButtonText = L("settings.wallpaper.pick_button", "Import Wallpaper");
FilePickerTitle = L("filepicker.title", "Select wallpaper");
}
private string L(string key, string fallback)
=> _localizationService.GetString(_languageCode, key, fallback);
}