From d0b552825302a429131fad3bf90e8d0161ad7b7b Mon Sep 17 00:00:00 2001 From: cr0manty Date: Wed, 5 Oct 2022 16:31:39 +0200 Subject: [PATCH 1/2] [ADD] full screen mode view for linux --- packages/desktop_webview_window/example/lib/main.dart | 1 + .../lib/src/create_configuration.dart | 3 +++ .../linux/desktop_webview_window_plugin.cc | 3 ++- packages/desktop_webview_window/linux/webview_window.cc | 9 +++++++-- packages/desktop_webview_window/linux/webview_window.h | 3 ++- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/desktop_webview_window/example/lib/main.dart b/packages/desktop_webview_window/example/lib/main.dart index b5bfae7e..c7b14f07 100644 --- a/packages/desktop_webview_window/example/lib/main.dart +++ b/packages/desktop_webview_window/example/lib/main.dart @@ -61,6 +61,7 @@ class _MyAppState extends State { title: "ExampleTestWindow", titleBarTopPadding: Platform.isMacOS ? 20 : 0, userDataFolderWindows: await _getWebViewPath(), + useFullScreen: false, ), ); webview diff --git a/packages/desktop_webview_window/lib/src/create_configuration.dart b/packages/desktop_webview_window/lib/src/create_configuration.dart index 9765ac4e..f3af6096 100644 --- a/packages/desktop_webview_window/lib/src/create_configuration.dart +++ b/packages/desktop_webview_window/lib/src/create_configuration.dart @@ -12,6 +12,7 @@ class CreateConfiguration { final int titleBarTopPadding; final String userDataFolderWindows; + final bool useFullScreen; const CreateConfiguration({ this.windowWidth = 1280, @@ -20,6 +21,7 @@ class CreateConfiguration { this.titleBarHeight = 40, this.titleBarTopPadding = 0, this.userDataFolderWindows = 'webview_window_WebView2', + this.useFullScreen = false, }); factory CreateConfiguration.platform() { @@ -35,5 +37,6 @@ class CreateConfiguration { "titleBarHeight": titleBarHeight, "titleBarTopPadding": titleBarTopPadding, "userDataFolderWindows": userDataFolderWindows, + "useFullScreen": useFullScreen, }; } diff --git a/packages/desktop_webview_window/linux/desktop_webview_window_plugin.cc b/packages/desktop_webview_window/linux/desktop_webview_window_plugin.cc index 3c9fcf01..2cbcb23f 100644 --- a/packages/desktop_webview_window/linux/desktop_webview_window_plugin.cc +++ b/packages/desktop_webview_window/linux/desktop_webview_window_plugin.cc @@ -47,6 +47,7 @@ static void webview_window_plugin_handle_method_call( auto height = fl_value_get_int(fl_value_lookup_string(args, "windowHeight")); auto title = fl_value_get_string(fl_value_lookup_string(args, "title")); auto title_bar_height = fl_value_get_int(fl_value_lookup_string(args, "titleBarHeight")); + auto use_full_screen = fl_value_get_int(fl_value_lookup_string(args, "useFullScreen")); auto window_id = next_window_id_; g_object_ref(self); @@ -55,7 +56,7 @@ static void webview_window_plugin_handle_method_call( [self, window_id]() { self->windows->erase(window_id); g_object_unref(self); - }, title, width, height, title_bar_height); + }, title, width, height, title_bar_height, use_full_screen); self->windows->insert({window_id, std::move(webview)}); next_window_id_++; fl_method_call_respond_success(method_call, fl_value_new_int(window_id), nullptr); diff --git a/packages/desktop_webview_window/linux/webview_window.cc b/packages/desktop_webview_window/linux/webview_window.cc index 7b47edb9..07d0321b 100644 --- a/packages/desktop_webview_window/linux/webview_window.cc +++ b/packages/desktop_webview_window/linux/webview_window.cc @@ -56,7 +56,8 @@ WebviewWindow::WebviewWindow( const std::string &title, int width, int height, - int title_bar_height + int title_bar_height, + bool use_fullscreen ) : method_channel_(method_channel), window_id_(window_id), on_close_callback_(std::move(on_close_callback)), @@ -76,8 +77,12 @@ WebviewWindow::WebviewWindow( FL_METHOD_CHANNEL(window->method_channel_), "onWindowClose", args, nullptr, nullptr, nullptr); }), this); + if (use_fullscreen) { + gtk_window_fullscreen(GTK_WINDOW(window_)); + } else { + gtk_window_set_default_size(GTK_WINDOW(window_), width, height); + } gtk_window_set_title(GTK_WINDOW(window_), title.c_str()); - gtk_window_set_default_size(GTK_WINDOW(window_), width, height); gtk_window_set_position(GTK_WINDOW(window_), GTK_WIN_POS_CENTER); box_ = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0)); diff --git a/packages/desktop_webview_window/linux/webview_window.h b/packages/desktop_webview_window/linux/webview_window.h index 37706c33..c180e07b 100644 --- a/packages/desktop_webview_window/linux/webview_window.h +++ b/packages/desktop_webview_window/linux/webview_window.h @@ -19,7 +19,8 @@ class WebviewWindow { int64_t window_id, std::function on_close_callback, const std::string &title, int width, int height, - int title_bar_height + int title_bar_height, + bool use_fullscreen ); virtual ~WebviewWindow(); From 67c5b27f130d266936a5f267ec322d774c4eebfa Mon Sep 17 00:00:00 2001 From: cr0manty Date: Wed, 5 Oct 2022 17:37:22 +0200 Subject: [PATCH 2/2] [ADD] ability to disable title bar --- .../lib/src/create_configuration.dart | 7 +++- .../lib/src/title_bar.dart | 39 ++++++++++++++++--- .../linux/desktop_webview_window_plugin.cc | 5 ++- .../linux/webview_window.cc | 12 ++++-- .../linux/webview_window.h | 3 +- 5 files changed, 52 insertions(+), 14 deletions(-) diff --git a/packages/desktop_webview_window/lib/src/create_configuration.dart b/packages/desktop_webview_window/lib/src/create_configuration.dart index f3af6096..fe1ddbaf 100644 --- a/packages/desktop_webview_window/lib/src/create_configuration.dart +++ b/packages/desktop_webview_window/lib/src/create_configuration.dart @@ -13,6 +13,7 @@ class CreateConfiguration { final String userDataFolderWindows; final bool useFullScreen; + final bool disableTitleBar; const CreateConfiguration({ this.windowWidth = 1280, @@ -22,6 +23,7 @@ class CreateConfiguration { this.titleBarTopPadding = 0, this.userDataFolderWindows = 'webview_window_WebView2', this.useFullScreen = false, + this.disableTitleBar = false, }); factory CreateConfiguration.platform() { @@ -34,9 +36,10 @@ class CreateConfiguration { "windowWidth": windowWidth, "windowHeight": windowHeight, "title": title, - "titleBarHeight": titleBarHeight, - "titleBarTopPadding": titleBarTopPadding, + "titleBarHeight": disableTitleBar ? 0 : titleBarHeight, + "titleBarTopPadding": disableTitleBar ? 0 : titleBarTopPadding, "userDataFolderWindows": userDataFolderWindows, "useFullScreen": useFullScreen, + "disableTitleBar": disableTitleBar, }; } diff --git a/packages/desktop_webview_window/lib/src/title_bar.dart b/packages/desktop_webview_window/lib/src/title_bar.dart index 8fd198c9..08f4dd6c 100644 --- a/packages/desktop_webview_window/lib/src/title_bar.dart +++ b/packages/desktop_webview_window/lib/src/title_bar.dart @@ -14,27 +14,52 @@ const _channel = ClientMessageChannel(); /// can use [TitleBarWebViewController] to controller the WebView /// use [TitleBarWebViewState] to triger the title bar status. /// + +@Deprecated('Use [runWebViewWidget], this method will be removed') bool runWebViewTitleBarWidget( List args, { WidgetBuilder? builder, Color? backgroundColor, void Function(Object error, StackTrace stack)? onError, +}) => + runWebViewWidget( + args, + builder: builder, + backgroundColor: backgroundColor, + onError: onError, + ); + +bool runWebViewWidget( + List args, { + WidgetBuilder? builder, + Color? backgroundColor, + void Function(Object error, StackTrace stack)? onError, }) { - if (args.isEmpty || args[0] != 'web_view_title_bar') { + if (args.isEmpty || !args[0].startsWith('web_view')) { return false; } final webViewId = int.tryParse(args[1]); if (webViewId == null) { return false; } - final titleBarTopPadding = int.tryParse(args.length > 2 ? args[2] : '0') ?? 0; + int titleBarTopPadding = 0; + if (args[0] != 'web_view') { + titleBarTopPadding = int.tryParse(args.length > 2 ? args[2] : '0') ?? 0; + } + + WidgetBuilder? widgetBuilder; + + if (args[0] == 'web_view_title_bar') { + widgetBuilder = _defaultTitleBar; + } + runZonedGuarded( () { runApp(_TitleBarApp( webViewId: webViewId, titleBarTopPadding: titleBarTopPadding, backgroundColor: backgroundColor, - builder: builder ?? _defaultTitleBar, + builder: widgetBuilder, )); }, onError ?? @@ -127,7 +152,7 @@ class _TitleBarApp extends StatefulWidget { Key? key, required this.webViewId, required this.titleBarTopPadding, - required this.builder, + this.builder, this.backgroundColor, }) : super(key: key); @@ -135,7 +160,7 @@ class _TitleBarApp extends StatefulWidget { final int titleBarTopPadding; - final WidgetBuilder builder; + final WidgetBuilder? builder; final Color? backgroundColor; @@ -204,7 +229,9 @@ class _TitleBarAppState extends State<_TitleBarApp> canGoBack: _canGoBack, canGoForward: _canGoForward, url: _url, - child: Builder(builder: widget.builder), + child: widget.builder != null + ? Builder(builder: widget.builder!) + : const SizedBox.shrink(), ), ), ), diff --git a/packages/desktop_webview_window/linux/desktop_webview_window_plugin.cc b/packages/desktop_webview_window/linux/desktop_webview_window_plugin.cc index 2cbcb23f..77681c83 100644 --- a/packages/desktop_webview_window/linux/desktop_webview_window_plugin.cc +++ b/packages/desktop_webview_window/linux/desktop_webview_window_plugin.cc @@ -47,7 +47,8 @@ static void webview_window_plugin_handle_method_call( auto height = fl_value_get_int(fl_value_lookup_string(args, "windowHeight")); auto title = fl_value_get_string(fl_value_lookup_string(args, "title")); auto title_bar_height = fl_value_get_int(fl_value_lookup_string(args, "titleBarHeight")); - auto use_full_screen = fl_value_get_int(fl_value_lookup_string(args, "useFullScreen")); + auto use_full_screen = fl_value_get_bool(fl_value_lookup_string(args, "useFullScreen")); + auto disable_title_bar = fl_value_get_bool(fl_value_lookup_string(args, "disableTitleBar")); auto window_id = next_window_id_; g_object_ref(self); @@ -56,7 +57,7 @@ static void webview_window_plugin_handle_method_call( [self, window_id]() { self->windows->erase(window_id); g_object_unref(self); - }, title, width, height, title_bar_height, use_full_screen); + }, title, width, height, title_bar_height, use_full_screen, disable_title_bar); self->windows->insert({window_id, std::move(webview)}); next_window_id_++; fl_method_call_respond_success(method_call, fl_value_new_int(window_id), nullptr); diff --git a/packages/desktop_webview_window/linux/webview_window.cc b/packages/desktop_webview_window/linux/webview_window.cc index 07d0321b..280abad6 100644 --- a/packages/desktop_webview_window/linux/webview_window.cc +++ b/packages/desktop_webview_window/linux/webview_window.cc @@ -57,7 +57,8 @@ WebviewWindow::WebviewWindow( int width, int height, int title_bar_height, - bool use_fullscreen + bool use_fullscreen, + bool disable_title_bar ) : method_channel_(method_channel), window_id_(window_id), on_close_callback_(std::move(on_close_callback)), @@ -90,8 +91,13 @@ WebviewWindow::WebviewWindow( // initial flutter_view g_autoptr(FlDartProject) project = fl_dart_project_new(); - const char *args[] = {"web_view_title_bar", g_strdup_printf("%ld", window_id), nullptr}; - fl_dart_project_set_dart_entrypoint_arguments(project, const_cast(args)); + if (disable_title_bar) { + const char *args[] = {"web_view", g_strdup_printf("%ld", window_id), nullptr}; + fl_dart_project_set_dart_entrypoint_arguments(project, const_cast(args)); + } else { + const char *args[] = {"web_view_title_bar", g_strdup_printf("%ld", window_id), nullptr}; + fl_dart_project_set_dart_entrypoint_arguments(project, const_cast(args)); + } auto *title_bar = fl_view_new(project); g_autoptr(FlPluginRegistrar) desktop_webview_window_registrar = diff --git a/packages/desktop_webview_window/linux/webview_window.h b/packages/desktop_webview_window/linux/webview_window.h index c180e07b..f91b3ff5 100644 --- a/packages/desktop_webview_window/linux/webview_window.h +++ b/packages/desktop_webview_window/linux/webview_window.h @@ -20,7 +20,8 @@ class WebviewWindow { std::function on_close_callback, const std::string &title, int width, int height, int title_bar_height, - bool use_fullscreen + bool use_fullscreen, + bool disable_title_bar ); virtual ~WebviewWindow();