Skip to content

Commit

Permalink
基本完成对TaskList的改造
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 6, 2024
1 parent c5cdb83 commit e44bff4
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 281 deletions.
1 change: 1 addition & 0 deletions SimpleFFmpegGUI.Core/DependencyInjectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static void AddFFmpegServices(this IServiceCollection services)
.AddTransient<ConfigManager>()
.AddTransient<PresetManager>()
.AddTransient<TaskManager>()
.AddTransient<PowerManager>()
.AddSingleton<QueueManager>();
}
}
Expand Down
4 changes: 0 additions & 4 deletions SimpleFFmpegGUI.Core/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public class Logger : IDisposable
public Logger(FFmpegDbContext db)
{
this.db = db;
}
public Logger()
{
lock (lockObj)
{
StartTimer();
Expand Down Expand Up @@ -86,7 +83,6 @@ public void Dispose()
timer.Dispose();
Save();
}
db.Dispose();
}
}

Expand Down
6 changes: 3 additions & 3 deletions SimpleFFmpegGUI.Core/Manager/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ public class ConfigManager
private static readonly Dictionary<string, object> cache = new Dictionary<string, object>();

private readonly FFmpegDbContext db;
private readonly Logger logger;

public ConfigManager(FFmpegDbContext db)
public ConfigManager(FFmpegDbContext db,Logger logger)
{
this.db = db;
this.logger = logger;
}

public int DefaultProcessPriority
Expand All @@ -40,7 +42,6 @@ public T GetConfig<T>(string key, T defaultValue)
T value = Parse<T>(item.Value);
cache.Add(key, value);

using Logger logger = new Logger();
logger.Info($"读取配置:[{key}]={value}");
return value;
}
Expand Down Expand Up @@ -68,7 +69,6 @@ public void SetConfig<T>(string key, T value)

db.SaveChanges();

using Logger logger = new Logger();
logger.Info($"写入配置:[{key}]={value}");
}

Expand Down
5 changes: 3 additions & 2 deletions SimpleFFmpegGUI.Core/Manager/FFmpegManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class FFmpegManager : INotifyPropertyChanged
/// <summary>
/// 日志
/// </summary>
private Logger logger = new Logger();
private Logger logger;

/// <summary>
/// 任务是否被暂停
Expand All @@ -91,9 +91,10 @@ public class FFmpegManager : INotifyPropertyChanged
private DateTime pauseStartTime;
private FFmpegProcess process;

public FFmpegManager(TaskInfo task)
public FFmpegManager(TaskInfo task, Logger logger)
{
this.task = task;
this.logger = logger;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.Core/Manager/FFmpegProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class FFmpegProcess

private FFmpegProcess()
{
Priority = new ConfigManager(new FFmpegDbContext()).DefaultProcessPriority;
Priority = new ConfigManager(new FFmpegDbContext(), null).DefaultProcessPriority;
}
public FFmpegProcess(string argument) : this()
{
Expand Down
6 changes: 2 additions & 4 deletions SimpleFFmpegGUI.Core/Manager/PowerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ namespace SimpleFFmpegGUI.Manager
/// <summary>
/// 计算机电源管理,控制关机
/// </summary>
public class PowerManager
public class PowerManager(Logger logger)
{
private static readonly string abortShutdownCommand = "-a";
private static readonly string shutdownCommand = $"-s -t 180 -c \"{FzLib.Program.App.ProgramName}\"";
private readonly Logger logger = logger;
private bool shutdownAfterQueueFinished = false;

public bool ShutdownAfterQueueFinished
Expand All @@ -24,7 +25,6 @@ public bool ShutdownAfterQueueFinished
set
{
shutdownAfterQueueFinished = value;
using Logger logger = new Logger();
logger.Info("收到队列结束后自动关机命令:" + value.ToString());
}
}
Expand Down Expand Up @@ -105,14 +105,12 @@ public static async Task<CpuCoreUsageDto[]> GetCpuUsageAsync(TimeSpan sampleSpan

public void AbortShutdown()
{
using Logger logger = new Logger();
logger.Warn("收到终止关机命令");
Shutdown(false);
}

public void Shutdown()
{
using Logger logger = new Logger();
logger.Warn("收到关机命令");
Shutdown(true);
}
Expand Down
7 changes: 4 additions & 3 deletions SimpleFFmpegGUI.Core/Manager/QueueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public class QueueManager
private DateTime? scheduleTime = null;
private List<FFmpegManager> taskProcessManagers = new List<FFmpegManager>();

public QueueManager(FFmpegDbContext db, Logger logger)
public QueueManager(FFmpegDbContext db, Logger logger, PowerManager powerManager)
{
this.db = db;
this.logger = logger;
PowerManager = powerManager;
}

/// <summary>
Expand All @@ -54,7 +55,7 @@ public QueueManager(FFmpegDbContext db, Logger logger)
/// <summary>
/// 电源性能管理
/// </summary>
public PowerManager PowerManager { get; } = new PowerManager();
public PowerManager PowerManager { get; }

/// <summary>
/// 独立任务
Expand Down Expand Up @@ -218,7 +219,7 @@ private IQueryable<TaskInfo> GetQueueTasksQuery(FFmpegDbContext db)

private async Task ProcessTaskAsync(TaskInfo task, bool main)
{
FFmpegManager ffmpegManager = new FFmpegManager(task);
FFmpegManager ffmpegManager = new FFmpegManager(task, logger);

task.Status = TaskStatus.Processing;
task.StartTime = DateTime.Now;
Expand Down
3 changes: 1 addition & 2 deletions SimpleFFmpegGUI.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ protected override void OnStartup(StartupEventArgs e)

private void ConfigureServices(IServiceCollection services)
{
services.AddFFmpegServices();
services.AddSingleton<Config>();

services.AddSingleton<TasksAndStatuses>();
Expand Down Expand Up @@ -120,8 +121,6 @@ private void ConfigureServices(IServiceCollection services)
services.AddTransient<FileIOPanelViewModel>();
services.AddTransient<PresetsPanelViewModel>();
services.AddTransient<StatusPanelViewModel>();

services.AddFFmpegServices();
}

private void InitializeLogs()
Expand Down
50 changes: 45 additions & 5 deletions SimpleFFmpegGUI.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,35 @@ private void RegisterMessages()
IsEnabled = m.IsEnabled;
});


WeakReferenceMessenger.Default.Register<AddNewTabMessage>(this, (_, m) =>
{
m.Page = AddNewTab(m.Type);
});

WeakReferenceMessenger.Default.Register<ShowCodeArgumentsMessage>(this, (_, m) =>
{
var task = m.Task;
Debug.Assert(task != null);
var panel = new CodeArgumentsPanel
{
IsHitTestVisible = false
};
panel.ViewModel.Update(task.Type, task.Arguments);
ScrollViewer scr = new ScrollViewer();
scr.Content = panel;
Window win = new Window()
{
Owner = this.GetWindow(),
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Content = scr,
Width = 600,
Height = 800,
Title = "详细参数 - FFmpeg工具箱"
};
win.Show();
});

WeakReferenceMessenger.Default.Register<QueueMessagesMessage>(this, (_, m) =>
{
switch (m.Type)
Expand Down Expand Up @@ -119,15 +148,26 @@ private void RegisterMessages()
/// <typeparam name="T"></typeparam>
/// <param name="title"></param>
/// <param name="beforeLoad"></param>
public T AddNewTab<T>(string title = null/*, Action<T> beforeLoad = null*/) where T : UserControl
public T AddNewTab<T>(string title = null) where T : UserControl
{
title ??= PageHelper.GetTitle<T>();
T panel = App.ServiceProvider.GetService<T>();
return AddNewTab(typeof(T), title) as T;
}

/// <summary>
/// 新增一个Tab项
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="title"></param>
/// <param name="beforeLoad"></param>
public object AddNewTab(Type type, string title = null)
{
title ??= PageHelper.GetTitle(type);
object page = App.ServiceProvider.GetService(type);
//beforeLoad?.Invoke(panel);
var tabItem = new TabItem() { Header = title, Content = panel };
var tabItem = new TabItem() { Header = title, Content = page };
tab.Items.Add(tabItem);
tab.SelectedIndex = tab.Items.Count - 1;
return panel;
return page;
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions SimpleFFmpegGUI.WPF/Messages/AddNewTabMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Win32;
using System;
using System.Windows.Controls;

namespace SimpleFFmpegGUI.WPF.Messages
{
public class AddNewTabMessage(Type type)
{
public object Page { get; set; }
public Type Type { get; } = type;
}
}
10 changes: 10 additions & 0 deletions SimpleFFmpegGUI.WPF/Messages/ShowCodeArgumentsMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using SimpleFFmpegGUI.Model;
using SimpleFFmpegGUI.WPF.Model;

namespace SimpleFFmpegGUI.WPF.Messages
{
public class ShowCodeArgumentsMessage(UITaskInfo task)
{
public UITaskInfo Task { get; } = task;
}
}
2 changes: 0 additions & 2 deletions SimpleFFmpegGUI.WPF/Model/TasksAndStatuses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ private async void Queue_TaskManagersChanged(object sender, System.Collections.S
var manager = e.NewItems[0] as FFmpegManager;
var unstartStatus = new StatusDto(manager.Task); //先放入一个StatusDto进行占位,因为此时Status还未生成

throw new NotImplementedException();
//Null reference
var task = Tasks.FirstOrDefault(p => p.Id == manager.Task.Id);//找到对应的UITaskInfo
Debug.Assert(task != null);
await task.UpdateSelfAsync(); //用TaskInfo实体更新UITaskInfo
Expand Down
4 changes: 2 additions & 2 deletions SimpleFFmpegGUI.WPF/Pages/PageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace SimpleFFmpegGUI.WPF.Pages
{
public static class PageHelper
{
public static string GetTitle<T>()
public static string GetTitle(Type type)
{
return typeof(T).Name switch
return type.Name switch
{
nameof(AddTaskPage) => "新增任务",
nameof(MediaInfoPage) => "媒体信息",
Expand Down
43 changes: 7 additions & 36 deletions SimpleFFmpegGUI.WPF/Panels/TaskList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
xmlns:model="clr-namespace:SimpleFFmpegGUI.Model;assembly=SimpleFFmpegGUI.Core"
xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:uimodel="clr-namespace:SimpleFFmpegGUI.WPF.Model"
x:Name="root"
d:DesignHeight="450"
d:DesignWidth="800"
Loaded="UserControl_Loaded"
mc:Ignorable="d">
<Grid x:Name="root">
<Grid>
<Grid.Resources />
<Grid.RowDefinitions>
<RowDefinition Height="*" />
Expand All @@ -28,7 +29,6 @@
ItemsSource="{Binding Tasks.Tasks}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectedItem="{Binding Tasks.SelectedTask}"
SelectionChanged="Tasks_SelectionChanged"
SelectionMode="{Binding SelectionMode}">
<ListView.ItemContainerStyle>
<Style
Expand Down Expand Up @@ -206,7 +206,7 @@
Margin="-8,-4"
Padding="8,4"
Background="Transparent"
Click="ArgumentsButton_Click"
Command="{Binding DataContext.ShowArgumentsCommand,ElementName=root}"
Cursor="Hand">
<TextBlock TextDecorations="Underline">详细参数</TextBlock>
</Button>
Expand Down Expand Up @@ -314,55 +314,26 @@
IsEnabled="{Binding CanReset}"
ToolTip="将任务设置为排队状态"
Visibility="{Binding ShowAllTasks, Converter={StaticResource Bool2VisibilityConverter}, ConverterParameter=i}" />
<Button
Click="DeleteButton_Click"
Content="删除"
Visibility="Collapsed" />
<Button
VerticalAlignment="Center"
Background="Transparent"
DataContext="{Binding Tasks.SelectedTask}"
Visibility="{Binding ElementName=lvwTasks, Path=SelectedItems.Count, Converter={StaticResource CountEqualsOneValueConverter}}">

<ui:SymbolIcon Symbol="More" />
<ui:FlyoutService.Flyout>
<ui:MenuFlyout>
<MenuItem
Click="CloneButton_Click"
Command="{Binding CloneCommand}"
Header="建立副本" />
<MenuItem
Click="LogsButton_Click"
Command="{Binding ShowLogsCommand}"
Header="查看日志" />
<Separator Visibility="{Binding Inputs, Converter={StaticResource CountMoreThanZeroConverter}}" />
<MenuItem
Click="OpenInputFileMenuItem_Click"
Header="打开输入文件"
ItemsSource="{Binding Inputs}"
Visibility="{Binding Inputs, Converter={StaticResource CountMoreThanZeroConverter}}">
<MenuItem.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FilePath}" />
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
<MenuItem
Click="OpenInputDirMenuItem_Click"
Header="打开输入文件所在目录"
ItemsSource="{Binding Inputs}"
Visibility="{Binding Inputs, Converter={StaticResource CountMoreThanZeroConverter}}">
<MenuItem.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FilePath}" />
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
<Separator Visibility="{Binding Status, Converter={StaticResource EqualConverter}, ConverterParameter={x:Static model:TaskStatus.Done}}" />
<MenuItem
Click="OpenOutputFileButton_Click"
Command="{Binding OpenOutputFileCommand}"
Header="打开目标文件"
Visibility="{Binding Status, Converter={StaticResource EqualConverter}, ConverterParameter={x:Static model:TaskStatus.Done}}" />
<MenuItem
Click="OpenOutputDirButton_Click"
Command="{Binding OpenOutputDirCommand}"
Header="打开目标文件目录"
Visibility="{Binding Status, Converter={StaticResource EqualConverter}, ConverterParameter={x:Static model:TaskStatus.Done}}" />
</ui:MenuFlyout>
Expand Down
Loading

0 comments on commit e44bff4

Please sign in to comment.