JD2022-TU1/main/tools/modules/Orbit.Module/ViewModel/ImageViewerViewModel.cs

127 lines
3.3 KiB
C#

using System;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Plume.WPF.Commands;
using Plume.WPF.Mvvm;
namespace Orbit.Module.ViewModel
{
class ImageViewerViewModel : BindableBase
{
private readonly ImageManager _imageManager;
private BitmapSource _imageSource;
private string _title;
private string _errorText;
private bool _isLoading;
private Stretch _imageStretch;
public BitmapSource ImageSource
{
get => _imageSource;
set => SetProperty(ref _imageSource, value);
}
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
public bool IsLoading
{
get => _isLoading;
set => SetProperty(ref _isLoading, value);
}
public Stretch ImageStretch
{
get => _imageStretch;
set => SetProperty(ref _imageStretch, value);
}
public string ErrorText
{
get => _errorText;
set => SetProperty(ref _errorText, value);
}
public DelegateCommand CommandRight { get; }
public DelegateCommand CommandLeft { get; }
public DelegateCommand CommandUp { get; }
public DelegateCommand CommandDown { get; }
public DelegateCommand CommandToggleFitToScreen { get; }
public ImageViewerViewModel()
{
_imageManager = new ImageManager();
_imageManager.Loaded += On_Loaded;
_imageManager.Loading += On_Loading;
CommandRight = new DelegateCommand(OnCommandRight);
CommandLeft = new DelegateCommand(OnCommandLeft);
CommandUp = new DelegateCommand(OnCommandUp);
CommandDown = new DelegateCommand(OnCommandDown);
CommandToggleFitToScreen = new DelegateCommand(OnCommandToggleFitToScreen);
ImageStretch = Stretch.None;
}
public async Task Initialize(string path)
{
await _imageManager.Load(path);
}
private void On_Loading(object sender, EventArgs e)
{
IsLoading = true;
ErrorText = "Loading Image";
ImageSource = null;
}
private void On_Loaded(object sender, Exception e)
{
IsLoading = false;
ErrorText = string.Empty;
if (_imageManager.Image != null)
{
ImageSource = _imageManager.Image.ToBitmapSource();
Title = _imageManager.FileInfo;
}
else
{
ErrorText = e?.Message;
}
}
private void OnCommandToggleFitToScreen()
{
if (ImageStretch == Stretch.None)
ImageStretch = Stretch.Uniform;
else
ImageStretch = Stretch.None;
}
private async void OnCommandLeft()
{
await _imageManager.Previous();
}
private async void OnCommandRight()
{
await _imageManager.Next();
}
private void OnCommandUp()
{
_imageManager.NextFrame();
}
private void OnCommandDown()
{
_imageManager.PreviousFrame();
}
}
}