Reduced code duplication when inserting widgets

This commit is contained in:
Scoopta 2019-08-28 20:43:47 -07:00
parent 5cbc889868
commit a499c1cacd

View File

@ -139,7 +139,7 @@ static GtkWidget* create_label(char* text, char* action) {
return box; return box;
} }
static gboolean insert_widget(gpointer data) { static gboolean _insert_widget(gpointer data) {
struct node* node = data; struct node* node = data;
GtkWidget* box = create_label(node->text, node->action); GtkWidget* box = create_label(node->text, node->action);
gtk_container_add(node->container, box); gtk_container_add(node->container, box);
@ -202,12 +202,20 @@ static struct wl_list* read_cache(char* mode) {
return cache; return cache;
} }
static void insert_widget(char* text, char* action, GtkContainer* container) {
struct node* widget = malloc(sizeof(struct node));
widget->text = text;
widget->action = action;
widget->container = container;
g_idle_add(_insert_widget, widget);
utils_sleep_millis(1);
}
static struct map* load_cache(char* mode) { static struct map* load_cache(char* mode) {
struct map* cached = map_init(); struct map* cached = map_init();
struct wl_list* cache = read_cache(mode); struct wl_list* cache = read_cache(mode);
struct cache_line* node, *tmp; struct cache_line* node, *tmp;
wl_list_for_each_reverse_safe(node, tmp, cache, link) { wl_list_for_each_reverse_safe(node, tmp, cache, link) {
struct node* label = malloc(sizeof(struct node));
char* text = strrchr(node->line, '/'); char* text = strrchr(node->line, '/');
char* action = strchr(node->line, ' ') + 1; char* action = strchr(node->line, ' ') + 1;
if(text == NULL) { if(text == NULL) {
@ -216,11 +224,7 @@ static struct map* load_cache(char* mode) {
++text; ++text;
} }
map_put(cached, action, "true"); map_put(cached, action, "true");
label->text = strdup(text); insert_widget(strdup(text), strdup(action), GTK_CONTAINER(inner_box));
label->action = strdup(action);
label->container = GTK_CONTAINER(inner_box);
g_idle_add(insert_widget, label);
utils_sleep_millis(1);
free(node->line); free(node->line);
wl_list_remove(&node->link); wl_list_remove(&node->link);
free(node); free(node);
@ -249,12 +253,7 @@ static void* do_run(void* data) {
struct stat info; struct stat info;
stat(full_path, &info); stat(full_path, &info);
if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) && !map_contains(cached, full_path)) { if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) && !map_contains(cached, full_path)) {
struct node* node = malloc(sizeof(struct node)); insert_widget(strdup(entry->d_name), strdup(full_path), GTK_CONTAINER(inner_box));
node->text = strdup(entry->d_name);
node->action = strdup(full_path);
node->container = GTK_CONTAINER(inner_box);
g_idle_add(insert_widget, node);
utils_sleep_millis(1);
} }
free(full_path); free(full_path);
} }
@ -280,12 +279,7 @@ static void* do_dmenu(void* data) {
if(map_contains(cached, line)) { if(map_contains(cached, line)) {
continue; continue;
} }
struct node* node = malloc(sizeof(struct node)); insert_widget(strdup(line), strdup(line), GTK_CONTAINER(inner_box));
node->text = strdup(line);
node->action = strdup(line);
node->container = GTK_CONTAINER(inner_box);
g_idle_add(insert_widget, node);
utils_sleep_millis(1);
} }
free(line); free(line);
return NULL; return NULL;
@ -345,12 +339,7 @@ static void* do_drun(void* data) {
} else { } else {
text = strdup(name); text = strdup(name);
} }
struct node* node = malloc(sizeof(struct node)); insert_widget(text, strdup(full_path), GTK_CONTAINER(inner_box));
node->text = text;
node->action = strdup(full_path);
node->container = GTK_CONTAINER(inner_box);
g_idle_add(insert_widget, node);
utils_sleep_millis(1);
free(full_path); free(full_path);
} }
closedir(dir); closedir(dir);