|
| 1 | +#include "my_application.h" |
| 2 | + |
| 3 | +#include <flutter_linux/flutter_linux.h> |
| 4 | +#ifdef GDK_WINDOWING_X11 |
| 5 | +#include <gdk/gdkx.h> |
| 6 | +#endif |
| 7 | + |
| 8 | +#include "flutter/generated_plugin_registrant.h" |
| 9 | + |
| 10 | +struct _MyApplication { |
| 11 | + GtkApplication parent_instance; |
| 12 | + char** dart_entrypoint_arguments; |
| 13 | +}; |
| 14 | + |
| 15 | +G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) |
| 16 | + |
| 17 | +// Implements GApplication::activate. |
| 18 | +static void my_application_activate(GApplication* application) { |
| 19 | + MyApplication* self = MY_APPLICATION(application); |
| 20 | + GtkWindow* window = |
| 21 | + GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application))); |
| 22 | + |
| 23 | + // Use a header bar when running in GNOME as this is the common style used |
| 24 | + // by applications and is the setup most users will be using (e.g. Ubuntu |
| 25 | + // desktop). |
| 26 | + // If running on X and not using GNOME then just use a traditional title bar |
| 27 | + // in case the window manager does more exotic layout, e.g. tiling. |
| 28 | + // If running on Wayland assume the header bar will work (may need changing |
| 29 | + // if future cases occur). |
| 30 | + gboolean use_header_bar = TRUE; |
| 31 | +#ifdef GDK_WINDOWING_X11 |
| 32 | + GdkScreen *screen = gtk_window_get_screen(window); |
| 33 | + if (GDK_IS_X11_SCREEN(screen)) { |
| 34 | + const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen); |
| 35 | + if (g_strcmp0(wm_name, "GNOME Shell") != 0) { |
| 36 | + use_header_bar = FALSE; |
| 37 | + } |
| 38 | + } |
| 39 | +#endif |
| 40 | + if (use_header_bar) { |
| 41 | + GtkHeaderBar *header_bar = GTK_HEADER_BAR(gtk_header_bar_new()); |
| 42 | + gtk_widget_show(GTK_WIDGET(header_bar)); |
| 43 | + gtk_header_bar_set_title(header_bar, "example"); |
| 44 | + gtk_header_bar_set_show_close_button(header_bar, TRUE); |
| 45 | + gtk_window_set_titlebar(window, GTK_WIDGET(header_bar)); |
| 46 | + } |
| 47 | + else { |
| 48 | + gtk_window_set_title(window, "example"); |
| 49 | + } |
| 50 | + |
| 51 | + gtk_window_set_default_size(window, 1280, 720); |
| 52 | + gtk_widget_show(GTK_WIDGET(window)); |
| 53 | + |
| 54 | + g_autoptr(FlDartProject) project = fl_dart_project_new(); |
| 55 | + fl_dart_project_set_dart_entrypoint_arguments(project, self->dart_entrypoint_arguments); |
| 56 | + |
| 57 | + FlView* view = fl_view_new(project); |
| 58 | + gtk_widget_show(GTK_WIDGET(view)); |
| 59 | + gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view)); |
| 60 | + |
| 61 | + fl_register_plugins(FL_PLUGIN_REGISTRY(view)); |
| 62 | + |
| 63 | + gtk_widget_grab_focus(GTK_WIDGET(view)); |
| 64 | +} |
| 65 | + |
| 66 | +// Implements GApplication::local_command_line. |
| 67 | +static gboolean my_application_local_command_line(GApplication* application, gchar ***arguments, int *exit_status) { |
| 68 | + MyApplication* self = MY_APPLICATION(application); |
| 69 | + // Strip out the first argument as it is the binary name. |
| 70 | + self->dart_entrypoint_arguments = g_strdupv(*arguments + 1); |
| 71 | + |
| 72 | + g_autoptr(GError) error = nullptr; |
| 73 | + if (!g_application_register(application, nullptr, &error)) { |
| 74 | + g_warning("Failed to register: %s", error->message); |
| 75 | + *exit_status = 1; |
| 76 | + return TRUE; |
| 77 | + } |
| 78 | + |
| 79 | + g_application_activate(application); |
| 80 | + *exit_status = 0; |
| 81 | + |
| 82 | + return TRUE; |
| 83 | +} |
| 84 | + |
| 85 | +// Implements GObject::dispose. |
| 86 | +static void my_application_dispose(GObject *object) { |
| 87 | + MyApplication* self = MY_APPLICATION(object); |
| 88 | + g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev); |
| 89 | + G_OBJECT_CLASS(my_application_parent_class)->dispose(object); |
| 90 | +} |
| 91 | + |
| 92 | +static void my_application_class_init(MyApplicationClass* klass) { |
| 93 | + G_APPLICATION_CLASS(klass)->activate = my_application_activate; |
| 94 | + G_APPLICATION_CLASS(klass)->local_command_line = my_application_local_command_line; |
| 95 | + G_OBJECT_CLASS(klass)->dispose = my_application_dispose; |
| 96 | +} |
| 97 | + |
| 98 | +static void my_application_init(MyApplication* self) {} |
| 99 | + |
| 100 | +MyApplication* my_application_new() { |
| 101 | + return MY_APPLICATION(g_object_new(my_application_get_type(), |
| 102 | + "application-id", APPLICATION_ID, |
| 103 | + nullptr)); |
| 104 | +} |
0 commit comments