I have a Modal SWindow that our users need to be able to resize. Also, the window will need to resize itself because users can add rows to the SListView inside the window.
Here is the SWindow definition:
void BuildMainEditableWidget()
{
RowWindow = SNew(SWindow)
.AutoCenter(EAutoCenter::None)
.Title(FText::FromString(TEXT("Add New Lines")))
.IsInitiallyMaximized(false)
.ScreenPosition(FVector2D(100, 100))
.CreateTitleBar(true)
.SizingRule(ESizingRule::Autosized)
[
BuildEditableBox()
];
// Using the size that UE figures out, by first setting the Sizing rule to "Autosized"
RowWindow->SlatePrepass();
RowWindow->SlatePrepass();
NewDesiredSize = RowWindow->GetDesiredSize();
// Widget that is defined below
SelectedRowsBox->SlatePrepass();
SelectedRowsBox->SlatePrepass();
float YOffset = AddedRowsArray.Num() > 1 ? ((SelectedRowsBox->GetDesiredSize().Y * 0.55f) * AddedRowsArray.Num()) : 0.f;
NewDesiredSize.Y += YOffset;
// Now we set the window to be "UserSized"
RowWindow = SNew(SWindow)
.AutoCenter(EAutoCenter::None)
.Title(FText::FromString(TEXT("Add New Lines")))
.ScreenPosition(FVector2D(100, 100))
.ClientSize(NewDesiredSize)
.CreateTitleBar(true)
.SizingRule(ESizingRule::UserSized)
[
BuildEditableBox()
];
TSharedPtr<SWindow> RootWindow = FGlobalTabmanager::Get()->GetRootWindow();
FSlateApplication::Get().AddModalWindow(RowWindow.ToSharedRef(), RootWindow, IsSlowTask);
}
Here’s BuildEditableBox()
TSharedRef<SWidget> BuildEditableBox()
{
MainEditableBox = SNew(SBox)
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.VAlign(VAlign_Top)
.AutoHeight()
[
SNew(SBorder)
.Padding(10)
.Content()
[
SNew(SButton).ButtonStyle(&ButtonStyle)
.Text(FText::FromString("Add New Line"))
.OnClicked(FOnClicked::CreateRaw(this, &SLAMSDataEntryWidget::OnAddNewRowButtonPressed))
]
]
+ SVerticalBox::Slot()
.VAlign(VAlign_Top)
.AutoHeight()
[
BuildSelectedRowsWidget()
]
];
return MainEditableBox.ToSharedRef();
}
Finally, the SListView:
TSharedRef<SWidget> BuildSelectedRowsWidget()
{
float DPI_Scale = -1;
FVector2D viewportSize(0, 0);
if (GEditor && MainPluginTab)
{
FIntPoint SizeXY;
FViewport* Viewport = GEditor->GetActiveViewport();
SizeXY = Viewport ? Viewport->GetSizeXY() : FIntPoint(2000,1000);
viewportSize = FVector2D(SizeXY.X, SizeXY.Y);
int32 X = FGenericPlatformMath::FloorToInt((float)SizeXY.X);
int32 Y = FGenericPlatformMath::FloorToInt((float)SizeXY.Y);
DPI_Scale = GetDefault<UUserInterfaceSettings>(UUserInterfaceSettings::StaticClass())->GetDPIScaleBasedOnSize(FIntPoint(X, Y));
}
AdjustedSize = (1 / DPI_Scale) * viewportSize;
AdjustedSize.X *= 0.90f;
float ActualWidth = AdjustedSize.X;
SelectedRowsBox =
SNew(SBox)
.WidthOverride(ActualWidth)
[
SNew(SBorder)
.Padding(10)
.Content()
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.VAlign(VAlign_Top)
.AutoHeight()
[
SAssignNew(LamsAddedRowsWidget, SListView<TSharedPtr<FLAMSWidgetRowData>>)
.ItemHeight(20)
.ScrollBarStyle(&ScrollStyle)
.ListViewStyle(&TableViewStyle)
.ListItemsSource(&AddedRowsArray)
.HeaderRow(
SNew(SHeaderRow)
+ SHeaderRow::Column("Select").DefaultLabel(NSLOCTEXT("Data", "SelectColumn", "Select")) .FillWidth((ActualWidth / 10.f)/ActualWidth)
+ SHeaderRow::Column("Direction").DefaultLabel(NSLOCTEXT("Data", "DirectionColumn", "Direction")) .FillWidth((ActualWidth / 5.f)/ActualWidth)
+ SHeaderRow::Column("Location").DefaultLabel(NSLOCTEXT("Data", "LocationColumn", "Location")) .FillWidth((ActualWidth / 5.f)/ActualWidth)
+ SHeaderRow::Column("Text").DefaultLabel(NSLOCTEXT("Data", "TextColumn", "Text")) .FillWidth((ActualWidth / 2.f )/ActualWidth)
+ SHeaderRow::Column("Remove").DefaultLabel(NSLOCTEXT("Data", "RemoveColumn", "Remove")) .FillWidth((ActualWidth / 10.f)/ActualWidth))
]
]
];
return SelectedRowsBox.ToSharedRef();
}