This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Make FlApplication class #54637
Merged
Merged
Make FlApplication class #54637
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
280cc52
Move the FlApplication from the template into the linux embedder.
robert-ancell 8376a68
Update license
robert-ancell 34c05aa
Fix code style for signal enum
robert-ancell 457bf8c
Add a test
robert-ancell 49d05c6
Remove mock file
robert-ancell 66aafb8
Fix ordering of includes
robert-ancell 66c3c9f
Update shell/platform/linux/public/flutter_linux/fl_application.h
robert-ancell e7d5adc
Merge remote-tracking branch 'origin/main' into linux-application
robert-ancell ef9c4c5
Remove default window properties and use the create-window signal for…
robert-ancell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "flutter/shell/platform/linux/public/flutter_linux/fl_application.h" | ||
| #include "flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h" | ||
| #include "flutter/shell/platform/linux/public/flutter_linux/fl_plugin_registry.h" | ||
| #include "flutter/shell/platform/linux/public/flutter_linux/fl_view.h" | ||
|
|
||
| #include <gtk/gtk.h> | ||
|
|
||
| #ifdef GDK_WINDOWING_X11 | ||
| #include <gdk/gdkx.h> | ||
| #endif | ||
|
|
||
| struct FlApplicationPrivate { | ||
| // Default window title to use. | ||
| gchar* window_title; | ||
|
|
||
| // Arguments to pass to Dart. | ||
| gchar** dart_entrypoint_arguments; | ||
|
|
||
| // Default width of a Flutter window in pixels. | ||
| int window_width; | ||
|
|
||
| // Default height of a Flutter window in pixels. | ||
| int window_height; | ||
| }; | ||
|
|
||
| #define FL_APPLICATION_GET_PRIVATE(app) \ | ||
| ((FlApplicationPrivate*)fl_application_get_instance_private( \ | ||
| FL_APPLICATION(app))) | ||
|
|
||
| enum { kSignalRegisterPlugins, kSignalLastSignal }; | ||
|
|
||
| static guint fl_application_signals[kSignalLastSignal]; | ||
|
|
||
| G_DEFINE_TYPE_WITH_CODE(FlApplication, | ||
| fl_application, | ||
| GTK_TYPE_APPLICATION, | ||
| G_ADD_PRIVATE(FlApplication)) | ||
|
|
||
| // Default implementation of FlApplication::register_plugins | ||
| static void fl_application_register_plugins(FlApplication* self, | ||
| FlPluginRegistry* registry) {} | ||
|
|
||
| // Implements GApplication::activate. | ||
| static void fl_application_activate(GApplication* application) { | ||
| FlApplication* self = FL_APPLICATION(application); | ||
| FlApplicationPrivate* priv = FL_APPLICATION_GET_PRIVATE(self); | ||
|
|
||
| GtkApplicationWindow* window = | ||
| GTK_APPLICATION_WINDOW(gtk_application_window_new(GTK_APPLICATION(self))); | ||
|
|
||
| // Use a header bar when running in GNOME as this is the common style used | ||
| // by applications and is the setup most users will be using (e.g. Ubuntu | ||
| // desktop). | ||
| // If running on X and not using GNOME then just use a traditional title bar | ||
| // in case the window manager does more exotic layout, e.g. tiling. | ||
| // If running on Wayland assume the header bar will work (may need changing | ||
| // if future cases occur). | ||
| gboolean use_header_bar = TRUE; | ||
| #ifdef GDK_WINDOWING_X11 | ||
| GdkScreen* screen = gtk_window_get_screen(GTK_WINDOW(window)); | ||
| if (GDK_IS_X11_SCREEN(screen)) { | ||
| const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen); | ||
| if (g_strcmp0(wm_name, "GNOME Shell") != 0) { | ||
| use_header_bar = FALSE; | ||
| } | ||
| } | ||
| #endif | ||
| if (use_header_bar) { | ||
| GtkHeaderBar* header_bar = GTK_HEADER_BAR(gtk_header_bar_new()); | ||
| gtk_widget_show(GTK_WIDGET(header_bar)); | ||
| gtk_header_bar_set_title(header_bar, priv->window_title); | ||
| gtk_header_bar_set_show_close_button(header_bar, TRUE); | ||
| gtk_window_set_titlebar(GTK_WINDOW(window), GTK_WIDGET(header_bar)); | ||
| } else { | ||
| gtk_window_set_title(GTK_WINDOW(window), priv->window_title); | ||
| } | ||
|
|
||
| gtk_window_set_default_size(GTK_WINDOW(window), priv->window_width, | ||
| priv->window_height); | ||
| gtk_widget_show(GTK_WIDGET(window)); | ||
|
|
||
| g_autoptr(FlDartProject) project = fl_dart_project_new(); | ||
| fl_dart_project_set_dart_entrypoint_arguments( | ||
| project, priv->dart_entrypoint_arguments); | ||
|
|
||
| FlView* view = fl_view_new(project); | ||
robert-ancell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| gtk_widget_show(GTK_WIDGET(view)); | ||
| gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view)); | ||
|
|
||
| g_signal_emit(self, fl_application_signals[kSignalRegisterPlugins], 0, | ||
| FL_PLUGIN_REGISTRY(view)); | ||
robert-ancell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| gtk_widget_grab_focus(GTK_WIDGET(view)); | ||
| } | ||
|
|
||
| // Implements GApplication::local_command_line. | ||
| static gboolean fl_application_local_command_line(GApplication* application, | ||
| gchar*** arguments, | ||
| int* exit_status) { | ||
robert-ancell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| FlApplication* self = FL_APPLICATION(application); | ||
| FlApplicationPrivate* priv = FL_APPLICATION_GET_PRIVATE(self); | ||
|
|
||
| // Strip out the first argument as it is the binary name. | ||
| priv->dart_entrypoint_arguments = g_strdupv(*arguments + 1); | ||
|
|
||
| g_autoptr(GError) error = nullptr; | ||
| if (!g_application_register(application, nullptr, &error)) { | ||
| g_warning("Failed to register: %s", error->message); | ||
| *exit_status = 1; | ||
| return TRUE; | ||
| } | ||
|
|
||
| // This will only run on the primary instance or this instance with | ||
| // G_APPLICATION_NON_UNIQUE | ||
| g_application_activate(application); | ||
| *exit_status = 0; | ||
|
|
||
| return TRUE; | ||
| } | ||
|
|
||
| // Implements GObject::dispose. | ||
| static void fl_application_dispose(GObject* object) { | ||
| FlApplication* self = FL_APPLICATION(object); | ||
| FlApplicationPrivate* priv = FL_APPLICATION_GET_PRIVATE(self); | ||
|
|
||
| g_clear_pointer(&priv->window_title, g_free); | ||
| g_clear_pointer(&priv->dart_entrypoint_arguments, g_strfreev); | ||
|
|
||
| G_OBJECT_CLASS(fl_application_parent_class)->dispose(object); | ||
| } | ||
|
|
||
| static void fl_application_class_init(FlApplicationClass* klass) { | ||
| G_APPLICATION_CLASS(klass)->activate = fl_application_activate; | ||
| G_APPLICATION_CLASS(klass)->local_command_line = | ||
| fl_application_local_command_line; | ||
| G_OBJECT_CLASS(klass)->dispose = fl_application_dispose; | ||
|
|
||
| klass->register_plugins = fl_application_register_plugins; | ||
|
|
||
| fl_application_signals[kSignalRegisterPlugins] = g_signal_new( | ||
| "register-plugins", fl_application_get_type(), G_SIGNAL_RUN_LAST, | ||
| G_STRUCT_OFFSET(FlApplicationClass, register_plugins), NULL, NULL, | ||
| g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, | ||
| fl_plugin_registry_get_type()); | ||
| } | ||
|
|
||
| static void fl_application_init(FlApplication* self) { | ||
| FlApplicationPrivate* priv = FL_APPLICATION_GET_PRIVATE(self); | ||
| priv->window_title = g_strdup(""); | ||
| priv->window_width = 1280; | ||
| priv->window_height = 720; | ||
| } | ||
|
|
||
| G_MODULE_EXPORT | ||
| FlApplication* fl_application_new(const gchar* application_id, | ||
| GApplicationFlags flags) { | ||
| return FL_APPLICATION(g_object_new(fl_application_get_type(), | ||
| "application-id", application_id, "flags", | ||
| flags, nullptr)); | ||
| } | ||
|
|
||
| G_MODULE_EXPORT | ||
| void fl_application_set_default_window_title(FlApplication* self, | ||
| const gchar* window_title) { | ||
| g_return_if_fail(FL_IS_APPLICATION(self)); | ||
| FlApplicationPrivate* priv = FL_APPLICATION_GET_PRIVATE(self); | ||
| g_free(priv->window_title); | ||
| priv->window_title = g_strdup(window_title); | ||
| } | ||
|
|
||
| G_MODULE_EXPORT | ||
| void fl_application_set_default_window_size(FlApplication* self, | ||
| int width, | ||
| int height) { | ||
| g_return_if_fail(FL_IS_APPLICATION(self)); | ||
| FlApplicationPrivate* priv = FL_APPLICATION_GET_PRIVATE(self); | ||
| priv->window_width = width; | ||
| priv->window_height = height; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| #include "flutter/shell/platform/linux/public/flutter_linux/fl_application.h" | ||
|
|
||
| TEST(FlApplicationTest, ConstructorArgs) { | ||
| g_autoptr(FlApplication) app = fl_application_new( | ||
| "com.example.TestApplication", G_APPLICATION_FLAGS_NONE); | ||
|
|
||
| EXPECT_STREQ(g_application_get_application_id(G_APPLICATION(app)), | ||
| "com.example.TestApplication"); | ||
| EXPECT_EQ(g_application_get_flags(G_APPLICATION(app)), | ||
| G_APPLICATION_FLAGS_NONE); | ||
| } |
89 changes: 89 additions & 0 deletions
89
shell/platform/linux/public/flutter_linux/fl_application.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_SHELL_PLATFORM_LINUX_PUBLIC_FLUTTER_LINUX_FL_APPLICATION_H_ | ||
| #define FLUTTER_SHELL_PLATFORM_LINUX_PUBLIC_FLUTTER_LINUX_FL_APPLICATION_H_ | ||
|
|
||
| #if !defined(__FLUTTER_LINUX_INSIDE__) && !defined(FLUTTER_LINUX_COMPILATION) | ||
| #error "Only <flutter_linux/flutter_linux.h> can be included directly." | ||
| #endif | ||
|
|
||
| #include "fl_plugin_registry.h" | ||
| #include "fl_view.h" | ||
|
|
||
| #include <gmodule.h> | ||
| #include <gtk/gtk.h> | ||
|
|
||
| G_BEGIN_DECLS | ||
|
|
||
| G_MODULE_EXPORT | ||
| G_DECLARE_DERIVABLE_TYPE(FlApplication, | ||
| fl_application, | ||
| FL, | ||
| APPLICATION, | ||
| GtkApplication); | ||
|
|
||
| /** | ||
| * FlApplicationClass: | ||
| * @register_plugins: invoked when plugins should be registered. | ||
| */ | ||
| struct _FlApplicationClass { | ||
| GtkApplicationClass parent_class; | ||
|
|
||
| /** | ||
| * FlApplication::register_plugins: | ||
| * @application: the application | ||
| * @registry: registry to use. | ||
| * | ||
| * The ::register_plugins signal is emitted when plugins can be registered. | ||
| */ | ||
| void (*register_plugins)(FlApplication* application, | ||
| FlPluginRegistry* registry); | ||
| }; | ||
|
|
||
| /** | ||
| * FlApplication: | ||
| * | ||
| * #Flutter-based application with the GTK embedder. | ||
| * | ||
| * Provides default behaviour for basic Flutter applications. | ||
| */ | ||
|
|
||
| /** | ||
| * fl_application_new: | ||
| * @application_id: (allow-none): The application ID or %NULL. | ||
| * @flags: The application flags. | ||
| * | ||
| * Creates a new Flutter-based application. | ||
| * | ||
| * Returns: a new #FlApplication | ||
| */ | ||
| FlApplication* fl_application_new(const gchar* application_id, | ||
| GApplicationFlags flags); | ||
robert-ancell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * fl_application_set_default_window_title: | ||
| * @application: an #FlApplication. | ||
| * @window_title: window title text. | ||
| * | ||
| * Sets the title to apply to Flutter windows. | ||
robert-ancell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
robert-ancell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| void fl_application_set_default_window_title(FlApplication* application, | ||
| const gchar* window_title); | ||
|
|
||
| /** | ||
| * fl_application_set_default_window_size: | ||
| * @application: an #FlApplication. | ||
| * @width: width in pixels or -1 to set the default width. | ||
| * @height: height in pixels or -1 to set the default height. | ||
robert-ancell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
robert-ancell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * Sets the dimensions to apply to Flutter windows. | ||
| */ | ||
| void fl_application_set_default_window_size(FlApplication* application, | ||
| int width, | ||
| int height); | ||
|
|
||
| G_END_DECLS | ||
|
|
||
| #endif // FLUTTER_SHELL_PLATFORM_LINUX_PUBLIC_FLUTTER_LINUX_FL_APPLICATION_H_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.