creek

The AI Image Editor of 2030

linux/runner/my_application.cc (4761B)


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