SProxyWeb.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright aXiuShen. All Rights Reserved.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "Layout/Visibility.h"
  5. #include "Input/Reply.h"
  6. #include "Widgets/DeclarativeSyntaxSupport.h"
  7. #include "Widgets/SCompoundWidget.h"
  8. #include "Framework/SlateDelegates.h"
  9. #include "Styling/SlateTypes.h"
  10. #include "Widgets/Text/STextBlock.h"
  11. #ifdef USING_WEBBROWSER
  12. class SWebBrowserView;
  13. class IWebBrowserWindow;
  14. class SProxyWeb
  15. : public SCompoundWidget
  16. {
  17. public:
  18. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnKeyUp, const FKeyEvent& );
  19. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnKeyDown, const FKeyEvent& );
  20. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnKeyChar, const FCharacterEvent& );
  21. DECLARE_DELEGATE_OneParam(FOnLoadState, const int);
  22. DECLARE_DELEGATE_RetVal_TwoParams(bool, FOnBeforePopup, FString, FString);
  23. DECLARE_DELEGATE_TwoParams(FOnDownloadComplete, FString, FString);
  24. SLATE_BEGIN_ARGS(SProxyWeb)
  25. : _ViewportSize(FVector2D::ZeroVector)
  26. , _SwitchInputMethod(false)
  27. , _EnableMouseTransparency(false)
  28. , _InitialURL(TEXT("https://www.baidu.com"))
  29. , _BackgroundColor(255, 255, 255, 255)
  30. , _ShowControls(true)
  31. , _ShowAddressBar(false)
  32. //, _webCursor(false)
  33. , _BrowserFrameRate(30)
  34. {
  35. _Visibility = EVisibility::SelfHitTestInvisible;
  36. }
  37. /* this party for event */
  38. /** Called before a popup window happens */
  39. SLATE_EVENT(FOnBeforePopup, OnBeforePopup)
  40. /** Called when document loading change. */
  41. SLATE_EVENT(FOnLoadState, OnLoadState)
  42. /** Called when the Url changes. */
  43. SLATE_EVENT(FOnTextChanged, OnUrlChanged)
  44. /** Called when file download finish. */
  45. SLATE_EVENT(FOnDownloadComplete, OnDownloadComplete)
  46. /* this party for params */
  47. /** Control and Editor show text style */
  48. SLATE_ARGUMENT(FTextBlockStyle, TextStyle)
  49. /** Desired size of the web browser viewport. */
  50. SLATE_ARGUMENT(FVector2D, ViewportSize)
  51. /** Desired size of the web browser viewport. */
  52. SLATE_ARGUMENT(bool, SwitchInputMethod)
  53. /** allow mouse transcparency webpage */
  54. SLATE_ARGUMENT(bool, EnableMouseTransparency)
  55. /** URL that the browser will initially navigate to. The URL should include the protocol, eg http:// */
  56. SLATE_ARGUMENT(FString, InitialURL)
  57. /** Opaque background color used before a document is loaded and when no document color is specified. */
  58. SLATE_ARGUMENT(FColor, BackgroundColor)
  59. /** Whether to show standard controls like Back, Forward, Reload etc. */
  60. SLATE_ARGUMENT(bool, ShowControls)
  61. /** Whether to show an address bar. */
  62. SLATE_ARGUMENT(bool, RightKeyPopup)
  63. /** Whether to show an address bar. */
  64. SLATE_ARGUMENT(bool, ShowAddressBar)
  65. /** Whether to show an Web Cursor . */
  66. //SLATE_ARGUMENT(bool, webCursor)
  67. /** The frames per second rate that the browser will attempt to use. */
  68. SLATE_ARGUMENT(int, BrowserFrameRate)
  69. /** fixed pixel. */
  70. SLATE_ARGUMENT(FIntPoint, Pixel)
  71. /** zoom level*/
  72. SLATE_ARGUMENT(float, zoom)
  73. /** download show tip */
  74. SLATE_ARGUMENT(bool, downloadTip)
  75. //SLATE_ARGUMENT(float, zoomlevel)
  76. SLATE_END_ARGS()
  77. /**
  78. * Load the specified URL.
  79. * @param NewURL New URL to load.
  80. */
  81. void LoadURL(FString NewURL);
  82. void LoadString(FString NewURL,FString content);
  83. /**
  84. * reopen a new render to replace old render.
  85. * @param NewURL New URL to load.
  86. * if NewURL is empty,will Assign old URL.
  87. */
  88. void ReopenRender(FString NewURL);
  89. /** Get the current title of the web page. */
  90. FText GetTitleText() const;
  91. /** Stop loading the page. */
  92. void StopLoad();
  93. /** Reload the current page. */
  94. void Reload();
  95. /** Whether the document is currently being loaded. */
  96. bool IsLoading() const;
  97. /** Execute javascript on the current window */
  98. void ExecuteJavascript(const FString& ScriptText);
  99. /** Returns true if the browser can navigate backwards. */
  100. bool CanGoBack() const;
  101. /** Returns true if the browser can navigate forwards. */
  102. bool CanGoForward() const;
  103. /** Navigate backwards. */
  104. void GoBack();
  105. /**
  106. * Gets the currently loaded URL.
  107. * @return The URL, or empty string if no document is loaded.
  108. */
  109. FString GetUrl() const;
  110. /** Navigate forwards. */
  111. void GoForward();
  112. /** Set Page Zoom level */
  113. void ZoomLevel(float zoomlevel);
  114. /** Set Page pixel */
  115. void WebPixel(FIntPoint pixel);
  116. /** show or hide address bar */
  117. void ShowAddress(bool isShow);
  118. /**
  119. * Expose a UObject instance to the browser runtime.
  120. * Properties and Functions will be accessible from JavaScript side.
  121. * As all communication with the rendering procesis asynchronous, return values (both for properties and function results) are wrapped into JS Future objects.
  122. * @param Name The name of the object. The object will show up as window.ue4.{Name} on the javascript side. If there is an existing object of the same name, this object will replace it. If bIsPermanent is false and there is an existing permanent binding, the permanent binding will be restored when the temporary one is removed.
  123. * @param Object The object instance.
  124. * @param bIsPermanent If true, the object will be visible to all pages loaded through this browser widget, otherwise, it will be deleted when navigating away from the current page. Non-permanent bindings should be registered from inside an OnLoadStarted event handler in order to be available before JS code starts loading.
  125. */
  126. void BindUObject(const FString& Name, UObject* Object, bool bIsPermanent = true);
  127. /**
  128. * Remove an existing script binding registered by BindUObject.
  129. * @param Name The name of the object to remove.
  130. * @param Object The object will only be removed if it is the same object as the one passed in.
  131. * @param bIsPermanent Must match the bIsPermanent argument passed to BindUObject.
  132. */
  133. void UnbindUObject(const FString& Name, UObject* Object, bool bIsPermanent = true);
  134. //virtual bool SupportsKeyboardFocus() const override { return true; }
  135. public:
  136. /** Default constructor. */
  137. SProxyWeb();
  138. ~SProxyWeb();
  139. /**
  140. * Construct the widget.
  141. * @param InArgs Declaration from which to construct the widget.
  142. */
  143. void Construct(const FArguments& InArgs);
  144. private:
  145. //
  146. virtual void Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime);
  147. virtual void OnDragEnter(const FGeometry& MyGeometry, const FDragDropEvent& DragDropEvent) override;
  148. virtual void OnDragLeave(const FDragDropEvent& DragDropEvent) override;
  149. virtual FReply OnDragOver(const FGeometry& MyGeometry, const FDragDropEvent& DragDropEvent) override;
  150. virtual FReply OnDrop(const FGeometry& MyGeometry, const FDragDropEvent& DragDropEvent)override;
  151. EVisibility GetViewportVisibility() const;
  152. bool ReadTexturePixel(FVector2D& LocalUV, FColor& MousePixel);
  153. FString dns_to_local(FString url)const;
  154. FString local_to_dns(FString url)const;
  155. private:
  156. TSharedPtr<SWebBrowserView> BrowserView;
  157. TSharedPtr<IWebBrowserWindow> BrowserWindow;
  158. bool bMouseTransparency;
  159. uint8 TransparencyThreadshold;
  160. FColor LastMousePixel;
  161. float sumDelay;
  162. int32 preMouseX;
  163. int32 preMouseY;
  164. FString local_domain;
  165. const FString local_dns;
  166. //#if UE_BUILD_DEVELOPMENT || UE_BUILD_DEBUG
  167. // TMap<TWeakPtr<IWebBrowserWindow>, TWeakPtr<SWindow>> BrowserWindowWidgets;
  168. //#endif
  169. };
  170. #endif