askrobots / network-computer-flutter
Public
One Flutter client for network-computer: drive a remote Linux/Mac desktop from Android, iPhone, iPad, or desktop over WebRTC.
Dan Quellhorst
No colour (compare) in ⋯: the desk's picture in grey on this device
c74471a
1 month, 3 weeks ago
History
my_application.cc · 5.2 KB · 144 lines
| 1 | #include "my_application.h" |
| 2 | |
| 3 | #include <flutter_linux/flutter_linux.h> |
| 4 | #ifdef GDK_WINDOWING_X11 |
| 5 | #include <gdk/gdkx.h> |
| 6 | #endif |
| 7 | |
| 8 | #include "flutter/generated_plugin_registrant.h" |
| 9 | |
| 10 | struct _MyApplication { |
| 11 | GtkApplication parent_instance; |
| 12 | char** dart_entrypoint_arguments; |
| 13 | }; |
| 14 | |
| 15 | G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) |
| 16 | |
| 17 | // Called when first Flutter frame received. |
| 18 | static void first_frame_cb(MyApplication* self, FlView *view) |
| 19 | { |
| 20 | gtk_widget_show(gtk_widget_get_toplevel(GTK_WIDGET(view))); |
| 21 | } |
| 22 | |
| 23 | // Implements GApplication::activate. |
| 24 | static void my_application_activate(GApplication* application) { |
| 25 | MyApplication* self = MY_APPLICATION(application); |
| 26 | GtkWindow* window = |
| 27 | GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application))); |
| 28 | |
| 29 | // Use a header bar when running in GNOME as this is the common style used |
| 30 | // by applications and is the setup most users will be using (e.g. Ubuntu |
| 31 | // desktop). |
| 32 | // If running on X and not using GNOME then just use a traditional title bar |
| 33 | // in case the window manager does more exotic layout, e.g. tiling. |
| 34 | // If running on Wayland assume the header bar will work (may need changing |
| 35 | // if future cases occur). |
| 36 | gboolean use_header_bar = TRUE; |
| 37 | #ifdef GDK_WINDOWING_X11 |
| 38 | GdkScreen* screen = gtk_window_get_screen(window); |
| 39 | if (GDK_IS_X11_SCREEN(screen)) { |
| 40 | const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen); |
| 41 | if (g_strcmp0(wm_name, "GNOME Shell") != 0) { |
| 42 | use_header_bar = FALSE; |
| 43 | } |
| 44 | } |
| 45 | #endif |
| 46 | if (use_header_bar) { |
| 47 | GtkHeaderBar* header_bar = GTK_HEADER_BAR(gtk_header_bar_new()); |
| 48 | gtk_widget_show(GTK_WIDGET(header_bar)); |
| 49 | gtk_header_bar_set_title(header_bar, "network_computer"); |
| 50 | gtk_header_bar_set_show_close_button(header_bar, TRUE); |
| 51 | gtk_window_set_titlebar(window, GTK_WIDGET(header_bar)); |
| 52 | } else { |
| 53 | gtk_window_set_title(window, "network_computer"); |
| 54 | } |
| 55 | |
| 56 | gtk_window_set_default_size(window, 1280, 720); |
| 57 | |
| 58 | g_autoptr(FlDartProject) project = fl_dart_project_new(); |
| 59 | fl_dart_project_set_dart_entrypoint_arguments(project, self->dart_entrypoint_arguments); |
| 60 | |
| 61 | FlView* view = fl_view_new(project); |
| 62 | GdkRGBA background_color; |
| 63 | // Background defaults to black, override it here if necessary, e.g. #00000000 for transparent. |
| 64 | gdk_rgba_parse(&background_color, "#000000"); |
| 65 | fl_view_set_background_color(view, &background_color); |
| 66 | gtk_widget_show(GTK_WIDGET(view)); |
| 67 | gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view)); |
| 68 | |
| 69 | // Show the window when Flutter renders. |
| 70 | // Requires the view to be realized so we can start rendering. |
| 71 | g_signal_connect_swapped(view, "first-frame", G_CALLBACK(first_frame_cb), self); |
| 72 | gtk_widget_realize(GTK_WIDGET(view)); |
| 73 | |
| 74 | fl_register_plugins(FL_PLUGIN_REGISTRY(view)); |
| 75 | |
| 76 | gtk_widget_grab_focus(GTK_WIDGET(view)); |
| 77 | } |
| 78 | |
| 79 | // Implements GApplication::local_command_line. |
| 80 | static gboolean my_application_local_command_line(GApplication* application, gchar*** arguments, int* exit_status) { |
| 81 | MyApplication* self = MY_APPLICATION(application); |
| 82 | // Strip out the first argument as it is the binary name. |
| 83 | self->dart_entrypoint_arguments = g_strdupv(*arguments + 1); |
| 84 | |
| 85 | g_autoptr(GError) error = nullptr; |
| 86 | if (!g_application_register(application, nullptr, &error)) { |
| 87 | g_warning("Failed to register: %s", error->message); |
| 88 | *exit_status = 1; |
| 89 | return TRUE; |
| 90 | } |
| 91 | |
| 92 | g_application_activate(application); |
| 93 | *exit_status = 0; |
| 94 | |
| 95 | return TRUE; |
| 96 | } |
| 97 | |
| 98 | // Implements GApplication::startup. |
| 99 | static void my_application_startup(GApplication* application) { |
| 100 | //MyApplication* self = MY_APPLICATION(object); |
| 101 | |
| 102 | // Perform any actions required at application startup. |
| 103 | |
| 104 | G_APPLICATION_CLASS(my_application_parent_class)->startup(application); |
| 105 | } |
| 106 | |
| 107 | // Implements GApplication::shutdown. |
| 108 | static void my_application_shutdown(GApplication* application) { |
| 109 | //MyApplication* self = MY_APPLICATION(object); |
| 110 | |
| 111 | // Perform any actions required at application shutdown. |
| 112 | |
| 113 | G_APPLICATION_CLASS(my_application_parent_class)->shutdown(application); |
| 114 | } |
| 115 | |
| 116 | // Implements GObject::dispose. |
| 117 | static void my_application_dispose(GObject* object) { |
| 118 | MyApplication* self = MY_APPLICATION(object); |
| 119 | g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev); |
| 120 | G_OBJECT_CLASS(my_application_parent_class)->dispose(object); |
| 121 | } |
| 122 | |
| 123 | static void my_application_class_init(MyApplicationClass* klass) { |
| 124 | G_APPLICATION_CLASS(klass)->activate = my_application_activate; |
| 125 | G_APPLICATION_CLASS(klass)->local_command_line = my_application_local_command_line; |
| 126 | G_APPLICATION_CLASS(klass)->startup = my_application_startup; |
| 127 | G_APPLICATION_CLASS(klass)->shutdown = my_application_shutdown; |
| 128 | G_OBJECT_CLASS(klass)->dispose = my_application_dispose; |
| 129 | } |
| 130 | |
| 131 | static void my_application_init(MyApplication* self) {} |
| 132 | |
| 133 | MyApplication* my_application_new() { |
| 134 | // Set the program name to the application ID, which helps various systems |
| 135 | // like GTK and desktop environments map this running application to its |
| 136 | // corresponding .desktop file. This ensures better integration by allowing |
| 137 | // the application to be recognized beyond its binary name. |
| 138 | g_set_prgname(APPLICATION_ID); |
| 139 | |
| 140 | return MY_APPLICATION(g_object_new(my_application_get_type(), |
| 141 | "application-id", APPLICATION_ID, |
| 142 | "flags", G_APPLICATION_NON_UNIQUE, |
| 143 | nullptr)); |
| 144 | } |