I’m working on an Unreal Engine project, and I’m encountering an issue where my application crashes or becomes unresponsive on macOS when applying fullscreen to a specific monitor.
I have a function, ApplyFullscreenToMonitor(), that applies fullscreen mode to a selected monitor. It first retrieves the display metrics and then tries to move and resize the window based on the monitor’s resolution and position. The function works fine on Windows, but on macOS, the app freezes or becomes unresponsive.
Here’s the code:
void UEtrResolutionSetting::ApplyFullscreenToMonitor()
{
// Get game user settings
UGameUserSettings* GameUserSettings = GEngine->GetGameUserSettings();
if (GameUserSettings)
{
FDisplayMetrics displayMetrics;
FSlateApplication::Get().GetDisplayMetrics(displayMetrics);
const int32 num = displayMetrics.MonitorInfo.Num();
int32 chosenIndex = -1;
int32 primaryIndex = 0;
for (int32 i = 0; i < num; i++)
{
FMonitorInfo& info = displayMetrics.MonitorInfo[i];
if (info.Name == SelectedMonitor.Name)
{
chosenIndex = i;
break;
}
if (info.bIsPrimary) { primaryIndex = i; }
}
int32 finalIndex = (chosenIndex < 0) ? primaryIndex : chosenIndex;
if (chosenIndex < 0 && !SelectedMonitor.Name.IsEmpty())
{
// NOTE: Log something here
}
const FMonitorInfo& info = displayMetrics.MonitorInfo[finalIndex];
const FIntPoint resolution = GameUserSettings->GetScreenResolution();
if (resolution.X > info.NativeWidth || resolution.Y > info.NativeHeight)
{
GameUserSettings->SetScreenResolution({ info.NativeWidth, info.NativeHeight });
}
GameUserSettings->SetWindowPosition(info.WorkArea.Left, info.WorkArea.Top);
if (GEngine && GEngine->GameViewport && GEngine->GameViewport->GetWindow())
{
TSharedPtr<SWindow> window = GEngine->GameViewport->GetWindow();
EWindowMode::Type const WindowMode = GEngine->GetGameUserSettings()->GetFullscreenMode();
window->MoveWindowTo(GameUserSettings->GetWindowPosition());
window->Resize(GameUserSettings->GetScreenResolution());
window->SetWindowMode(WindowMode);
GameUserSettings->SetFullscreenMode(WindowMode);
GameUserSettings->ApplyResolutionSettings(false);
}
else
{
mDelegateHandleViewportCreated = UGameViewportClient::OnViewportCreated().AddUObject(
this, &UEtrResolutionSetting::ApplyFullscreenToMonitor);
}
if (mDelegateHandleViewportCreated.IsValid())
{
UGameViewportClient::OnViewportCreated().Remove(mDelegateHandleViewportCreated);
mDelegateHandleViewportCreated.Reset();
}
}
}