fox_utils 0.1.7: clipboard, drag & drop, Android file saving
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
dc16bba054
46 files changed
+4738
No files matched your search
@@ -0,0 +1,27 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
set(PROJECT_NAME "fox_utils")
|
||||
project(${PROJECT_NAME} LANGUAGES CXX)
|
||||
|
||||
# This value is used when generating builds using this plugin, so it must
|
||||
# not be changed
|
||||
set(PLUGIN_NAME "fox_utils_plugin")
|
||||
|
||||
add_library(${PLUGIN_NAME} SHARED
|
||||
"fox_utils_plugin.cc"
|
||||
"pasteboard_plugin.cc"
|
||||
"desktop_drop_plugin.cc"
|
||||
)
|
||||
apply_standard_settings(${PLUGIN_NAME})
|
||||
set_target_properties(${PLUGIN_NAME} PROPERTIES
|
||||
CXX_VISIBILITY_PRESET hidden)
|
||||
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
|
||||
target_include_directories(${PLUGIN_NAME} INTERFACE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter)
|
||||
target_link_libraries(${PLUGIN_NAME} PRIVATE PkgConfig::GTK)
|
||||
|
||||
# List of absolute paths to libraries that should be bundled with the plugin
|
||||
set(fox_utils_bundled_libraries
|
||||
""
|
||||
PARENT_SCOPE
|
||||
)
|
||||
@@ -0,0 +1,123 @@
|
||||
#include "include/fox_utils/desktop_drop_plugin.h"
|
||||
|
||||
#include <flutter_linux/flutter_linux.h>
|
||||
#include <gtk/gtk.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
#define DESKTOP_DROP_PLUGIN(obj) \
|
||||
(G_TYPE_CHECK_INSTANCE_CAST((obj), desktop_drop_plugin_get_type(), \
|
||||
DesktopDropPlugin))
|
||||
|
||||
struct _DesktopDropPlugin {
|
||||
GObject parent_instance;
|
||||
};
|
||||
|
||||
static gboolean isKDE = FALSE;
|
||||
static gboolean ignoreNext = FALSE;
|
||||
|
||||
G_DEFINE_TYPE(DesktopDropPlugin, desktop_drop_plugin, g_object_get_type())
|
||||
|
||||
void on_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
|
||||
gint x, gint y, GtkSelectionData *sdata, guint info,
|
||||
guint time, gpointer user_data) {
|
||||
auto *channel = static_cast<FlMethodChannel *>(user_data);
|
||||
auto *data = gtk_selection_data_get_data(sdata);
|
||||
double point[] = {double(x), double(y)};
|
||||
auto args = fl_value_new_list();
|
||||
fl_value_append(args, fl_value_new_string((gchar *) data));
|
||||
fl_value_append(args, fl_value_new_float_list(point, 2));
|
||||
fl_method_channel_invoke_method(channel, "performOperation_linux", args,
|
||||
nullptr, nullptr, nullptr);
|
||||
}
|
||||
|
||||
void on_drag_motion(GtkWidget *widget, GdkDragContext *drag_context,
|
||||
gint x, gint y, guint time, gpointer user_data) {
|
||||
if (ignoreNext) {
|
||||
ignoreNext = FALSE;
|
||||
return;
|
||||
}
|
||||
|
||||
auto *channel = static_cast<FlMethodChannel *>(user_data);
|
||||
double point[] = {double(x), double(y)};
|
||||
g_autoptr(FlValue) args = fl_value_new_float_list(point, 2);
|
||||
fl_method_channel_invoke_method(channel, "updated", args,
|
||||
nullptr, nullptr, nullptr);
|
||||
}
|
||||
|
||||
void on_drag_leave(GtkWidget *widget, GdkDragContext *drag_context, guint time, gpointer user_data) {
|
||||
auto *channel = static_cast<FlMethodChannel *>(user_data);
|
||||
fl_method_channel_invoke_method(channel, "exited", nullptr,
|
||||
nullptr, nullptr, nullptr);
|
||||
}
|
||||
|
||||
// Called when a method call is received from Flutter.
|
||||
static void desktop_drop_plugin_handle_method_call(
|
||||
DesktopDropPlugin *self,
|
||||
FlMethodCall *method_call) {
|
||||
fl_method_call_respond_not_implemented(method_call, nullptr);
|
||||
}
|
||||
|
||||
static void desktop_drop_plugin_dispose(GObject *object) {
|
||||
G_OBJECT_CLASS(desktop_drop_plugin_parent_class)->dispose(object);
|
||||
}
|
||||
|
||||
static void desktop_drop_plugin_class_init(DesktopDropPluginClass *klass) {
|
||||
G_OBJECT_CLASS(klass)->dispose = desktop_drop_plugin_dispose;
|
||||
}
|
||||
|
||||
static void desktop_drop_plugin_init(DesktopDropPlugin *self) {
|
||||
const char * desktopEnv = getenv("XDG_CURRENT_DESKTOP");
|
||||
if (desktopEnv) {
|
||||
const char * lowercaseDesktopEnv = g_ascii_strdown(desktopEnv, -1);
|
||||
|
||||
if (strcmp(lowercaseDesktopEnv, "kde") == 0 || strcmp(lowercaseDesktopEnv, "plasma") == 0) {
|
||||
isKDE = TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean on_focus_in_event(GtkWidget *widget, GdkEventFocus *event, gpointer user_data) {
|
||||
if (isKDE) {
|
||||
ignoreNext = TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static void method_call_cb(FlMethodChannel *channel, FlMethodCall *method_call,
|
||||
gpointer user_data) {
|
||||
DesktopDropPlugin *plugin = DESKTOP_DROP_PLUGIN(user_data);
|
||||
desktop_drop_plugin_handle_method_call(plugin, method_call);
|
||||
}
|
||||
|
||||
void desktop_drop_plugin_register_with_registrar(FlPluginRegistrar *registrar) {
|
||||
DesktopDropPlugin *plugin = DESKTOP_DROP_PLUGIN(
|
||||
g_object_new(desktop_drop_plugin_get_type(), nullptr));
|
||||
|
||||
auto *fl_view = fl_plugin_registrar_get_view(registrar);
|
||||
static GtkTargetEntry entries[] = {
|
||||
{strdup("STRING"), GTK_TARGET_OTHER_APP, 0}
|
||||
};
|
||||
gtk_drag_dest_set(GTK_WIDGET(fl_view), GTK_DEST_DEFAULT_ALL, entries, 1, GDK_ACTION_COPY);
|
||||
gtk_drag_dest_add_uri_targets(GTK_WIDGET(fl_view));
|
||||
|
||||
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
|
||||
FlMethodChannel *channel =
|
||||
fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
|
||||
"desktop_drop",
|
||||
FL_METHOD_CODEC(codec));
|
||||
fl_method_channel_set_method_call_handler(channel, method_call_cb,
|
||||
g_object_ref(plugin),
|
||||
g_object_unref);
|
||||
|
||||
g_signal_connect(fl_view, "drag-motion",
|
||||
G_CALLBACK(on_drag_motion), channel);
|
||||
g_signal_connect(GTK_WIDGET(fl_view), "drag-data-received",
|
||||
G_CALLBACK(on_drag_data_received), channel);
|
||||
g_signal_connect(GTK_WIDGET(fl_view), "drag-leave",
|
||||
G_CALLBACK(on_drag_leave), channel);
|
||||
g_signal_connect(fl_view, "focus-in-event",
|
||||
G_CALLBACK(on_focus_in_event), nullptr);
|
||||
|
||||
g_object_unref(plugin);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include "include/fox_utils/fox_utils_plugin.h"
|
||||
#include "include/fox_utils/pasteboard_plugin.h"
|
||||
#include "include/fox_utils/desktop_drop_plugin.h"
|
||||
|
||||
void fox_utils_plugin_register_with_registrar(FlPluginRegistrar* registrar) {
|
||||
pasteboard_plugin_register_with_registrar(registrar);
|
||||
desktop_drop_plugin_register_with_registrar(registrar);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef FLUTTER_PLUGIN_DESKTOP_DROP_PLUGIN_H_
|
||||
#define FLUTTER_PLUGIN_DESKTOP_DROP_PLUGIN_H_
|
||||
|
||||
#include <flutter_linux/flutter_linux.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#ifdef FLUTTER_PLUGIN_IMPL
|
||||
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
|
||||
#else
|
||||
#define FLUTTER_PLUGIN_EXPORT
|
||||
#endif
|
||||
|
||||
typedef struct _DesktopDropPlugin DesktopDropPlugin;
|
||||
typedef struct {
|
||||
GObjectClass parent_class;
|
||||
} DesktopDropPluginClass;
|
||||
|
||||
FLUTTER_PLUGIN_EXPORT GType desktop_drop_plugin_get_type();
|
||||
|
||||
FLUTTER_PLUGIN_EXPORT void desktop_drop_plugin_register_with_registrar(
|
||||
FlPluginRegistrar* registrar);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif // FLUTTER_PLUGIN_DESKTOP_DROP_PLUGIN_H_
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef FLUTTER_PLUGIN_FOX_UTILS_PLUGIN_H_
|
||||
#define FLUTTER_PLUGIN_FOX_UTILS_PLUGIN_H_
|
||||
|
||||
#include <flutter_linux/flutter_linux.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#ifdef FLUTTER_PLUGIN_IMPL
|
||||
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
|
||||
#else
|
||||
#define FLUTTER_PLUGIN_EXPORT
|
||||
#endif
|
||||
|
||||
// Single native registration entry for the fox_utils package on Linux.
|
||||
// The generated plugin registrant calls this function; it in turn registers
|
||||
// every vendored engine in this package (pasteboard, desktop_drop).
|
||||
FLUTTER_PLUGIN_EXPORT void fox_utils_plugin_register_with_registrar(
|
||||
FlPluginRegistrar* registrar);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif // FLUTTER_PLUGIN_FOX_UTILS_PLUGIN_H_
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef FLUTTER_PLUGIN_PASTEBOARD_PLUGIN_H_
|
||||
#define FLUTTER_PLUGIN_PASTEBOARD_PLUGIN_H_
|
||||
|
||||
#include <flutter_linux/flutter_linux.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#ifdef FLUTTER_PLUGIN_IMPL
|
||||
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
|
||||
#else
|
||||
#define FLUTTER_PLUGIN_EXPORT
|
||||
#endif
|
||||
|
||||
typedef struct _PasteboardPlugin PasteboardPlugin;
|
||||
typedef struct {
|
||||
GObjectClass parent_class;
|
||||
} PasteboardPluginClass;
|
||||
|
||||
FLUTTER_PLUGIN_EXPORT GType pasteboard_plugin_get_type();
|
||||
|
||||
FLUTTER_PLUGIN_EXPORT void pasteboard_plugin_register_with_registrar(
|
||||
FlPluginRegistrar* registrar);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif // FLUTTER_PLUGIN_PASTEBOARD_PLUGIN_H_
|
||||
@@ -0,0 +1,190 @@
|
||||
#include "include/fox_utils/pasteboard_plugin.h"
|
||||
|
||||
#include <flutter_linux/flutter_linux.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
#define GNOME_COPIED_FILES gdk_atom_intern_static_string ("x-special/gnome-copied-files")
|
||||
|
||||
#define PASTEBOARD_PLUGIN(obj) \
|
||||
(G_TYPE_CHECK_INSTANCE_CAST((obj), pasteboard_plugin_get_type(), \
|
||||
PasteboardPlugin))
|
||||
|
||||
struct _PasteboardPlugin {
|
||||
GObject parent_instance;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE(PasteboardPlugin, pasteboard_plugin, g_object_get_type())
|
||||
|
||||
static void gtk_clipboard_request_uris_callback(
|
||||
GtkClipboard *clipboard,
|
||||
gchar **uris,
|
||||
gpointer user_data
|
||||
) {
|
||||
g_autoptr(FlMethodCall) method_call = static_cast<FlMethodCall *>(user_data);
|
||||
|
||||
g_autoptr(FlValue) result = fl_value_new_list();
|
||||
for (auto uri = uris; uri != nullptr && *uri != nullptr; uri++) {
|
||||
g_autoptr(GFile) file = g_file_new_for_uri(*uri);
|
||||
auto *file_path = g_file_get_path(file);
|
||||
if (file_path) {
|
||||
fl_value_append(result, fl_value_new_string(file_path));
|
||||
}
|
||||
}
|
||||
fl_method_call_respond_success(method_call, result, nullptr);
|
||||
}
|
||||
|
||||
static void gtk_clipboard_get_file_uri(GtkClipboard *clipboard,
|
||||
GtkSelectionData *selection_data,
|
||||
guint info,
|
||||
gpointer user_data_or_owner) {
|
||||
auto target = gtk_selection_data_get_target(selection_data);
|
||||
auto **uris = reinterpret_cast<gchar **>(user_data_or_owner);
|
||||
if (gtk_targets_include_uri(&target, 1)) {
|
||||
gtk_selection_data_set_uris(selection_data, reinterpret_cast<gchar **>(user_data_or_owner));
|
||||
} else if (gtk_targets_include_text(&target, 1)) {
|
||||
g_autoptr(GString) string = g_string_new(nullptr);
|
||||
bool should_insert_new_line = false;
|
||||
for (auto uri = uris; uri != nullptr && *uri != nullptr; uri++) {
|
||||
if (should_insert_new_line) {
|
||||
g_string_append_c(string, '\n');
|
||||
}
|
||||
g_string_append(string, *uri);
|
||||
should_insert_new_line = true;
|
||||
}
|
||||
gtk_selection_data_set_text(selection_data, string->str, int(string->len));
|
||||
} else if (target == GNOME_COPIED_FILES) {
|
||||
g_autoptr(GString) string = g_string_new("copy");
|
||||
for (auto uri = uris; uri != nullptr && *uri != nullptr; uri++) {
|
||||
g_string_append(string, "\n");
|
||||
g_string_append(string, *uri);
|
||||
}
|
||||
gtk_selection_data_set(selection_data, target, 8, (const guchar *) string->str, int(string->len));
|
||||
} else {
|
||||
g_critical("unsupported action: gdk_atom_name(target) = %s", gdk_atom_name(target));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void gtk_clipboard_clear(GtkClipboard *clipboard,
|
||||
gpointer user_data_or_owner) {
|
||||
auto **uris = reinterpret_cast<gchar **>(user_data_or_owner);
|
||||
delete[] uris;
|
||||
}
|
||||
|
||||
static void clipboard_request_image_callback(
|
||||
GtkClipboard *clipboard,
|
||||
GdkPixbuf *pixbuf,
|
||||
gpointer user_data) {
|
||||
g_autoptr(FlMethodCall) method_call = static_cast<FlMethodCall *>(user_data);
|
||||
|
||||
if (!pixbuf) {
|
||||
fl_method_call_respond_success(method_call, nullptr, nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
gchar *buffer = nullptr;
|
||||
gsize buffer_size = 0;
|
||||
GError *error = nullptr;
|
||||
|
||||
gdk_pixbuf_save_to_buffer(pixbuf, &buffer, &buffer_size, "png", &error, nullptr);
|
||||
if (error) {
|
||||
fl_method_call_respond_error(method_call, "0", error->message, nullptr, nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!buffer) {
|
||||
fl_method_call_respond_error(method_call, "0", "failed to get image", nullptr, nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
fl_method_call_respond_success(method_call,
|
||||
fl_value_new_uint8_list(reinterpret_cast<const uint8_t *>(buffer), buffer_size),
|
||||
nullptr);
|
||||
|
||||
}
|
||||
|
||||
// Called when a method call is received from Flutter.
|
||||
static void pasteboard_plugin_handle_method_call(
|
||||
PasteboardPlugin *self,
|
||||
FlMethodCall *method_call) {
|
||||
const gchar *method = fl_method_call_get_name(method_call);
|
||||
if (strcmp(method, "files") == 0) {
|
||||
auto *clipboard = gtk_clipboard_get_default(gdk_display_get_default());
|
||||
gtk_clipboard_request_uris(clipboard, gtk_clipboard_request_uris_callback, g_object_ref(method_call));
|
||||
} else if (strcmp(method, "writeFiles") == 0) {
|
||||
auto *clipboard = gtk_clipboard_get_default(gdk_display_get_default());
|
||||
|
||||
auto args = fl_method_call_get_args(method_call);
|
||||
auto size = fl_value_get_length(args);
|
||||
|
||||
auto **paths = new gchar *[size + 1];
|
||||
paths[size] = nullptr;
|
||||
for (unsigned int i = 0; i < size; ++i) {
|
||||
auto path = fl_value_get_string(fl_value_get_list_value(args, i));
|
||||
paths[i] = g_strconcat("file://", path, nullptr);
|
||||
}
|
||||
|
||||
GtkTargetList *target_list = gtk_target_list_new(nullptr, 0);
|
||||
gtk_target_list_add(target_list, GNOME_COPIED_FILES, 0, 0);
|
||||
|
||||
gtk_target_list_add_uri_targets(target_list, 0);
|
||||
gtk_target_list_add_text_targets(target_list, 0);
|
||||
|
||||
int n_targets = 1;
|
||||
GtkTargetEntry *target_table = gtk_target_table_new_from_list(target_list, &n_targets);
|
||||
|
||||
gtk_clipboard_set_with_data(clipboard,
|
||||
target_table,
|
||||
n_targets,
|
||||
gtk_clipboard_get_file_uri,
|
||||
gtk_clipboard_clear,
|
||||
paths);
|
||||
|
||||
gtk_target_list_unref(target_list);
|
||||
gtk_target_table_free(target_table, n_targets);
|
||||
|
||||
fl_method_call_respond_success(method_call, nullptr, nullptr);
|
||||
} else if (strcmp(method, "image") == 0) {
|
||||
auto *clipboard = gtk_clipboard_get_default(gdk_display_get_default());
|
||||
|
||||
gtk_clipboard_request_image(clipboard, clipboard_request_image_callback, g_object_ref(method_call));
|
||||
|
||||
} else {
|
||||
fl_method_call_respond_not_implemented(method_call, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
static void pasteboard_plugin_dispose(GObject *object) {
|
||||
G_OBJECT_CLASS(pasteboard_plugin_parent_class)->dispose(object);
|
||||
}
|
||||
|
||||
static void pasteboard_plugin_class_init(PasteboardPluginClass *klass) {
|
||||
G_OBJECT_CLASS(klass)->dispose = pasteboard_plugin_dispose;
|
||||
}
|
||||
|
||||
static void pasteboard_plugin_init(PasteboardPlugin *self) {}
|
||||
|
||||
static void method_call_cb(FlMethodChannel *channel, FlMethodCall *method_call,
|
||||
gpointer user_data) {
|
||||
PasteboardPlugin *plugin = PASTEBOARD_PLUGIN(user_data);
|
||||
pasteboard_plugin_handle_method_call(plugin, method_call);
|
||||
}
|
||||
|
||||
void pasteboard_plugin_register_with_registrar(FlPluginRegistrar *registrar) {
|
||||
PasteboardPlugin *plugin = PASTEBOARD_PLUGIN(
|
||||
g_object_new(pasteboard_plugin_get_type(), nullptr));
|
||||
|
||||
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
|
||||
g_autoptr(FlMethodChannel) channel =
|
||||
fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
|
||||
"pasteboard",
|
||||
FL_METHOD_CODEC(codec));
|
||||
fl_method_channel_set_method_call_handler(channel, method_call_cb,
|
||||
g_object_ref(plugin),
|
||||
g_object_unref);
|
||||
|
||||
g_object_unref(plugin);
|
||||
}
|
||||
Reference in new issue
Block a user