Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mouse click effect capability for screen recording #7622

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
run effect outside Task.Run
  • Loading branch information
Vertygo committed Oct 13, 2024
commit 95c9174e28fd4fb08274c2354d386d79b61c9b64
45 changes: 24 additions & 21 deletions ShareX.HelpersLib/Input/MouseClickEffectManager.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
using System;
using System.Windows.Forms;

namespace ShareX.HelpersLib
{
public class MouseClickEffectManager : IDisposable
{
private bool disposed;
private bool showEffect;
private MouseHook mouseHook;
private MouseClickEffectForm clickEffectForm;
private bool _disposed;


// start click mouse effects
public void Start()
{
var action = new Action(() =>
{
clickEffectForm = new MouseClickEffectForm();
mouseHook = new MouseHook();
mouseHook.OnMouseEvent += OnMouseEvent;
clickEffectForm.Show();
});

// form needs to be running on main UI thread otherwise it will not show up
if (Application.OpenForms[0].InvokeRequired)
{
Application.OpenForms[0].Invoke(action);
}
else
{
action();
}
showEffect = true;
clickEffectForm = new MouseClickEffectForm();
mouseHook = new MouseHook();
mouseHook.OnMouseEvent += OnMouseEvent;
clickEffectForm.Show();
}

/// <summary>
/// Mouse event handler
/// </summary>
private void OnMouseEvent(MouseEventInfo eventInfo)
{
if (!showEffect)
return;

switch (eventInfo.ButtonState)
{
case ButtonState.LeftButtonDown:
Expand All @@ -47,8 +39,19 @@ private void OnMouseEvent(MouseEventInfo eventInfo)
}
}

public void Pause()
{
showEffect = false;
}

public void Resume()
{
showEffect = true;
}

public void Stop()
{
showEffect = false;
Dispose();
}

Expand All @@ -59,7 +62,7 @@ public void Dispose()

protected virtual void Dispose(bool disposing)
{
if (_disposed)
if (disposed)
{
return;
}
Expand All @@ -75,7 +78,7 @@ protected virtual void Dispose(bool disposing)
mouseHook = null;
clickEffectForm = null;

_disposed = true;
disposed = true;
}
}
}
4 changes: 1 addition & 3 deletions ShareX.HelpersLib/Input/MouseEventInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@


using System.Drawing;
using System.Drawing;

namespace ShareX.HelpersLib
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class ScreenRecordingOptions
public Rectangle CaptureArea { get; set; }
public float Duration { get; set; }
public bool DrawCursor { get; set; }
public bool ShowCursorEffect { get; set; }
public FFmpegOptions FFmpeg { get; set; } = new FFmpegOptions();

public string GetFFmpegCommands()
Expand Down
1 change: 0 additions & 1 deletion ShareX/Forms/TaskSettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,6 @@ private void btnScreenRecorderFFmpegOptions_Click(object sender, EventArgs e)
OutputPath = "output.mp4",
CaptureArea = Screen.PrimaryScreen.Bounds,
DrawCursor = TaskSettings.CaptureSettings.ScreenRecordShowCursor,
ShowCursorEffect = TaskSettings.CaptureSettings.ScreenRecordShowCursorEffect,
};

using (FFmpegOptionsForm form = new FFmpegOptionsForm(options))
Expand Down
35 changes: 18 additions & 17 deletions ShareX/ScreenRecordManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,23 @@ public static void StartStopRecording(ScreenRecordOutput outputType, ScreenRecor

public static void StopRecording()
{
if (IsRecording && screenRecorder != null)
if (IsRecording)
{
screenRecorder.StopRecording();
}

if (IsRecording && mouseClickEffectManager != null)
{
mouseClickEffectManager.Stop();
mouseClickEffectManager?.Pause();
screenRecorder?.StopRecording();
}
}

public static void PauseScreenRecording()
{
if (IsRecording && recordForm != null && !recordForm.IsDisposed)
if (IsRecording)
{
recordForm.PauseResumeRecording();
mouseClickEffectManager?.Pause();

if (recordForm != null && !recordForm.IsDisposed)
{
recordForm.PauseResumeRecording();
}
}
}

Expand Down Expand Up @@ -208,6 +209,12 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
recordForm.StopRequested += StopRecording;
recordForm.Show();

if (taskSettings.CaptureSettings.ScreenRecordShowCursorEffect)
{
mouseClickEffectManager = new MouseClickEffectManager();
mouseClickEffectManager.Start();
}

Task.Run(() =>
{
try
Expand Down Expand Up @@ -270,6 +277,7 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
}

recordForm.ChangeState(ScreenRecordState.AfterStart);
mouseClickEffectManager.Resume();

captureRectangle = recordForm.RecordingRegion;

Expand All @@ -283,18 +291,11 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
OutputPath = path,
CaptureArea = captureRectangle,
DrawCursor = taskSettings.CaptureSettings.ScreenRecordShowCursor,
ShowCursorEffect = taskSettings.CaptureSettings.ScreenRecordShowCursorEffect
};

Screenshot screenshot = TaskHelpers.GetScreenshot(taskSettings);
screenshot.CaptureCursor = taskSettings.CaptureSettings.ScreenRecordShowCursor;

if (options.ShowCursorEffect)
{
mouseClickEffectManager = new MouseClickEffectManager();
mouseClickEffectManager.Start();
}

screenRecorder?.Dispose();
screenRecorder = new ScreenRecorder(ScreenRecordOutput.FFmpeg, options, screenshot, captureRectangle);
screenRecorder.RecordingStarted += ScreenRecorder_RecordingStarted;
Expand Down Expand Up @@ -351,7 +352,7 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t

if(mouseClickEffectManager != null)
{
mouseClickEffectManager.Dispose();
mouseClickEffectManager.Stop();
mouseClickEffectManager = null;
}

Expand Down