my_application.cc 5.2 KB

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