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, "agora_rtc_engine_example");
  38. gtk_header_bar_set_show_close_button(header_bar, TRUE);
  39. gtk_window_set_titlebar(window, GTK_WIDGET(header_bar));
  40. }
  41. else {
  42. gtk_window_set_title(window, "agora_rtc_engine_example");
  43. }
  44. gtk_window_set_default_size(window, 1280, 720);
  45. gtk_widget_show(GTK_WIDGET(window));
  46. g_autoptr(FlDartProject) project = fl_dart_project_new();
  47. fl_dart_project_set_dart_entrypoint_arguments(project, self->dart_entrypoint_arguments);
  48. FlView* view = fl_view_new(project);
  49. gtk_widget_show(GTK_WIDGET(view));
  50. gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view));
  51. fl_register_plugins(FL_PLUGIN_REGISTRY(view));
  52. gtk_widget_grab_focus(GTK_WIDGET(view));
  53. }
  54. // Implements GApplication::local_command_line.
  55. static gboolean my_application_local_command_line(GApplication* application, gchar ***arguments, int *exit_status) {
  56. MyApplication* self = MY_APPLICATION(application);
  57. // Strip out the first argument as it is the binary name.
  58. self->dart_entrypoint_arguments = g_strdupv(*arguments + 1);
  59. g_autoptr(GError) error = nullptr;
  60. if (!g_application_register(application, nullptr, &error)) {
  61. g_warning("Failed to register: %s", error->message);
  62. *exit_status = 1;
  63. return TRUE;
  64. }
  65. g_application_activate(application);
  66. *exit_status = 0;
  67. return TRUE;
  68. }
  69. // Implements GObject::dispose.
  70. static void my_application_dispose(GObject *object) {
  71. MyApplication* self = MY_APPLICATION(object);
  72. g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev);
  73. G_OBJECT_CLASS(my_application_parent_class)->dispose(object);
  74. }
  75. static void my_application_class_init(MyApplicationClass* klass) {
  76. G_APPLICATION_CLASS(klass)->activate = my_application_activate;
  77. G_APPLICATION_CLASS(klass)->local_command_line = my_application_local_command_line;
  78. G_OBJECT_CLASS(klass)->dispose = my_application_dispose;
  79. }
  80. static void my_application_init(MyApplication* self) {}
  81. MyApplication* my_application_new() {
  82. return MY_APPLICATION(g_object_new(my_application_get_type(),
  83. "application-id", APPLICATION_ID,
  84. nullptr));
  85. }