my_application.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. extern bool gIsConnectionManager;
  13. // Implements GApplication::activate.
  14. static void my_application_activate(GApplication* application) {
  15. MyApplication* self = MY_APPLICATION(application);
  16. GtkWindow* window =
  17. GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application)));
  18. gtk_window_set_decorated(window, FALSE);
  19. // try setting icon for rustdesk, which uses the system cache
  20. GtkIconTheme* theme = gtk_icon_theme_get_default();
  21. gint icons[4] = {256, 128, 64, 32};
  22. for (int i = 0; i < 4; i++) {
  23. GdkPixbuf* icon = gtk_icon_theme_load_icon(theme, "rustdesk", icons[i], GTK_ICON_LOOKUP_NO_SVG, NULL);
  24. if (icon != nullptr) {
  25. gtk_window_set_icon(window, icon);
  26. }
  27. }
  28. // Use a header bar when running in GNOME as this is the common style used
  29. // by applications and is the setup most users will be using (e.g. Ubuntu
  30. // desktop).
  31. // If running on X and not using GNOME then just use a traditional title bar
  32. // in case the window manager does more exotic layout, e.g. tiling.
  33. // If running on Wayland assume the header bar will work (may need changing
  34. // if future cases occur).
  35. gboolean use_header_bar = TRUE;
  36. #ifdef GDK_WINDOWING_X11
  37. GdkScreen* screen = gtk_window_get_screen(window);
  38. if (GDK_IS_X11_SCREEN(screen)) {
  39. const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen);
  40. if (g_strcmp0(wm_name, "GNOME Shell") != 0) {
  41. use_header_bar = FALSE;
  42. }
  43. }
  44. #endif
  45. if (use_header_bar) {
  46. GtkHeaderBar* header_bar = GTK_HEADER_BAR(gtk_header_bar_new());
  47. gtk_widget_show(GTK_WIDGET(header_bar));
  48. gtk_header_bar_set_title(header_bar, "rustdesk");
  49. gtk_header_bar_set_show_close_button(header_bar, TRUE);
  50. gtk_window_set_titlebar(window, GTK_WIDGET(header_bar));
  51. } else {
  52. gtk_window_set_title(window, "rustdesk");
  53. }
  54. // auto bdw = bitsdojo_window_from(window); // <--- add this line
  55. // bdw->setCustomFrame(true); // <-- add this line
  56. int width = 800, height = 600;
  57. if (gIsConnectionManager) {
  58. width = 300;
  59. height = 490;
  60. }
  61. gtk_window_set_default_size(window, width, height); // <-- comment this line
  62. gtk_widget_show(GTK_WIDGET(window));
  63. gtk_widget_set_opacity(GTK_WIDGET(window), 0);
  64. g_autoptr(FlDartProject) project = fl_dart_project_new();
  65. fl_dart_project_set_dart_entrypoint_arguments(project, self->dart_entrypoint_arguments);
  66. FlView* view = fl_view_new(project);
  67. gtk_widget_show(GTK_WIDGET(view));
  68. gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view));
  69. fl_register_plugins(FL_PLUGIN_REGISTRY(view));
  70. gtk_widget_grab_focus(GTK_WIDGET(view));
  71. }
  72. // Implements GApplication::local_command_line.
  73. static gboolean my_application_local_command_line(GApplication* application, gchar*** arguments, int* exit_status) {
  74. MyApplication* self = MY_APPLICATION(application);
  75. // Strip out the first argument as it is the binary name.
  76. self->dart_entrypoint_arguments = g_strdupv(*arguments + 1);
  77. g_autoptr(GError) error = nullptr;
  78. if (!g_application_register(application, nullptr, &error)) {
  79. g_warning("Failed to register: %s", error->message);
  80. *exit_status = 1;
  81. return TRUE;
  82. }
  83. g_application_activate(application);
  84. *exit_status = 0;
  85. return TRUE;
  86. }
  87. // Implements GObject::dispose.
  88. static void my_application_dispose(GObject* object) {
  89. MyApplication* self = MY_APPLICATION(object);
  90. g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev);
  91. G_OBJECT_CLASS(my_application_parent_class)->dispose(object);
  92. }
  93. static void my_application_class_init(MyApplicationClass* klass) {
  94. G_APPLICATION_CLASS(klass)->activate = my_application_activate;
  95. G_APPLICATION_CLASS(klass)->local_command_line = my_application_local_command_line;
  96. G_OBJECT_CLASS(klass)->dispose = my_application_dispose;
  97. }
  98. static void my_application_init(MyApplication* self) {}
  99. MyApplication* my_application_new() {
  100. return MY_APPLICATION(g_object_new(my_application_get_type(),
  101. "application-id", APPLICATION_ID,
  102. "flags", G_APPLICATION_NON_UNIQUE,
  103. nullptr));
  104. }