Btw in case anyone wants to support mouse also, I found a solution and verified it works in production
Mostly when you click outside any widget, Viewport
receives focus thats not what we want, to prevent such behavior:
- Create button class inherited from
UCommonButtonBase
withNativeOnFocusChanging
// UBBaseCommonButton.cpp
class BOGATYR_API UBBaseCommonButton : public UCommonButtonBase, public IUserObjectListEntry
{
GENERATED_BODY()
public:
#if PLATFORM_DESKTOP || WITH_EDITOR
virtual void NativeOnFocusChanging(const FWeakWidgetPath& PreviousFocusPath, const FWidgetPath& NewWidgetPath, const FFocusEvent& InFocusEvent) override;
#endif
};
- in
NativeOnFocusChanging
check thatFFocusEvent
was caused by mouse and new focused widget type isSViewport
or for editor -SPIEViewport
and prevent such event by focusing back button, not ideal but good solution in my case
.h
// UBBaseCommonButton.h
#if PLATFORM_DESKTOP || WITH_EDITOR
void UBBaseCommonButton::NativeOnFocusChanging(const FWeakWidgetPath& PreviousFocusPath, const FWidgetPath& NewWidgetPath, const FFocusEvent& InFocusEvent)
{
TSharedRef<SWidget> NewWidget = NewWidgetPath.GetLastWidget();
TWeakPtr<SWidget> PreviousWidget = PreviousFocusPath.GetLastWidget();
FName WidgetType = NewWidget.Get().GetType();
if (InFocusEvent.GetCause() == EFocusCause::Mouse
&& (WidgetType == FName("SPIEViewport") || WidgetType == FName("SViewport")))
{
SetKeyboardFocus();
return;
}
Super::NativeOnFocusChanging(PreviousFocusPath, NewWidgetPath, InFocusEvent);
}
#endif