-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenCapture.cs
More file actions
51 lines (41 loc) · 1.73 KB
/
Copy pathScreenCapture.cs
File metadata and controls
51 lines (41 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Runtime.InteropServices;
using ThatCapture.Platforms;
namespace ThatCapture;
public sealed class ScreenCapture : IScreenCapture
{
private readonly IScreenCapture _impl;
/// <summary>The platform backend currently in use.</summary>
public CapturePlatform Platform { get; }
/// <summary>
/// Creates a <see cref="ScreenCapture"/> using the auto-detected platform backend.
/// Pass <paramref name="platform"/> to override the detected platform.
/// </summary>
public ScreenCapture(CapturePlatform? platform = null)
{
Platform = platform ?? DetectPlatform();
_impl = CreateImpl(Platform);
}
public Task<CaptureResult> CaptureAreaAsync(int x, int y, int width, int height) =>
_impl.CaptureAreaAsync(x, y, width, height);
private static CapturePlatform DetectPlatform()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return CapturePlatform.Windows;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return CapturePlatform.MacOS;
var sessionType = Environment.GetEnvironmentVariable("XDG_SESSION_TYPE") ?? "";
return sessionType.Equals("wayland", StringComparison.OrdinalIgnoreCase)
? CapturePlatform.Wayland
: CapturePlatform.X11;
}
private static IScreenCapture CreateImpl(CapturePlatform platform) => platform switch
{
#if WINDOWS
CapturePlatform.Windows => new WindowsScreenCapture(),
#endif
CapturePlatform.MacOS => new MacScreenCapture(),
CapturePlatform.X11 => new X11ScreenCapture(),
CapturePlatform.Wayland => new WaylandScreenCapture(),
_ => throw new ArgumentOutOfRangeException(nameof(platform), platform, null),
};
}