Flutter Linux Embedder
fl_application.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <gtk/gtk.h>
8 #ifdef GDK_WINDOWING_X11
9 #include <gdk/gdkx.h>
10 #endif
11 
16 
18  // Arguments to pass to Dart.
20 };
21 
22 #define FL_APPLICATION_GET_PRIVATE(app) \
23  ((FlApplicationPrivate*)fl_application_get_instance_private( \
24  FL_APPLICATION(app)))
25 
27 
29 
31  fl_application,
32  GTK_TYPE_APPLICATION,
33  G_ADD_PRIVATE(FlApplication))
34 
35 // Called when the first frame is received.
36 static void first_frame_cb(FlApplication* self, FlView* view) {
37  GtkWidget* window = gtk_widget_get_toplevel(GTK_WIDGET(view));
38 
39  // Show the main window.
40  if (window != nullptr && GTK_IS_WINDOW(window)) {
41  gtk_window_present(GTK_WINDOW(window));
42  }
43 }
44 
45 // Default implementation of FlApplication::register_plugins
46 static void fl_application_register_plugins(FlApplication* self,
47  FlPluginRegistry* registry) {}
48 
49 // Default implementation of FlApplication::create_window
50 static GtkWindow* fl_application_create_window(FlApplication* self,
51  FlView* view) {
52  GtkApplicationWindow* window =
53  GTK_APPLICATION_WINDOW(gtk_application_window_new(GTK_APPLICATION(self)));
54 
55  // Use a header bar when running in GNOME as this is the common style used
56  // by applications and is the setup most users will be using (e.g. Ubuntu
57  // desktop).
58  // If running on X and not using GNOME then just use a traditional title bar
59  // in case the window manager does more exotic layout, e.g. tiling.
60  // If running on Wayland assume the header bar will work (may need changing
61  // if future cases occur).
62  gboolean use_header_bar = TRUE;
63 #ifdef GDK_WINDOWING_X11
64  GdkScreen* screen = gtk_window_get_screen(GTK_WINDOW(window));
65  if (GDK_IS_X11_SCREEN(screen)) {
66  const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen);
67  if (g_strcmp0(wm_name, "GNOME Shell") != 0) {
68  use_header_bar = FALSE;
69  }
70  }
71 #endif
72  if (use_header_bar) {
73  GtkHeaderBar* header_bar = GTK_HEADER_BAR(gtk_header_bar_new());
74  gtk_widget_show(GTK_WIDGET(header_bar));
75  gtk_header_bar_set_show_close_button(header_bar, TRUE);
76  gtk_window_set_titlebar(GTK_WINDOW(window), GTK_WIDGET(header_bar));
77  }
78 
79  gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view));
80 
81  return GTK_WINDOW(window);
82 }
83 
84 // Implements GApplication::activate.
85 static void fl_application_activate(GApplication* application) {
86  FlApplication* self = FL_APPLICATION(application);
88 
89  g_autoptr(FlDartProject) project = fl_dart_project_new();
91  project, priv->dart_entrypoint_arguments);
92 
93  FlView* view = fl_view_new(project);
94  g_signal_connect_swapped(view, "first-frame", G_CALLBACK(first_frame_cb),
95  self);
96  gtk_widget_show(GTK_WIDGET(view));
97 
98  GtkWindow* window;
99  g_signal_emit(self, fl_application_signals[SIGNAL_CREATE_WINDOW], 0, view,
100  &window);
101 
102  // Make the resources for the view so rendering can start.
103  // We'll show the view when we have the first frame.
104  gtk_widget_realize(GTK_WIDGET(view));
105 
106  g_signal_emit(self, fl_application_signals[SIGNAL_REGISTER_PLUGINS], 0,
107  FL_PLUGIN_REGISTRY(view));
108 }
109 
110 // Implements GApplication::local_command_line.
111 static gboolean fl_application_local_command_line(GApplication* application,
112  gchar*** arguments,
113  int* exit_status) {
114  FlApplication* self = FL_APPLICATION(application);
116 
117  // Strip out the first argument as it is the binary name.
118  priv->dart_entrypoint_arguments = g_strdupv(*arguments + 1);
119 
120  g_autoptr(GError) error = nullptr;
121  if (!g_application_register(application, nullptr, &error)) {
122  g_warning("Failed to register: %s", error->message);
123  *exit_status = 1;
124  return TRUE;
125  }
126 
127  // This will only run on the primary instance or this instance with
128  // G_APPLICATION_NON_UNIQUE
129  g_application_activate(application);
130  *exit_status = 0;
131 
132  return TRUE;
133 }
134 
135 // Implements GObject::dispose.
136 static void fl_application_dispose(GObject* object) {
137  FlApplication* self = FL_APPLICATION(object);
139 
140  g_clear_pointer(&priv->dart_entrypoint_arguments, g_strfreev);
141 
142  G_OBJECT_CLASS(fl_application_parent_class)->dispose(object);
143 }
144 
145 static void fl_application_class_init(FlApplicationClass* klass) {
146  G_APPLICATION_CLASS(klass)->activate = fl_application_activate;
147  G_APPLICATION_CLASS(klass)->local_command_line =
149  G_OBJECT_CLASS(klass)->dispose = fl_application_dispose;
150 
151  klass->register_plugins = fl_application_register_plugins;
152  klass->create_window = fl_application_create_window;
153 
155  "register-plugins", fl_application_get_type(), G_SIGNAL_RUN_LAST,
156  G_STRUCT_OFFSET(FlApplicationClass, register_plugins), nullptr, nullptr,
157  nullptr, G_TYPE_NONE, 1, fl_plugin_registry_get_type());
159  "create-window", fl_application_get_type(), G_SIGNAL_RUN_LAST,
160  G_STRUCT_OFFSET(FlApplicationClass, create_window),
161  g_signal_accumulator_first_wins, nullptr, nullptr, GTK_TYPE_WINDOW, 1,
162  fl_view_get_type());
163 }
164 
165 static void fl_application_init(FlApplication* self) {}
166 
167 G_MODULE_EXPORT
168 FlApplication* fl_application_new(const gchar* application_id,
169  GApplicationFlags flags) {
170  return FL_APPLICATION(g_object_new(fl_application_get_type(),
171  "application-id", application_id, "flags",
172  flags, nullptr));
173 }
g_autoptr(FlEngine) engine
FlAccessibilityHandlerPrivate * priv
FlView * view
static void fl_application_dispose(GObject *object)
static void fl_application_init(FlApplication *self)
static guint fl_application_signals[LAST_SIGNAL]
static gboolean fl_application_local_command_line(GApplication *application, gchar ***arguments, int *exit_status)
static GtkWindow * fl_application_create_window(FlApplication *self, FlView *view)
G_MODULE_EXPORT FlApplication * fl_application_new(const gchar *application_id, GApplicationFlags flags)
#define FL_APPLICATION_GET_PRIVATE(app)
static void fl_application_register_plugins(FlApplication *self, FlPluginRegistry *registry)
static void fl_application_class_init(FlApplicationClass *klass)
@ LAST_SIGNAL
@ SIGNAL_CREATE_WINDOW
@ SIGNAL_REGISTER_PLUGINS
static void fl_application_activate(GApplication *application)
G_DEFINE_TYPE_WITH_CODE(FlApplication, fl_application, GTK_TYPE_APPLICATION, G_ADD_PRIVATE(FlApplication)) static void first_frame_cb(FlApplication *self
return TRUE
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()
G_MODULE_EXPORT void fl_dart_project_set_dart_entrypoint_arguments(FlDartProject *self, char **argv)
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT FlView * fl_view_new(FlDartProject *project)
Definition: fl_view.cc:763
gchar ** dart_entrypoint_arguments