Make the Delete key move files to trash in browser

This commit is contained in:
Přemysl Eric Janouch 2024-01-26 15:45:59 +01:00
parent 84269b2ba2
commit 935506b120
Signed by: p
GPG Key ID: A0420B94F92B9493
3 changed files with 29 additions and 14 deletions

View File

@ -1,7 +1,7 @@
// //
// fiv-browser.c: filesystem browsing widget // fiv-browser.c: filesystem browsing widget
// //
// Copyright (c) 2021 - 2023, Přemysl Eric Janouch <p@janouch.name> // Copyright (c) 2021 - 2024, Přemysl Eric Janouch <p@janouch.name>
// //
// Permission to use, copy, modify, and/or distribute this software for any // Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted. // purpose with or without fee is hereby granted.
@ -1562,6 +1562,14 @@ fiv_browser_key_press_event(GtkWidget *widget, GdkEventKey *event)
switch ((event->state & gtk_accelerator_get_default_mod_mask())) { switch ((event->state & gtk_accelerator_get_default_mod_mask())) {
case 0: case 0:
switch (event->keyval) { switch (event->keyval) {
case GDK_KEY_Delete:
if (self->selected) {
GtkWindow *window = GTK_WINDOW(gtk_widget_get_toplevel(widget));
GFile *file = g_file_new_for_uri(self->selected->e->uri);
fiv_context_menu_remove(window, file);
g_object_unref(file);
}
return GDK_EVENT_STOP;
case GDK_KEY_Return: case GDK_KEY_Return:
if (self->selected) if (self->selected)
return open_entry(widget, self->selected, FALSE); return open_entry(widget, self->selected, FALSE);

View File

@ -1,7 +1,7 @@
// //
// fiv-context-menu.c: popup menu // fiv-context-menu.c: popup menu
// //
// Copyright (c) 2021 - 2022, Přemysl Eric Janouch <p@janouch.name> // Copyright (c) 2021 - 2024, Přemysl Eric Janouch <p@janouch.name>
// //
// Permission to use, copy, modify, and/or distribute this software for any // Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted. // purpose with or without fee is hereby granted.
@ -328,17 +328,13 @@ open_context_unref(gpointer data, G_GNUC_UNUSED GClosure *closure)
} }
static void static void
open_context_show_error_dialog(OpenContext *self, GError *error) show_error_dialog(GtkWindow *parent, GError *error)
{ {
GtkWindow *window = g_weak_ref_get(&self->window);
GtkWidget *dialog = GtkWidget *dialog =
gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL, gtk_message_dialog_new(GTK_WINDOW(parent), GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", error->message); GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", error->message);
gtk_dialog_run(GTK_DIALOG(dialog)); gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog); gtk_widget_destroy(dialog);
g_clear_object(&window);
g_error_free(error); g_error_free(error);
} }
@ -357,7 +353,9 @@ open_context_launch(GtkWidget *widget, OpenContext *self)
(void) g_app_info_set_as_last_used_for_type( (void) g_app_info_set_as_last_used_for_type(
self->app_info, self->content_type, NULL); self->app_info, self->content_type, NULL);
} else { } else {
open_context_show_error_dialog(self, error); GtkWindow *window = g_weak_ref_get(&self->window);
show_error_dialog(window, error);
g_clear_object(&window);
} }
g_list_free(files); g_list_free(files);
g_object_unref(context); g_object_unref(context);
@ -437,14 +435,22 @@ on_info_activate(G_GNUC_UNUSED GtkMenuItem *item, gpointer user_data)
g_free(uri); g_free(uri);
} }
void
fiv_context_menu_remove(GtkWindow *parent, GFile *file)
{
// TODO(p): Use g_file_trash_async(), for which we need a task manager.
GError *error = NULL;
if (!g_file_trash(file, NULL, &error))
show_error_dialog(parent, error);
}
static void static void
on_trash_activate(G_GNUC_UNUSED GtkMenuItem *item, gpointer user_data) on_trash_activate(G_GNUC_UNUSED GtkMenuItem *item, gpointer user_data)
{ {
// TODO(p): Use g_file_trash_async(), for which we need a task manager.
OpenContext *ctx = user_data; OpenContext *ctx = user_data;
GError *error = NULL; GtkWindow *window = g_weak_ref_get(&ctx->window);
if (!g_file_trash(ctx->file, NULL, &error)) fiv_context_menu_remove(window, ctx->file);
open_context_show_error_dialog(ctx, error); g_clear_object(&window);
} }
static gboolean static gboolean

View File

@ -1,7 +1,7 @@
// //
// fiv-context-menu.h: popup menu // fiv-context-menu.h: popup menu
// //
// Copyright (c) 2022, Přemysl Eric Janouch <p@janouch.name> // Copyright (c) 2022 - 2024, Přemysl Eric Janouch <p@janouch.name>
// //
// Permission to use, copy, modify, and/or distribute this software for any // Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted. // purpose with or without fee is hereby granted.
@ -18,4 +18,5 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
void fiv_context_menu_information(GtkWindow *parent, const char *uri); void fiv_context_menu_information(GtkWindow *parent, const char *uri);
void fiv_context_menu_remove(GtkWindow *parent, GFile *file);
GtkMenu *fiv_context_menu_new(GtkWidget *widget, GFile *file); GtkMenu *fiv_context_menu_new(GtkWidget *widget, GFile *file);