将开始录像和停止录像拆分为独立按钮

This commit is contained in:
tfarcraw 2026-11-10 17:49:45 +08:00
parent ad7007221c
commit abc3290e41
4 changed files with 51 additions and 21 deletions

View File

@ -59,13 +59,13 @@ public sealed class CameraViewModel : BindableBase
public Func<Task<bool>>? ConfirmRestartAsync { get; set; }
public bool CanChangeDirectory => _recipeRecordingDirectory is null && !IsBusy && !IsRecording && !_closing;
public string PreviewButtonText => IsPreviewing ? "关闭相机" : "开启相机预览";
public string RecordButtonText => IsRecording ? "停止录像" : "开始录像";
public string RecordingInfo { get => _recordingInfo; private set => SetProperty(ref _recordingInfo, value); }
public string SaveDirectory { get => _directory; set => SetProperty(ref _directory, _recipeRecordingDirectory ?? value); }
public string LastRecording { get => _lastRecording; private set { SetProperty(ref _lastRecording, value); UpdateCommands(); } }
public double RecordingFps { get; set; } = 30;
public DelegateCommand PreviewCommand { get; }
public DelegateCommand RecordCommand { get; }
public DelegateCommand StartRecordingCommand { get; }
public DelegateCommand StopRecordingCommand { get; }
public DelegateCommand BrowseCommand { get; }
public DelegateCommand RevealCommand { get; }
public DelegateCommand PauseCommand { get; }
@ -91,7 +91,8 @@ public sealed class CameraViewModel : BindableBase
}
catch (Exception e) { Status = "录像目录配置读取失败:" + e.Message; }
PreviewCommand = Command(TogglePreviewAsync, () => !IsBusy && !_closing && (IsPreviewing || SelectedCamera != null));
RecordCommand = Command(ToggleRecordingAsync, () => !IsBusy && !_closing && (IsRecording || (!HasStandardVideo && IsPreviewing && Preview != null && HasRecentFrame)));
StartRecordingCommand = Command(StartRecordingAsync, () => !IsBusy && !_closing && !IsRecording && !HasStandardVideo && IsPreviewing && Preview != null && HasRecentFrame);
StopRecordingCommand = Command(() => { StopRecording(); return Task.CompletedTask; }, () => !IsBusy && !_closing && IsRecording);
PauseCommand = Command(() => { TogglePause(); return Task.CompletedTask; }, () => !IsBusy && !_closing && IsRecording);
RestartCommand = Command(RestartRecordingAsync, () => _sopStandard && !IsBusy && !_closing && HasStandardVideo && IsPreviewing && Preview != null && HasRecentFrame);
PlayVideoCommand = Command(ToggleVideoPlaybackAsync, () => _sopStandard && !IsBusy && !_closing && !IsRecording && HasStandardVideo);
@ -150,7 +151,7 @@ public sealed class CameraViewModel : BindableBase
private void StateChanged()
{
RaisePropertyChanged(nameof(CanChangeDirectory)); RaisePropertyChanged(nameof(SelectedCameraName));
RaisePropertyChanged(nameof(PreviewButtonText)); RaisePropertyChanged(nameof(RecordButtonText)); UpdateCommands();
RaisePropertyChanged(nameof(PreviewButtonText)); UpdateCommands();
RaisePropertyChanged(nameof(PauseButtonText)); RaisePropertyChanged(nameof(HasStandardVideo));
}
private async Task TogglePreviewAsync()
@ -187,9 +188,9 @@ public sealed class CameraViewModel : BindableBase
await Playback.PlayAsync(StandardVideoPath);
if (Playback.IsPlaying) Status = "正在播放已录制的SOP流程视频";
}
private Task ToggleRecordingAsync()
private Task StartRecordingAsync()
{
if (IsRecording) { StopRecording(); return Task.CompletedTask; }
if (IsRecording) return Task.CompletedTask;
if (HasStandardVideo) throw new InvalidOperationException("已有SOP流程视频,请使用“重新录制”并确认删除旧视频");
if (string.IsNullOrWhiteSpace(SaveDirectory)) throw new ArgumentException("请选择录像保存目录");
var directory = Path.GetFullPath(SaveDirectory.Trim());
@ -234,7 +235,7 @@ public sealed class CameraViewModel : BindableBase
File.Delete(StandardVideoPath);
LastRecording = "";
StateChanged();
await ToggleRecordingAsync();
await StartRecordingAsync();
}
private void StopRecording()
{

View File

@ -46,8 +46,9 @@
<Button Grid.Column="2" Content="打开保存位置" Command="{Binding RevealCommand}" Margin="0,6,0,8" />
</Grid>
<DockPanel>
<Button DockPanel.Dock="Left" Content="{Binding RecordButtonText}" Command="{Binding RecordCommand}" BorderBrush="{StaticResource Accent}" MinWidth="130" />
<Button DockPanel.Dock="Left" Content="开始录像" Command="{Binding StartRecordingCommand}" BorderBrush="{StaticResource Accent}" MinWidth="110" />
<Button DockPanel.Dock="Left" Content="{Binding PauseButtonText}" Command="{Binding PauseCommand}" MinWidth="110" Margin="8,0,0,0" />
<Button DockPanel.Dock="Left" Content="停止录像" Command="{Binding StopRecordingCommand}" MinWidth="110" Margin="8,0,0,0" />
<TextBlock Text="AVI / MJPEG · 30 fps · 最长5分钟,到时自动停止并提示" Foreground="{StaticResource Dim}" Margin="12,0,0,6" TextWrapping="Wrap" />
</DockPanel>
<TextBlock Text="{Binding LastRecording}" TextTrimming="CharacterEllipsis" Foreground="{StaticResource Dim}" Margin="0,0,0,8" ToolTip.Tip="{Binding LastRecording}" />

View File

@ -451,15 +451,21 @@
</Grid>
</Border>
<StackPanel Grid.Row="2" Spacing="8" Margin="0,12,0,8">
<StackPanel Orientation="Horizontal" Spacing="8">
<WrapPanel Orientation="Horizontal">
<WrapPanel.Styles>
<Style Selector="WrapPanel > Button">
<Setter Property="Margin" Value="0,0,8,8" />
</Style>
</WrapPanel.Styles>
<Button Content="{Binding PreviewButtonText}" Command="{Binding PreviewCommand}" />
<Button Content="{Binding RecordButtonText}" Command="{Binding RecordCommand}" />
<Button Content="开始录像" Command="{Binding StartRecordingCommand}" />
<Button Content="{Binding PauseButtonText}" Command="{Binding PauseCommand}" />
<Button Content="停止录像" Command="{Binding StopRecordingCommand}" />
<Button Content="重新录制" Command="{Binding RestartCommand}" />
<Button Content="{Binding PlayVideoButtonText}" Command="{Binding PlayVideoCommand}" />
<Button Content="停止播放" Command="{Binding StopVideoCommand}" />
<Button Content="打开保存位置" Command="{Binding RevealCommand}" />
</StackPanel>
</WrapPanel>
<Grid ColumnDefinitions="*,Auto">
<Slider x:Name="VideoSeekSlider" Minimum="0" Maximum="100" Value="{Binding Playback.Progress, Mode=TwoWay}" IsEnabled="{Binding Playback.IsLoaded}" SmallChange="1" LargeChange="10" VerticalAlignment="Center" ToolTip.Tip="拖动进度条跳转播放位置" />
<TextBlock Grid.Column="1" Text="{Binding Playback.TimeText}" Margin="12,0,0,0" />

View File

@ -39,16 +39,19 @@ internal static class Program
var window = new CameraWindow(model); window.Show(); Dispatcher.UIThread.RunJobs(); window.UpdateLayout();
var pauseButton = window.GetVisualDescendants().OfType<Button>().Single(b => ReferenceEquals(b.Command, model.PauseCommand));
Check(!pauseButton.IsEffectivelyEnabled && pauseButton.Content?.ToString() == "暂停录制", "Part recording exposes a pause button disabled before recording");
CheckRecordingButtons(window, model, false, false);
Check(!model.PreviewCommand.CanExecute() && model.SelectedCameraName.Contains("运行设置"), "Recording requires the camera selected in runtime settings");
Check(!window.GetVisualDescendants().OfType<ComboBox>().Any() && !window.GetVisualDescendants().OfType<Button>().Any(b => b.Content?.ToString() == "刷新相机"), "Recording window has no camera selector or refresh button");
selected = new CameraInfo { Kind = CameraKind.Hikvision, Index = 7, SerialNumber = "GLOBAL-CAMERA", Name = "运行设置中的相机" };
Tick(model); model.PreviewCommand.Execute();
Check(ReferenceEquals(camera.ConnectedCamera, selected) && camera.EnumerationCount == 0, "Recording uses the exact global camera without enumerating devices");
Thread.Sleep(75); camera.Emit(); Dispatcher.UIThread.RunJobs();
Check(model.RecordCommand.CanExecute(), "Preview is ready for recording");
Check(model.StartRecordingCommand.CanExecute(), "Preview is ready for recording");
CheckRecordingButtons(window, model, true, false);
var notices = 0;
model.RecordingLimitReached += message => { notices++; Check(message.Contains("5分钟") && message.Contains(model.LastRecording), "Timeout notice contains the limit and saved path"); };
model.RecordCommand.Execute();
model.StartRecordingCommand.Execute();
CheckRecordingButtons(window, model, false, true);
for (var second = 1; second <= 299; second++) { clock.Advance(TimeSpan.FromSeconds(1)); camera.Emit(); }
Tick(model);
Check(model.IsRecording && notices == 0, "Recording continues at 4:59 without a popup");
@ -57,13 +60,14 @@ internal static class Program
clock.Advance(TimeSpan.FromMilliseconds(700)); camera.Emit();
Tick(model);
Check(!model.IsRecording && model.IsPreviewing && notices == 1, "Timeout stops recording once and keeps the preview running");
CheckRecordingButtons(window, model, true, false);
CheckVideo(model.LastRecording, 300);
var popup = (Window?)typeof(CameraWindow).GetField("_recordingLimitDialog", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(window);
Check(popup is { IsVisible: true } && popup.Title == "录像时长提示", "Timeout opens the modal notification");
Check(popup!.GetVisualDescendants().OfType<TextBlock>().Any(t => t.Text?.Contains("5分钟") == true), "Popup displays the five-minute explanation");
Tick(model); Tick(model); Check(notices == 1, "Later timer ticks do not repeat the popup");
popup!.Close(); Dispatcher.UIThread.RunJobs();
model.RecordCommand.Execute();
model.StartRecordingCommand.Execute();
Check(model.IsRecording, "A second recording starts with a fresh timeout");
clock.Advance(TimeSpan.FromSeconds(1)); camera.Emit(); Tick(model);
Check(model.IsRecording && notices == 1, "Previous recording duration does not carry over");
@ -72,17 +76,19 @@ internal static class Program
clock.Advance(TimeSpan.FromMinutes(6)); camera.Emit(); Tick(model);
Check(model.IsRecording && model.IsPaused && model.IsPreviewing && notices == 1 && model.RecordingInfo.Contains("00:00:01"), "Part recording excludes paused time from duration and timeout while keeping preview active");
Check(pauseButton.Content?.ToString() == "继续录制", "Part recording button offers resume when paused");
CheckRecordingButtons(window, model, false, true);
pauseButton.Command.Execute(pauseButton.CommandParameter); Dispatcher.UIThread.RunJobs();
Check(!model.IsPaused && pauseButton.Content?.ToString() == "暂停录制", "Part recording resumes through the same button");
clock.Advance(TimeSpan.FromSeconds(1)); camera.Emit(); Tick(model);
pauseButton.Command.Execute(pauseButton.CommandParameter);
model.RecordCommand.Execute();
model.StopRecordingCommand.Execute();
Check(!model.IsRecording && notices == 1, "Manual stop saves without a timeout popup");
Check(!model.IsPaused && !pauseButton.IsEffectivelyEnabled && pauseButton.Content?.ToString() == "暂停录制", "Stopping a paused part recording resets the pause button");
CheckRecordingButtons(window, model, true, false);
CheckVideo(model.LastRecording, 2);
clock.Advance(TimeSpan.FromMinutes(6)); Tick(model);
Check(notices == 1, "Stopped recordings do not later trigger a timeout");
camera.Emit(); model.RecordCommand.Execute();
camera.Emit(); model.StartRecordingCommand.Execute();
for (var second = 1; second <= 300; second++) { clock.Advance(TimeSpan.FromSeconds(1)); camera.Emit(); }
Tick(model);
Check(notices == 2 && !model.IsRecording, "A later recording independently stops at its own five-minute limit");
@ -102,6 +108,16 @@ internal static class Program
}
}
private static void Tick(CameraViewModel model) => typeof(CameraViewModel).GetMethod("Tick", BindingFlags.Instance | BindingFlags.NonPublic)!.Invoke(model, null);
private static void CheckRecordingButtons(Control view, CameraViewModel model, bool canStart, bool canStop)
{
Dispatcher.UIThread.RunJobs();
var buttons = view.GetVisualDescendants().OfType<Button>().ToArray();
var start = buttons.Single(b => ReferenceEquals(b.Command, model.StartRecordingCommand));
var stop = buttons.Single(b => ReferenceEquals(b.Command, model.StopRecordingCommand));
Check(start.Content?.ToString() == "开始录像" && stop.Content?.ToString() == "停止录像" && start.IsVisible && stop.IsVisible
&& start.IsEffectivelyEnabled == canStart && stop.IsEffectivelyEnabled == canStop,
$"Separate recording buttons keep fixed labels and correct availability (start={canStart}, stop={canStop})");
}
private static void RunSopRecording(string root)
{
var store = new RecipeWorkspace(root);
@ -120,13 +136,18 @@ internal static class Program
var tabs = editor.GetVisualDescendants().OfType<TabControl>().Single(); tabs.SelectedIndex = 5; window.UpdateLayout();
Check(tabs.ItemCount == 7 && ((TabItem)tabs.Items[5]!).Header?.ToString() == "SOP流程视频", "Recipe editor contains the SOP recording page");
var previewButton = editor.GetVisualDescendants().OfType<Button>().Single(b => ReferenceEquals(b.Command, model.PreviewCommand));
var videoButtons = ((StackPanel)previewButton.Parent!).Children.OfType<Button>().ToArray();
Check(ReferenceEquals(videoButtons[0].Command, model.PreviewCommand) && ReferenceEquals(videoButtons[1].Command, model.RecordCommand) && ReferenceEquals(videoButtons[^1].Command, model.RevealCommand), "Preview precedes recording and saved location is last");
var videoButtons = ((Panel)previewButton.Parent!).Children.OfType<Button>().ToArray();
Check(ReferenceEquals(videoButtons[0].Command, model.PreviewCommand) && ReferenceEquals(videoButtons[1].Command, model.StartRecordingCommand) && ReferenceEquals(videoButtons[^1].Command, model.RevealCommand), "Preview precedes recording and saved location is last");
Check(videoButtons[^1].TranslatePoint(new Point(videoButtons[^1].Bounds.Width, 0), editor)!.Value.X <= editor.Bounds.Width, "Video controls fit the minimum editor width");
window.Width = 840; window.UpdateLayout();
Check(videoButtons.All(b => b.TranslatePoint(new Point(b.Bounds.Width, 0), editor)!.Value.X <= editor.Bounds.Width), "Separate recording buttons wrap within a narrow editor");
window.Width = 1040; window.UpdateLayout();
Check(!editor.GetVisualDescendants().OfType<Button>().Any(b => b.Content?.ToString() == "刷新相机"), "SOP page has no independent camera refresh");
model.PreviewCommand.Execute(); Thread.Sleep(75); camera.Emit(); Dispatcher.UIThread.RunJobs();
Check(ReferenceEquals(camera.ConnectedCamera, selected) && camera.EnumerationCount == 0, "SOP recording also uses the global camera directly");
model.RecordCommand.Execute();
CheckRecordingButtons(editor, model, true, false);
model.StartRecordingCommand.Execute();
CheckRecordingButtons(editor, model, false, true);
Check(!model.PlayVideoCommand.CanExecute(), "Playback is disabled until recording has stopped and saved");
var expectedPath = Path.Combine(store.DirectoryFor(config.Sop.Name), "sop_stand", "sop.avi");
Check(model.LastRecording == expectedPath && !model.CanChangeDirectory, "SOP video is stored separately under its current recipe");
@ -137,8 +158,9 @@ internal static class Program
model.RestartCommand.Execute(); FindSopDialog(editor).Close(false); PumpIdle(model);
Check(model.IsPaused && model.IsRecording, "Cancelling replacement preserves an intentionally paused recording");
model.PauseCommand.Execute(); clock.Advance(TimeSpan.FromSeconds(1)); camera.Emit();
model.RecordCommand.Execute(); CheckVideo(model.LastRecording, 2);
Check(!model.RecordCommand.CanExecute() && model.RestartCommand.CanExecute(), "Existing video can only be replaced through confirmed re-recording");
model.StopRecordingCommand.Execute(); CheckVideo(model.LastRecording, 2);
CheckRecordingButtons(editor, model, false, false);
Check(!model.StartRecordingCommand.CanExecute() && model.RestartCommand.CanExecute(), "Existing video can only be replaced through confirmed re-recording");
var original = File.ReadAllBytes(expectedPath);
model.PlayVideoCommand.Execute(); PumpIdle(model);
Check(model.Playback.IsPlaying && !model.IsPreviewing && model.DisplayImage is { PixelWidth: 64, PixelHeight: 48 }, "Saved SOP video plays inside the page after releasing the camera");