my_application.cc 3.8 KB

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