SWebInterfaceBrowserView.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. // Engine/Source/Runtime/WebBrowser/Private/SWebBrowserView.cpp
  2. #include "SWebInterfaceBrowserView.h"
  3. #include "Misc/CommandLine.h"
  4. #include "Misc/ConfigCacheIni.h"
  5. #include "Containers/Ticker.h"
  6. #include "WebInterfaceBrowserModule.h"
  7. #include "Layout/WidgetPath.h"
  8. #include "Framework/Application/MenuStack.h"
  9. #include "Framework/Application/SlateApplication.h"
  10. #include "IWebInterfaceBrowserDialog.h"
  11. #include "IWebInterfaceBrowserWindow.h"
  12. #include "WebInterfaceBrowserViewport.h"
  13. #include "IWebInterfaceBrowserAdapter.h"
  14. #if WITH_CEF3
  15. # include "CEF/CEFWebInterfaceBrowserWindow.h"
  16. #else
  17. # define DUMMY_WEB_BROWSER 1
  18. #endif
  19. #define LOCTEXT_NAMESPACE "WebInterfaceBrowser"
  20. SWebInterfaceBrowserView::SWebInterfaceBrowserView()
  21. {
  22. }
  23. SWebInterfaceBrowserView::~SWebInterfaceBrowserView()
  24. {
  25. if (BrowserWindow.IsValid())
  26. {
  27. BrowserWindow->OnDocumentStateChanged().RemoveAll(this);
  28. BrowserWindow->OnNeedsRedraw().RemoveAll(this);
  29. BrowserWindow->OnTitleChanged().RemoveAll(this);
  30. BrowserWindow->OnUrlChanged().RemoveAll(this);
  31. BrowserWindow->OnToolTip().RemoveAll(this);
  32. BrowserWindow->OnShowPopup().RemoveAll(this);
  33. BrowserWindow->OnDismissPopup().RemoveAll(this);
  34. BrowserWindow->OnShowDialog().Unbind();
  35. BrowserWindow->OnDismissAllDialogs().Unbind();
  36. BrowserWindow->OnCreateWindow().Unbind();
  37. BrowserWindow->OnCloseWindow().Unbind();
  38. BrowserWindow->OnSuppressContextMenu().Unbind();
  39. BrowserWindow->OnDragWindow().Unbind();
  40. BrowserWindow->OnConsoleMessage().Unbind();
  41. if (BrowserWindow->OnBeforeBrowse().IsBoundToObject(this))
  42. {
  43. BrowserWindow->OnBeforeBrowse().Unbind();
  44. }
  45. if (BrowserWindow->OnLoadUrl().IsBoundToObject(this))
  46. {
  47. BrowserWindow->OnLoadUrl().Unbind();
  48. }
  49. if (BrowserWindow->OnBeforePopup().IsBoundToObject(this))
  50. {
  51. BrowserWindow->OnBeforePopup().Unbind();
  52. }
  53. }
  54. TSharedPtr<SWindow> SlateParentWindow = SlateParentWindowPtr.Pin();
  55. if (SlateParentWindow.IsValid())
  56. {
  57. SlateParentWindow->GetOnWindowDeactivatedEvent().RemoveAll(this);
  58. }
  59. if (SlateParentWindow.IsValid())
  60. {
  61. SlateParentWindow->GetOnWindowActivatedEvent().RemoveAll(this);
  62. }
  63. }
  64. void SWebInterfaceBrowserView::Construct(const FArguments& InArgs, const TSharedPtr<IWebInterfaceBrowserWindow>& InWebBrowserWindow)
  65. {
  66. OnLoadCompleted = InArgs._OnLoadCompleted;
  67. OnLoadError = InArgs._OnLoadError;
  68. OnLoadStarted = InArgs._OnLoadStarted;
  69. OnTitleChanged = InArgs._OnTitleChanged;
  70. OnUrlChanged = InArgs._OnUrlChanged;
  71. OnBeforeNavigation = InArgs._OnBeforeNavigation;
  72. OnLoadUrl = InArgs._OnLoadUrl;
  73. OnShowDialog = InArgs._OnShowDialog;
  74. OnDismissAllDialogs = InArgs._OnDismissAllDialogs;
  75. OnBeforePopup = InArgs._OnBeforePopup;
  76. OnCreateWindow = InArgs._OnCreateWindow;
  77. OnCloseWindow = InArgs._OnCloseWindow;
  78. AddressBarUrl = FText::FromString(InArgs._InitialURL);
  79. PopupMenuMethod = InArgs._PopupMenuMethod;
  80. OnUnhandledKeyDown = InArgs._OnUnhandledKeyDown;
  81. OnUnhandledKeyUp = InArgs._OnUnhandledKeyUp;
  82. OnUnhandledKeyChar = InArgs._OnUnhandledKeyChar;
  83. OnConsoleMessage = InArgs._OnConsoleMessage;
  84. BrowserWindow = InWebBrowserWindow;
  85. if(!BrowserWindow.IsValid())
  86. {
  87. static bool AllowCEF = !FParse::Param(FCommandLine::Get(), TEXT("nocef"));
  88. bool bBrowserEnabled = true;
  89. GConfig->GetBool(TEXT("Browser"), TEXT("bEnabled"), bBrowserEnabled, GEngineIni);
  90. if (AllowCEF && bBrowserEnabled)
  91. {
  92. FCreateInterfaceBrowserWindowSettings Settings;
  93. Settings.InitialURL = InArgs._InitialURL;
  94. Settings.bUseTransparency = InArgs._SupportsTransparency;
  95. Settings.bInterceptLoadRequests = InArgs._InterceptLoadRequests;
  96. Settings.bThumbMouseButtonNavigation = InArgs._SupportsThumbMouseButtonNavigation;
  97. Settings.ContentsToLoad = InArgs._ContentsToLoad;
  98. Settings.bShowErrorMessage = InArgs._ShowErrorMessage;
  99. Settings.BackgroundColor = InArgs._BackgroundColor;
  100. Settings.BrowserFrameRate = InArgs._BrowserFrameRate;
  101. Settings.Context = InArgs._ContextSettings;
  102. Settings.AltRetryDomains = InArgs._AltRetryDomains;
  103. // IWebBrowserModule::Get() was already callled in WebBrowserWidgetModule.cpp so we don't need to force the load again here
  104. if (IWebInterfaceBrowserModule::IsAvailable() && IWebInterfaceBrowserModule::Get().IsWebModuleAvailable())
  105. {
  106. BrowserWindow = IWebInterfaceBrowserModule::Get().GetSingleton()->CreateBrowserWindow(Settings);
  107. }
  108. }
  109. }
  110. SlateParentWindowPtr = InArgs._ParentWindow;
  111. if (BrowserWindow.IsValid())
  112. {
  113. #ifndef DUMMY_WEB_BROWSER
  114. // The inner widget creation is handled by the WebBrowserWindow implementation.
  115. const auto& BrowserWidgetRef = static_cast<FWebInterfaceBrowserWindow*>(BrowserWindow.Get())->CreateWidget();
  116. ChildSlot
  117. [
  118. BrowserWidgetRef
  119. ];
  120. BrowserWidget = BrowserWidgetRef;
  121. #endif
  122. if(OnCreateWindow.IsBound())
  123. {
  124. BrowserWindow->OnCreateWindow().BindSP(this, &SWebInterfaceBrowserView::HandleCreateWindow);
  125. }
  126. if(OnCloseWindow.IsBound())
  127. {
  128. BrowserWindow->OnCloseWindow().BindSP(this, &SWebInterfaceBrowserView::HandleCloseWindow);
  129. }
  130. BrowserWindow->OnDocumentStateChanged().AddSP(this, &SWebInterfaceBrowserView::HandleBrowserWindowDocumentStateChanged);
  131. BrowserWindow->OnNeedsRedraw().AddSP(this, &SWebInterfaceBrowserView::HandleBrowserWindowNeedsRedraw);
  132. BrowserWindow->OnTitleChanged().AddSP(this, &SWebInterfaceBrowserView::HandleTitleChanged);
  133. BrowserWindow->OnUrlChanged().AddSP(this, &SWebInterfaceBrowserView::HandleUrlChanged);
  134. BrowserWindow->OnToolTip().AddSP(this, &SWebInterfaceBrowserView::HandleToolTip);
  135. OnCreateToolTip = InArgs._OnCreateToolTip;
  136. if (!BrowserWindow->OnBeforeBrowse().IsBound())
  137. {
  138. BrowserWindow->OnBeforeBrowse().BindSP(this, &SWebInterfaceBrowserView::HandleBeforeNavigation);
  139. }
  140. else
  141. {
  142. check(!OnBeforeNavigation.IsBound());
  143. }
  144. if (!BrowserWindow->OnLoadUrl().IsBound())
  145. {
  146. BrowserWindow->OnLoadUrl().BindSP(this, &SWebInterfaceBrowserView::HandleLoadUrl);
  147. }
  148. else
  149. {
  150. check(!OnLoadUrl.IsBound());
  151. }
  152. if (!BrowserWindow->OnBeforePopup().IsBound())
  153. {
  154. BrowserWindow->OnBeforePopup().BindSP(this, &SWebInterfaceBrowserView::HandleBeforePopup);
  155. }
  156. else
  157. {
  158. check(!OnBeforePopup.IsBound());
  159. }
  160. if (!BrowserWindow->OnUnhandledKeyDown().IsBound())
  161. {
  162. BrowserWindow->OnUnhandledKeyDown().BindSP(this, &SWebInterfaceBrowserView::UnhandledKeyDown);
  163. }
  164. if (!BrowserWindow->OnUnhandledKeyUp().IsBound())
  165. {
  166. BrowserWindow->OnUnhandledKeyUp().BindSP(this, &SWebInterfaceBrowserView::UnhandledKeyUp);
  167. }
  168. if (!BrowserWindow->OnUnhandledKeyChar().IsBound())
  169. {
  170. BrowserWindow->OnUnhandledKeyChar().BindSP(this, &SWebInterfaceBrowserView::UnhandledKeyChar);
  171. }
  172. BrowserWindow->OnShowDialog().BindSP(this, &SWebInterfaceBrowserView::HandleShowDialog);
  173. BrowserWindow->OnDismissAllDialogs().BindSP(this, &SWebInterfaceBrowserView::HandleDismissAllDialogs);
  174. BrowserWindow->OnShowPopup().AddSP(this, &SWebInterfaceBrowserView::HandleShowPopup);
  175. BrowserWindow->OnDismissPopup().AddSP(this, &SWebInterfaceBrowserView::HandleDismissPopup);
  176. BrowserWindow->OnSuppressContextMenu().BindSP(this, &SWebInterfaceBrowserView::HandleSuppressContextMenu);
  177. OnSuppressContextMenu = InArgs._OnSuppressContextMenu;
  178. BrowserWindow->OnDragWindow().BindSP(this, &SWebInterfaceBrowserView::HandleDrag);
  179. OnDragWindow = InArgs._OnDragWindow;
  180. if (!BrowserWindow->OnConsoleMessage().IsBound())
  181. {
  182. BrowserWindow->OnConsoleMessage().BindSP(this, &SWebInterfaceBrowserView::HandleConsoleMessage);
  183. }
  184. BrowserViewport = MakeShareable(new FWebInterfaceBrowserViewport(BrowserWindow));
  185. #if WITH_CEF3
  186. BrowserWidget->SetViewportInterface(BrowserViewport.ToSharedRef());
  187. #endif
  188. // If we could not obtain the parent window during widget construction, we'll defer and keep trying.
  189. SetupParentWindowHandlers();
  190. }
  191. else
  192. {
  193. OnLoadError.ExecuteIfBound();
  194. }
  195. }
  196. int32 SWebInterfaceBrowserView::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
  197. {
  198. if (!SlateParentWindowPtr.IsValid())
  199. {
  200. SWebInterfaceBrowserView* MutableThis = const_cast<SWebInterfaceBrowserView*>(this);
  201. MutableThis->SetupParentWindowHandlers();
  202. }
  203. int32 Layer = SCompoundWidget::OnPaint(Args, AllottedGeometry, MyCullingRect, OutDrawElements, LayerId, InWidgetStyle, bParentEnabled);
  204. // Cache a reference to our parent window, if we didn't already reference it.
  205. if (!SlateParentWindowPtr.IsValid())
  206. {
  207. SWindow* ParentWindow = OutDrawElements.GetPaintWindow();
  208. TSharedRef<SWindow> SlateParentWindowRef = StaticCastSharedRef<SWindow>(ParentWindow->AsShared());
  209. SlateParentWindowPtr = SlateParentWindowRef;
  210. if (BrowserWindow.IsValid())
  211. {
  212. BrowserWindow->SetParentWindow(SlateParentWindowRef);
  213. }
  214. }
  215. return Layer;
  216. }
  217. void SWebInterfaceBrowserView::HandleWindowDeactivated()
  218. {
  219. if (BrowserViewport.IsValid())
  220. {
  221. BrowserViewport->OnFocusLost(FFocusEvent());
  222. }
  223. }
  224. void SWebInterfaceBrowserView::HandleWindowActivated()
  225. {
  226. if (BrowserViewport.IsValid())
  227. {
  228. if (HasAnyUserFocusOrFocusedDescendants())
  229. {
  230. BrowserViewport->OnFocusReceived(FFocusEvent());
  231. }
  232. }
  233. }
  234. void SWebInterfaceBrowserView::LoadURL(FString NewURL)
  235. {
  236. AddressBarUrl = FText::FromString(NewURL);
  237. if (BrowserWindow.IsValid())
  238. {
  239. BrowserWindow->LoadURL(NewURL);
  240. }
  241. }
  242. void SWebInterfaceBrowserView::LoadString(FString Contents, FString DummyURL)
  243. {
  244. if (BrowserWindow.IsValid())
  245. {
  246. BrowserWindow->LoadString(Contents, DummyURL);
  247. }
  248. }
  249. void SWebInterfaceBrowserView::Reload()
  250. {
  251. if (BrowserWindow.IsValid())
  252. {
  253. BrowserWindow->Reload();
  254. }
  255. }
  256. void SWebInterfaceBrowserView::StopLoad()
  257. {
  258. if (BrowserWindow.IsValid())
  259. {
  260. BrowserWindow->StopLoad();
  261. }
  262. }
  263. FText SWebInterfaceBrowserView::GetTitleText() const
  264. {
  265. if (BrowserWindow.IsValid())
  266. {
  267. return FText::FromString(BrowserWindow->GetTitle());
  268. }
  269. return LOCTEXT("InvalidWindow", "Browser Window is not valid/supported");
  270. }
  271. FString SWebInterfaceBrowserView::GetUrl() const
  272. {
  273. if (BrowserWindow.IsValid())
  274. {
  275. return BrowserWindow->GetUrl();
  276. }
  277. return FString();
  278. }
  279. FText SWebInterfaceBrowserView::GetAddressBarUrlText() const
  280. {
  281. if(BrowserWindow.IsValid())
  282. {
  283. return AddressBarUrl;
  284. }
  285. return FText::GetEmpty();
  286. }
  287. bool SWebInterfaceBrowserView::IsLoaded() const
  288. {
  289. if (BrowserWindow.IsValid())
  290. {
  291. return (BrowserWindow->GetDocumentLoadingState() == EWebInterfaceBrowserDocumentState::Completed);
  292. }
  293. return false;
  294. }
  295. bool SWebInterfaceBrowserView::IsLoading() const
  296. {
  297. if (BrowserWindow.IsValid())
  298. {
  299. return (BrowserWindow->GetDocumentLoadingState() == EWebInterfaceBrowserDocumentState::Loading);
  300. }
  301. return false;
  302. }
  303. bool SWebInterfaceBrowserView::CanGoBack() const
  304. {
  305. if (BrowserWindow.IsValid())
  306. {
  307. return BrowserWindow->CanGoBack();
  308. }
  309. return false;
  310. }
  311. void SWebInterfaceBrowserView::GoBack()
  312. {
  313. if (BrowserWindow.IsValid())
  314. {
  315. BrowserWindow->GoBack();
  316. }
  317. }
  318. bool SWebInterfaceBrowserView::CanGoForward() const
  319. {
  320. if (BrowserWindow.IsValid())
  321. {
  322. return BrowserWindow->CanGoForward();
  323. }
  324. return false;
  325. }
  326. void SWebInterfaceBrowserView::GoForward()
  327. {
  328. if (BrowserWindow.IsValid())
  329. {
  330. BrowserWindow->GoForward();
  331. }
  332. }
  333. bool SWebInterfaceBrowserView::IsInitialized() const
  334. {
  335. return BrowserWindow.IsValid() && BrowserWindow->IsInitialized();
  336. }
  337. void SWebInterfaceBrowserView::SetupParentWindowHandlers()
  338. {
  339. if (!SlateParentWindowPtr.IsValid())
  340. {
  341. SlateParentWindowPtr = FSlateApplication::Get().FindWidgetWindow(SharedThis(this));
  342. TSharedPtr<SWindow> SlateParentWindow = SlateParentWindowPtr.Pin();
  343. if (SlateParentWindow.IsValid() && BrowserWindow.IsValid())
  344. {
  345. if (!SlateParentWindow->GetOnWindowDeactivatedEvent().IsBoundToObject(this))
  346. {
  347. SlateParentWindow->GetOnWindowDeactivatedEvent().AddSP(this, &SWebInterfaceBrowserView::HandleWindowDeactivated);
  348. }
  349. if (!SlateParentWindow->GetOnWindowActivatedEvent().IsBoundToObject(this))
  350. {
  351. SlateParentWindow->GetOnWindowActivatedEvent().AddSP(this, &SWebInterfaceBrowserView::HandleWindowActivated);
  352. }
  353. BrowserWindow->SetParentWindow(SlateParentWindow);
  354. }
  355. }
  356. }
  357. void SWebInterfaceBrowserView::HandleBrowserWindowDocumentStateChanged(EWebInterfaceBrowserDocumentState NewState)
  358. {
  359. switch (NewState)
  360. {
  361. case EWebInterfaceBrowserDocumentState::Completed:
  362. {
  363. if (BrowserWindow.IsValid())
  364. {
  365. for (auto Adapter : Adapters)
  366. {
  367. Adapter->ConnectTo(BrowserWindow.ToSharedRef());
  368. }
  369. }
  370. OnLoadCompleted.ExecuteIfBound();
  371. }
  372. break;
  373. case EWebInterfaceBrowserDocumentState::Error:
  374. OnLoadError.ExecuteIfBound();
  375. break;
  376. case EWebInterfaceBrowserDocumentState::Loading:
  377. OnLoadStarted.ExecuteIfBound();
  378. break;
  379. }
  380. }
  381. void SWebInterfaceBrowserView::HandleBrowserWindowNeedsRedraw()
  382. {
  383. if (FSlateApplication::Get().IsSlateAsleep())
  384. {
  385. // Tell slate that the widget needs to wake up for one frame to get redrawn
  386. RegisterActiveTimer(0.f, FWidgetActiveTimerDelegate::CreateLambda([this](double InCurrentTime, float InDeltaTime) { return EActiveTimerReturnType::Stop; }));
  387. }
  388. }
  389. void SWebInterfaceBrowserView::HandleTitleChanged( FString NewTitle )
  390. {
  391. const FText NewTitleText = FText::FromString(NewTitle);
  392. OnTitleChanged.ExecuteIfBound(NewTitleText);
  393. }
  394. void SWebInterfaceBrowserView::HandleUrlChanged( FString NewUrl )
  395. {
  396. AddressBarUrl = FText::FromString(NewUrl);
  397. OnUrlChanged.ExecuteIfBound(AddressBarUrl);
  398. }
  399. void SWebInterfaceBrowserView::CloseBrowser()
  400. {
  401. BrowserWindow->CloseBrowser(true /*force*/, true /*block until closed*/);
  402. }
  403. void SWebInterfaceBrowserView::HandleToolTip(FString ToolTipText)
  404. {
  405. if(ToolTipText.IsEmpty())
  406. {
  407. FSlateApplication::Get().CloseToolTip();
  408. SetToolTip(nullptr);
  409. }
  410. else if (OnCreateToolTip.IsBound())
  411. {
  412. SetToolTip(OnCreateToolTip.Execute(FText::FromString(ToolTipText)));
  413. FSlateApplication::Get().UpdateToolTip(true);
  414. }
  415. else
  416. {
  417. SetToolTipText(FText::FromString(ToolTipText));
  418. FSlateApplication::Get().UpdateToolTip(true);
  419. }
  420. }
  421. bool SWebInterfaceBrowserView::HandleBeforeNavigation(const FString& Url, const FWebNavigationRequest& Request)
  422. {
  423. if(OnBeforeNavigation.IsBound())
  424. {
  425. return OnBeforeNavigation.Execute(Url, Request);
  426. }
  427. return false;
  428. }
  429. bool SWebInterfaceBrowserView::HandleLoadUrl(const FString& Method, const FString& Url, FString& OutResponse)
  430. {
  431. if(OnLoadUrl.IsBound())
  432. {
  433. return OnLoadUrl.Execute(Method, Url, OutResponse);
  434. }
  435. return false;
  436. }
  437. EWebInterfaceBrowserDialogEventResponse SWebInterfaceBrowserView::HandleShowDialog(const TWeakPtr<IWebInterfaceBrowserDialog>& DialogParams)
  438. {
  439. if(OnShowDialog.IsBound())
  440. {
  441. return OnShowDialog.Execute(DialogParams);
  442. }
  443. return EWebInterfaceBrowserDialogEventResponse::Unhandled;
  444. }
  445. void SWebInterfaceBrowserView::HandleDismissAllDialogs()
  446. {
  447. OnDismissAllDialogs.ExecuteIfBound();
  448. }
  449. bool SWebInterfaceBrowserView::HandleBeforePopup(FString URL, FString Target)
  450. {
  451. if (OnBeforePopup.IsBound())
  452. {
  453. return OnBeforePopup.Execute(URL, Target);
  454. }
  455. return false;
  456. }
  457. void SWebInterfaceBrowserView::ExecuteJavascript(const FString& ScriptText)
  458. {
  459. if (BrowserWindow.IsValid())
  460. {
  461. BrowserWindow->ExecuteJavascript(ScriptText);
  462. }
  463. }
  464. void SWebInterfaceBrowserView::GetSource(TFunction<void (const FString&)> Callback) const
  465. {
  466. if (BrowserWindow.IsValid())
  467. {
  468. BrowserWindow->GetSource(Callback);
  469. }
  470. }
  471. bool SWebInterfaceBrowserView::HandleCreateWindow(const TWeakPtr<IWebInterfaceBrowserWindow>& NewBrowserWindow, const TWeakPtr<IWebInterfaceBrowserPopupFeatures>& PopupFeatures)
  472. {
  473. if(OnCreateWindow.IsBound())
  474. {
  475. return OnCreateWindow.Execute(NewBrowserWindow, PopupFeatures);
  476. }
  477. return false;
  478. }
  479. bool SWebInterfaceBrowserView::HandleCloseWindow(const TWeakPtr<IWebInterfaceBrowserWindow>& NewBrowserWindow)
  480. {
  481. if(OnCloseWindow.IsBound())
  482. {
  483. return OnCloseWindow.Execute(NewBrowserWindow);
  484. }
  485. return false;
  486. }
  487. void SWebInterfaceBrowserView::BindUObject(const FString& Name, UObject* Object, bool bIsPermanent)
  488. {
  489. if (BrowserWindow.IsValid())
  490. {
  491. BrowserWindow->BindUObject(Name, Object, bIsPermanent);
  492. }
  493. }
  494. void SWebInterfaceBrowserView::UnbindUObject(const FString& Name, UObject* Object, bool bIsPermanent)
  495. {
  496. if (BrowserWindow.IsValid())
  497. {
  498. BrowserWindow->UnbindUObject(Name, Object, bIsPermanent);
  499. }
  500. }
  501. void SWebInterfaceBrowserView::BindAdapter(const TSharedRef<IWebInterfaceBrowserAdapter>& Adapter)
  502. {
  503. Adapters.Add(Adapter);
  504. if (BrowserWindow.IsValid())
  505. {
  506. Adapter->ConnectTo(BrowserWindow.ToSharedRef());
  507. }
  508. }
  509. void SWebInterfaceBrowserView::UnbindAdapter(const TSharedRef<IWebInterfaceBrowserAdapter>& Adapter)
  510. {
  511. Adapters.Remove(Adapter);
  512. if (BrowserWindow.IsValid())
  513. {
  514. Adapter->DisconnectFrom(BrowserWindow.ToSharedRef());
  515. }
  516. }
  517. void SWebInterfaceBrowserView::BindInputMethodSystem(ITextInputMethodSystem* TextInputMethodSystem)
  518. {
  519. if (BrowserWindow.IsValid())
  520. {
  521. BrowserWindow->BindInputMethodSystem(TextInputMethodSystem);
  522. }
  523. }
  524. void SWebInterfaceBrowserView::UnbindInputMethodSystem()
  525. {
  526. if (BrowserWindow.IsValid())
  527. {
  528. BrowserWindow->UnbindInputMethodSystem();
  529. }
  530. }
  531. void SWebInterfaceBrowserView::HandleShowPopup(const FIntRect& PopupSize)
  532. {
  533. check(!PopupMenuPtr.IsValid())
  534. TSharedPtr<SViewport> MenuContent;
  535. SAssignNew(MenuContent, SViewport)
  536. .ViewportSize(PopupSize.Size())
  537. .EnableGammaCorrection(false)
  538. .EnableBlending(false)
  539. .IgnoreTextureAlpha(true)
  540. #if WITH_CEF3
  541. .RenderTransform(this, &SWebInterfaceBrowserView::GetPopupRenderTransform)
  542. #endif
  543. .Visibility(EVisibility::Visible);
  544. MenuViewport = MakeShareable(new FWebInterfaceBrowserViewport(BrowserWindow, true));
  545. MenuContent->SetViewportInterface(MenuViewport.ToSharedRef());
  546. FWidgetPath WidgetPath;
  547. FSlateApplication::Get().GeneratePathToWidgetUnchecked(SharedThis(this), WidgetPath);
  548. if (WidgetPath.IsValid())
  549. {
  550. TSharedRef< SWidget > MenuContentRef = MenuContent.ToSharedRef();
  551. const FGeometry& BrowserGeometry = WidgetPath.Widgets.Last().Geometry;
  552. const FVector2D NewPosition = BrowserGeometry.LocalToAbsolute(PopupSize.Min);
  553. // Open the pop-up. The popup method will be queried from the widget path passed in.
  554. TSharedPtr<IMenu> NewMenu = FSlateApplication::Get().PushMenu(SharedThis(this), WidgetPath, MenuContentRef, NewPosition, FPopupTransitionEffect( FPopupTransitionEffect::ComboButton ), false);
  555. NewMenu->GetOnMenuDismissed().AddSP(this, &SWebInterfaceBrowserView::HandleMenuDismissed);
  556. PopupMenuPtr = NewMenu;
  557. }
  558. }
  559. TOptional <FSlateRenderTransform> SWebInterfaceBrowserView::GetPopupRenderTransform() const
  560. {
  561. if (BrowserWindow.IsValid())
  562. {
  563. #if !defined(DUMMY_WEB_BROWSER) && WITH_CEF3
  564. TOptional<FSlateRenderTransform> LocalRenderTransform = FSlateRenderTransform();
  565. if (static_cast<FWebInterfaceBrowserWindow*>(BrowserWindow.Get())->UsingAcceleratedPaint())
  566. {
  567. // the accelerated renderer for CEF generates inverted textures (compared to the slate co-ord system), so flip it here
  568. LocalRenderTransform = FSlateRenderTransform(Concatenate(FScale2D(1, -1), FVector2D(0, PopupMenuPtr.Pin()->GetContent()->GetDesiredSize().Y)));
  569. }
  570. return LocalRenderTransform;
  571. #else
  572. return FSlateRenderTransform();
  573. #endif
  574. }
  575. else
  576. {
  577. return FSlateRenderTransform();
  578. }
  579. }
  580. void SWebInterfaceBrowserView::HandleMenuDismissed(TSharedRef<IMenu>)
  581. {
  582. PopupMenuPtr.Reset();
  583. }
  584. void SWebInterfaceBrowserView::HandleDismissPopup()
  585. {
  586. if (PopupMenuPtr.IsValid())
  587. {
  588. PopupMenuPtr.Pin()->Dismiss();
  589. FSlateApplication::Get().SetKeyboardFocus(SharedThis(this), EFocusCause::SetDirectly);
  590. }
  591. }
  592. bool SWebInterfaceBrowserView::HandleSuppressContextMenu()
  593. {
  594. if (OnSuppressContextMenu.IsBound())
  595. {
  596. return OnSuppressContextMenu.Execute();
  597. }
  598. return false;
  599. }
  600. bool SWebInterfaceBrowserView::HandleDrag(const FPointerEvent& MouseEvent)
  601. {
  602. if (OnDragWindow.IsBound())
  603. {
  604. return OnDragWindow.Execute(MouseEvent);
  605. }
  606. return false;
  607. }
  608. bool SWebInterfaceBrowserView::UnhandledKeyDown(const FKeyEvent& KeyEvent)
  609. {
  610. if (OnUnhandledKeyDown.IsBound())
  611. {
  612. return OnUnhandledKeyDown.Execute(KeyEvent);
  613. }
  614. return false;
  615. }
  616. bool SWebInterfaceBrowserView::UnhandledKeyUp(const FKeyEvent& KeyEvent)
  617. {
  618. if (OnUnhandledKeyUp.IsBound())
  619. {
  620. return OnUnhandledKeyUp.Execute(KeyEvent);
  621. }
  622. return false;
  623. }
  624. bool SWebInterfaceBrowserView::UnhandledKeyChar(const FCharacterEvent& CharacterEvent)
  625. {
  626. if (OnUnhandledKeyChar.IsBound())
  627. {
  628. return OnUnhandledKeyChar.Execute(CharacterEvent);
  629. }
  630. return false;
  631. }
  632. void SWebInterfaceBrowserView::SetParentWindow(TSharedPtr<SWindow> Window)
  633. {
  634. SetupParentWindowHandlers();
  635. if (BrowserWindow.IsValid())
  636. {
  637. BrowserWindow->SetParentWindow(Window);
  638. }
  639. }
  640. void SWebInterfaceBrowserView::SetBrowserKeyboardFocus()
  641. {
  642. BrowserWindow->OnFocus(HasAnyUserFocusOrFocusedDescendants(), false);
  643. }
  644. void SWebInterfaceBrowserView::HandleConsoleMessage(const FString& Message, const FString& Source, int32 Line, EWebInterfaceBrowserConsoleLogSeverity Serverity)
  645. {
  646. OnConsoleMessage.ExecuteIfBound(Message, Source, Line, Serverity);
  647. }
  648. #undef LOCTEXT_NAMESPACE