Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit 0eec186

Browse files
committed
Refactor
1 parent fc720fa commit 0eec186

File tree

8 files changed

+127
-110
lines changed

8 files changed

+127
-110
lines changed

src/Captura.FFmpeg/Audio/FFmpegAudioWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Captura.Audio;
2-
using System;
32
using System.Diagnostics;
43
using System.IO;
4+
using Captura.FFmpeg;
55

66
namespace Captura.Models
77
{
@@ -47,7 +47,7 @@ public void Write(byte[] Data, int Offset, int Count)
4747
{
4848
if (_ffmpegProcess.HasExited)
4949
{
50-
throw new Exception("An Error Occurred with FFmpeg");
50+
throw new FFmpegException(_ffmpegProcess.ExitCode);
5151
}
5252

5353
_ffmpegIn.Write(Data, Offset, Count);

src/Captura.FFmpeg/FFmpegTrimmer.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Captura.Models;
4+
5+
namespace Captura.FFmpeg
6+
{
7+
public class FFmpegTrimmer
8+
{
9+
public async Task Run(string SourceFile,
10+
TimeSpan From,
11+
TimeSpan To,
12+
string DestFile,
13+
bool HasAudio)
14+
{
15+
var argsBuilder = new FFmpegArgsBuilder();
16+
17+
var inputArgs = argsBuilder.AddInputFile(SourceFile)
18+
.AddArg($"-ss {From}")
19+
.AddArg($"-to {To}");
20+
21+
if (HasAudio)
22+
inputArgs.SetAudioCodec("copy");
23+
24+
argsBuilder.AddOutputFile(DestFile);
25+
26+
var args = argsBuilder.GetArgs();
27+
28+
var process = FFmpegService.StartFFmpeg(args, DestFile);
29+
30+
await Task.Factory.StartNew(process.WaitForExit);
31+
32+
if (process.ExitCode != 0)
33+
{
34+
throw new FFmpegException(process.ExitCode);
35+
}
36+
}
37+
}
38+
}

src/Captura.FFmpeg/Video/FFmpegVideoWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void WriteAudio(byte[] Buffer, int Length)
116116
{
117117
if (_ffmpegProcess.HasExited)
118118
{
119-
throw new Exception("An Error Occurred with FFmpeg");
119+
throw new FFmpegException( _ffmpegProcess.ExitCode);
120120
}
121121

122122
if (_firstAudio)

src/Captura/ViewModels/AboutViewModel.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Diagnostics;
44
using System.Windows.Input;
5+
using Reactive.Bindings;
56

67
namespace Captura
78
{
@@ -23,20 +24,8 @@ public AboutViewModel(Settings Settings, ILocalizationProvider Loc) : base(Setti
2324
{
2425
AppVersion = "v" + Version.ToString(3);
2526

26-
HyperlinkCommand = new DelegateCommand(Link =>
27-
{
28-
if (Link is string s)
29-
{
30-
try
31-
{
32-
Process.Start(s);
33-
}
34-
catch
35-
{
36-
// Suppress Errors
37-
}
38-
}
39-
});
27+
HyperlinkCommand = new ReactiveCommand<string>()
28+
.WithSubscribe(M => Process.Start(M));
4029
}
4130
}
4231
}

src/Captura/ViewModels/ExceptionViewModel.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Collections.ObjectModel;
3+
using System.Reactive.Linq;
34
using System.Windows.Input;
5+
using Reactive.Bindings;
6+
using Reactive.Bindings.Extensions;
47
using Screna;
58

69
namespace Captura
@@ -9,13 +12,11 @@ public class ExceptionViewModel : NotifyPropertyChanged
912
{
1013
public ExceptionViewModel()
1114
{
12-
CopyToClipboardCommand = new DelegateCommand(() =>
13-
{
14-
if (Exceptions.Count > 0)
15-
{
16-
Exceptions[0].ToString().WriteToClipboard();
17-
}
18-
});
15+
CopyToClipboardCommand = Exceptions
16+
.ObserveProperty(M => M.Count)
17+
.Select(M => M > 0)
18+
.ToReactiveCommand()
19+
.WithSubscribe(() => Exceptions[0].ToString().WriteToClipboard());
1920
}
2021

2122
string _message = "An unhandled exception occurred. Here are the details.";

src/Captura/ViewModels/RegionSelectorViewModel.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Drawing;
33
using System.Windows.Input;
4+
using Reactive.Bindings;
45

56
// ReSharper disable MemberCanBePrivate.Global
67

@@ -21,25 +22,31 @@ public class RegionSelectorViewModel : NotifyPropertyChanged
2122

2223
public RegionSelectorViewModel()
2324
{
24-
MoveLeftCommand = new DelegateCommand(() => Left -= KeyMoveDelta);
25-
MoveRightCommand = new DelegateCommand(() => Left += KeyMoveDelta);
26-
MoveUpCommand = new DelegateCommand(() => Top -= KeyMoveDelta);
27-
MoveDownCommand = new DelegateCommand(() => Top += KeyMoveDelta);
28-
29-
IncreaseWidthCommand = new DelegateCommand(() => Width += KeyMoveDelta);
30-
DecreaseWidthCommand = new DelegateCommand(() => Width -= KeyMoveDelta);
31-
IncreaseHeightCommand = new DelegateCommand(() => Height += KeyMoveDelta);
32-
DecreaseHeightCommand = new DelegateCommand(() => Height -= KeyMoveDelta);
25+
MoveLeftCommand = new ReactiveCommand()
26+
.WithSubscribe(() => Left -= KeyMoveDelta);
27+
MoveRightCommand = new ReactiveCommand()
28+
.WithSubscribe(() => Left += KeyMoveDelta);
29+
MoveUpCommand = new ReactiveCommand()
30+
.WithSubscribe(() => Top -= KeyMoveDelta);
31+
MoveDownCommand = new ReactiveCommand()
32+
.WithSubscribe(() => Top += KeyMoveDelta);
33+
34+
IncreaseWidthCommand = new ReactiveCommand()
35+
.WithSubscribe(() => Width += KeyMoveDelta);
36+
DecreaseWidthCommand = new ReactiveCommand()
37+
.WithSubscribe(() => Width -= KeyMoveDelta);
38+
IncreaseHeightCommand = new ReactiveCommand()
39+
.WithSubscribe(() => Height += KeyMoveDelta);
40+
DecreaseHeightCommand = new ReactiveCommand()
41+
.WithSubscribe(() => Height -= KeyMoveDelta);
3342
}
3443

3544
public int Left
3645
{
3746
get => _left;
3847
set
3948
{
40-
_left = value;
41-
42-
OnPropertyChanged();
49+
Set(ref _left, value);
4350
RaisePropertyChanged(nameof(LeftDip));
4451
}
4552
}
@@ -55,9 +62,7 @@ public int Top
5562
get => _top;
5663
set
5764
{
58-
_top = value;
59-
60-
OnPropertyChanged();
65+
Set(ref _top, value);
6166
RaisePropertyChanged(nameof(TopDip));
6267
}
6368
}
@@ -73,9 +78,7 @@ public int Width
7378
get => _width;
7479
set
7580
{
76-
_width = Math.Max(value, MinWidth);
77-
78-
OnPropertyChanged();
81+
Set(ref _width, Math.Max(value, MinWidth));
7982
RaisePropertyChanged(nameof(WidthDip));
8083
}
8184
}
@@ -91,9 +94,7 @@ public int Height
9194
get => _height;
9295
set
9396
{
94-
_height = Math.Max(value, MinHeight);
95-
96-
OnPropertyChanged();
97+
Set(ref _height, Math.Max(value, MinHeight));
9798
RaisePropertyChanged(nameof(HeightDip));
9899
}
99100
}

0 commit comments

Comments
 (0)