Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

desktop webview app setup #291

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions .idea/libraries/Dart_SDK.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/libraries/Flutter_Plugins.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 61 additions & 33 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class _ExampleBrowser extends State<ExampleBrowser> {
try {
await _controller.initialize();
_subscriptions.add(_controller.url.listen((url) {
print('url change -- $url');
_textController.text = url;
}));
_subscriptions.add(_controller.newRequestUrl.listen((url) {
print('newRequestUrl -- $url');
_textController.text = url;
}));

Expand Down
26 changes: 26 additions & 0 deletions example/windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Generated file, do not edit.
#

list(APPEND FLUTTER_PLUGIN_LIST
screen_retriever
webview_windows
window_manager
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
)

set(PLUGIN_BUNDLED_LIBRARIES)

foreach(plugin ${FLUTTER_PLUGIN_LIST})
add_subdirectory(flutter/ephemeral/.plugin_symlinks/${plugin}/windows plugins/${plugin})
target_link_libraries(${BINARY_NAME} PRIVATE ${plugin}_plugin)
list(APPEND PLUGIN_BUNDLED_LIBRARIES $<TARGET_FILE:${plugin}_plugin>)
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries})
endforeach(plugin)

foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST})
add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/windows plugins/${ffi_plugin})
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries})
endforeach(ffi_plugin)
16 changes: 16 additions & 0 deletions lib/src/webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ class WebviewController extends ValueNotifier<WebviewValue> {
final StreamController<String> _urlStreamController =
StreamController<String>();

final StreamController<String> _newWindowRequestedStreamController =
StreamController<String>();


/// A stream reflecting the current URL.
Stream<String> get url => _urlStreamController.stream;
Stream<String> get newRequestUrl => _newWindowRequestedStreamController.stream;

final StreamController<LoadingState> _loadingStateStreamController =
StreamController<LoadingState>.broadcast();
Expand Down Expand Up @@ -172,15 +177,23 @@ class WebviewController extends ValueNotifier<WebviewValue> {
await _pluginChannel.invokeMapMethod<String, dynamic>('initialize');

_textureId = reply!['textureId'];

print('_textureId -- $_textureId');
_methodChannel = MethodChannel('$_pluginChannelPrefix/$_textureId');
_eventChannel = EventChannel('$_pluginChannelPrefix/$_textureId/events');
_eventStreamSubscription =
_eventChannel.receiveBroadcastStream().listen((event) {
final map = event as Map<dynamic, dynamic>;

print('event type -- $map');
switch (map['type']) {
case 'urlChanged':
_urlStreamController.add(map['value']);
break;

case 'newTabUrl':
_newWindowRequestedStreamController.add(map['value']);
break;
case 'onLoadError':
final value = WebErrorStatus.values[map['value']];
_onLoadErrorStreamController.add(value);
Expand All @@ -203,6 +216,9 @@ class WebviewController extends ValueNotifier<WebviewValue> {
case 'cursorChanged':
_cursorStreamController.add(getCursorByName(map['value']));
break;
case 'click':
_urlStreamController.add(map['value']);
break;
case 'webMessageReceived':
try {
final message = json.decode(map['value']);
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
flutter:
sdk: flutter


dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
4 changes: 2 additions & 2 deletions webview_windows.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
<excludeFolder url="file://$MODULE_DIR$/.idea" />
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/example/.pub" />
<excludeFolder url="file://$MODULE_DIR$/example/build" />
<excludeFolder url="file://$MODULE_DIR$/example/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/example/.pub" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart Packages" level="project" />
<orderEntry type="library" name="Dart SDK" level="project" />
<orderEntry type="library" name="Flutter Plugins" level="project" />
</component>
Expand Down
3 changes: 3 additions & 0 deletions windows/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions windows/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions windows/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions windows/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions windows/.idea/windows.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions windows/webview.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "webview.h"

#include <iostream>
#include <wrl.h>

#include <string>
#include <wil/com.h>

#include <format>
#include <iostream>

#include "util/composition.desktop.interop.h"
#include "util/string_converter.h"
Expand Down Expand Up @@ -354,11 +356,23 @@ void Webview::RegisterEventHandlers() {
break;
}

// Extract relevant information
// Open the URI in the default browser.
// Extract relevant information
LPWSTR wurl;
args->get_Uri(&wurl);
std::cout << "The value of myVariable is result: " << std::endl;

std::string url = util::Utf8FromUtf16(wurl);
url_new_tab_callback_(url);

return S_OK;
})
.Get(),
&event_registrations_.new_windows_requested_token_);


// url_new_tab_callback_(args);
webview_->add_ContainsFullScreenElementChanged(
Callback<ICoreWebView2ContainsFullScreenElementChangedEventHandler>(
[this](ICoreWebView2* sender, IUnknown* args) -> HRESULT {
Expand Down
6 changes: 6 additions & 0 deletions windows/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Webview {
friend class WebviewHost;

typedef std::function<void(const std::string&)> UrlChangedCallback;
typedef std::function<void(const std::string&)> UrlNewTabCallback;
typedef std::function<void(WebviewLoadingState)> LoadingStateChangedCallback;
typedef std::function<void(COREWEBVIEW2_WEB_ERROR_STATUS)>
OnLoadErrorCallback;
Expand Down Expand Up @@ -164,6 +165,10 @@ class Webview {
url_changed_callback_ = std::move(callback);
}

void OnUrlNewTab(UrlNewTabCallback callback) {
url_new_tab_callback_ = std::move(callback);
}

void OnLoadError(OnLoadErrorCallback callback) {
on_load_error_callback_ = std::move(callback);
}
Expand Down Expand Up @@ -233,6 +238,7 @@ class Webview {
EventRegistrations event_registrations_{};

UrlChangedCallback url_changed_callback_;
UrlNewTabCallback url_new_tab_callback_;
LoadingStateChangedCallback loading_state_changed_callback_;
OnLoadErrorCallback on_load_error_callback_;
HistoryChangedCallback history_changed_callback_;
Expand Down
9 changes: 9 additions & 0 deletions windows/webview_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ void WebviewBridge::RegisterEventHandlers() {
EmitEvent(event);
});

webview_->OnUrlNewTab([this](const std::string& url) {
const auto event = flutter::EncodableValue(flutter::EncodableMap{
{flutter::EncodableValue(kEventType),
flutter::EncodableValue("newTabUrl")},
{flutter::EncodableValue(kEventValue), flutter::EncodableValue(url)},
});
EmitEvent(event);
});

webview_->OnLoadError([this](COREWEBVIEW2_WEB_ERROR_STATUS web_status) {
const auto event = flutter::EncodableValue(flutter::EncodableMap{
{flutter::EncodableValue(kEventType),
Expand Down