my_application.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "my_application.h"
  2. #include <flutter_linux/flutter_linux.h>
  3. #ifdef GDK_WINDOWING_X11
  4. #include <gdk/gdkx.h>
  5. #endif
  6. #include "flutter/generated_plugin_registrant.h"
  7. struct _MyApplication {
  8. GtkApplication parent_instance;
  9. char** dart_entrypoint_arguments;
  10. };
  11. G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION)
  12. // Called when first Flutter frame received.
  13. static void first_frame_cb(MyApplication* self, FlView* view) {
  14. gtk_widget_show(gtk_widget_get_toplevel(GTK_WIDGET(view)));
  15. }
  16. // Implements GApplication::activate.
  17. static void my_application_activate(GApplication* application) {
  18. MyApplication* self = MY_APPLICATION(application);
  19. GtkWindow* window =
  20. GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application)));
  21. // Use a header bar when running in GNOME as this is the common style used
  22. // by applications and is the setup most users will be using (e.g. Ubuntu
  23. // desktop).
  24. // If running on X and not using GNOME then just use a traditional title bar
  25. // in case the window manager does more exotic layout, e.g. tiling.
  26. // If running on Wayland assume the header bar will work (may need changing
  27. // if future cases occur).
  28. gboolean use_header_bar = TRUE;
  29. #ifdef GDK_WINDOWING_X11
  30. GdkScreen* screen = gtk_window_get_screen(window);
  31. if (GDK_IS_X11_SCREEN(screen)) {
  32. const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen);
  33. if (g_strcmp0(wm_name, "GNOME Shell") != 0) {
  34. use_header_bar = FALSE;
  35. }
  36. }
  37. #endif
  38. if (use_header_bar) {
  39. GtkHeaderBar* header_bar = GTK_HEADER_BAR(gtk_header_bar_new());
  40. gtk_widget_show(GTK_WIDGET(header_bar));
  41. gtk_header_bar_set_title(header_bar, "chicken_farm");
  42. gtk_header_bar_set_show_close_button(header_bar, TRUE);
  43. gtk_window_set_titlebar(window, GTK_WIDGET(header_bar));
  44. } else {
  45. gtk_window_set_title(window, "chicken_farm");
  46. }
  47. gtk_window_set_default_size(window, 1280, 720);
  48. g_autoptr(FlDartProject) project = fl_dart_project_new();
  49. fl_dart_project_set_dart_entrypoint_arguments(
  50. project, self->dart_entrypoint_arguments);
  51. FlView* view = fl_view_new(project);
  52. GdkRGBA background_color;
  53. // Background defaults to black, override it here if necessary, e.g. #00000000
  54. // for transparent.
  55. gdk_rgba_parse(&background_color, "#000000");
  56. fl_view_set_background_color(view, &background_color);
  57. gtk_widget_show(GTK_WIDGET(view));
  58. gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view));
  59. // Show the window when Flutter renders.
  60. // Requires the view to be realized so we can start rendering.
  61. g_signal_connect_swapped(view, "first-frame", G_CALLBACK(first_frame_cb),
  62. self);
  63. gtk_widget_realize(GTK_WIDGET(view));
  64. fl_register_plugins(FL_PLUGIN_REGISTRY(view));
  65. gtk_widget_grab_focus(GTK_WIDGET(view));
  66. }
  67. // Implements GApplication::local_command_line.
  68. static gboolean my_application_local_command_line(GApplication* application,
  69. gchar*** arguments,
  70. int* exit_status) {
  71. MyApplication* self = MY_APPLICATION(application);
  72. // Strip out the first argument as it is the binary name.
  73. self->dart_entrypoint_arguments = g_strdupv(*arguments + 1);
  74. g_autoptr(GError) error = nullptr;
  75. if (!g_application_register(application, nullptr, &error)) {
  76. g_warning("Failed to register: %s", error->message);
  77. *exit_status = 1;
  78. return TRUE;
  79. }
  80. g_application_activate(application);
  81. *exit_status = 0;
  82. return TRUE;
  83. }
  84. // Implements GApplication::startup.
  85. static void my_application_startup(GApplication* application) {
  86. // MyApplication* self = MY_APPLICATION(object);
  87. // Perform any actions required at application startup.
  88. G_APPLICATION_CLASS(my_application_parent_class)->startup(application);
  89. }
  90. // Implements GApplication::shutdown.
  91. static void my_application_shutdown(GApplication* application) {
  92. // MyApplication* self = MY_APPLICATION(object);
  93. // Perform any actions required at application shutdown.
  94. G_APPLICATION_CLASS(my_application_parent_class)->shutdown(application);
  95. }
  96. // Implements GObject::dispose.
  97. static void my_application_dispose(GObject* object) {
  98. MyApplication* self = MY_APPLICATION(object);
  99. g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev);
  100. G_OBJECT_CLASS(my_application_parent_class)->dispose(object);
  101. }
  102. static void my_application_class_init(MyApplicationClass* klass) {
  103. G_APPLICATION_CLASS(klass)->activate = my_application_activate;
  104. G_APPLICATION_CLASS(klass)->local_command_line =
  105. my_application_local_command_line;
  106. G_APPLICATION_CLASS(klass)->startup = my_application_startup;
  107. G_APPLICATION_CLASS(klass)->shutdown = my_application_shutdown;
  108. G_OBJECT_CLASS(klass)->dispose = my_application_dispose;
  109. }
  110. static void my_application_init(MyApplication* self) {}
  111. MyApplication* my_application_new() {
  112. // Set the program name to the application ID, which helps various systems
  113. // like GTK and desktop environments map this running application to its
  114. // corresponding .desktop file. This ensures better integration by allowing
  115. // the application to be recognized beyond its binary name.
  116. g_set_prgname(APPLICATION_ID);
  117. return MY_APPLICATION(g_object_new(my_application_get_type(),
  118. "application-id", APPLICATION_ID, "flags",
  119. G_APPLICATION_NON_UNIQUE, nullptr));
  120. }