Skip to content

Commit

Permalink
Add full-screen support (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnschulze authored Sep 26, 2022
1 parent 705187a commit ec75b39
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
24 changes: 21 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import 'package:flutter/services.dart';
import 'dart:async';

import 'package:webview_windows/webview_windows.dart';
import 'package:window_manager/window_manager.dart';

final navigatorKey = GlobalKey<NavigatorState>();

void main() {
void main() async {
// For full-screen example
WidgetsFlutterBinding.ensureInitialized();
await windowManager.ensureInitialized();

runApp(MyApp());
}

Expand All @@ -25,6 +30,7 @@ class ExampleBrowser extends StatefulWidget {
class _ExampleBrowser extends State<ExampleBrowser> {
final _controller = WebviewController();
final _textController = TextEditingController();
final List<StreamSubscription> _subscriptions = [];
bool _isWebviewSuspended = false;

@override
Expand All @@ -43,9 +49,15 @@ class _ExampleBrowser extends State<ExampleBrowser> {

try {
await _controller.initialize();
_controller.url.listen((url) {
_subscriptions.add(_controller.url.listen((url) {
_textController.text = url;
});
}));

_subscriptions
.add(_controller.containsFullScreenElementChanged.listen((flag) {
debugPrint('Contains fullscreen element: $flag');
windowManager.setFullScreen(flag);
}));

await _controller.setBackgroundColor(Colors.transparent);
await _controller.setPopupWindowPolicy(WebviewPopupWindowPolicy.deny);
Expand Down Expand Up @@ -211,4 +223,10 @@ class _ExampleBrowser extends State<ExampleBrowser> {

return decision ?? WebviewPermissionDecision.none;
}

@override
void dispose() {
_subscriptions.forEach((s) => s.cancel());
super.dispose();
}
}
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:

webview_windows:
path: ../
window_manager: ^0.2.7

dev_dependencies:
flutter_test:
Expand Down
12 changes: 12 additions & 0 deletions lib/src/webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ class WebviewController extends ValueNotifier<WebviewValue> {

Stream<dynamic> get webMessage => _webMessageStreamController.stream;

final StreamController<bool>
_containsFullScreenElementChangedStreamController =
StreamController<bool>.broadcast();

/// A stream reflecting whether the document currently contains full-screen elements.
Stream<bool> get containsFullScreenElementChanged =>
_containsFullScreenElementChangedStreamController.stream;

WebviewController() : super(WebviewValue.uninitialized());

/// Initializes the underlying platform view.
Expand Down Expand Up @@ -194,6 +202,10 @@ class WebviewController extends ValueNotifier<WebviewValue> {
} catch (ex) {
_webMessageStreamController.addError(ex);
}
break;
case 'containsFullScreenElementChanged':
_containsFullScreenElementChangedStreamController.add(map['value']);
break;
}
});

Expand Down
13 changes: 13 additions & 0 deletions windows/webview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,19 @@ void Webview::RegisterEventHandlers() {
})
.Get(),
&event_registrations_.new_windows_requested_token_);

webview_->add_ContainsFullScreenElementChanged(
Callback<ICoreWebView2ContainsFullScreenElementChangedEventHandler>(
[this](ICoreWebView2* sender, IUnknown* args) -> HRESULT {
BOOL flag = FALSE;
if (contains_fullscreen_element_changed_callback_ &&
SUCCEEDED(sender->get_ContainsFullScreenElement(&flag))) {
contains_fullscreen_element_changed_callback_(flag);
}
return S_OK;
})
.Get(),
&event_registrations_.contains_fullscreen_element_changed_token_);
}

void Webview::SetSurfaceSize(size_t width, size_t height) {
Expand Down
10 changes: 10 additions & 0 deletions windows/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct EventRegistrations {
EventRegistrationToken permission_requested_token_{};
EventRegistrationToken devtools_protocol_event_token_{};
EventRegistrationToken new_windows_requested_token_{};
EventRegistrationToken contains_fullscreen_element_changed_token_{};
};

class Webview {
Expand Down Expand Up @@ -113,6 +114,8 @@ class Webview {
bool is_user_initiated,
WebviewPermissionRequestedCompleter completer)>
PermissionRequestedCallback;
typedef std::function<void(bool contains_fullscreen_element)>
ContainsFullScreenElementChangedCallback;

~Webview();

Expand Down Expand Up @@ -200,6 +203,11 @@ class Webview {
devtools_protocol_event_callback_ = std::move(callback);
}

void OnContainsFullScreenElementChanged(
ContainsFullScreenElementChangedCallback callback) {
contains_fullscreen_element_changed_callback_ = std::move(callback);
}

private:
HWND hwnd_;
bool owns_window_;
Expand Down Expand Up @@ -233,6 +241,8 @@ class Webview {
WebMessageReceivedCallback web_message_received_callback_;
PermissionRequestedCallback permission_requested_callback_;
DevtoolsProtocolEventCallback devtools_protocol_event_callback_;
ContainsFullScreenElementChangedCallback
contains_fullscreen_element_changed_callback_;

Webview(
wil::com_ptr<ICoreWebView2CompositionController> composition_controller,
Expand Down
10 changes: 10 additions & 0 deletions windows/webview_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ void WebviewBridge::RegisterEventHandlers() {
Webview::WebviewPermissionRequestedCompleter completer) {
OnPermissionRequested(url, kind, is_user_initiated, completer);
});

webview_->OnContainsFullScreenElementChanged(
[this](bool contains_fullscreen_element) {
const auto event = flutter::EncodableValue(flutter::EncodableMap{
{flutter::EncodableValue(kEventType),
flutter::EncodableValue("containsFullScreenElementChanged")},
{flutter::EncodableValue(kEventValue),
contains_fullscreen_element}});
EmitEvent(event);
});
}

void WebviewBridge::OnPermissionRequested(
Expand Down

0 comments on commit ec75b39

Please sign in to comment.