#include "my_application.h" #include #ifdef GDK_WINDOWING_X11 #include #endif #include "flutter/generated_plugin_registrant.h" struct _MyApplication { GtkApplication parent_instance; char** dart_entrypoint_arguments; }; G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) // Implements GApplication::activate. static void my_application_activate(GApplication* application) { MyApplication* self = MY_APPLICATION(application); // Second-instance dispatch path: when the browser opens a wger://app-auth // link while the app is already running, GApplication forwards the call // to the primary instance (via D-Bus) and that instance's activate fires. // Present the existing window instead of spawning a second one; the deep // link itself flows separately through command_line → gtk plugin → app_links. GtkWindow* existing = gtk_application_get_active_window(GTK_APPLICATION(application)); if (existing != nullptr) { gtk_window_present_with_time(existing, GDK_CURRENT_TIME); // Modern Wayland compositors routinely refuse the present() under // focus-stealing prevention, mark the window as needing attention so // the taskbar/dock highlights it and the user can click over. gtk_window_set_urgency_hint(existing, TRUE); return; } GtkWindow* window = GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application))); // Use a header bar when running in GNOME as this is the common style used // by applications and is the setup most users will be using (e.g. Ubuntu // desktop). // If running on X and not using GNOME then just use a traditional title bar // in case the window manager does more exotic layout, e.g. tiling. // If running on Wayland assume the header bar will work (may need changing // if future cases occur). gboolean use_header_bar = TRUE; #ifdef GDK_WINDOWING_X11 GdkScreen* screen = gtk_window_get_screen(window); if (GDK_IS_X11_SCREEN(screen)) { const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen); if (g_strcmp0(wm_name, "GNOME Shell") != 0) { use_header_bar = FALSE; } } #endif if (use_header_bar) { GtkHeaderBar* header_bar = GTK_HEADER_BAR(gtk_header_bar_new()); gtk_widget_show(GTK_WIDGET(header_bar)); gtk_header_bar_set_title(header_bar, "wger"); gtk_header_bar_set_show_close_button(header_bar, TRUE); gtk_window_set_titlebar(window, GTK_WIDGET(header_bar)); } else { gtk_window_set_title(window, "wger"); } gtk_window_set_default_size(window, 1280, 720); gtk_widget_show(GTK_WIDGET(window)); g_autoptr(FlDartProject) project = fl_dart_project_new(); fl_dart_project_set_dart_entrypoint_arguments(project, self->dart_entrypoint_arguments); FlView* view = fl_view_new(project); gtk_widget_show(GTK_WIDGET(view)); gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view)); fl_register_plugins(FL_PLUGIN_REGISTRY(view)); gtk_widget_grab_focus(GTK_WIDGET(view)); } // Implements GApplication::command_line. // // Called on the primary instance for both the initial launch (with the // process's own argv) and any subsequent launch dispatched here via // D-Bus single-instance activation (with that second invocation's argv). // The gtk plugin separately forwards the args to Dart via the // `gtk/application` method channel, which is what app_links_linux listens // on — so a wger://app-auth URI from xdg-open arrives at the Dart side // regardless of whether we were cold- or warm-started. static int my_application_command_line(GApplication* application, GApplicationCommandLine* command_line) { MyApplication* self = MY_APPLICATION(application); gchar** arguments = g_application_command_line_get_arguments(command_line, nullptr); // Strip out the first argument as it is the binary name. g_strfreev(self->dart_entrypoint_arguments); self->dart_entrypoint_arguments = g_strdupv(arguments + 1); g_strfreev(arguments); g_application_activate(application); return 0; } // Implements GApplication::startup. static void my_application_startup(GApplication* application) { //MyApplication* self = MY_APPLICATION(object); // Perform any actions required at application startup. G_APPLICATION_CLASS(my_application_parent_class)->startup(application); } // Implements GApplication::shutdown. static void my_application_shutdown(GApplication* application) { //MyApplication* self = MY_APPLICATION(object); // Perform any actions required at application shutdown. G_APPLICATION_CLASS(my_application_parent_class)->shutdown(application); } // Implements GObject::dispose. static void my_application_dispose(GObject* object) { MyApplication* self = MY_APPLICATION(object); g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev); G_OBJECT_CLASS(my_application_parent_class)->dispose(object); } static void my_application_class_init(MyApplicationClass* klass) { G_APPLICATION_CLASS(klass)->activate = my_application_activate; G_APPLICATION_CLASS(klass)->command_line = my_application_command_line; G_APPLICATION_CLASS(klass)->startup = my_application_startup; G_APPLICATION_CLASS(klass)->shutdown = my_application_shutdown; G_OBJECT_CLASS(klass)->dispose = my_application_dispose; } static void my_application_init(MyApplication* self) {} MyApplication* my_application_new() { // Set the program name to the application ID, which helps various systems // like GTK and desktop environments map this running application to its // corresponding .desktop file. This ensures better integration by allowing // the application to be recognized beyond its binary name. g_set_prgname(APPLICATION_ID); // Single-instance: the application-id namespaces us on the user's session // bus, so subsequent launches dispatch to the running primary instead of // forking a second window. HANDLES_COMMAND_LINE forwards their argv to // the primary's `command-line` signal, which the bundled gtk plugin // relays to Dart for app_links to consume as a deep-link URI. return MY_APPLICATION(g_object_new(my_application_get_type(), "application-id", APPLICATION_ID, "flags", G_APPLICATION_HANDLES_COMMAND_LINE, nullptr)); }