Added widget builder API

This commit is contained in:
Scoopta 2020-06-22 22:45:51 -07:00
parent 2afc0b9809
commit 5c59d8317c
7 changed files with 296 additions and 27 deletions

34
inc/widget_builder.h Normal file
View File

@ -0,0 +1,34 @@
/*
* Copyright (C) 2020 Scoopta
* This file is part of Wofi
* Wofi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Wofi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Wofi. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WIDGET_BUILDER_H
#define WIDGET_BUILDER_H
#include <widget_builder_api.h>
#include <property_box.h>
struct widget_builder {
WofiPropertyBox* box;
struct widget* widget;
struct mode* mode;
size_t actions;
};
void wofi_free_widget_builder(struct widget_builder* builder);
#endif

41
inc/widget_builder_api.h Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2020 Scoopta
* This file is part of Wofi
* Wofi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Wofi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Wofi. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WIDGET_BUILDER_API_H
#define WIDGET_BUILDER_API_H
#include <stddef.h>
#include <wofi_api.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
struct widget_builder* wofi_create_widget_builder(struct mode* mode, size_t actions);
void wofi_widget_builder_set_search_text(struct widget_builder* builder, char* search_text);
void wofi_widget_builder_set_action(struct widget_builder* builder, char* action);
void wofi_widget_builder_insert_text(struct widget_builder* builder, char* text, char* css_name);
void wofi_widget_builder_insert_image(struct widget_builder* builder, GdkPixbuf* pixbuf, char* css_name);
struct widget_builder* wofi_widget_builder_get_idx(struct widget_builder* builder, size_t idx);
struct widget* wofi_widget_builder_get_widget(struct widget_builder* builder);
#endif

View File

@ -24,6 +24,21 @@
#include <map.h> #include <map.h>
#include <gtk/gtk.h>
struct widget {
size_t action_count;
char* mode, **text, *search_text, **actions;
struct widget_builder* builder;
};
struct mode {
void (*mode_exec)(const gchar* cmd);
struct widget* (*mode_get_widget)(void);
char* name, *dso;
struct wl_list link;
};
void wofi_init(struct map* config); void wofi_init(struct map* config);
void wofi_load_css(bool nyan); void wofi_load_css(bool nyan);

79
man/wofi-widget-builder.3 Normal file
View File

@ -0,0 +1,79 @@
.TH wofi\-widget\-builder 3
.SH NAME
wofi \- Widget builder API functions
.SH DESCRIPTION
The functions documented here are used for building custom widgets with more power and flexibility than previously allowed. They are defined in wofi_widget_builder_api.h
.TP
.B struct widget_builder* wofi_create_widget_builder(struct mode* mode, size_t actions)
Creates multiple widget builders. The number of builders created is specified by actions and is returned as an array.
.B struct mode* mode
\- The \fBstruct mode*\fR given to your mode's \fBinit()\fR function.
.B size_t actions
\- The number of builders to create
.TP
.B void wofi_widget_builder_set_search_text(struct widget_builder* builder, char* search_text)
Sets the search text for the widget specified by the builder
.B struct widget_builder* builder
\- The builder that contains the widget to set the search text for
.B char* search_text
\- The text to set as the search text
.TP
.B void wofi_widget_builder_set_action(struct widget_builder* builder, char* action)
Sets the action for the widget specified by the builder
.B struct widget_builder* builder
\- The builder that contains the widget to set the action for
.B char* action
\- The text to set as the action
.TP
.B void wofi_widget_builder_insert_text(struct widget_builder* builder, char* text, char* css_name)
Inserts text into the widget specified by the builder
.B struct widget_builder* builder
\- The builder that contains the widget to add the text to
.B char* text
\- The text to add to the widget
.B char* css_name
\- The name of the CSS node for this text. The name that will be assigned is #mode_name-css_name where mode_name is the name of the mode, i.e. drun etc.
.TP
.B void wofi_widget_builder_insert_image(struct widget_builder* builder, GdkPixbuf* pixbuf, char* css_name)
Inserts an image into the widget specified by the builder
.B struct widget_builder* builder
\- The builder that contains the widget to add the image to
.B GdkPixbuf* pixbuf
\- The image to add to the widget
.B char* css_name
\- The name of the CSS node for this image. The name that will be assigned is #mode_name-css_name where mode_name is the name of the mode, i.e. drun etc.
.TP
.B struct widget_builder* wofi_widget_builder_get_idx(struct widget_builder* builder, size_t idx)
Gets the widget_builder at the provided index in the array
.B struct widget_builder* builder
\- The array of builders to get the builder from
.B size_t idx
\- The index in the array to get
.TP
.B struct widget* wofi_widget_builder_get_widget(struct widget_builder* builder)
Constructs a new widget from the specified builder, the widget can be returned by \fBstruct widget* get_widget(void)\fR
.B struct widget_builder* builder
\- The builder to construct a widget for

View File

@ -24,6 +24,7 @@ sources = ['src/config.c',
'src/map.c', 'src/map.c',
'src/property_box.c', 'src/property_box.c',
'src/utils.c', 'src/utils.c',
'src/widget_builder.c',
'src/wofi.c', 'src/wofi.c',
'proto/wlr-layer-shell-unstable-v1-protocol.c', 'proto/wlr-layer-shell-unstable-v1-protocol.c',
'proto/xdg-output-unstable-v1-protocol.c', 'proto/xdg-output-unstable-v1-protocol.c',

92
src/widget_builder.c Normal file
View File

@ -0,0 +1,92 @@
/*
* Copyright (C) 2020 Scoopta
* This file is part of Wofi
* Wofi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Wofi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Wofi. If not, see <http://www.gnu.org/licenses/>.
*/
#include <widget_builder.h>
#include <wofi.h>
#include <utils.h>
struct widget_builder* wofi_create_widget_builder(struct mode* mode, size_t actions) {
struct widget_builder* builder = calloc(actions, sizeof(struct widget_builder));
for(size_t count = 0; count < actions; ++count) {
builder[count].mode = mode;
builder[count].box = WOFI_PROPERTY_BOX(wofi_property_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
if(count == 0) {
builder->actions = actions;
}
}
return builder;
}
void wofi_widget_builder_set_search_text(struct widget_builder* builder, char* search_text) {
wofi_property_box_add_property(builder->box, "filter", search_text);
}
void wofi_widget_builder_set_action(struct widget_builder* builder, char* action) {
wofi_property_box_add_property(builder->box, "action", action);
}
void wofi_widget_builder_insert_text(struct widget_builder* builder, char* text, char* css_name) {
GtkWidget* label = gtk_label_new(text);
gtk_container_add(GTK_CONTAINER(builder->box), label);
if(css_name != NULL) {
char* tmp = utils_concat(3, builder->mode->name, "-", css_name);
gtk_widget_set_name(label, tmp);
free(tmp);
}
}
void wofi_widget_builder_insert_image(struct widget_builder* builder, GdkPixbuf* pixbuf, char* css_name) {
GtkWidget* img = gtk_image_new_from_pixbuf(pixbuf);
gtk_container_add(GTK_CONTAINER(builder->box), img);
if(css_name != NULL) {
char* tmp = utils_concat(3, builder->mode->name, "-", css_name);
gtk_widget_set_name(img, tmp);
free(tmp);
}
}
struct widget_builder* wofi_widget_builder_get_idx(struct widget_builder* builder, size_t idx) {
return builder + idx;
}
struct widget* wofi_widget_builder_get_widget(struct widget_builder* builder) {
if(builder->actions == 0) {
fprintf(stderr, "%s: This is not the 0 index of the widget_builder array\n", builder->mode->name);
return NULL;
}
if(builder->widget == NULL) {
builder->widget = malloc(sizeof(struct widget));
builder->widget->builder = builder;
builder->widget->action_count = builder->actions;
}
for(size_t count = 0; count < builder->actions; ++count) {
}
return builder->widget;
}
void wofi_free_widget_builder(struct widget_builder* builder) {
if(builder->widget != NULL) {
free(builder->widget);
}
free(builder);
}

View File

@ -30,11 +30,11 @@
#include <utils.h> #include <utils.h>
#include <config.h> #include <config.h>
#include <property_box.h> #include <property_box.h>
#include <widget_builder.h>
#include <xdg-output-unstable-v1-client-protocol.h> #include <xdg-output-unstable-v1-client-protocol.h>
#include <wlr-layer-shell-unstable-v1-client-protocol.h> #include <wlr-layer-shell-unstable-v1-client-protocol.h>
#include <gtk/gtk.h>
#include <pango/pango.h> #include <pango/pango.h>
#include <gdk/gdkwayland.h> #include <gdk/gdkwayland.h>
@ -109,18 +109,6 @@ static struct wl_list outputs;
static struct zxdg_output_manager_v1* output_manager; static struct zxdg_output_manager_v1* output_manager;
static struct zwlr_layer_surface_v1* wlr_surface; static struct zwlr_layer_surface_v1* wlr_surface;
struct mode {
void (*mode_exec)(const gchar* cmd);
struct widget* (*mode_get_widget)(void);
char* name, *dso;
struct wl_list link;
};
struct widget {
size_t action_count;
char* mode, **text, *search_text, **actions;
};
struct output_node { struct output_node {
char* name; char* name;
struct wl_output* output; struct wl_output* output;
@ -502,11 +490,17 @@ static gboolean _insert_widget(gpointer data) {
if(node == NULL) { if(node == NULL) {
return FALSE; return FALSE;
} }
GtkWidget* parent; GtkWidget* parent;
if(node->action_count > 1 && !no_actions) { if(node->action_count > 1 && !no_actions) {
parent = gtk_expander_new(""); parent = gtk_expander_new("");
g_signal_connect(parent, "activate", G_CALLBACK(expand), NULL); g_signal_connect(parent, "activate", G_CALLBACK(expand), NULL);
GtkWidget* box = create_label(node->mode, node->text[0], node->search_text, node->actions[0]); GtkWidget* box;
if(node->builder == NULL) {
box = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
} else {
box = GTK_WIDGET(node->builder->box);
}
gtk_expander_set_label_widget(GTK_EXPANDER(parent), box); gtk_expander_set_label_widget(GTK_EXPANDER(parent), box);
GtkWidget* exp_box = gtk_list_box_new(); GtkWidget* exp_box = gtk_list_box_new();
@ -514,7 +508,11 @@ static gboolean _insert_widget(gpointer data) {
g_signal_connect(exp_box, "row-activated", G_CALLBACK(activate_item), NULL); g_signal_connect(exp_box, "row-activated", G_CALLBACK(activate_item), NULL);
gtk_container_add(GTK_CONTAINER(parent), exp_box); gtk_container_add(GTK_CONTAINER(parent), exp_box);
for(size_t count = 1; count < node->action_count; ++count) { for(size_t count = 1; count < node->action_count; ++count) {
box = create_label(node->mode, node->text[count], node->search_text, node->actions[count]); if(node->builder == NULL) {
box = create_label(node->mode, node->text[count], node->search_text, node->actions[count]);
} else {
box = GTK_WIDGET(node->builder[count].box);
}
GtkWidget* row = gtk_list_box_row_new(); GtkWidget* row = gtk_list_box_row_new();
gtk_widget_set_name(row, "entry"); gtk_widget_set_name(row, "entry");
@ -523,8 +521,13 @@ static gboolean _insert_widget(gpointer data) {
gtk_container_add(GTK_CONTAINER(exp_box), row); gtk_container_add(GTK_CONTAINER(exp_box), row);
} }
} else { } else {
parent = create_label(node->mode, node->text[0], node->search_text, node->actions[0]); if(node->builder == NULL) {
parent = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
} else {
parent = GTK_WIDGET(node->builder->box);
}
} }
gtk_widget_set_halign(parent, content_halign); gtk_widget_set_halign(parent, content_halign);
GtkWidget* child = gtk_flow_box_child_new(); GtkWidget* child = gtk_flow_box_child_new();
gtk_widget_set_name(child, "entry"); gtk_widget_set_name(child, "entry");
@ -546,17 +549,21 @@ static gboolean _insert_widget(gpointer data) {
gtk_widget_set_visible(box, FALSE); gtk_widget_set_visible(box, FALSE);
} }
free(node->mode); if(node->builder != NULL) {
for(size_t count = 0; count < node->action_count; ++count) { wofi_free_widget_builder(node->builder);
free(node->text[count]); } else {
free(node->mode);
for(size_t count = 0; count < node->action_count; ++count) {
free(node->text[count]);
}
free(node->text);
free(node->search_text);
for(size_t count = 0; count < node->action_count; ++count) {
free(node->actions[count]);
}
free(node->actions);
free(node);
} }
free(node->text);
free(node->search_text);
for(size_t count = 0; count < node->action_count; ++count) {
free(node->actions[count]);
}
free(node->actions);
free(node);
return TRUE; return TRUE;
} }
@ -774,7 +781,7 @@ struct wl_list* wofi_read_cache(struct mode* mode) {
} }
struct widget* wofi_create_widget(struct mode* mode, char* text[], char* search_text, char* actions[], size_t action_count) { struct widget* wofi_create_widget(struct mode* mode, char* text[], char* search_text, char* actions[], size_t action_count) {
struct widget* widget = malloc(sizeof(struct widget)); struct widget* widget = calloc(1, sizeof(struct widget));
widget->mode = strdup(mode->name); widget->mode = strdup(mode->name);
widget->text = malloc(action_count * sizeof(char*)); widget->text = malloc(action_count * sizeof(char*));
for(size_t count = 0; count < action_count; ++count) { for(size_t count = 0; count < action_count; ++count) {