diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-03-15 01:35:37 +0100 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-03-15 01:35:37 +0100 |
commit | 5e1a9a66817f14e2d39962b3ebcee7752925aa95 (patch) | |
tree | 0c0d5ae48f648ce28edbc25e8dc958beefe85ed1 | |
parent | 87d68ebf5ba44793de328fde54a6d830f6b6157b (diff) |
Reduce redundant callbacks
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | src/application.c | 5 | ||||
-rw-r--r-- | src/event.c | 119 | ||||
-rw-r--r-- | src/ui/accounts.c | 2 | ||||
-rw-r--r-- | src/ui/chat_entry.c | 5 | ||||
-rw-r--r-- | src/ui/chat_entry.h | 2 | ||||
-rw-r--r-- | src/ui/messenger.c | 38 | ||||
-rw-r--r-- | src/ui/messenger.h | 4 |
7 files changed, 134 insertions, 41 deletions
diff --git a/src/application.c b/src/application.c index 638e6f5..335db11 100644 --- a/src/application.c +++ b/src/application.c @@ -239,7 +239,10 @@ _application_event_call(gpointer user_data) MESSENGER_ApplicationEventCall *call; call = (MESSENGER_ApplicationEventCall*) user_data; + + pthread_mutex_lock(&(call->app->chat.mutex)); call->event(call->app); + pthread_mutex_unlock(&(call->app->chat.mutex)); GNUNET_free(call); return FALSE; @@ -258,7 +261,7 @@ application_call_event(MESSENGER_Application *app, call->app = app; call->event = event; - g_idle_add(G_SOURCE_FUNC(_application_event_call), call); + g_timeout_add(0, G_SOURCE_FUNC(_application_event_call), call); } typedef struct MESSENGER_ApplicationMessageEventCall diff --git a/src/event.c b/src/event.c index 2094bdd..5330e5b 100644 --- a/src/event.c +++ b/src/event.c @@ -103,11 +103,100 @@ event_handle_warning(MESSENGER_Application *app, ); } +static gboolean +_idle_refresh_accounts(gpointer user_data) +{ + MESSENGER_Application *app = (MESSENGER_Application*) user_data; + + if (!(app->ui.messenger.main_window)) + goto refresh_exit; + + if (gtk_widget_is_visible(GTK_WIDGET(app->ui.messenger.main_window))) + ui_messenger_refresh(app, &(app->ui.messenger)); + else + ui_accounts_dialog_refresh(app, &(app->ui.accounts)); + +refresh_exit: + app->ui.messenger.account_refresh = 0; + return FALSE; +} + void event_refresh_accounts(MESSENGER_Application *app) { - ui_accounts_dialog_refresh(app, &(app->ui.accounts)); - ui_messenger_refresh(app, &(app->ui.messenger)); + if (app->ui.messenger.account_refresh) + g_source_remove(app->ui.messenger.account_refresh); + + if (app->ui.messenger.main_window) + app->ui.messenger.account_refresh = g_idle_add( + G_SOURCE_FUNC(_idle_refresh_accounts), + app + ); + else + app->ui.messenger.account_refresh = 0; +} + +static gboolean +_select_chat_to_activate(gpointer user_data) +{ + UI_CHAT_ENTRY_Handle *entry = (UI_CHAT_ENTRY_Handle*) user_data; + + if (!(entry->chat)) + return FALSE; + + MESSENGER_Application *app = entry->chat->app; + + if (!app) + return FALSE; + + UI_MESSENGER_Handle *ui = &(app->ui.messenger); + + GtkListBoxRow *row = GTK_LIST_BOX_ROW( + gtk_widget_get_parent(entry->entry_box) + ); + + gtk_list_box_select_row(ui->chats_listbox, row); + gtk_list_box_invalidate_filter(ui->chats_listbox); + + gtk_widget_activate(GTK_WIDGET(row)); + + ui->chat_selection = 0; + return FALSE; +} + +static gboolean +_idle_chat_entry_update(gpointer user_data) +{ + UI_CHAT_ENTRY_Handle *entry = (UI_CHAT_ENTRY_Handle*) user_data; + + if ((!(entry->chat)) || (!(entry->chat->app)) || + (!(entry->chat->send_text_view))) + goto update_exit; + + struct GNUNET_CHAT_Context *context = (struct GNUNET_CHAT_Context*) ( + g_object_get_qdata( + G_OBJECT(entry->chat->send_text_view), + entry->chat->app->quarks.data + ) + ); + + ui_chat_entry_update(entry, entry->chat->app, context); + +update_exit: + entry->update = 0; + return FALSE; +} + +static void +enqueue_chat_entry_update(UI_CHAT_ENTRY_Handle *entry) +{ + if (entry->update) + g_source_remove(entry->update); + + entry->update = g_idle_add( + G_SOURCE_FUNC(_idle_chat_entry_update), + entry + ); } static void @@ -117,7 +206,7 @@ _add_new_chat_entry(MESSENGER_Application *app, UI_MESSENGER_Handle *ui = &(app->ui.messenger); UI_CHAT_ENTRY_Handle *entry = ui_chat_entry_new(app); - ui_chat_entry_update(entry, app, context); + enqueue_chat_entry_update(entry); gtk_container_add(GTK_CONTAINER(ui->chats_listbox), entry->entry_box); GNUNET_CHAT_context_set_user_pointer(context, entry); @@ -145,10 +234,13 @@ _add_new_chat_entry(MESSENGER_Application *app, entry ); - gtk_list_box_select_row(ui->chats_listbox, row); - gtk_list_box_invalidate_filter(ui->chats_listbox); + if (ui->chat_selection) + g_source_remove(ui->chat_selection); - gtk_widget_activate(GTK_WIDGET(row)); + ui->chat_selection = g_idle_add( + G_SOURCE_FUNC(_select_chat_to_activate), + entry + ); } static int @@ -275,7 +367,7 @@ event_update_chats(MESSENGER_Application *app, if (!handle) _add_new_chat_entry(app, context); else - ui_chat_entry_update(handle, app, context); + enqueue_chat_entry_update(handle); else if (handle) _clear_chat_entry(gtk_widget_get_parent(handle->entry_box), app); @@ -298,7 +390,7 @@ _update_contact_context(MESSENGER_Application *app, if (!handle) return; - ui_chat_entry_update(handle, app, context); + enqueue_chat_entry_update(handle); } void @@ -365,7 +457,7 @@ event_presence_contact(MESSENGER_Application *app, GNUNET_CHAT_member_set_user_pointer(context, contact, message); - ui_chat_entry_update(handle, app, context); + enqueue_chat_entry_update(handle); } void @@ -388,7 +480,7 @@ event_update_contacts(MESSENGER_Application *app, if (!handle) return; - ui_chat_entry_update(handle, app, context); + enqueue_chat_entry_update(handle); } static void @@ -453,7 +545,8 @@ event_invitation(MESSENGER_Application *app, gtk_widget_show(GTK_WIDGET(message->accept_button)); ui_chat_add_message(handle->chat, app, message); - ui_chat_entry_update(handle, app, context); + + enqueue_chat_entry_update(handle); } void @@ -518,7 +611,7 @@ event_receive_message(MESSENGER_Application *app, ui_chat_add_message(handle->chat, app, message); skip_message: - ui_chat_entry_update(handle, app, context); + enqueue_chat_entry_update(handle); } void @@ -546,5 +639,5 @@ event_delete_message(MESSENGER_Application *app, messages = messages->next; } - ui_chat_entry_update(handle, app, context); + enqueue_chat_entry_update(handle); } diff --git a/src/ui/accounts.c b/src/ui/accounts.c index 1a95b7b..16cd621 100644 --- a/src/ui/accounts.c +++ b/src/ui/accounts.c @@ -51,6 +51,8 @@ _show_messenger_main_window(gpointer user_data) { MESSENGER_Application *app = (MESSENGER_Application*) user_data; + ui_messenger_refresh(app, &(app->ui.messenger)); + gtk_widget_show(GTK_WIDGET(app->ui.messenger.main_window)); return FALSE; } diff --git a/src/ui/chat_entry.c b/src/ui/chat_entry.c index 3c9461e..f538978 100644 --- a/src/ui/chat_entry.c +++ b/src/ui/chat_entry.c @@ -34,6 +34,8 @@ ui_chat_entry_new(MESSENGER_Application *app) { UI_CHAT_ENTRY_Handle* handle = g_malloc(sizeof(UI_CHAT_ENTRY_Handle)); + memset(handle, 0, sizeof(*handle)); + handle->chat = ui_chat_new(app); handle->builder = gtk_builder_new_from_resource( application_get_resource_path(app, "ui/chat_entry.ui") @@ -153,5 +155,8 @@ ui_chat_entry_delete(UI_CHAT_ENTRY_Handle *handle) g_object_unref(handle->builder); + if (handle->update) + g_source_remove(handle->update); + g_free(handle); } diff --git a/src/ui/chat_entry.h b/src/ui/chat_entry.h index 8c90624..3a614c4 100644 --- a/src/ui/chat_entry.h +++ b/src/ui/chat_entry.h @@ -29,6 +29,8 @@ typedef struct UI_CHAT_ENTRY_Handle { + guint update; + UI_CHAT_Handle *chat; GtkBuilder *builder; diff --git a/src/ui/messenger.c b/src/ui/messenger.c index 633920c..45da0cd 100644 --- a/src/ui/messenger.c +++ b/src/ui/messenger.c @@ -280,31 +280,12 @@ handle_main_window_destroy(UNUSED GtkWidget *window, application_exit(app, MESSENGER_QUIT); } -static void -_switch_accounts_listbox_connection(MESSENGER_Application *app, - UI_MESSENGER_Handle *handle, - gboolean enabled) -{ - if (enabled) - handle->accounts_signal = g_signal_connect( - handle->accounts_listbox, - "row-activated", - G_CALLBACK(handle_accounts_listbox_row_activated), - app - ); - else - g_signal_handler_disconnect( - handle->accounts_listbox, - handle->accounts_signal - ); -} - void ui_messenger_init(MESSENGER_Application *app, UI_MESSENGER_Handle *handle) { + memset(handle, 0, sizeof(*handle)); handle->app = app; - handle->chat_entries = NULL; handle->builder = gtk_builder_new_from_resource( application_get_resource_path(app, "ui/messenger.ui") @@ -415,7 +396,12 @@ ui_messenger_init(MESSENGER_Application *app, gtk_builder_get_object(handle->builder, "add_account_listbox_row") ); - _switch_accounts_listbox_connection(app, handle, TRUE); + g_signal_connect( + handle->accounts_listbox, + "row-activated", + G_CALLBACK(handle_accounts_listbox_row_activated), + app + ); handle->new_contact_button = GTK_BUTTON( gtk_builder_get_object(handle->builder, "new_contact_button") @@ -582,8 +568,6 @@ ui_messenger_refresh(MESSENGER_Application *app, if (!(handle->accounts_listbox)) return; - _switch_accounts_listbox_connection(app, handle, FALSE); - gtk_container_foreach( GTK_CONTAINER(handle->accounts_listbox), _clear_accounts_listbox, @@ -595,8 +579,6 @@ ui_messenger_refresh(MESSENGER_Application *app, _messenger_iterate_accounts, app ); - - _switch_accounts_listbox_connection(app, handle, TRUE); } gboolean @@ -626,5 +608,11 @@ ui_messenger_cleanup(UI_MESSENGER_Handle *handle) if (handle->chat_entries) g_list_free_full(handle->chat_entries, (GDestroyNotify) ui_chat_entry_delete); + if (handle->chat_selection) + g_source_remove(handle->chat_selection); + + if (handle->account_refresh) + g_source_remove(handle->account_refresh); + memset(handle, 0, sizeof(*handle)); } diff --git a/src/ui/messenger.h b/src/ui/messenger.h index ef1ae60..54f8dfa 100644 --- a/src/ui/messenger.h +++ b/src/ui/messenger.h @@ -38,6 +38,8 @@ typedef struct UI_MESSENGER_Handle MESSENGER_Application *app; GList *chat_entries; + guint chat_selection; + guint account_refresh; GtkBuilder *builder; GtkApplicationWindow *main_window; @@ -72,8 +74,6 @@ typedef struct UI_MESSENGER_Handle GtkStack *chats_stack; GtkWidget *no_chat_box; - - gulong accounts_signal; } UI_MESSENGER_Handle; void |