Merge branch 'worktree-agent-ae3af4174afca7f4d'
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ project(runner LANGUAGES CXX)
|
||||
add_executable(${BINARY_NAME}
|
||||
"main.cc"
|
||||
"my_application.cc"
|
||||
"clipboard_files.cc"
|
||||
"${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
#include "clipboard_files.h"
|
||||
|
||||
#include <gio/gio.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
namespace {
|
||||
|
||||
FlMethodChannel* channel = nullptr;
|
||||
|
||||
// The MIME type of [path] from its name and contents, or "".
|
||||
gchar* mime_of(const gchar* path, const guchar* data, gsize length) {
|
||||
gboolean uncertain = FALSE;
|
||||
g_autofree gchar* type =
|
||||
g_content_type_guess(path, data, MIN(length, (gsize)4096), &uncertain);
|
||||
gchar* mime = type == nullptr ? nullptr : g_content_type_get_mime_type(type);
|
||||
return mime == nullptr ? g_strdup("") : mime;
|
||||
}
|
||||
|
||||
FlValue* file_entry(const gchar* name, const gchar* mime, const guchar* data,
|
||||
gsize length) {
|
||||
FlValue* entry = fl_value_new_map();
|
||||
fl_value_set_string_take(entry, "name", fl_value_new_string(name));
|
||||
fl_value_set_string_take(entry, "mime", fl_value_new_string(mime));
|
||||
fl_value_set_string_take(entry, "bytes",
|
||||
fl_value_new_uint8_list(data, length));
|
||||
return entry;
|
||||
}
|
||||
|
||||
// Adds each copied local file; folders and unreadable files become errors.
|
||||
gboolean add_uris(GtkClipboard* clipboard, FlValue* files, FlValue* errors) {
|
||||
g_auto(GStrv) uris = gtk_clipboard_wait_for_uris(clipboard);
|
||||
if (uris == nullptr || uris[0] == nullptr) return FALSE;
|
||||
gboolean any = FALSE;
|
||||
for (gchar** uri = uris; *uri != nullptr; uri++) {
|
||||
g_autofree gchar* path = g_filename_from_uri(*uri, nullptr, nullptr);
|
||||
if (path == nullptr) continue;
|
||||
any = TRUE;
|
||||
g_autofree gchar* name = g_path_get_basename(path);
|
||||
if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
|
||||
g_autofree gchar* message =
|
||||
g_strdup_printf("%s is a folder; paste its files instead.", name);
|
||||
fl_value_append_take(errors, fl_value_new_string(message));
|
||||
continue;
|
||||
}
|
||||
g_autofree gchar* contents = nullptr;
|
||||
gsize length = 0;
|
||||
g_autoptr(GError) error = nullptr;
|
||||
if (!g_file_get_contents(path, &contents, &length, &error)) {
|
||||
g_autofree gchar* message =
|
||||
g_strdup_printf("Could not read %s: %s", name, error->message);
|
||||
fl_value_append_take(errors, fl_value_new_string(message));
|
||||
continue;
|
||||
}
|
||||
g_autofree gchar* mime =
|
||||
mime_of(path, reinterpret_cast<const guchar*>(contents), length);
|
||||
fl_value_append_take(
|
||||
files, file_entry(name, mime,
|
||||
reinterpret_cast<const guchar*>(contents), length));
|
||||
}
|
||||
return any;
|
||||
}
|
||||
|
||||
// The clipboard's own PNG (a screenshot tool's), else any image GTK can read
|
||||
// converted to PNG.
|
||||
void add_image(GtkClipboard* clipboard, FlValue* files) {
|
||||
g_autoptr(GtkSelectionData) png = gtk_clipboard_wait_for_contents(
|
||||
clipboard, gdk_atom_intern_static_string("image/png"));
|
||||
if (png != nullptr && gtk_selection_data_get_length(png) > 0) {
|
||||
fl_value_append_take(
|
||||
files, file_entry("", "image/png", gtk_selection_data_get_data(png),
|
||||
gtk_selection_data_get_length(png)));
|
||||
return;
|
||||
}
|
||||
g_autoptr(GdkPixbuf) image = gtk_clipboard_wait_for_image(clipboard);
|
||||
if (image == nullptr) return;
|
||||
g_autofree gchar* buffer = nullptr;
|
||||
gsize length = 0;
|
||||
if (gdk_pixbuf_save_to_buffer(image, &buffer, &length, "png", nullptr,
|
||||
nullptr)) {
|
||||
fl_value_append_take(
|
||||
files, file_entry("", "image/png",
|
||||
reinterpret_cast<const guchar*>(buffer), length));
|
||||
}
|
||||
}
|
||||
|
||||
FlValue* read_clipboard() {
|
||||
GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
|
||||
FlValue* files = fl_value_new_list();
|
||||
FlValue* errors = fl_value_new_list();
|
||||
FlValue* types = fl_value_new_list();
|
||||
GdkAtom* targets = nullptr;
|
||||
gint count = 0;
|
||||
if (gtk_clipboard_wait_for_targets(clipboard, &targets, &count)) {
|
||||
for (gint i = 0; i < count; i++) {
|
||||
g_autofree gchar* name = gdk_atom_name(targets[i]);
|
||||
fl_value_append_take(types, fl_value_new_string(name));
|
||||
}
|
||||
g_free(targets);
|
||||
}
|
||||
if (!add_uris(clipboard, files, errors)) add_image(clipboard, files);
|
||||
FlValue* reply = fl_value_new_map();
|
||||
fl_value_set_string_take(reply, "files", files);
|
||||
fl_value_set_string_take(reply, "errors", errors);
|
||||
fl_value_set_string_take(reply, "types", types);
|
||||
return reply;
|
||||
}
|
||||
|
||||
void method_call_cb(FlMethodChannel* channel, FlMethodCall* call,
|
||||
gpointer user_data) {
|
||||
g_autoptr(FlMethodResponse) response = nullptr;
|
||||
if (g_strcmp0(fl_method_call_get_name(call), "read") == 0) {
|
||||
g_autoptr(FlValue) reply = read_clipboard();
|
||||
response = FL_METHOD_RESPONSE(fl_method_success_response_new(reply));
|
||||
} else {
|
||||
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
|
||||
}
|
||||
g_autoptr(GError) error = nullptr;
|
||||
if (!fl_method_call_respond(call, response, &error)) {
|
||||
g_warning("Failed to answer a clipboard read: %s", error->message);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void clipboard_files_register(FlPluginRegistry* registry) {
|
||||
g_autoptr(FlPluginRegistrar) registrar =
|
||||
fl_plugin_registry_get_registrar_for_plugin(registry, "ClipboardFiles");
|
||||
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
|
||||
g_clear_object(&channel);
|
||||
channel = fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
|
||||
"pipelines/clipboard_files",
|
||||
FL_METHOD_CODEC(codec));
|
||||
fl_method_channel_set_method_call_handler(channel, method_call_cb, nullptr,
|
||||
nullptr);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef RUNNER_CLIPBOARD_FILES_H_
|
||||
#define RUNNER_CLIPBOARD_FILES_H_
|
||||
|
||||
#include <flutter_linux/flutter_linux.h>
|
||||
|
||||
// Answers `read` on the `pipelines/clipboard_files` channel with the
|
||||
// clipboard's files (a file manager's copied URIs), else its image as PNG,
|
||||
// plus the clipboard's target names; the same reply shape as the macOS
|
||||
// Runner (lib/features/chat/domain/chat_clipboard_paste_native.dart).
|
||||
void clipboard_files_register(FlPluginRegistry* registry);
|
||||
|
||||
#endif // RUNNER_CLIPBOARD_FILES_H_
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <gdk/gdkx.h>
|
||||
#endif
|
||||
|
||||
#include "clipboard_files.h"
|
||||
#include "flutter/generated_plugin_registrant.h"
|
||||
|
||||
struct _MyApplication {
|
||||
@@ -15,6 +16,18 @@ struct _MyApplication {
|
||||
|
||||
G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION)
|
||||
|
||||
// The bundle's icon (data/app_icon.png beside the executable) for X11 window
|
||||
// managers and task bars. Wayland compositors take the icon from the desktop
|
||||
// entry named by the application id instead.
|
||||
static void set_window_icon(GtkWindow* window) {
|
||||
g_autofree gchar* executable = g_file_read_link("/proc/self/exe", nullptr);
|
||||
if (executable == nullptr) return;
|
||||
g_autofree gchar* directory = g_path_get_dirname(executable);
|
||||
g_autofree gchar* icon =
|
||||
g_build_filename(directory, "data", "app_icon.png", nullptr);
|
||||
gtk_window_set_icon_from_file(window, icon, nullptr);
|
||||
}
|
||||
|
||||
// Called when first Flutter frame received.
|
||||
static void first_frame_cb(MyApplication* self, FlView* view) {
|
||||
gtk_widget_show(gtk_widget_get_toplevel(GTK_WIDGET(view)));
|
||||
@@ -33,21 +46,18 @@ static void my_application_activate(GApplication* application) {
|
||||
? detached_title
|
||||
: "Pipelines";
|
||||
|
||||
// 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;
|
||||
// A GTK header bar only on GNOME, whose shell expects client-side title
|
||||
// bars. Elsewhere (Hyprland, Sway, KDE, any X11 window manager) the window
|
||||
// takes the compositor's own decorations, or none on a tiling compositor,
|
||||
// instead of drawing a second title bar inside the tile.
|
||||
const gchar* desktop = g_getenv("XDG_CURRENT_DESKTOP");
|
||||
gboolean use_header_bar =
|
||||
desktop != nullptr && g_strrstr(desktop, "GNOME") != nullptr;
|
||||
#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;
|
||||
}
|
||||
use_header_bar = g_strcmp0(wm_name, "GNOME Shell") == 0;
|
||||
}
|
||||
#endif
|
||||
if (use_header_bar) {
|
||||
@@ -59,6 +69,7 @@ static void my_application_activate(GApplication* application) {
|
||||
} else {
|
||||
gtk_window_set_title(window, title);
|
||||
}
|
||||
set_window_icon(window);
|
||||
|
||||
gint width = 1280, height = 720;
|
||||
const gchar* detached_size = g_getenv("PIPELINES_WINDOW_SIZE");
|
||||
@@ -91,6 +102,7 @@ static void my_application_activate(GApplication* application) {
|
||||
gtk_widget_realize(GTK_WIDGET(view));
|
||||
|
||||
fl_register_plugins(FL_PLUGIN_REGISTRY(view));
|
||||
clipboard_files_register(FL_PLUGIN_REGISTRY(view));
|
||||
|
||||
gtk_widget_grab_focus(GTK_WIDGET(view));
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 72 KiB |
Reference in New Issue
Block a user