Skip to content

Commit

Permalink
完成对MediaInfoPage的改造
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 8, 2024
1 parent aacfe7e commit 2e791f3
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 120 deletions.
1 change: 1 addition & 0 deletions SimpleFFmpegGUI.WPF/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ResourceDictionary.MergedDictionaries>
<converters:NameDescriptionAttributeValueConverter x:Key="NameDescriptionAttributeValueConverter" />
<converters:HourMinSecTimeSpanConverter x:Key="HourMinSecTimeSpanConverter" />
<converters:Bitrate2StringConverter x:Key="Bitrate2StringConverter" />
<converters:CountEqualsOneValueConverter x:Key="CountEqualsOneValueConverter" />
<converters:Index2StringConverter x:Key="Index2StringConverter" />
<system:String x:Key="DateTimeFormat">yyyy-MM-dd HH:mm:ss</system:String>
Expand Down
22 changes: 22 additions & 0 deletions SimpleFFmpegGUI.WPF/Converters/Bitrate2StringConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using FzLib;
using System;
using System.Globalization;
using System.Windows.Data;

namespace SimpleFFmpegGUI.WPF.Converters
{
public class Bitrate2StringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
long num = System.Convert.ToInt64(value);
string str = NumberConverter.ByteToFitString(num, 2, " bps", " Kbps", " Mbps", " Gbps", " Tbps");
return str;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
6 changes: 6 additions & 0 deletions SimpleFFmpegGUI.WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SimpleFFmpegGUI.WPF"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mext="clr-namespace:ModernWpf.FzExtension;assembly=ModernWpf.FzExtension"
xmlns:panels="clr-namespace:SimpleFFmpegGUI.WPF.Panels"
xmlns:ui="http://schemas.modernwpf.com/2019"
Title="FFmpeg 工具箱"
Expand Down Expand Up @@ -316,5 +317,10 @@
Grid.Column="2"
Background="{DynamicResource SystemControlBackgroundChromeBlackHighBrush}"
Visibility="{Binding TopTabVisibility}" />
<mext:ProgressRingOverlay
x:Name="ring"
Grid.RowSpan="99"
Grid.ColumnSpan="99"
Margin="-8" />
</Grid>
</Window>
10 changes: 9 additions & 1 deletion SimpleFFmpegGUI.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,14 @@ private void RegisterMessages()

WeakReferenceMessenger.Default.Register<WindowEnableMessage>(this, (_, m) =>
{
IsEnabled = m.IsEnabled;
if(m.IsEnabled)
{
ring.Hide();
}
else
{
ring.Show();
}
});


Expand Down Expand Up @@ -379,6 +386,7 @@ private void RegisterMessages()
}
});
}

private void ResetUI(bool force = false)
{
if (tab.SelectedIndex == 0 && !topTab.HasContent
Expand Down
8 changes: 1 addition & 7 deletions SimpleFFmpegGUI.WPF/Pages/MediaInfoPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
AllowDrop="True"
mc:Ignorable="d">
<UserControl.Resources>
<local:Bitrate2StringConverter x:Key="Bitrate2StringConverter" />
<Style
x:Key="KeyStyle"
TargetType="{x:Type TextBlock}">
Expand Down Expand Up @@ -42,7 +41,7 @@
<TextBox Text="{Binding FilePath, UpdateSourceTrigger=PropertyChanged}" />
<Button
Grid.Column="2"
Click="Button_Click">
Command="{Binding BrowseFileCommand}">
<ui:SymbolIcon Symbol="BrowsePhotos" />
</Button>
</Grid>
Expand Down Expand Up @@ -318,10 +317,5 @@
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Transparent" />
<mext:ProgressRingOverlay
x:Name="ring"
Grid.RowSpan="99"
Grid.ColumnSpan="99"
Margin="-8" />
</Grid>
</UserControl>
116 changes: 4 additions & 112 deletions SimpleFFmpegGUI.WPF/Pages/MediaInfoPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
using Enterwell.Clients.Wpf.Notifications;
using FzLib;
using FzLib.WPF;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using SimpleFFmpegGUI.Dto;
using SimpleFFmpegGUI.Manager;
using SimpleFFmpegGUI.Model;
using SimpleFFmpegGUI.Model.MediaInfo;
using SimpleFFmpegGUI.WPF.Model;
using SimpleFFmpegGUI.WPF.ViewModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
Expand All @@ -28,36 +23,16 @@

namespace SimpleFFmpegGUI.WPF.Pages
{
public class Bitrate2StringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
long num = System.Convert.ToInt64(value);
string str = NumberConverter.ByteToFitString(num, 2, " bps", " Kbps", " Mbps", " Gbps", " Tbps");
return str;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

/// <summary>
/// Interaction logic for MediaInfoPage.xaml
/// </summary>
public partial class MediaInfoPage : UserControl
{
public MediaInfoPage(MediaInfoPageViewModel viewModel)
public MediaInfoPage()
{
ViewModel = viewModel;
DataContext = ViewModel;
ViewModel = this.SetDataContext<MediaInfoPageViewModel>();
InitializeComponent();
ViewModel.PropertyChanged += ViewModel_PropertyChanged;
ViewModel.ReadMediaInfoError += (s, e) => this.CreateMessage().QueueError("读取媒体信息失败", e.ExceptionObject as Exception);
}

public MediaInfoPageViewModel ViewModel { get; set; }

public void SetFile(string file)
{
ViewModel.FilePath = file;
Expand Down Expand Up @@ -85,92 +60,9 @@ protected override void OnDrop(DragEventArgs e)
}
}

private void Button_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog().AddAllFilesFilter();

string path = dialog.GetPath(this.GetWindow());
if (path != null)
{
ViewModel.FilePath = path;
}
}

private void TextBox_MouseLeave(object sender, MouseEventArgs e)
{
Keyboard.ClearFocus();
}

private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ViewModel.Working))
{
if (ViewModel.Working)
{
ring.Show();
}
else
{
ring.Hide();
}
}
}
}

public class MediaInfoPageViewModel : INotifyPropertyChanged
{
private string filePath;

private MediaInfoGeneral mediaInfo;

private bool working;

public MediaInfoPageViewModel()
{
}
public event PropertyChangedEventHandler PropertyChanged;

public event EventHandler<UnhandledExceptionEventArgs> ReadMediaInfoError;

public string FilePath
{
get => filePath;
set
{
this.SetValueAndNotify(ref filePath, value, nameof(FilePath));
if (!string.IsNullOrWhiteSpace(filePath) && System.IO.File.Exists(value))
{
ShowInfoAsync().ConfigureAwait(false);
}
}
}
public MediaInfoGeneral MediaInfo
{
get => mediaInfo;
set => this.SetValueAndNotify(ref mediaInfo, value, nameof(MediaInfo));
}

public bool Working
{
get => working;
set => this.SetValueAndNotify(ref working, value, nameof(Working));
}

private async Task ShowInfoAsync()
{
Working = true;
try
{
MediaInfo = await MediaInfoManager.GetMediaInfoAsync(FilePath);
}
catch (Exception ex)
{
ReadMediaInfoError?.Invoke(this, new UnhandledExceptionEventArgs(ex, false));
}
finally
{
Working = false;
}
}
}
}
70 changes: 70 additions & 0 deletions SimpleFFmpegGUI.WPF/ViewModels/MediaInfoPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FzLib;
using Microsoft.Win32;
using Newtonsoft.Json.Linq;
using SimpleFFmpegGUI.Manager;
using SimpleFFmpegGUI.Model.MediaInfo;
using SimpleFFmpegGUI.WPF.Messages;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;

namespace SimpleFFmpegGUI.WPF.ViewModels
{
public partial class MediaInfoPageViewModel : ViewModelBase
{
[ObservableProperty]
private string filePath;

[ObservableProperty]
private MediaInfoGeneral mediaInfo;
public MediaInfoPageViewModel()
{
}

protected override async void OnPropertyChanged(PropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.PropertyName == nameof(FilePath))
{
if (!string.IsNullOrWhiteSpace(FilePath) && System.IO.File.Exists(FilePath))
{
await ShowInfoAsync();
}
}
}

private async Task ShowInfoAsync()
{
SendMessage(new WindowEnableMessage(false));
try
{
MediaInfo = await MediaInfoManager.GetMediaInfoAsync(FilePath);
}
catch (Exception ex)
{
QueueErrorMessage("读取媒体信息失败", ex);
}
finally
{
SendMessage(new WindowEnableMessage(true));
}
}

[RelayCommand]
private void BrowseFile()
{
var dialog = new OpenFileDialog().AddAllFilesFilter();
SendMessage(new FileDialogMessage(dialog));

string path = dialog.FileName;
if (path != null)
{
FilePath = path;
}
}

}
}

0 comments on commit 2e791f3

Please sign in to comment.