my_application.cc 3.6 KB

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