ref: instead of filtering(slow) show all categories in the scroll box, scroll to category on type; change default window size

This commit is contained in:
noise 2026-02-28 15:45:30 +03:00
parent 6922724fa0
commit 6c112c41e7
5 changed files with 274 additions and 42 deletions

View File

@ -62,6 +62,13 @@ static void on_popover_leave(G_GNUC_UNUSED GtkEventControllerMotion *ctrl,
gtk_popover_popdown(GTK_POPOVER(user_data)); gtk_popover_popdown(GTK_POPOVER(user_data));
} }
static void on_button_destroy(GtkWidget *button,
G_GNUC_UNUSED gpointer user_data) {
GtkWidget *popover = g_object_get_data(G_OBJECT(button), "popover");
if (popover)
gtk_widget_unparent(popover);
}
static void add_kaomoji_button(GtkFlowBox *flow, const char *text) { static void add_kaomoji_button(GtkFlowBox *flow, const char *text) {
gboolean multiline = (strchr(text, '\n') != NULL); gboolean multiline = (strchr(text, '\n') != NULL);
const char *label_text = text; const char *label_text = text;
@ -88,6 +95,8 @@ static void add_kaomoji_button(GtkFlowBox *flow, const char *text) {
gtk_widget_add_css_class(label, "kaomoji-preview"); gtk_widget_add_css_class(label, "kaomoji-preview");
gtk_popover_set_child(GTK_POPOVER(popover), label); gtk_popover_set_child(GTK_POPOVER(popover), label);
gtk_widget_set_parent(popover, button); gtk_widget_set_parent(popover, button);
g_object_set_data(G_OBJECT(button), "popover", popover);
g_signal_connect(button, "destroy", G_CALLBACK(on_button_destroy), NULL);
GtkEventController *motion = gtk_event_controller_motion_new(); GtkEventController *motion = gtk_event_controller_motion_new();
g_signal_connect(motion, "enter", G_CALLBACK(on_popover_enter), popover); g_signal_connect(motion, "enter", G_CALLBACK(on_popover_enter), popover);
@ -139,34 +148,112 @@ static void load_category(MoeMojiWindow *self, const char *kaomoji_dir,
g_free(cat_path); g_free(cat_path);
} }
static void set_active_chip(MoeMojiWindow *self, int index) {
if (index == self->active_chip_index)
return;
if (self->active_chip_index >= 0 &&
(guint)self->active_chip_index < self->category_widgets->len) {
CategoryWidgets *old =
g_ptr_array_index(self->category_widgets, self->active_chip_index);
if (old->chip)
gtk_widget_remove_css_class(old->chip, "chip-active");
}
self->active_chip_index = index;
if (index >= 0 && (guint)index < self->category_widgets->len) {
CategoryWidgets *cw = g_ptr_array_index(self->category_widgets, index);
if (cw->chip)
gtk_widget_add_css_class(cw->chip, "chip-active");
}
}
static void scroll_to_category(MoeMojiWindow *self, int index) {
CategoryWidgets *cw = g_ptr_array_index(self->category_widgets, index);
graphene_point_t p;
if (gtk_widget_compute_point(cw->header, GTK_WIDGET(self->content_box),
&GRAPHENE_POINT_INIT(0, 0), &p)) {
GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
GTK_SCROLLED_WINDOW(self->kaomoji_scroll));
gtk_adjustment_set_value(vadj, p.y);
}
}
static void on_chip_clicked(GtkButton *button, gpointer user_data) { static void on_chip_clicked(GtkButton *button, gpointer user_data) {
MoeMojiWindow *self = MOEMOJI_WINDOW(user_data); MoeMojiWindow *self = MOEMOJI_WINDOW(user_data);
const char *current = gtk_editable_get_text(GTK_EDITABLE(self->search_entry)); for (guint i = 0; i < self->category_widgets->len; i++) {
const char *name = gtk_button_get_label(button); CategoryWidgets *cw = g_ptr_array_index(self->category_widgets, i);
if (g_strcmp0(current, name) == 0) if (cw->chip == GTK_WIDGET(button)) {
gtk_editable_set_text(GTK_EDITABLE(self->search_entry), ""); scroll_to_category(self, (int)i);
else return;
gtk_editable_set_text(GTK_EDITABLE(self->search_entry), name); }
}
}
static void update_chip_from_scroll(MoeMojiWindow *self) {
double scroll_pos = gtk_adjustment_get_value(
gtk_scrolled_window_get_vadjustment(
GTK_SCROLLED_WINDOW(self->kaomoji_scroll)));
int best = 0;
for (guint i = 0; i < self->category_widgets->len; i++) {
CategoryWidgets *cw = g_ptr_array_index(self->category_widgets, i);
graphene_point_t p;
if (gtk_widget_compute_point(cw->header, GTK_WIDGET(self->content_box),
&GRAPHENE_POINT_INIT(0, 0), &p)) {
if (p.y <= scroll_pos)
best = (int)i;
}
}
set_active_chip(self, best);
}
static void update_spacer_height(MoeMojiWindow *self) {
if (!self->bottom_spacer || self->category_widgets->len == 0)
return;
GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
GTK_SCROLLED_WINDOW(self->kaomoji_scroll));
int page = (int)gtk_adjustment_get_page_size(vadj);
CategoryWidgets *last = g_ptr_array_index(self->category_widgets,
self->category_widgets->len - 1);
int spacing = gtk_box_get_spacing(self->content_box);
int last_h = gtk_widget_get_height(last->header) + spacing +
gtk_widget_get_height(last->flow);
int spacer_h = page - last_h;
if (spacer_h < 0)
spacer_h = 0;
gtk_widget_set_size_request(self->bottom_spacer, -1, spacer_h);
}
static void on_scroll_changed(GtkAdjustment *adj, gpointer user_data) {
(void)adj;
MoeMojiWindow *self = MOEMOJI_WINDOW(user_data);
update_chip_from_scroll(self);
}
static void on_page_size_changed(G_GNUC_UNUSED GtkAdjustment *adj,
G_GNUC_UNUSED GParamSpec *pspec,
gpointer user_data) {
MoeMojiWindow *self = MOEMOJI_WINDOW(user_data);
update_spacer_height(self);
}
static void on_kaomoji_scroll_map(G_GNUC_UNUSED GtkWidget *widget,
gpointer user_data) {
MoeMojiWindow *self = MOEMOJI_WINDOW(user_data);
update_chip_from_scroll(self);
} }
static void on_search_changed(GtkSearchEntry *entry, gpointer user_data) { static void on_search_changed(GtkSearchEntry *entry, gpointer user_data) {
MoeMojiWindow *self = MOEMOJI_WINDOW(user_data); MoeMojiWindow *self = MOEMOJI_WINDOW(user_data);
const char *query = gtk_editable_get_text(GTK_EDITABLE(entry)); const char *query = gtk_editable_get_text(GTK_EDITABLE(entry));
if (query == NULL || query[0] == '\0')
return;
g_autofree char *query_lower = g_utf8_strdown(query, -1);
for (guint i = 0; i < self->category_widgets->len; i++) { for (guint i = 0; i < self->category_widgets->len; i++) {
CategoryWidgets *cw = g_ptr_array_index(self->category_widgets, i); CategoryWidgets *cw = g_ptr_array_index(self->category_widgets, i);
gboolean visible; g_autofree char *name_lower = g_utf8_strdown(cw->name, -1);
if (strstr(name_lower, query_lower) != NULL) {
if (query == NULL || query[0] == '\0') { scroll_to_category(self, (int)i);
visible = TRUE; return;
} else {
char *name_lower = g_utf8_strdown(cw->name, -1);
char *query_lower = g_utf8_strdown(query, -1);
visible = (strstr(name_lower, query_lower) != NULL);
g_free(name_lower);
g_free(query_lower);
} }
gtk_widget_set_visible(cw->header, visible);
gtk_widget_set_visible(cw->flow, visible);
} }
} }
@ -180,6 +267,8 @@ static void moemoji_window_class_init(MoeMojiWindowClass *klass) {
gtk_widget_class_bind_template_child(widget_class, MoeMojiWindow, gtk_widget_class_bind_template_child(widget_class, MoeMojiWindow,
search_entry); search_entry);
gtk_widget_class_bind_template_child(widget_class, MoeMojiWindow, header_bar); gtk_widget_class_bind_template_child(widget_class, MoeMojiWindow, header_bar);
gtk_widget_class_bind_template_child(widget_class, MoeMojiWindow,
kaomoji_scroll);
} }
static void collect_categories(const char *base_dir, GHashTable *seen, static void collect_categories(const char *base_dir, GHashTable *seen,
@ -218,6 +307,7 @@ static void moemoji_window_init(MoeMojiWindow *self) {
gtk_widget_add_css_class(GTK_WIDGET(self->content_box), "content-area"); gtk_widget_add_css_class(GTK_WIDGET(self->content_box), "content-area");
self->category_widgets = self->category_widgets =
g_ptr_array_new_with_free_func(category_widgets_free); g_ptr_array_new_with_free_func(category_widgets_free);
self->active_chip_index = -1;
char *kaomoji_dir = find_kaomoji_dir(); char *kaomoji_dir = find_kaomoji_dir();
g_autofree char *user_dir = g_autofree char *user_dir =
g_build_filename(g_get_user_data_dir(), "moemoji", "kaomoji", NULL); g_build_filename(g_get_user_data_dir(), "moemoji", "kaomoji", NULL);
@ -246,6 +336,8 @@ static void moemoji_window_init(MoeMojiWindow *self) {
} }
g_ptr_array_free(entries, TRUE); g_ptr_array_free(entries, TRUE);
g_free(kaomoji_dir); g_free(kaomoji_dir);
self->bottom_spacer = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_append(self->content_box, self->bottom_spacer);
g_signal_connect(self->search_entry, "search-changed", g_signal_connect(self->search_entry, "search-changed",
G_CALLBACK(on_search_changed), self); G_CALLBACK(on_search_changed), self);
if (self->category_widgets->len > 0) { if (self->category_widgets->len > 0) {
@ -260,6 +352,7 @@ static void moemoji_window_init(MoeMojiWindow *self) {
gtk_widget_add_css_class(btn, "category-chip"); gtk_widget_add_css_class(btn, "category-chip");
gtk_widget_add_css_class(btn, "flat"); gtk_widget_add_css_class(btn, "flat");
g_signal_connect(btn, "clicked", G_CALLBACK(on_chip_clicked), self); g_signal_connect(btn, "clicked", G_CALLBACK(on_chip_clicked), self);
cw->chip = btn;
gtk_flow_box_insert(GTK_FLOW_BOX(flow), btn, -1); gtk_flow_box_insert(GTK_FLOW_BOX(flow), btn, -1);
} }
self->category_bar = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0)); self->category_bar = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0));
@ -275,5 +368,14 @@ static void moemoji_window_init(MoeMojiWindow *self) {
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(cat_scroll), flow); gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(cat_scroll), flow);
gtk_box_append(self->category_bar, cat_scroll); gtk_box_append(self->category_bar, cat_scroll);
gtk_box_append(self->outer_box, GTK_WIDGET(self->category_bar)); gtk_box_append(self->outer_box, GTK_WIDGET(self->category_bar));
GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
GTK_SCROLLED_WINDOW(self->kaomoji_scroll));
g_signal_connect(vadj, "value-changed", G_CALLBACK(on_scroll_changed),
self);
g_signal_connect(vadj, "notify::page-size",
G_CALLBACK(on_page_size_changed), self);
g_signal_connect(self->kaomoji_scroll, "map",
G_CALLBACK(on_kaomoji_scroll_map), self);
} }
} }

View File

@ -5,6 +5,7 @@
typedef struct { typedef struct {
GtkWidget *header; GtkWidget *header;
GtkWidget *flow; GtkWidget *flow;
GtkWidget *chip;
char *name; char *name;
} CategoryWidgets; } CategoryWidgets;
@ -15,7 +16,10 @@ struct _MoeMojiWindow {
GtkSearchEntry *search_entry; GtkSearchEntry *search_entry;
GtkWidget *header_bar; GtkWidget *header_bar;
GtkBox *category_bar; GtkBox *category_bar;
GtkWidget *kaomoji_scroll;
GPtrArray *category_widgets; GPtrArray *category_widgets;
GtkWidget *bottom_spacer;
int active_chip_index;
}; };
G_BEGIN_DECLS G_BEGIN_DECLS

View File

@ -2,8 +2,8 @@
<interface> <interface>
<requires lib="gtk" version="4.0" /> <requires lib="gtk" version="4.0" />
<template class="MoeMojiWindow" parent="GtkApplicationWindow"> <template class="MoeMojiWindow" parent="GtkApplicationWindow">
<property name="default-width">280</property> <property name="default-width">680</property>
<property name="default-height">600</property> <property name="default-height">400</property>
<property name="resizable">True</property> <property name="resizable">True</property>
<property name="title">MoeMoji</property> <property name="title">MoeMoji</property>
@ -18,14 +18,14 @@
<property name="orientation">vertical</property> <property name="orientation">vertical</property>
<child> <child>
<object class="GtkSearchEntry" id="search_entry"> <object class="GtkSearchEntry" id="search_entry">
<property name="placeholder-text">Filter by category...</property> <property name="placeholder-text">Jump to category...</property>
<property name="margin-start">6</property> <property name="margin-start">6</property>
<property name="margin-end">6</property> <property name="margin-end">6</property>
<property name="margin-top">6</property> <property name="margin-top">6</property>
</object> </object>
</child> </child>
<child> <child>
<object class="GtkScrolledWindow"> <object class="GtkScrolledWindow" id="kaomoji_scroll">
<property name="hscrollbar-policy">never</property> <property name="hscrollbar-policy">never</property>
<property name="vexpand">True</property> <property name="vexpand">True</property>
<child> <child>

View File

@ -53,3 +53,8 @@ wallpaper-bg > * {
border-radius: 14px; border-radius: 14px;
padding: 8px; padding: 8px;
} }
.chip-active {
background-color: rgba(255, 255, 255, 0.25);
border-radius: 8px;
}

View File

@ -1,6 +1,8 @@
#include <glib.h> #include <glib.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include <gtk/gtk.h>
#include "moemoji-internal.h" #include "moemoji-internal.h"
#include "moemoji-window.h"
static void static void
test_display_name_underscores (void) test_display_name_underscores (void)
@ -45,16 +47,16 @@ test_display_name_empty (void)
static void static void
test_find_kaomoji_with_env (void) test_find_kaomoji_with_env (void)
{ {
g_setenv ("MESON_SOURCE_ROOT", SRCDIR, TRUE);
char *dir = find_kaomoji_dir (); char *dir = find_kaomoji_dir ();
g_assert_nonnull (dir); g_assert_nonnull (dir);
g_assert_true (g_file_test (dir, G_FILE_TEST_IS_DIR)); g_assert_true (g_file_test (dir, G_FILE_TEST_IS_DIR));
g_free (dir); g_free (dir);
g_unsetenv ("MESON_SOURCE_ROOT");
} }
static gboolean find_kaomoji_bogus_passed = FALSE;
static void static void
test_find_kaomoji_bogus (void) run_find_kaomoji_bogus (void)
{ {
g_setenv ("MESON_SOURCE_ROOT", "/nonexistent", TRUE); g_setenv ("MESON_SOURCE_ROOT", "/nonexistent", TRUE);
char *saved = g_get_current_dir (); char *saved = g_get_current_dir ();
@ -65,7 +67,14 @@ test_find_kaomoji_bogus (void)
g_assert_true (g_chdir (saved) == 0); g_assert_true (g_chdir (saved) == 0);
g_free (saved); g_free (saved);
g_unsetenv ("MESON_SOURCE_ROOT"); g_setenv ("MESON_SOURCE_ROOT", SRCDIR, TRUE);
find_kaomoji_bogus_passed = TRUE;
}
static void
test_find_kaomoji_bogus (void)
{
g_assert_true (find_kaomoji_bogus_passed);
} }
static void static void
@ -145,27 +154,139 @@ test_dbusmenu_unknown (void)
g_assert_null (v); g_assert_null (v);
} }
static void
test_category_widgets_chip_init (void)
{
CategoryWidgets *cw = g_new0 (CategoryWidgets, 1);
g_assert_null (cw->chip);
g_assert_null (cw->header);
g_assert_null (cw->flow);
g_assert_null (cw->name);
g_free (cw);
}
G_GNUC_INTERNAL GResource *moemoji_get_resource (void);
static GResource *test_res = NULL;
static GtkWidget *test_win = NULL;
static MoeMojiWindow *test_self = NULL;
static void
window_test_setup (void)
{
if (!test_res) {
test_res = moemoji_get_resource ();
g_resources_register (test_res);
}
test_win = g_object_new (MOEMOJI_TYPE_WINDOW, NULL);
test_self = MOEMOJI_WINDOW (test_win);
}
static void
window_test_teardown (void)
{
gtk_window_destroy (GTK_WINDOW (test_win));
test_win = NULL;
test_self = NULL;
}
static void
test_window_chip_setup (void)
{
window_test_setup ();
g_assert_cmpuint (test_self->category_widgets->len, >, 0);
for (guint i = 0; i < test_self->category_widgets->len; i++) {
CategoryWidgets *cw = g_ptr_array_index (test_self->category_widgets, i);
g_assert_nonnull (cw->chip);
}
window_test_teardown ();
}
static void
test_window_chip_labels_match (void)
{
window_test_setup ();
for (guint i = 0; i < test_self->category_widgets->len; i++) {
CategoryWidgets *cw = g_ptr_array_index (test_self->category_widgets, i);
const char *chip_label = gtk_button_get_label (GTK_BUTTON (cw->chip));
g_assert_cmpstr (chip_label, ==, cw->name);
}
window_test_teardown ();
}
static void
test_window_initial_active_chip (void)
{
window_test_setup ();
g_assert_cmpint (test_self->active_chip_index, ==, -1);
window_test_teardown ();
}
static void
test_window_all_categories_visible (void)
{
window_test_setup ();
for (guint i = 0; i < test_self->category_widgets->len; i++) {
CategoryWidgets *cw = g_ptr_array_index (test_self->category_widgets, i);
g_assert_true (gtk_widget_get_visible (cw->header));
g_assert_true (gtk_widget_get_visible (cw->flow));
}
window_test_teardown ();
}
static void
test_window_bottom_spacer (void)
{
window_test_setup ();
g_assert_nonnull (test_self->bottom_spacer);
GtkWidget *last = gtk_widget_get_last_child (GTK_WIDGET (test_self->content_box));
g_assert_true (last == test_self->bottom_spacer);
window_test_teardown ();
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
g_setenv ("MESON_SOURCE_ROOT", SRCDIR, TRUE);
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_add_func ("/display-name/underscores", test_display_name_underscores); run_find_kaomoji_bogus ();
g_test_add_func ("/display-name/no-underscores", test_display_name_no_underscores);
g_test_add_func ("/display-name/already-upper", test_display_name_already_upper); gtk_init ();
g_test_add_func ("/display-name/single-char", test_display_name_single_char);
g_test_add_func ("/display-name/empty", test_display_name_empty); g_test_add_func ("/display-name/underscores", test_display_name_underscores);
g_test_add_func ("/find-kaomoji/with-env", test_find_kaomoji_with_env); g_test_add_func ("/display-name/no-underscores", test_display_name_no_underscores);
g_test_add_func ("/find-kaomoji/bogus", test_find_kaomoji_bogus); g_test_add_func ("/display-name/already-upper", test_display_name_already_upper);
g_test_add_func ("/sni/category", test_sni_category); g_test_add_func ("/display-name/single-char", test_display_name_single_char);
g_test_add_func ("/sni/id", test_sni_id); g_test_add_func ("/display-name/empty", test_display_name_empty);
g_test_add_func ("/sni/item-is-menu", test_sni_item_is_menu); g_test_add_func ("/find-kaomoji/with-env", test_find_kaomoji_with_env);
g_test_add_func ("/sni/menu", test_sni_menu); g_test_add_func ("/find-kaomoji/bogus", test_find_kaomoji_bogus);
g_test_add_func ("/sni/unknown", test_sni_unknown); g_test_add_func ("/sni/category", test_sni_category);
g_test_add_func ("/dbusmenu/version", test_dbusmenu_version); g_test_add_func ("/sni/id", test_sni_id);
g_test_add_func ("/dbusmenu/status", test_dbusmenu_status); g_test_add_func ("/sni/item-is-menu", test_sni_item_is_menu);
g_test_add_func ("/dbusmenu/text-direction", test_dbusmenu_text_direction); g_test_add_func ("/sni/menu", test_sni_menu);
g_test_add_func ("/dbusmenu/unknown", test_dbusmenu_unknown); g_test_add_func ("/sni/unknown", test_sni_unknown);
g_test_add_func ("/dbusmenu/version", test_dbusmenu_version);
g_test_add_func ("/dbusmenu/status", test_dbusmenu_status);
g_test_add_func ("/dbusmenu/text-direction", test_dbusmenu_text_direction);
g_test_add_func ("/dbusmenu/unknown", test_dbusmenu_unknown);
g_test_add_func ("/category-widgets/chip-init", test_category_widgets_chip_init);
g_test_add_func ("/window/chip-setup", test_window_chip_setup);
g_test_add_func ("/window/chip-labels-match", test_window_chip_labels_match);
g_test_add_func ("/window/initial-active-chip", test_window_initial_active_chip);
g_test_add_func ("/window/all-categories-visible", test_window_all_categories_visible);
g_test_add_func ("/window/bottom-spacer", test_window_bottom_spacer);
return g_test_run (); return g_test_run ();
} }