Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 3 additions & 35 deletions plugin-dev/Source/Sentry/Private/Mac/MacSentrySubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "SentrySettings.h"

#include "Utils/SentryFileUtils.h"
#include "Utils/SentryScreenshotUtils.h"

#include "Misc/CoreDelegates.h"
#include "Misc/FileHelper.h"
Expand Down Expand Up @@ -42,41 +43,8 @@ TSharedPtr<ISentryId> FMacSentrySubsystem::CaptureEnsure(const FString& type, co

FString FMacSentrySubsystem::TryCaptureScreenshot() const
{
NSWindow* MainWindow = [NSApp mainWindow];
if (!MainWindow)
{
UE_LOG(LogSentrySdk, Error, TEXT("No main window found!"));
return FString("");
}

NSRect WindowRect = [MainWindow frame];
CGWindowID WindowID = (CGWindowID)[MainWindow windowNumber];
CGImageRef ScreenshotRef = CGWindowListCreateImage(WindowRect, kCGWindowListOptionIncludingWindow, WindowID, kCGWindowImageDefault);

if (!ScreenshotRef)
{
UE_LOG(LogSentrySdk, Error, TEXT("Failed to capture screenshot - invalid ScreenshotRef."));
return FString("");
}

NSBitmapImageRep* BitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:ScreenshotRef];
NSData* ImageData = [BitmapRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];

TArray<uint8> ImageBytes;
uint32 SavedSize = (uint32)[ImageData length];
ImageBytes.AddUninitialized(SavedSize);
FPlatformMemory::Memcpy(ImageBytes.GetData(), [ImageData bytes], SavedSize);

CGImageRelease(ScreenshotRef);

FString ScreenshotPath = GetScreenshotPath();

if (!FFileHelper::SaveArrayToFile(ImageBytes, *ScreenshotPath))
{
UE_LOG(LogSentrySdk, Error, TEXT("Failed to save screenshot to: %s"), *ScreenshotPath);
return FString("");
}

const FString ScreenshotPath = GetScreenshotPath();
SentryScreenshotUtils::CaptureScreenshot(ScreenshotPath);
return ScreenshotPath;
}

Expand Down
Loading