my_application.cc 3.6 KB

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