Skip to content

[desktop_webview_window] full screen mode view for linux #150

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

Open
wants to merge 3 commits 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
1 change: 1 addition & 0 deletions packages/desktop_webview_window/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class _MyAppState extends State<MyApp> {
title: "ExampleTestWindow",
titleBarTopPadding: Platform.isMacOS ? 20 : 0,
userDataFolderWindows: await _getWebViewPath(),
useFullScreen: false,
),
);
webview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class CreateConfiguration {
final int titleBarTopPadding;

final String userDataFolderWindows;
final bool useFullScreen;
final bool disableTitleBar;

final bool useWindowPositionAndSize;
final bool openMaximized;
Expand All @@ -29,8 +31,9 @@ class CreateConfiguration {
this.titleBarHeight = 40,
this.titleBarTopPadding = 0,
this.userDataFolderWindows = 'webview_window_WebView2',
this.useFullScreen = false,
this.disableTitleBar = false,
this.useWindowPositionAndSize = false,
this.openMaximized = false,
});

factory CreateConfiguration.platform() {
Expand All @@ -45,10 +48,11 @@ class CreateConfiguration {
"windowPosX": windowPosX,
"windowPosY": windowPosY,
"title": title,
"titleBarHeight": titleBarHeight,
"titleBarTopPadding": titleBarTopPadding,
"titleBarHeight": disableTitleBar ? 0 : titleBarHeight,
"titleBarTopPadding": disableTitleBar ? 0 : titleBarTopPadding,
"userDataFolderWindows": userDataFolderWindows,
"useFullScreen": useFullScreen,
"disableTitleBar": disableTitleBar,
"useWindowPositionAndSize": useWindowPositionAndSize,
"openMaximized": openMaximized,
};
}
39 changes: 33 additions & 6 deletions packages/desktop_webview_window/lib/src/title_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,53 @@ 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<String> args, {
WidgetBuilder? builder,
Color? backgroundColor,
void Function(Object error, StackTrace stack)? onError,
}) =>
runWebViewWidget(
args,
builder: builder,
backgroundColor: backgroundColor,
onError: onError,
);

bool runWebViewWidget(
List<String> 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(
() {
WidgetsFlutterBinding.ensureInitialized();
runApp(_TitleBarApp(
webViewId: webViewId,
titleBarTopPadding: titleBarTopPadding,
backgroundColor: backgroundColor,
builder: builder ?? _defaultTitleBar,
builder: widgetBuilder,
));
},
onError ??
Expand Down Expand Up @@ -128,15 +153,15 @@ class _TitleBarApp extends StatefulWidget {
Key? key,
required this.webViewId,
required this.titleBarTopPadding,
required this.builder,
this.builder,
this.backgroundColor,
}) : super(key: key);

final int webViewId;

final int titleBarTopPadding;

final WidgetBuilder builder;
final WidgetBuilder? builder;

final Color? backgroundColor;

Expand Down Expand Up @@ -205,7 +230,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(),
),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +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_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);
Expand All @@ -55,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);
}, 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);
Expand Down
19 changes: 15 additions & 4 deletions packages/desktop_webview_window/linux/webview_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ WebviewWindow::WebviewWindow(
const std::string &title,
int width,
int height,
int title_bar_height
int title_bar_height,
bool use_fullscreen,
bool disable_title_bar
) : method_channel_(method_channel),
window_id_(window_id),
on_close_callback_(std::move(on_close_callback)),
Expand All @@ -75,17 +77,26 @@ 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));
gtk_container_add(GTK_CONTAINER(window_), GTK_WIDGET(box_));

// 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<char **>(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<char **>(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<char **>(args));
}
auto *title_bar = fl_view_new(project);

g_autoptr(FlPluginRegistrar) desktop_webview_window_registrar =
Expand Down
4 changes: 3 additions & 1 deletion packages/desktop_webview_window/linux/webview_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class WebviewWindow {
int64_t window_id,
std::function<void()> on_close_callback,
const std::string &title, int width, int height,
int title_bar_height
int title_bar_height,
bool use_fullscreen,
bool disable_title_bar
);

virtual ~WebviewWindow();
Expand Down