Implement filtering by supported extensions

This commit is contained in:
Přemysl Eric Janouch 2021-11-21 11:00:25 +01:00
parent 6dd0414d0a
commit c77bccccb8
Signed by: p
GPG Key ID: A0420B94F92B9493
5 changed files with 66 additions and 38 deletions

View File

@ -644,7 +644,8 @@ entry_add_thumbnail(gpointer data, G_GNUC_UNUSED gpointer user_data)
}
void
fastiv_browser_load(FastivBrowser *self, const char *path)
fastiv_browser_load(
FastivBrowser *self, FastivBrowserFilterCallback cb, const char *path)
{
g_array_set_size(self->entries, 0);
g_array_set_size(self->layouted_rows, 0);
@ -666,8 +667,9 @@ fastiv_browser_load(FastivBrowser *self, const char *path)
if (g_file_info_get_file_type(info) == G_FILE_TYPE_DIRECTORY)
continue;
// TODO(p): Support being passed filter/sort functions.
g_file_info_get_name(info);
// TODO(p): Support being passed a sort function.
if (cb && !cb(g_file_info_get_name(info)))
continue;
g_array_append_val(self->entries,
((Entry) {.thumbnail = NULL, .filename = g_file_get_path(child)}));

View File

@ -22,4 +22,7 @@
#define FASTIV_TYPE_BROWSER (fastiv_browser_get_type())
G_DECLARE_FINAL_TYPE(FastivBrowser, fastiv_browser, FASTIV, BROWSER, GtkWidget)
void fastiv_browser_load(FastivBrowser *self, const char *path);
typedef gboolean (*FastivBrowserFilterCallback) (const char *);
void fastiv_browser_load(
FastivBrowser *self, FastivBrowserFilterCallback cb, const char *path);

View File

@ -22,6 +22,7 @@
struct _FastivSidebar {
GtkScrolledWindow parent_instance;
GtkPlacesSidebar *places;
GtkWidget *toolbar;
GtkWidget *listbox;
GFile *location;
};
@ -358,56 +359,29 @@ fastiv_sidebar_init(FastivSidebar *self)
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self->places),
GTK_POLICY_NEVER, GTK_POLICY_NEVER);
// Fill up what would otherwise be wasted space,
// as it is in the examples of Nautilus and Thunar.
GtkWidget *plus = gtk_button_new_from_icon_name("zoom-in-symbolic",
GTK_ICON_SIZE_BUTTON);
gtk_widget_set_tooltip_text(plus, "Larger thumbnails");
GtkWidget *minus = gtk_button_new_from_icon_name("zoom-out-symbolic",
GTK_ICON_SIZE_BUTTON);
gtk_widget_set_tooltip_text(minus, "Smaller thumbnails");
GtkWidget *zoom_group = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_style_context_add_class(
gtk_widget_get_style_context(zoom_group), GTK_STYLE_CLASS_LINKED);
gtk_box_pack_start(GTK_BOX(zoom_group), plus, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(zoom_group), minus, FALSE, FALSE, 0);
GtkWidget *funnel = gtk_toggle_button_new();
gtk_container_add(GTK_CONTAINER(funnel),
gtk_image_new_from_icon_name("funnel-symbolic", GTK_ICON_SIZE_BUTTON));
gtk_widget_set_tooltip_text(funnel, "Hide unsupported files");
// None of GtkActionBar, GtkToolbar, .inline-toolbar is appropriate.
// It is either borders or padding.
GtkWidget *buttons = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 12);
// It is either side-favouring borders or excess button padding.
self->toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 12);
gtk_style_context_add_class(
gtk_widget_get_style_context(buttons), GTK_STYLE_CLASS_TOOLBAR);
gtk_box_pack_start(GTK_BOX(buttons), zoom_group, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(buttons), funnel, FALSE, FALSE, 0);
gtk_widget_set_halign(buttons, GTK_ALIGN_CENTER);
// TODO(p): Implement. Probably fill `buttons` in externally.
gtk_widget_set_sensitive(plus, FALSE);
gtk_widget_set_sensitive(minus, FALSE);
gtk_widget_set_sensitive(funnel, FALSE);
gtk_widget_get_style_context(self->toolbar), GTK_STYLE_CLASS_TOOLBAR);
self->listbox = gtk_list_box_new();
gtk_list_box_set_selection_mode(
GTK_LIST_BOX(self->listbox), GTK_SELECTION_NONE);
g_signal_connect(self->listbox, "row-activated",
G_CALLBACK(on_open_breadcrumb), self);
gtk_list_box_set_sort_func(
GTK_LIST_BOX(self->listbox), listbox_sort, self, NULL);
// Fill up what would otherwise be wasted space,
// as it is in the examples of Nautilus and Thunar.
GtkWidget *superbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(
GTK_CONTAINER(superbox), GTK_WIDGET(self->places));
gtk_container_add(
GTK_CONTAINER(superbox), gtk_separator_new(GTK_ORIENTATION_VERTICAL));
gtk_container_add(
GTK_CONTAINER(superbox), buttons);
GTK_CONTAINER(superbox), self->toolbar);
gtk_container_add(
GTK_CONTAINER(superbox), gtk_separator_new(GTK_ORIENTATION_VERTICAL));
gtk_container_add(
@ -437,3 +411,10 @@ fastiv_sidebar_show_enter_location(FastivSidebar *self)
g_return_if_fail(FASTIV_IS_SIDEBAR(self));
g_signal_emit_by_name(self->places, "show-enter-location");
}
GtkBox *
fastiv_sidebar_get_toolbar(FastivSidebar *self)
{
g_return_val_if_fail(FASTIV_IS_SIDEBAR(self), NULL);
return GTK_BOX(self->toolbar);
}

View File

@ -25,3 +25,4 @@ G_DECLARE_FINAL_TYPE(
void fastiv_sidebar_set_location(FastivSidebar *self, GFile *location);
void fastiv_sidebar_show_enter_location(FastivSidebar *self);
GtkBox *fastiv_sidebar_get_toolbar(FastivSidebar *self);

View File

@ -55,6 +55,7 @@ exit_fatal(const gchar *format, ...)
struct {
gchar **supported_globs;
gboolean filtering;
gchar *directory;
GPtrArray *files;
@ -118,7 +119,8 @@ load_directory(const gchar *dirname)
GFile *file = g_file_new_for_path(g.directory);
fastiv_sidebar_set_location(FASTIV_SIDEBAR(g.browser_sidebar), file);
g_object_unref(file);
fastiv_browser_load(FASTIV_BROWSER(g.browser), g.directory);
fastiv_browser_load(FASTIV_BROWSER(g.browser),
g.filtering ? is_supported : NULL, g.directory);
GError *error = NULL;
GDir *dir = g_dir_open(g.directory, 0, &error);
@ -152,6 +154,14 @@ load_directory(const gchar *dirname)
}
}
static void
on_filtering_toggled(GtkToggleButton *button, G_GNUC_UNUSED gpointer user_data)
{
g.filtering = gtk_toggle_button_get_active(button);
if (g.directory)
load_directory(NULL);
}
static void
open(const gchar *path)
{
@ -549,6 +559,34 @@ main(int argc, char *argv[])
g_signal_connect(g.browser_sidebar, "open-location",
G_CALLBACK(on_open_location), NULL);
GtkWidget *plus = gtk_button_new_from_icon_name("zoom-in-symbolic",
GTK_ICON_SIZE_BUTTON);
gtk_widget_set_tooltip_text(plus, "Larger thumbnails");
GtkWidget *minus = gtk_button_new_from_icon_name("zoom-out-symbolic",
GTK_ICON_SIZE_BUTTON);
gtk_widget_set_tooltip_text(minus, "Smaller thumbnails");
GtkWidget *zoom_group = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_style_context_add_class(
gtk_widget_get_style_context(zoom_group), GTK_STYLE_CLASS_LINKED);
gtk_box_pack_start(GTK_BOX(zoom_group), plus, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(zoom_group), minus, FALSE, FALSE, 0);
GtkWidget *funnel = gtk_toggle_button_new();
gtk_container_add(GTK_CONTAINER(funnel),
gtk_image_new_from_icon_name("funnel-symbolic", GTK_ICON_SIZE_BUTTON));
gtk_widget_set_tooltip_text(funnel, "Hide unsupported files");
// TODO(p): Implement these as well.
gtk_widget_set_sensitive(plus, FALSE);
gtk_widget_set_sensitive(minus, FALSE);
GtkBox *toolbar =
fastiv_sidebar_get_toolbar(FASTIV_SIDEBAR(g.browser_sidebar));
gtk_box_pack_start(toolbar, zoom_group, FALSE, FALSE, 0);
gtk_box_pack_start(toolbar, funnel, FALSE, FALSE, 0);
gtk_widget_set_halign(GTK_WIDGET(toolbar), GTK_ALIGN_CENTER);
g.browser_paned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
gtk_paned_add1(GTK_PANED(g.browser_paned), g.browser_sidebar);
gtk_paned_add2(GTK_PANED(g.browser_paned), g.browser_scroller);
@ -573,6 +611,9 @@ main(int argc, char *argv[])
g.supported_globs = extract_mime_globs((const char **) types);
g_strfreev(types);
g_signal_connect(funnel, "toggled", G_CALLBACK(on_filtering_toggled), NULL);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(funnel), TRUE);
g.files = g_ptr_array_new_full(16, g_free);
g.directory = g_get_current_dir();
if (!path_arg || !open_any_path(path_arg))