Files
captureimg/src/CaptureImage.ViewModels/Preview/PreviewViewModel.cs
T
poli0981andClaude Opus 4.7 d3e9b7df5c fix(v1.1.2): live localize picker titles, toast error, preview window + status message
M1 of v1.1.2 localization hotfix — 6 fixes the user sees directly when
switching languages at runtime:

- PreviewWindow now binds Title + FlowDirection to Localization, so the
  modal's title and mirrored layout track the current culture (Arabic
  RTL used to stay LTR, English title used to stay "Preview").
- PreviewViewModel is IDisposable and subscribes to Localization.
  PropertyChanged with a named handler, matching the v1.1.1 pattern the
  other VMs use. AvaloniaPreviewPresenter Dispose()s it after each
  modal so the subscription doesn't leak into the singleton service.
- Settings file picker titles ("Import settings" / "Export settings")
  and the generic error-toast title ("Error") now come from resx
  (Settings_ImportTitle, Settings_ExportTitle, Toast_Error), with
  vi-VN + ar-SA translations added in lockstep.
- Dashboard StatusMessage retranslates on culture change — previously
  it was an [ObservableProperty] stored field, so the PropertyChanged
  notification handed the UI the same stale string. Call
  UpdateStatusForState() instead to rederive it from the state machine
  under the new culture.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-20 22:11:36 +07:00

71 lines
2.2 KiB
C#

using System;
using System.ComponentModel;
using CaptureImage.Core.Abstractions;
using CaptureImage.Core.Models;
using CommunityToolkit.Mvvm.ComponentModel;
namespace CaptureImage.ViewModels.Preview;
/// <summary>
/// VM for the preview window. Holds the captured frame's dimensions + PNG bytes so the view
/// can render it without asking the engine to re-encode.
/// </summary>
public sealed partial class PreviewViewModel : ViewModelBase, IDisposable
{
private bool _disposed;
public ILocalizationService Localization { get; }
[ObservableProperty]
private byte[]? _pngBytes;
[ObservableProperty]
private string _targetName = string.Empty;
[ObservableProperty]
private int _width;
[ObservableProperty]
private int _height;
public PreviewViewModel(ILocalizationService localization)
{
Localization = localization;
// Preview window is modal and short-lived, but the user can still switch language
// from the tray menu while it is open. Raise Localization on culture change so the
// `{Binding Localization[Preview_*]}` indexer bindings in PreviewWindow.axaml
// re-resolve in place. Matches the v1.1.1 pattern used by every other VM.
Localization.PropertyChanged += OnLocalizationChanged;
}
private void OnLocalizationChanged(object? sender, PropertyChangedEventArgs e)
{
if (_disposed) return;
if (e.PropertyName is "Item[]" or nameof(ILocalizationService.CurrentCulture))
{
OnPropertyChanged(nameof(Localization));
}
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
Localization.PropertyChanged -= OnLocalizationChanged;
}
/// <summary>
/// Fill the VM from a captured frame. The frame is encoded to PNG bytes once so the
/// WPF-style binding to <see cref="PngBytes"/> works with our existing
/// <c>BytesToBitmapConverter</c>.
/// </summary>
public void SetFrame(CapturedFrame frame, GameTarget target, byte[] pngBytes)
{
PngBytes = pngBytes;
TargetName = target.DisplayName;
Width = frame.Width;
Height = frame.Height;
}
}