2024-04-30 23:38:49 +08:00
|
|
|
#include "my_application.h"
|
|
|
|
|
|
|
|
|
|
#include <flutter_linux/flutter_linux.h>
|
|
|
|
|
#ifdef GDK_WINDOWING_X11
|
|
|
|
|
#include <gdk/gdkx.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-09-25 03:56:56 +08:00
|
|
|
#include <window/window_plugin.h>
|
2026-09-05 20:09:35 +08:00
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
#include "flutter/generated_plugin_registrant.h"
|
|
|
|
|
|
|
|
|
|
struct _MyApplication {
|
|
|
|
|
GtkApplication parent_instance;
|
|
|
|
|
char** dart_entrypoint_arguments;
|
2026-09-05 20:09:35 +08:00
|
|
|
gboolean had_window_before_emit;
|
2024-04-30 23:38:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION)
|
|
|
|
|
|
|
|
|
|
// Implements GApplication::activate.
|
|
|
|
|
static void my_application_activate(GApplication* application) {
|
|
|
|
|
MyApplication* self = MY_APPLICATION(application);
|
2026-09-05 20:09:35 +08:00
|
|
|
if (self->had_window_before_emit) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
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, "FlClash");
|
|
|
|
|
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, "FlClash");
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 20:09:35 +08:00
|
|
|
gtk_window_set_default_size(window, 680, 580);
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2026-10-08 16:09:04 +08:00
|
|
|
// X11 panels that do not match the window to a desktop entry read its icon;
|
|
|
|
|
// GDK leaves one over 512x512 out of _NET_WM_ICON without a word.
|
|
|
|
|
g_autofree gchar* executable = g_file_read_link("/proc/self/exe", nullptr);
|
|
|
|
|
if (executable != nullptr) {
|
|
|
|
|
g_autofree gchar* directory = g_path_get_dirname(executable);
|
|
|
|
|
g_autofree gchar* path = g_build_filename(
|
|
|
|
|
directory, "data", "flutter_assets", "assets", "images", "icon.png",
|
|
|
|
|
nullptr);
|
|
|
|
|
g_autoptr(GdkPixbuf) icon =
|
|
|
|
|
gdk_pixbuf_new_from_file_at_size(path, 256, 256, nullptr);
|
|
|
|
|
if (icon != nullptr) {
|
|
|
|
|
gtk_window_set_icon(window, icon);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
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);
|
2025-12-16 11:23:09 +08:00
|
|
|
GdkRGBA background_color;
|
|
|
|
|
// Background defaults to black, override it here if necessary, e.g. #00000000 for transparent.
|
|
|
|
|
gdk_rgba_parse(&background_color, "#000000");
|
|
|
|
|
fl_view_set_background_color(view, &background_color);
|
2024-04-30 23:38:49 +08:00
|
|
|
gtk_widget_show(GTK_WIDGET(view));
|
|
|
|
|
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view));
|
|
|
|
|
|
2026-03-26 03:29:55 +08:00
|
|
|
// Realize the view so Flutter can render even if the window stays hidden
|
|
|
|
|
// until Dart startup logic explicitly shows it.
|
2025-12-16 11:23:09 +08:00
|
|
|
gtk_widget_realize(GTK_WIDGET(view));
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
fl_register_plugins(FL_PLUGIN_REGISTRY(view));
|
|
|
|
|
|
|
|
|
|
gtk_widget_grab_focus(GTK_WIDGET(view));
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 20:09:35 +08:00
|
|
|
// Implements GApplication::command_line.
|
|
|
|
|
static gint my_application_command_line(GApplication* application, GApplicationCommandLine* command_line) {
|
2024-04-30 23:38:49 +08:00
|
|
|
MyApplication* self = MY_APPLICATION(application);
|
2026-09-05 20:09:35 +08:00
|
|
|
if (self->dart_entrypoint_arguments == nullptr) {
|
|
|
|
|
gchar** arguments = g_application_command_line_get_arguments(command_line, nullptr);
|
|
|
|
|
// Strip out the first argument as it is the binary name.
|
|
|
|
|
self->dart_entrypoint_arguments = g_strdupv(arguments + 1);
|
|
|
|
|
g_strfreev(arguments);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
g_application_activate(application);
|
2026-09-05 20:09:35 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2026-09-05 20:09:35 +08:00
|
|
|
// Implements GApplication::before_emit.
|
|
|
|
|
static void my_application_before_emit(GApplication* application, GVariant* platform_data) {
|
|
|
|
|
MyApplication* self = MY_APPLICATION(application);
|
|
|
|
|
self->had_window_before_emit =
|
|
|
|
|
gtk_application_get_windows(GTK_APPLICATION(application)) != nullptr;
|
|
|
|
|
G_APPLICATION_CLASS(my_application_parent_class)->before_emit(application, platform_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implements GApplication::after_emit. A relaunch reaches the primary as a
|
2026-10-08 16:09:04 +08:00
|
|
|
// command-line signal whose activate returns early on the existing window, so
|
|
|
|
|
// the window is raised here, while the launcher's activation token is still
|
|
|
|
|
// current.
|
2026-09-05 20:09:35 +08:00
|
|
|
static void my_application_after_emit(GApplication* application, GVariant* platform_data) {
|
|
|
|
|
MyApplication* self = MY_APPLICATION(application);
|
|
|
|
|
if (self->had_window_before_emit) {
|
2026-09-25 03:56:56 +08:00
|
|
|
window_plugin_activate();
|
2026-09-05 20:09:35 +08:00
|
|
|
}
|
|
|
|
|
G_APPLICATION_CLASS(my_application_parent_class)->after_emit(application, platform_data);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-02 02:24:12 +08:00
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
// 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;
|
2026-09-05 20:09:35 +08:00
|
|
|
G_APPLICATION_CLASS(klass)->command_line = my_application_command_line;
|
|
|
|
|
G_APPLICATION_CLASS(klass)->before_emit = my_application_before_emit;
|
|
|
|
|
G_APPLICATION_CLASS(klass)->after_emit = my_application_after_emit;
|
2025-05-02 02:24:12 +08:00
|
|
|
G_APPLICATION_CLASS(klass)->startup = my_application_startup;
|
|
|
|
|
G_APPLICATION_CLASS(klass)->shutdown = my_application_shutdown;
|
2024-04-30 23:38:49 +08:00
|
|
|
G_OBJECT_CLASS(klass)->dispose = my_application_dispose;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void my_application_init(MyApplication* self) {}
|
|
|
|
|
|
|
|
|
|
MyApplication* my_application_new() {
|
2025-05-02 02:24:12 +08:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
return MY_APPLICATION(g_object_new(my_application_get_type(),
|
|
|
|
|
"application-id", APPLICATION_ID,
|
2026-09-05 20:09:35 +08:00
|
|
|
"flags", G_APPLICATION_HANDLES_COMMAND_LINE,
|
2025-05-02 02:24:12 +08:00
|
|
|
nullptr));
|
|
|
|
|
}
|