Skip to content

Commit

Permalink
Implement action search
Browse files Browse the repository at this point in the history
This commit ditches g_desktop_app_info_search() with a modified copy of
the search code in order to support searching for actions in desktop
entries. That additionally required implementing own sorting, which
currently is based on the specificity of the match type (e.g. name ranks
above display name and keywords and so on) and whether we’ve
prefix-matched.

Resolves #5.
  • Loading branch information
ernestask committed Jul 21, 2024
1 parent 6b9d179 commit ddb301d
Show file tree
Hide file tree
Showing 14 changed files with 504 additions and 66 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gio_unix = dependency('gio-unix-2.0')
glib = dependency('glib-2.0',
version: '>= 2.44'
)
gmodule_export = dependency('gmodule-export-2.0')
gtk4 = dependency('gtk4',
version: '>= 4.14.0',
)
Expand Down
69 changes: 24 additions & 45 deletions src/jogg-application-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct _JoggApplicationWindow
GtkSingleSelection *model;
GtkFilterListModel *filter_model;
GListStore *applications;
GtkCustomFilter *filter;
GtkCustomSorter *custom_sorter;
GtkWidget *revealer;
GtkWidget *revealer_box;
GtkWidget *search_entry;
Expand Down Expand Up @@ -108,42 +108,24 @@ jogg_application_window_search_entry_on_search_changed (GtkSearchEntry *self,
gpointer user_data)
{
JoggApplicationWindow *window = NULL;
size_t n = 0;
GtkApplication *application = NULL;
const char *text = NULL;
g_autofree GStrv *app_infos = NULL;
g_autoptr (GPtrArray) desktop_app_infos = NULL;
size_t n;
g_autoptr (GPtrArray) results = NULL;

window = JOGG_APPLICATION_WINDOW (user_data);
text = gtk_editable_get_text (GTK_EDITABLE (self));
app_infos = g_desktop_app_info_search (text);
desktop_app_infos = g_ptr_array_new_full (64, g_object_unref);
n = g_list_model_get_n_items (G_LIST_MODEL (window->applications));
application = gtk_window_get_application (GTK_WINDOW (window));
text = gtk_editable_get_text (GTK_EDITABLE (self));
results = jogg_application_app_info_search (JOGG_APPLICATION (application), text);

g_list_store_splice (window->applications, 0, n, NULL, 0);

for (size_t i = 0; app_infos[i] != NULL; i++)
{
for (size_t j = 0; app_infos[i][j] != NULL; j++)
{
GDesktopAppInfo *app_info = NULL;
JoggResult *result = NULL;

app_info = g_desktop_app_info_new (app_infos[i][j]);
if (app_info == NULL)
{
continue;
}
result = jogg_result_new (app_info);

g_ptr_array_add (desktop_app_infos, result);
}

g_strfreev (app_infos[i]);
}
g_list_store_splice (window->applications
, 0
, n
, results->pdata
, results->len
);

g_list_store_splice (window->applications, 0, 0,
desktop_app_infos->pdata,
desktop_app_infos->len);
}

static void
Expand Down Expand Up @@ -203,15 +185,12 @@ jogg_application_window_results_on_activate ( GtkListView *self
jogg_application_window_quit (user_data);
}

static gboolean
jogg_application_window_app_info_filter_func ( gpointer item
, gpointer user_data)
static gint
jogg_application_window_result_sort_func ( gconstpointer a
, gconstpointer b
, gpointer user_data)
{
g_autoptr (GDesktopAppInfo) app_info = NULL;

app_info = jogg_result_get_app_info (item);

return !g_desktop_app_info_get_nodisplay (app_info);
return jogg_result_compare (a, b);
}

static gboolean
Expand Down Expand Up @@ -336,11 +315,11 @@ jogg_application_window_init (JoggApplicationWindow *self)
gtk_search_entry_set_key_capture_widget ( GTK_SEARCH_ENTRY (self->search_entry)
, self->results
);
gtk_custom_filter_set_filter_func ( self->filter
, jogg_application_window_app_info_filter_func
, NULL
, NULL
);
gtk_custom_sorter_set_sort_func ( self->custom_sorter
, jogg_application_window_result_sort_func
, NULL
, NULL
);

controller = gtk_event_controller_key_new ();

Expand Down Expand Up @@ -408,7 +387,7 @@ jogg_application_window_class_init (JoggApplicationWindowClass *klass)
applications);
gtk_widget_class_bind_template_child (widget_class,
JoggApplicationWindow,
filter);
custom_sorter);
gtk_widget_class_bind_template_child (widget_class,
JoggApplicationWindow,
revealer);
Expand Down
107 changes: 107 additions & 0 deletions src/jogg-application.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@

#include "jogg-application.h"
#include "jogg-application-window.h"
#include "jogg-result.h"
#include "jogg-utils.h"

#include <gio/gdesktopappinfo.h>
#include <gtk4-layer-shell.h>

struct _JoggApplication
{
GtkApplication parent_instance;

GList *applications;
};

G_DEFINE_TYPE (JoggApplication, jogg_application, GTK_TYPE_APPLICATION);

static void
jogg_application_init (JoggApplication *self)
{
self->applications = g_app_info_get_all ();
}

static void
Expand All @@ -41,6 +47,16 @@ jogg_application_window_on_realize (GtkWidget *self,
geometry.height / 3);
}

static void
jogg_application_finalize (GObject *object)
{
JoggApplication *self = NULL;

self = JOGG_APPLICATION (object);

g_list_free_full (g_steal_pointer (&self->applications), g_object_unref);
}

static void
jogg_application_activate (GApplication *app)
{
Expand Down Expand Up @@ -76,11 +92,102 @@ jogg_application_activate (GApplication *app)
static void
jogg_application_class_init (JoggApplicationClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GApplicationClass *app_class = G_APPLICATION_CLASS (klass);

object_class->finalize = jogg_application_finalize;
app_class->activate = jogg_application_activate;
}

GPtrArray *
jogg_application_app_info_search ( JoggApplication *self
, const char *query)
{
GPtrArray *results = NULL;

g_return_val_if_fail (JOGG_IS_APPLICATION (self), NULL);
g_return_val_if_fail (query != NULL, NULL);

results = g_ptr_array_new_full (64, g_object_unref);

if (*query == '\0')
{
return results;
}

for (GList *l = self->applications; l != NULL; l = g_list_next (l))
{
GDesktopAppInfo *app_info = NULL;
const char *haystack = NULL;
bool prefix = false;
const char *const *haystacks = NULL;
JoggMatchType match_type = JOGG_MATCH_TYPE_INVALID;
JoggResult *result = NULL;

app_info = G_DESKTOP_APP_INFO (l->data);
haystack = g_app_info_get_name (G_APP_INFO (app_info));
if (jogg_has_substring (haystack, query, &prefix))
{
match_type = JOGG_MATCH_TYPE_NAME;

goto match_found;
}
haystack = g_app_info_get_display_name (G_APP_INFO (app_info));
if (jogg_has_substring (haystack, query, &prefix))
{
match_type = JOGG_MATCH_TYPE_GENERIC_NAME;

goto match_found;
}
haystacks = g_desktop_app_info_get_keywords (app_info);
for (; haystacks != NULL && *haystacks != NULL; haystacks++)
{
haystack = *haystacks;

if (jogg_has_substring (haystack, query, &prefix))
{
match_type = JOGG_MATCH_TYPE_KEYWORDS;

goto match_found;
}
}
haystack = g_app_info_get_executable (G_APP_INFO (app_info));
if (jogg_has_substring (haystack, query, &prefix))
{
match_type = JOGG_MATCH_TYPE_EXEC;
}

match_found:
if (match_type != JOGG_MATCH_TYPE_INVALID)
{
result = jogg_result_new (app_info, NULL, match_type, prefix);

g_ptr_array_add (results, result);

continue;
}

haystacks = g_desktop_app_info_list_actions (app_info);
for (; haystacks != NULL && *haystacks != NULL; haystacks++)
{
haystack = g_desktop_app_info_get_action_name (app_info, *haystacks);

if (jogg_has_substring (haystack, query, &prefix))
{
result = jogg_result_new ( app_info
, haystack
, JOGG_MATCH_TYPE_ACTIONS
, prefix
);

g_ptr_array_add (results, result);
}
}
}

return results;
}

JoggApplication *
jogg_application_new (void)
{
Expand Down
3 changes: 3 additions & 0 deletions src/jogg-application.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ G_DECLARE_FINAL_TYPE (JoggApplication, jogg_application,
JOGG, APPLICATION,
GtkApplication);

GPtrArray *jogg_application_app_info_search ( JoggApplication *self
, const char *query);

JoggApplication *jogg_application_new (void);

G_END_DECLS
26 changes: 26 additions & 0 deletions src/jogg-enum-types.c.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*** BEGIN file-header ***/
/*
* SPDX-License-Identifier: GPL-2.0-only
*
* Copyright (C) 2024 Ernestas Kulik <ernestas AT baltic DOT engineering>
*/

#include "jogg-enum-types.h"
/*** END file-header ***/

/*** BEGIN file-production ***/

/* Enumerations from "@basename@" */
#include "@basename@"
/*** END file-production ***/

/*** BEGIN value-header ***/
G_DEFINE_ENUM_TYPE ( @EnumName@
, @enum_name@
/*** END value-header ***/
/*** BEGIN value-production ***/
, G_DEFINE_ENUM_VALUE (@VALUENAME@, "@valuenick@")
/*** END value-production ***/
/*** BEGIN value-tail ***/
);
/*** END value-tail ***/
30 changes: 30 additions & 0 deletions src/jogg-enum-types.h.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*** BEGIN file-header ***/
/*
* SPDX-License-Identifier: GPL-2.0-only
*
* Copyright (C) 2024 Ernestas Kulik <ernestas AT baltic DOT engineering>
*/

#pragma once

#include <glib-object.h>

G_BEGIN_DECLS
/*** END file-header ***/

/*** BEGIN file-production ***/

/* Enumerations from "@basename@" */

/*** END file-production ***/

/*** BEGIN enumeration-production ***/
#define @ENUMPREFIX@_TYPE_@ENUMSHORT@ (@enum_name@_get_type ())
GType @enum_name@_get_type (void);

/*** END enumeration-production ***/

/*** BEGIN file-tail ***/

G_END_DECLS
/*** END file-tail ***/
23 changes: 23 additions & 0 deletions src/jogg-enums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

/*
* SPDX-License-Identifier: GPL-2.0-only
*
* Copyright (C) 2024 Ernestas Kulik <ernestas AT baltic DOT engineering>
*/

#include <glib.h>

G_BEGIN_DECLS

typedef enum
{
JOGG_MATCH_TYPE_INVALID,
JOGG_MATCH_TYPE_NAME,
JOGG_MATCH_TYPE_GENERIC_NAME,
JOGG_MATCH_TYPE_ACTIONS,
JOGG_MATCH_TYPE_KEYWORDS,
JOGG_MATCH_TYPE_EXEC,
} JoggMatchType;

G_END_DECLS
Loading

0 comments on commit ddb301d

Please sign in to comment.