@@ -14,6 +14,11 @@ struct _MyApplication {
1414
1515G_DEFINE_TYPE (MyApplication, my_application, GTK_TYPE_APPLICATION)
1616
17+ // Called when first Flutter frame received.
18+ static void first_frame_cb(MyApplication* self, FlView* view) {
19+ gtk_widget_show (gtk_widget_get_toplevel (GTK_WIDGET (view)));
20+ }
21+
1722// Implements GApplication::activate.
1823static void my_application_activate (GApplication* application) {
1924 MyApplication* self = MY_APPLICATION (application);
@@ -48,31 +53,44 @@ static void my_application_activate(GApplication* application) {
4853 }
4954
5055 gtk_window_set_default_size (window, 1280 , 720 );
51- gtk_widget_show (GTK_WIDGET (window));
5256
5357 g_autoptr (FlDartProject) project = fl_dart_project_new ();
54- fl_dart_project_set_dart_entrypoint_arguments (project, self->dart_entrypoint_arguments );
58+ fl_dart_project_set_dart_entrypoint_arguments (
59+ project, self->dart_entrypoint_arguments );
5560
5661 FlView* view = fl_view_new (project);
62+ GdkRGBA background_color;
63+ // Background defaults to black, override it here if necessary, e.g. #00000000
64+ // for transparent.
65+ gdk_rgba_parse (&background_color, " #000000" );
66+ fl_view_set_background_color (view, &background_color);
5767 gtk_widget_show (GTK_WIDGET (view));
5868 gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (view));
5969
70+ // Show the window when Flutter renders.
71+ // Requires the view to be realized so we can start rendering.
72+ g_signal_connect_swapped (view, " first-frame" , G_CALLBACK (first_frame_cb),
73+ self);
74+ gtk_widget_realize (GTK_WIDGET (view));
75+
6076 fl_register_plugins (FL_PLUGIN_REGISTRY (view));
6177
6278 gtk_widget_grab_focus (GTK_WIDGET (view));
6379}
6480
6581// Implements GApplication::local_command_line.
66- static gboolean my_application_local_command_line (GApplication* application, gchar*** arguments, int * exit_status) {
82+ static gboolean my_application_local_command_line (GApplication* application,
83+ gchar*** arguments,
84+ int * exit_status) {
6785 MyApplication* self = MY_APPLICATION (application);
6886 // Strip out the first argument as it is the binary name.
6987 self->dart_entrypoint_arguments = g_strdupv (*arguments + 1 );
7088
7189 g_autoptr (GError) error = nullptr ;
7290 if (!g_application_register (application, nullptr , &error)) {
73- g_warning (" Failed to register: %s" , error->message );
74- *exit_status = 1 ;
75- return TRUE ;
91+ g_warning (" Failed to register: %s" , error->message );
92+ *exit_status = 1 ;
93+ return TRUE ;
7694 }
7795
7896 g_application_activate (application);
@@ -81,6 +99,24 @@ static gboolean my_application_local_command_line(GApplication* application, gch
8199 return TRUE ;
82100}
83101
102+ // Implements GApplication::startup.
103+ static void my_application_startup (GApplication* application) {
104+ // MyApplication* self = MY_APPLICATION(object);
105+
106+ // Perform any actions required at application startup.
107+
108+ G_APPLICATION_CLASS (my_application_parent_class)->startup (application);
109+ }
110+
111+ // Implements GApplication::shutdown.
112+ static void my_application_shutdown (GApplication* application) {
113+ // MyApplication* self = MY_APPLICATION(object);
114+
115+ // Perform any actions required at application shutdown.
116+
117+ G_APPLICATION_CLASS (my_application_parent_class)->shutdown (application);
118+ }
119+
84120// Implements GObject::dispose.
85121static void my_application_dispose (GObject* object) {
86122 MyApplication* self = MY_APPLICATION (object);
@@ -90,15 +126,23 @@ static void my_application_dispose(GObject* object) {
90126
91127static void my_application_class_init (MyApplicationClass* klass) {
92128 G_APPLICATION_CLASS (klass)->activate = my_application_activate;
93- G_APPLICATION_CLASS (klass)->local_command_line = my_application_local_command_line;
129+ G_APPLICATION_CLASS (klass)->local_command_line =
130+ my_application_local_command_line;
131+ G_APPLICATION_CLASS (klass)->startup = my_application_startup;
132+ G_APPLICATION_CLASS (klass)->shutdown = my_application_shutdown;
94133 G_OBJECT_CLASS (klass)->dispose = my_application_dispose;
95134}
96135
97136static void my_application_init (MyApplication* self) {}
98137
99138MyApplication* my_application_new () {
139+ // Set the program name to the application ID, which helps various systems
140+ // like GTK and desktop environments map this running application to its
141+ // corresponding .desktop file. This ensures better integration by allowing
142+ // the application to be recognized beyond its binary name.
143+ g_set_prgname (APPLICATION_ID);
144+
100145 return MY_APPLICATION (g_object_new (my_application_get_type (),
101- " application-id" , APPLICATION_ID,
102- " flags" , G_APPLICATION_NON_UNIQUE,
103- nullptr ));
146+ " application-id" , APPLICATION_ID, " flags" ,
147+ G_APPLICATION_NON_UNIQUE, nullptr ));
104148}
0 commit comments