SWebInterfaceBrowser.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // Engine/Source/Runtime/WebBrowser/Private/SWebBrowser.cpp
  2. #include "SWebInterfaceBrowser.h"
  3. #include "Widgets/Text/STextBlock.h"
  4. #include "Widgets/Input/SEditableTextBox.h"
  5. #include "Widgets/Input/SButton.h"
  6. #include "Widgets/Images/SThrobber.h"
  7. #define LOCTEXT_NAMESPACE "WebInterfaceBrowser"
  8. SWebInterfaceBrowser::SWebInterfaceBrowser()
  9. {
  10. }
  11. SWebInterfaceBrowser::~SWebInterfaceBrowser()
  12. {
  13. }
  14. void SWebInterfaceBrowser::Construct(const FArguments& InArgs, const TSharedPtr<IWebInterfaceBrowserWindow>& BrowserWindow)
  15. {
  16. OnLoadCompleted = InArgs._OnLoadCompleted;
  17. OnLoadError = InArgs._OnLoadError;
  18. OnLoadStarted = InArgs._OnLoadStarted;
  19. OnTitleChanged = InArgs._OnTitleChanged;
  20. OnUrlChanged = InArgs._OnUrlChanged;
  21. OnBeforeNavigation = InArgs._OnBeforeNavigation;
  22. OnLoadUrl = InArgs._OnLoadUrl;
  23. OnShowDialog = InArgs._OnShowDialog;
  24. OnDismissAllDialogs = InArgs._OnDismissAllDialogs;
  25. OnBeforePopup = InArgs._OnBeforePopup;
  26. OnConsoleMessage = InArgs._OnConsoleMessage;
  27. OnCreateWindow = InArgs._OnCreateWindow;
  28. OnCloseWindow = InArgs._OnCloseWindow;
  29. bShowInitialThrobber = InArgs._ShowInitialThrobber;
  30. ChildSlot
  31. [
  32. SNew(SVerticalBox)
  33. +SVerticalBox::Slot()
  34. .AutoHeight()
  35. [
  36. SNew(SHorizontalBox)
  37. .Visibility((InArgs._ShowControls || InArgs._ShowAddressBar) ? EVisibility::Visible : EVisibility::Collapsed)
  38. + SHorizontalBox::Slot()
  39. .Padding(0, 5)
  40. .AutoWidth()
  41. [
  42. SNew(SHorizontalBox)
  43. .Visibility(InArgs._ShowControls ? EVisibility::Visible : EVisibility::Collapsed)
  44. +SHorizontalBox::Slot()
  45. .AutoWidth()
  46. [
  47. SNew(SButton)
  48. .Text(LOCTEXT("Back","Back"))
  49. .IsEnabled(this, &SWebInterfaceBrowser::CanGoBack)
  50. .OnClicked(this, &SWebInterfaceBrowser::OnBackClicked)
  51. ]
  52. +SHorizontalBox::Slot()
  53. .AutoWidth()
  54. [
  55. SNew(SButton)
  56. .Text(LOCTEXT("Forward", "Forward"))
  57. .IsEnabled(this, &SWebInterfaceBrowser::CanGoForward)
  58. .OnClicked(this, &SWebInterfaceBrowser::OnForwardClicked)
  59. ]
  60. +SHorizontalBox::Slot()
  61. .AutoWidth()
  62. [
  63. SNew(SButton)
  64. .Text(this, &SWebInterfaceBrowser::GetReloadButtonText)
  65. .OnClicked(this, &SWebInterfaceBrowser::OnReloadClicked)
  66. ]
  67. +SHorizontalBox::Slot()
  68. .FillWidth(1.0f)
  69. .VAlign(VAlign_Center)
  70. .HAlign(HAlign_Right)
  71. .Padding(5)
  72. [
  73. SNew(STextBlock)
  74. .Visibility(InArgs._ShowAddressBar ? EVisibility::Collapsed : EVisibility::Visible )
  75. .Text(this, &SWebInterfaceBrowser::GetTitleText)
  76. .Justification(ETextJustify::Right)
  77. ]
  78. ]
  79. +SHorizontalBox::Slot()
  80. .VAlign(VAlign_Center)
  81. .HAlign(HAlign_Fill)
  82. .Padding(5.f, 5.f)
  83. [
  84. // @todo: A proper addressbar widget should go here, for now we use a simple textbox.
  85. SAssignNew(InputText, SEditableTextBox)
  86. .Visibility(InArgs._ShowAddressBar ? EVisibility::Visible : EVisibility::Collapsed)
  87. .OnTextCommitted(this, &SWebInterfaceBrowser::OnUrlTextCommitted)
  88. .Text(this, &SWebInterfaceBrowser::GetAddressBarUrlText)
  89. .SelectAllTextWhenFocused(true)
  90. .ClearKeyboardFocusOnCommit(true)
  91. .RevertTextOnEscape(true)
  92. ]
  93. ]
  94. +SVerticalBox::Slot()
  95. [
  96. SNew(SOverlay)
  97. + SOverlay::Slot()
  98. [
  99. SAssignNew(BrowserView, SWebInterfaceBrowserView, BrowserWindow)
  100. .ParentWindow(InArgs._ParentWindow)
  101. .InitialURL(InArgs._InitialURL)
  102. .ContentsToLoad(InArgs._ContentsToLoad)
  103. .ShowErrorMessage(InArgs._ShowErrorMessage)
  104. .SupportsTransparency(InArgs._SupportsTransparency)
  105. .SupportsThumbMouseButtonNavigation(InArgs._SupportsThumbMouseButtonNavigation)
  106. .BackgroundColor(InArgs._BackgroundColor)
  107. .PopupMenuMethod(InArgs._PopupMenuMethod)
  108. .ViewportSize(InArgs._ViewportSize)
  109. .OnLoadCompleted(OnLoadCompleted)
  110. .OnLoadError(OnLoadError)
  111. .OnLoadStarted(OnLoadStarted)
  112. .OnTitleChanged(OnTitleChanged)
  113. .OnUrlChanged(OnUrlChanged)
  114. .OnBeforePopup(OnBeforePopup)
  115. .OnCreateWindow(OnCreateWindow)
  116. .OnCloseWindow(OnCloseWindow)
  117. .OnBeforeNavigation(OnBeforeNavigation)
  118. .OnLoadUrl(OnLoadUrl)
  119. .OnShowDialog(OnShowDialog)
  120. .OnDismissAllDialogs(OnDismissAllDialogs)
  121. .Visibility(this, &SWebInterfaceBrowser::GetViewportVisibility)
  122. .OnSuppressContextMenu(InArgs._OnSuppressContextMenu)
  123. .OnDragWindow(InArgs._OnDragWindow)
  124. .OnConsoleMessage(OnConsoleMessage)
  125. .BrowserFrameRate(InArgs._BrowserFrameRate)
  126. ]
  127. + SOverlay::Slot()
  128. .HAlign(HAlign_Center)
  129. .VAlign(VAlign_Center)
  130. [
  131. SNew(SCircularThrobber)
  132. .Radius(10.0f)
  133. .ToolTipText(LOCTEXT("LoadingThrobberToolTip", "Loading page..."))
  134. .Visibility(this, &SWebInterfaceBrowser::GetLoadingThrobberVisibility)
  135. ]
  136. ]
  137. ];
  138. }
  139. void SWebInterfaceBrowser::LoadURL(FString NewURL)
  140. {
  141. if (BrowserView.IsValid())
  142. {
  143. BrowserView->LoadURL(NewURL);
  144. }
  145. }
  146. void SWebInterfaceBrowser::LoadString(FString Contents, FString DummyURL)
  147. {
  148. if (BrowserView.IsValid())
  149. {
  150. BrowserView->LoadString(Contents, DummyURL);
  151. }
  152. }
  153. void SWebInterfaceBrowser::Reload()
  154. {
  155. if (BrowserView.IsValid())
  156. {
  157. BrowserView->Reload();
  158. }
  159. }
  160. void SWebInterfaceBrowser::StopLoad()
  161. {
  162. if (BrowserView.IsValid())
  163. {
  164. BrowserView->StopLoad();
  165. }
  166. }
  167. FText SWebInterfaceBrowser::GetTitleText() const
  168. {
  169. if (BrowserView.IsValid())
  170. {
  171. return BrowserView->GetTitleText();
  172. }
  173. return LOCTEXT("InvalidWindow", "Browser Window is not valid/supported");
  174. }
  175. FString SWebInterfaceBrowser::GetUrl() const
  176. {
  177. if (BrowserView.IsValid())
  178. {
  179. return BrowserView->GetUrl();
  180. }
  181. return FString();
  182. }
  183. FText SWebInterfaceBrowser::GetAddressBarUrlText() const
  184. {
  185. if(BrowserView.IsValid())
  186. {
  187. return BrowserView->GetAddressBarUrlText();
  188. }
  189. return FText::GetEmpty();
  190. }
  191. bool SWebInterfaceBrowser::IsLoaded() const
  192. {
  193. if (BrowserView.IsValid())
  194. {
  195. return BrowserView->IsLoaded();
  196. }
  197. return false;
  198. }
  199. bool SWebInterfaceBrowser::IsLoading() const
  200. {
  201. if (BrowserView.IsValid())
  202. {
  203. return BrowserView->IsLoading();
  204. }
  205. return false;
  206. }
  207. bool SWebInterfaceBrowser::CanGoBack() const
  208. {
  209. if (BrowserView.IsValid())
  210. {
  211. return BrowserView->CanGoBack();
  212. }
  213. return false;
  214. }
  215. void SWebInterfaceBrowser::GoBack()
  216. {
  217. if (BrowserView.IsValid())
  218. {
  219. BrowserView->GoBack();
  220. }
  221. }
  222. FReply SWebInterfaceBrowser::OnBackClicked()
  223. {
  224. GoBack();
  225. return FReply::Handled();
  226. }
  227. bool SWebInterfaceBrowser::CanGoForward() const
  228. {
  229. if (BrowserView.IsValid())
  230. {
  231. return BrowserView->CanGoForward();
  232. }
  233. return false;
  234. }
  235. void SWebInterfaceBrowser::GoForward()
  236. {
  237. if (BrowserView.IsValid())
  238. {
  239. BrowserView->GoForward();
  240. }
  241. }
  242. FReply SWebInterfaceBrowser::OnForwardClicked()
  243. {
  244. GoForward();
  245. return FReply::Handled();
  246. }
  247. FText SWebInterfaceBrowser::GetReloadButtonText() const
  248. {
  249. static FText ReloadText = LOCTEXT("Reload", "Reload");
  250. static FText StopText = LOCTEXT("StopText", "Stop");
  251. if (BrowserView.IsValid())
  252. {
  253. if (BrowserView->IsLoading())
  254. {
  255. return StopText;
  256. }
  257. }
  258. return ReloadText;
  259. }
  260. FReply SWebInterfaceBrowser::OnReloadClicked()
  261. {
  262. if (IsLoading())
  263. {
  264. StopLoad();
  265. }
  266. else
  267. {
  268. Reload();
  269. }
  270. return FReply::Handled();
  271. }
  272. void SWebInterfaceBrowser::OnUrlTextCommitted( const FText& NewText, ETextCommit::Type CommitType )
  273. {
  274. if(CommitType == ETextCommit::OnEnter)
  275. {
  276. LoadURL(NewText.ToString());
  277. }
  278. }
  279. EVisibility SWebInterfaceBrowser::GetViewportVisibility() const
  280. {
  281. if (!bShowInitialThrobber || BrowserView->IsInitialized())
  282. {
  283. return EVisibility::Visible;
  284. }
  285. return EVisibility::Hidden;
  286. }
  287. EVisibility SWebInterfaceBrowser::GetLoadingThrobberVisibility() const
  288. {
  289. if (bShowInitialThrobber && !BrowserView->IsInitialized())
  290. {
  291. return EVisibility::Visible;
  292. }
  293. return EVisibility::Hidden;
  294. }
  295. void SWebInterfaceBrowser::ExecuteJavascript(const FString& ScriptText)
  296. {
  297. if (BrowserView.IsValid())
  298. {
  299. BrowserView->ExecuteJavascript(ScriptText);
  300. }
  301. }
  302. void SWebInterfaceBrowser::GetSource(TFunction<void (const FString&)> Callback) const
  303. {
  304. if (BrowserView.IsValid())
  305. {
  306. BrowserView->GetSource(Callback);
  307. }
  308. }
  309. void SWebInterfaceBrowser::BindUObject(const FString& Name, UObject* Object, bool bIsPermanent)
  310. {
  311. if (BrowserView.IsValid())
  312. {
  313. BrowserView->BindUObject(Name, Object, bIsPermanent);
  314. }
  315. }
  316. void SWebInterfaceBrowser::UnbindUObject(const FString& Name, UObject* Object, bool bIsPermanent)
  317. {
  318. if (BrowserView.IsValid())
  319. {
  320. BrowserView->UnbindUObject(Name, Object, bIsPermanent);
  321. }
  322. }
  323. void SWebInterfaceBrowser::BindAdapter(const TSharedRef<IWebInterfaceBrowserAdapter>& Adapter)
  324. {
  325. if (BrowserView.IsValid())
  326. {
  327. BrowserView->BindAdapter(Adapter);
  328. }
  329. }
  330. void SWebInterfaceBrowser::UnbindAdapter(const TSharedRef<IWebInterfaceBrowserAdapter>& Adapter)
  331. {
  332. if (BrowserView.IsValid())
  333. {
  334. BrowserView->UnbindAdapter(Adapter);
  335. }
  336. }
  337. void SWebInterfaceBrowser::BindInputMethodSystem(ITextInputMethodSystem* TextInputMethodSystem)
  338. {
  339. if (BrowserView.IsValid())
  340. {
  341. BrowserView->BindInputMethodSystem(TextInputMethodSystem);
  342. }
  343. }
  344. void SWebInterfaceBrowser::UnbindInputMethodSystem()
  345. {
  346. if (BrowserView.IsValid())
  347. {
  348. BrowserView->UnbindInputMethodSystem();
  349. }
  350. }
  351. void SWebInterfaceBrowser::SetParentWindow(TSharedPtr<SWindow> Window)
  352. {
  353. if (BrowserView.IsValid())
  354. {
  355. BrowserView->SetParentWindow(Window);
  356. }
  357. }
  358. #undef LOCTEXT_NAMESPACE