Browse Source

WebView in content of UserControl

Tero Paananen 4 years ago
parent
commit
83df3fa77c

+ 20
- 18
windows/ReactNativeWebView/ReactWebView.cpp View File

@@ -26,11 +26,13 @@ namespace winrt::ReactNativeWebView::implementation {
26 26
 #else
27 27
         m_webView = winrt::WebView();
28 28
 #endif
29
+        this->Content(m_webView);
29 30
         RegisterEvents();
30 31
     }
31 32
 
32
-    winrt::WebView ReactWebView::GetView() {
33
-        return m_webView;
33
+    ReactWebView::~ReactWebView()
34
+    {
35
+      OutputDebugStringW(L"> ReactWebView::~ReactWebView()\n");
34 36
     }
35 37
 
36 38
     void ReactWebView::RegisterEvents() {
@@ -64,54 +66,54 @@ namespace winrt::ReactNativeWebView::implementation {
64 66
             });
65 67
     }
66 68
 
67
-    void ReactWebView::WriteWebViewNavigationEventArg(winrt::IJSValueWriter const& eventDataWriter) {
68
-        auto tag = m_webView.GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
69
-        WriteProperty(eventDataWriter, L"canGoBack", m_webView.CanGoBack());
70
-        WriteProperty(eventDataWriter, L"canGoForward", m_webView.CanGoForward());
71
-        WriteProperty(eventDataWriter, L"loading", !m_webView.IsLoaded());
69
+    void ReactWebView::WriteWebViewNavigationEventArg(winrt::WebView const& sender, winrt::IJSValueWriter const& eventDataWriter) {
70
+        auto tag = this->GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
71
+        WriteProperty(eventDataWriter, L"canGoBack", sender.CanGoBack());
72
+        WriteProperty(eventDataWriter, L"canGoForward", sender.CanGoForward());
73
+        WriteProperty(eventDataWriter, L"loading", !sender.IsLoaded());
72 74
         WriteProperty(eventDataWriter, L"target", tag);
73
-        WriteProperty(eventDataWriter, L"title", m_webView.DocumentTitle());
74
-        if (auto uri = m_webView.Source()) {
75
-            WriteProperty(eventDataWriter, L"url", uri.AbsoluteCanonicalUri());
75
+        WriteProperty(eventDataWriter, L"title", sender.DocumentTitle());
76
+        if (auto uri = sender.Source()) {
77
+          WriteProperty(eventDataWriter, L"url", uri.AbsoluteCanonicalUri());
76 78
         }
77 79
     }
78 80
 
79 81
     void ReactWebView::OnNavigationStarting(winrt::WebView const& webView, winrt::WebViewNavigationStartingEventArgs const& /*args*/) {
80 82
         m_reactContext.DispatchEvent(
81
-            webView,
83
+            *this,
82 84
             L"topLoadingStart",
83 85
             [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
84 86
                 eventDataWriter.WriteObjectBegin();
85
-                WriteWebViewNavigationEventArg(eventDataWriter);
87
+                WriteWebViewNavigationEventArg(webView, eventDataWriter);
86 88
                 eventDataWriter.WriteObjectEnd();
87 89
             });
88 90
     }
89 91
 
90 92
     void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {
91 93
         m_reactContext.DispatchEvent(
92
-            webView,
94
+            *this,
93 95
             L"topLoadingFinish",
94 96
             [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
95 97
                 eventDataWriter.WriteObjectBegin();
96
-                WriteWebViewNavigationEventArg(eventDataWriter);
98
+                WriteWebViewNavigationEventArg(webView, eventDataWriter);
97 99
                 eventDataWriter.WriteObjectEnd();
98 100
             });
99 101
 
100 102
         winrt::hstring windowAlert = L"window.alert = function (msg) {window.external.notify(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
101 103
         winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
102
-        m_webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
104
+        webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
103 105
     }
104 106
 
105 107
     void ReactWebView::OnNavigationFailed(winrt::IInspectable const& /*sender*/, winrt::WebViewNavigationFailedEventArgs const& args) {
106 108
         m_reactContext.DispatchEvent(
107
-            m_webView,
109
+            *this,
108 110
             L"topLoadingError",
109 111
             [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
110 112
                 auto httpCode = static_cast<int32_t>(args.WebErrorStatus());
111 113
                 eventDataWriter.WriteObjectBegin();
112 114
                 {
113 115
                     WriteProperty(eventDataWriter, L"code", httpCode);
114
-                    WriteWebViewNavigationEventArg(eventDataWriter);
116
+                    WriteWebViewNavigationEventArg(m_webView, eventDataWriter);
115 117
                 }
116 118
                 eventDataWriter.WriteObjectEnd();
117 119
             });
@@ -134,7 +136,7 @@ namespace winrt::ReactNativeWebView::implementation {
134 136
 
135 137
     void ReactWebView::PostMessage(winrt::hstring const& message) {
136 138
         m_reactContext.DispatchEvent(
137
-            m_webView,
139
+            *this,
138 140
             L"topMessage",
139 141
             [&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
140 142
                 eventDataWriter.WriteObjectBegin();

+ 2
- 2
windows/ReactNativeWebView/ReactWebView.h View File

@@ -12,8 +12,8 @@ namespace winrt::ReactNativeWebView::implementation {
12 12
     class ReactWebView : public ReactWebViewT<ReactWebView> {
13 13
     public:
14 14
         ReactWebView(Microsoft::ReactNative::IReactContext const& reactContext);
15
-        winrt::Windows::UI::Xaml::Controls::WebView GetView();
16 15
         void PostMessage(winrt::hstring const& message);
16
+        ~ReactWebView();
17 17
 
18 18
     private:
19 19
         winrt::Windows::UI::Xaml::Controls::WebView m_webView{ nullptr };
@@ -24,7 +24,7 @@ namespace winrt::ReactNativeWebView::implementation {
24 24
         winrt::Windows::UI::Xaml::Controls::WebView::ScriptNotify_revoker m_scriptNotifyRevoker{};
25 25
 
26 26
         void RegisterEvents();
27
-        void WriteWebViewNavigationEventArg(winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter);
27
+        void WriteWebViewNavigationEventArg(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter);
28 28
         void OnNavigationStarting(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationStartingEventArgs const& args);
29 29
         void OnNavigationCompleted(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationCompletedEventArgs const& args);
30 30
         void OnNavigationFailed(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationFailedEventArgs const& args);

+ 0
- 1
windows/ReactNativeWebView/ReactWebView.idl View File

@@ -2,7 +2,6 @@ namespace ReactNativeWebView{
2 2
     [default_interface]
3 3
     runtimeclass ReactWebView : Windows.UI.Xaml.Controls.UserControl{
4 4
         ReactWebView(Microsoft.ReactNative.IReactContext context);
5
-        Windows.UI.Xaml.Controls.WebView GetView();
6 5
         void PostMessage(String message);
7 6
     };
8 7
 } // namespace ReactNativeWebView

+ 13
- 5
windows/ReactNativeWebView/ReactWebViewManager.cpp View File

@@ -23,8 +23,8 @@ namespace winrt::ReactNativeWebView::implementation {
23 23
     }
24 24
 
25 25
     winrt::FrameworkElement ReactWebViewManager::CreateView() noexcept {
26
-        m_reactWebView = *winrt::make_self<ReactWebView>(m_reactContext);
27
-        return m_reactWebView.GetView();
26
+      auto view = winrt::ReactNativeWebView::ReactWebView(m_reactContext);
27
+      return view;
28 28
     }
29 29
 
30 30
     // IViewManagerWithReactContext
@@ -46,7 +46,11 @@ namespace winrt::ReactNativeWebView::implementation {
46 46
     void ReactWebViewManager::UpdateProperties(
47 47
         FrameworkElement const& view,
48 48
         IJSValueReader const& propertyMapReader) noexcept {
49
-        if (auto webView = view.try_as<winrt::WebView>()) {
49
+
50
+        auto control = view.try_as<winrt::UserControl>();
51
+        auto content = control.Content();
52
+
53
+        if (auto webView = content.try_as<winrt::WebView>()) {
50 54
             const JSValueObject& propertyMap = JSValueObject::ReadFrom(propertyMapReader);
51 55
 
52 56
             for (auto const& pair : propertyMap) {
@@ -116,8 +120,10 @@ namespace winrt::ReactNativeWebView::implementation {
116 120
         FrameworkElement const& view,
117 121
         winrt::hstring const& commandId,
118 122
         winrt::IJSValueReader const& commandArgsReader) noexcept {
123
+        auto control = view.try_as<winrt::UserControl>();
124
+        auto content = control.Content();
119 125
         auto commandArgs = JSValue::ReadArrayFrom(commandArgsReader);
120
-        if (auto webView = view.try_as<winrt::WebView>()) {
126
+        if (auto webView = content.try_as<winrt::WebView>()) {
121 127
             if (commandId == L"goForward") {
122 128
                 if (webView.CanGoForward()) {
123 129
                     webView.GoForward();
@@ -137,7 +143,9 @@ namespace winrt::ReactNativeWebView::implementation {
137 143
             else if (commandId == L"injectJavaScript") {
138 144
                 webView.InvokeScriptAsync(L"eval", { winrt::to_hstring(commandArgs[0].AsString()) });
139 145
             } else if(commandId == L"postMessage") {
140
-                m_reactWebView.PostMessage(winrt::to_hstring(commandArgs[0].AsString()));
146
+                if (auto reactWebView = content.try_as<ReactNativeWebView::ReactWebView>()) {
147
+                    reactWebView.PostMessage(winrt::to_hstring(commandArgs[0].AsString()));
148
+                }
141 149
             }
142 150
         }
143 151
     }

+ 0
- 1
windows/ReactNativeWebView/ReactWebViewManager.h View File

@@ -47,7 +47,6 @@ namespace winrt::ReactNativeWebView::implementation {
47 47
             winrt::Microsoft::ReactNative::IJSValueReader const& commandArgsReader) noexcept;
48 48
 
49 49
     private:
50
-        winrt::ReactNativeWebView::ReactWebView m_reactWebView{ nullptr };
51 50
         winrt::Microsoft::ReactNative::IReactContext m_reactContext{ nullptr };
52 51
 
53 52
     };