Added ScreenRecordingStatus

This commit is contained in:
Jaex 2022-09-03 17:47:08 +03:00
parent 930d8a9e1b
commit 71bd0e0264
3 changed files with 58 additions and 33 deletions

View file

@ -341,4 +341,9 @@ public enum ScreenRecordState
{ {
Waiting, BeforeStart, AfterStart, AfterRecordingStart, RecordingEnd, Encoding Waiting, BeforeStart, AfterStart, AfterRecordingStart, RecordingEnd, Encoding
} }
public enum ScreenRecordingStatus
{
Waiting, Countdown, Working, Recording, Paused, Stopped, Aborted
}
} }

View file

@ -37,15 +37,23 @@ public partial class ScreenRecordForm : Form
{ {
public event Action StopRequested; public event Action StopRequested;
public bool IsWorking { get; private set; } private ScreenRecordingStatus status;
public bool IsRecording { get; private set; }
public bool IsCountdown { get; set; } public ScreenRecordingStatus Status
{
get
{
return status;
}
private set
{
status = value;
}
}
public TimeSpan Countdown { get; set; } public TimeSpan Countdown { get; set; }
public Stopwatch Timer { get; private set; } public Stopwatch Timer { get; private set; }
public ManualResetEvent RecordResetEvent { get; set; } public ManualResetEvent RecordResetEvent { get; set; }
public bool IsStopRequested { get; private set; }
public bool IsPauseRequested { get; private set; }
public bool IsAbortRequested { get; private set; }
public bool ActivateWindow { get; set; } = true; public bool ActivateWindow { get; set; } = true;
public float Duration { get; set; } = 0; public float Duration { get; set; } = 0;
@ -137,7 +145,7 @@ private void ScreenRegionForm_Shown(object sender, EventArgs e)
private void ScreenRecordForm_FormClosed(object sender, FormClosedEventArgs e) private void ScreenRecordForm_FormClosed(object sender, FormClosedEventArgs e)
{ {
if (!IsStopRequested) if (Status != ScreenRecordingStatus.Stopped)
{ {
AbortRecording(); AbortRecording();
} }
@ -150,7 +158,7 @@ protected void OnStopRequested()
public void StartCountdown(int milliseconds) public void StartCountdown(int milliseconds)
{ {
IsCountdown = true; Status = ScreenRecordingStatus.Countdown;
Countdown = TimeSpan.FromMilliseconds(milliseconds); Countdown = TimeSpan.FromMilliseconds(milliseconds);
Timer.Start(); Timer.Start();
@ -160,7 +168,10 @@ public void StartCountdown(int milliseconds)
public void StartRecordingTimer() public void StartRecordingTimer()
{ {
IsCountdown = Duration > 0; if (Duration > 0)
{
Status = ScreenRecordingStatus.Countdown;
}
Countdown = TimeSpan.FromSeconds(Duration); Countdown = TimeSpan.FromSeconds(Duration);
borderColor = Color.FromArgb(0, 255, 0); borderColor = Color.FromArgb(0, 255, 0);
@ -185,7 +196,7 @@ private void UpdateTimer()
{ {
TimeSpan timer; TimeSpan timer;
if (IsCountdown) if (Status == ScreenRecordingStatus.Countdown)
{ {
timer = Countdown - Timer.Elapsed; timer = Countdown - Timer.Elapsed;
if (timer.Ticks < 0) timer = TimeSpan.Zero; if (timer.Ticks < 0) timer = TimeSpan.Zero;
@ -246,23 +257,29 @@ private void btnAbort_MouseClick(object sender, MouseEventArgs e)
public void StartStopRecording(bool isPause = false) public void StartStopRecording(bool isPause = false)
{ {
if (IsWorking) if (Status == ScreenRecordingStatus.Working || Status == ScreenRecordingStatus.Recording)
{ {
IsStopRequested = true;
if (isPause) if (isPause)
{ {
RecordResetEvent.Reset(); RecordResetEvent.Reset();
IsPauseRequested = true; Status = ScreenRecordingStatus.Paused;
} }
else if (Status != ScreenRecordingStatus.Recording && Status != ScreenRecordingStatus.Paused)
if (!IsRecording)
{ {
IsAbortRequested = true; Status = ScreenRecordingStatus.Aborted;
}
else
{
Status = ScreenRecordingStatus.Stopped;
} }
OnStopRequested(); OnStopRequested();
} }
else if (Status == ScreenRecordingStatus.Paused && !isPause)
{
Status = ScreenRecordingStatus.Stopped;
RecordResetEvent.Set();
}
else if (RecordResetEvent != null) else if (RecordResetEvent != null)
{ {
RecordResetEvent.Set(); RecordResetEvent.Set();
@ -271,7 +288,7 @@ public void StartStopRecording(bool isPause = false)
public void AbortRecording() public void AbortRecording()
{ {
IsAbortRequested = true; Status = ScreenRecordingStatus.Aborted;
StartStopRecording(); StartStopRecording();
} }
@ -295,22 +312,26 @@ public void ChangeState(ScreenRecordState state)
cmsMain.Enabled = true; cmsMain.Enabled = true;
break; break;
case ScreenRecordState.AfterStart: case ScreenRecordState.AfterStart:
IsWorking = true; Status = ScreenRecordingStatus.Working;
IsPauseRequested = false;
string trayTextAfterStart = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Click_tray_icon_to_stop_recording_; string trayTextAfterStart = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Click_tray_icon_to_stop_recording_;
niTray.Text = trayTextAfterStart.Truncate(63); niTray.Text = trayTextAfterStart.Truncate(63);
niTray.Icon = Resources.control_record.ToIcon(); niTray.Icon = Resources.control_record.ToIcon();
tsmiStart.Text = Resources.ScreenRecordForm_Stop; tsmiStart.Text = Resources.ScreenRecordForm_Stop;
btnStart.Text = Resources.ScreenRecordForm_Stop; btnStart.Text = Resources.ScreenRecordForm_Stop;
// TODO: Translate
btnPause.Text = "Pause";
break; break;
case ScreenRecordState.AfterRecordingStart: case ScreenRecordState.AfterRecordingStart:
IsRecording = true; Status = ScreenRecordingStatus.Recording;
StartRecordingTimer(); StartRecordingTimer();
break; break;
case ScreenRecordState.RecordingEnd: case ScreenRecordState.RecordingEnd:
IsWorking = false;
IsRecording = false;
StopRecordingTimer(); StopRecordingTimer();
// TODO: Translate
if (Status == ScreenRecordingStatus.Paused)
{
btnPause.Text = "Resume";
}
break; break;
case ScreenRecordState.Encoding: case ScreenRecordState.Encoding:
Hide(); Hide();

View file

@ -213,13 +213,13 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
FileHelpers.DeleteFile(tempPath); FileHelpers.DeleteFile(tempPath);
} }
do while (recordForm.Status == ScreenRecordingStatus.Waiting || recordForm.Status == ScreenRecordingStatus.Paused)
{ {
if (!abortRequested) if (!abortRequested)
{ {
recordForm.ChangeState(ScreenRecordState.BeforeStart); recordForm.ChangeState(ScreenRecordState.BeforeStart);
if (recordForm.IsPauseRequested || !taskSettings.CaptureSettings.ScreenRecordAutoStart) if (recordForm.Status == ScreenRecordingStatus.Paused || !taskSettings.CaptureSettings.ScreenRecordAutoStart)
{ {
recordForm.RecordResetEvent.WaitOne(); recordForm.RecordResetEvent.WaitOne();
} }
@ -235,18 +235,18 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
} }
} }
if (recordForm.IsAbortRequested) if (recordForm.Status == ScreenRecordingStatus.Aborted)
{ {
abortRequested = true; abortRequested = true;
} }
if (recordForm.IsPauseRequested && File.Exists(path)) if (recordForm.Status == ScreenRecordingStatus.Waiting || recordForm.Status == ScreenRecordingStatus.Paused)
{ {
FileHelpers.RenameFile(path, concatPath); if (recordForm.Status == ScreenRecordingStatus.Paused && File.Exists(path))
} {
FileHelpers.RenameFile(path, concatPath);
}
if (!abortRequested)
{
ScreenRecordingOptions options = new ScreenRecordingOptions() ScreenRecordingOptions options = new ScreenRecordingOptions()
{ {
IsRecording = true, IsRecording = true,
@ -269,7 +269,7 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
screenRecorder.StartRecording(); screenRecorder.StartRecording();
recordForm.ChangeState(ScreenRecordState.RecordingEnd); recordForm.ChangeState(ScreenRecordState.RecordingEnd);
if (recordForm.IsAbortRequested) if (recordForm.Status == ScreenRecordingStatus.Aborted)
{ {
abortRequested = true; abortRequested = true;
} }
@ -286,7 +286,6 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
} }
} }
} }
while (recordForm.IsPauseRequested);
} }
catch (Exception e) catch (Exception e)
{ {