Browse Source

WebView in content of UserControl

Tero Paananen 4 years ago
parent
commit
83df3fa77c

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

26
 #else
26
 #else
27
         m_webView = winrt::WebView();
27
         m_webView = winrt::WebView();
28
 #endif
28
 #endif
29
+        this->Content(m_webView);
29
         RegisterEvents();
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
     void ReactWebView::RegisterEvents() {
38
     void ReactWebView::RegisterEvents() {
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
         WriteProperty(eventDataWriter, L"target", tag);
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
     void ReactWebView::OnNavigationStarting(winrt::WebView const& webView, winrt::WebViewNavigationStartingEventArgs const& /*args*/) {
81
     void ReactWebView::OnNavigationStarting(winrt::WebView const& webView, winrt::WebViewNavigationStartingEventArgs const& /*args*/) {
80
         m_reactContext.DispatchEvent(
82
         m_reactContext.DispatchEvent(
81
-            webView,
83
+            *this,
82
             L"topLoadingStart",
84
             L"topLoadingStart",
83
             [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
85
             [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
84
                 eventDataWriter.WriteObjectBegin();
86
                 eventDataWriter.WriteObjectBegin();
85
-                WriteWebViewNavigationEventArg(eventDataWriter);
87
+                WriteWebViewNavigationEventArg(webView, eventDataWriter);
86
                 eventDataWriter.WriteObjectEnd();
88
                 eventDataWriter.WriteObjectEnd();
87
             });
89
             });
88
     }
90
     }
89
 
91
 
90
     void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {
92
     void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {
91
         m_reactContext.DispatchEvent(
93
         m_reactContext.DispatchEvent(
92
-            webView,
94
+            *this,
93
             L"topLoadingFinish",
95
             L"topLoadingFinish",
94
             [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
96
             [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
95
                 eventDataWriter.WriteObjectBegin();
97
                 eventDataWriter.WriteObjectBegin();
96
-                WriteWebViewNavigationEventArg(eventDataWriter);
98
+                WriteWebViewNavigationEventArg(webView, eventDataWriter);
97
                 eventDataWriter.WriteObjectEnd();
99
                 eventDataWriter.WriteObjectEnd();
98
             });
100
             });
99
 
101
 
100
         winrt::hstring windowAlert = L"window.alert = function (msg) {window.external.notify(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
102
         winrt::hstring windowAlert = L"window.alert = function (msg) {window.external.notify(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
101
         winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
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
     void ReactWebView::OnNavigationFailed(winrt::IInspectable const& /*sender*/, winrt::WebViewNavigationFailedEventArgs const& args) {
107
     void ReactWebView::OnNavigationFailed(winrt::IInspectable const& /*sender*/, winrt::WebViewNavigationFailedEventArgs const& args) {
106
         m_reactContext.DispatchEvent(
108
         m_reactContext.DispatchEvent(
107
-            m_webView,
109
+            *this,
108
             L"topLoadingError",
110
             L"topLoadingError",
109
             [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
111
             [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
110
                 auto httpCode = static_cast<int32_t>(args.WebErrorStatus());
112
                 auto httpCode = static_cast<int32_t>(args.WebErrorStatus());
111
                 eventDataWriter.WriteObjectBegin();
113
                 eventDataWriter.WriteObjectBegin();
112
                 {
114
                 {
113
                     WriteProperty(eventDataWriter, L"code", httpCode);
115
                     WriteProperty(eventDataWriter, L"code", httpCode);
114
-                    WriteWebViewNavigationEventArg(eventDataWriter);
116
+                    WriteWebViewNavigationEventArg(m_webView, eventDataWriter);
115
                 }
117
                 }
116
                 eventDataWriter.WriteObjectEnd();
118
                 eventDataWriter.WriteObjectEnd();
117
             });
119
             });
134
 
136
 
135
     void ReactWebView::PostMessage(winrt::hstring const& message) {
137
     void ReactWebView::PostMessage(winrt::hstring const& message) {
136
         m_reactContext.DispatchEvent(
138
         m_reactContext.DispatchEvent(
137
-            m_webView,
139
+            *this,
138
             L"topMessage",
140
             L"topMessage",
139
             [&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
141
             [&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
140
                 eventDataWriter.WriteObjectBegin();
142
                 eventDataWriter.WriteObjectBegin();

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

12
     class ReactWebView : public ReactWebViewT<ReactWebView> {
12
     class ReactWebView : public ReactWebViewT<ReactWebView> {
13
     public:
13
     public:
14
         ReactWebView(Microsoft::ReactNative::IReactContext const& reactContext);
14
         ReactWebView(Microsoft::ReactNative::IReactContext const& reactContext);
15
-        winrt::Windows::UI::Xaml::Controls::WebView GetView();
16
         void PostMessage(winrt::hstring const& message);
15
         void PostMessage(winrt::hstring const& message);
16
+        ~ReactWebView();
17
 
17
 
18
     private:
18
     private:
19
         winrt::Windows::UI::Xaml::Controls::WebView m_webView{ nullptr };
19
         winrt::Windows::UI::Xaml::Controls::WebView m_webView{ nullptr };
24
         winrt::Windows::UI::Xaml::Controls::WebView::ScriptNotify_revoker m_scriptNotifyRevoker{};
24
         winrt::Windows::UI::Xaml::Controls::WebView::ScriptNotify_revoker m_scriptNotifyRevoker{};
25
 
25
 
26
         void RegisterEvents();
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
         void OnNavigationStarting(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationStartingEventArgs const& args);
28
         void OnNavigationStarting(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationStartingEventArgs const& args);
29
         void OnNavigationCompleted(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationCompletedEventArgs const& args);
29
         void OnNavigationCompleted(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationCompletedEventArgs const& args);
30
         void OnNavigationFailed(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationFailedEventArgs const& args);
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
     [default_interface]
2
     [default_interface]
3
     runtimeclass ReactWebView : Windows.UI.Xaml.Controls.UserControl{
3
     runtimeclass ReactWebView : Windows.UI.Xaml.Controls.UserControl{
4
         ReactWebView(Microsoft.ReactNative.IReactContext context);
4
         ReactWebView(Microsoft.ReactNative.IReactContext context);
5
-        Windows.UI.Xaml.Controls.WebView GetView();
6
         void PostMessage(String message);
5
         void PostMessage(String message);
7
     };
6
     };
8
 } // namespace ReactNativeWebView
7
 } // namespace ReactNativeWebView

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

23
     }
23
     }
24
 
24
 
25
     winrt::FrameworkElement ReactWebViewManager::CreateView() noexcept {
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
     // IViewManagerWithReactContext
30
     // IViewManagerWithReactContext
46
     void ReactWebViewManager::UpdateProperties(
46
     void ReactWebViewManager::UpdateProperties(
47
         FrameworkElement const& view,
47
         FrameworkElement const& view,
48
         IJSValueReader const& propertyMapReader) noexcept {
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
             const JSValueObject& propertyMap = JSValueObject::ReadFrom(propertyMapReader);
54
             const JSValueObject& propertyMap = JSValueObject::ReadFrom(propertyMapReader);
51
 
55
 
52
             for (auto const& pair : propertyMap) {
56
             for (auto const& pair : propertyMap) {
116
         FrameworkElement const& view,
120
         FrameworkElement const& view,
117
         winrt::hstring const& commandId,
121
         winrt::hstring const& commandId,
118
         winrt::IJSValueReader const& commandArgsReader) noexcept {
122
         winrt::IJSValueReader const& commandArgsReader) noexcept {
123
+        auto control = view.try_as<winrt::UserControl>();
124
+        auto content = control.Content();
119
         auto commandArgs = JSValue::ReadArrayFrom(commandArgsReader);
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
             if (commandId == L"goForward") {
127
             if (commandId == L"goForward") {
122
                 if (webView.CanGoForward()) {
128
                 if (webView.CanGoForward()) {
123
                     webView.GoForward();
129
                     webView.GoForward();
137
             else if (commandId == L"injectJavaScript") {
143
             else if (commandId == L"injectJavaScript") {
138
                 webView.InvokeScriptAsync(L"eval", { winrt::to_hstring(commandArgs[0].AsString()) });
144
                 webView.InvokeScriptAsync(L"eval", { winrt::to_hstring(commandArgs[0].AsString()) });
139
             } else if(commandId == L"postMessage") {
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
             winrt::Microsoft::ReactNative::IJSValueReader const& commandArgsReader) noexcept;
47
             winrt::Microsoft::ReactNative::IJSValueReader const& commandArgsReader) noexcept;
48
 
48
 
49
     private:
49
     private:
50
-        winrt::ReactNativeWebView::ReactWebView m_reactWebView{ nullptr };
51
         winrt::Microsoft::ReactNative::IReactContext m_reactContext{ nullptr };
50
         winrt::Microsoft::ReactNative::IReactContext m_reactContext{ nullptr };
52
 
51
 
53
     };
52
     };