Skip to content

Commit

Permalink
Merge pull request #65 from ad1217/simple-css
Browse files Browse the repository at this point in the history
Allow the user to configure the style directly via CSS
  • Loading branch information
denesb authored Feb 28, 2022
2 parents 3dcf21b + 92da5fd commit a402de5
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 141 deletions.
187 changes: 87 additions & 100 deletions panel-plugin/i3w-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,11 @@ typedef struct {
} ConfigDialogClosedParam;

void
normal_color_changed(GtkWidget *button, i3WorkspacesConfig *config);
use_css_changed(GtkStack *stack, GParamSpec *pspec, i3WorkspacesConfig *config);
void
focused_color_changed(GtkWidget *button, i3WorkspacesConfig *config);
color_changed(GtkWidget *button, GdkRGBA *color_setting);
void
urgent_color_changed(GtkWidget *button, i3WorkspacesConfig *config);
void
visible_color_changed(GtkWidget *button, i3WorkspacesConfig *config);
void
mode_color_changed(GtkWidget *button, i3WorkspacesConfig *config);
css_changed(GtkTextBuffer *buffer, i3WorkspacesConfig *config);
void
strip_workspace_numbers_changed(GtkWidget *button, i3WorkspacesConfig *config);
void
Expand All @@ -57,43 +53,6 @@ config_dialog_closed(GtkWidget *dialog, int response, ConfigDialogClosedParam *p

/* Function Implementations */

guint32
serialize_gdkrgba(GdkRGBA *gdkrgba)
{
guint32 color = 0;

// convert the GdkRGBA components to 8 bit ints
guint8 red_component = gdkrgba->red * 255;
guint8 green_component = gdkrgba->green * 255;
guint8 blue_component = gdkrgba->blue * 255;

// shift and add the color components
color += ((guint32) red_component) << 16;
color += ((guint32) green_component) << 8;
color += ((guint32) blue_component);

return color;
}

GdkRGBA *
unserialize_gdkrgba(guint32 color)
{
GdkRGBA *gdkrgba = g_new0(GdkRGBA, 1);

// Mask and shift the color components
guint8 red_component = (color & 0xff0000) >> 16;
guint8 green_component = (color & 0x00ff00) >> 8;
guint8 blue_component = (color & 0x0000ff);

// convert back to floats in range 0.0 to 1.0
gdkrgba->red = red_component / 255.0;
gdkrgba->green = green_component / 255.0;
gdkrgba->blue = blue_component / 255.0;
gdkrgba->alpha = 1.0;

return gdkrgba;
}

i3WorkspacesConfig *
i3_workspaces_config_new()
{
Expand All @@ -103,6 +62,7 @@ i3_workspaces_config_new()
void
i3_workspaces_config_free(i3WorkspacesConfig *config)
{
g_free(config->css);
g_free(config->output);
g_free(config);
}
Expand All @@ -117,11 +77,27 @@ i3_workspaces_config_load(i3WorkspacesConfig *config, XfcePanelPlugin *plugin)
XfceRc *rc = xfce_rc_simple_open(file, FALSE);
g_free(file);

config->normal_color = xfce_rc_read_int_entry(rc, "normal_color", 0x000000);
config->focused_color = xfce_rc_read_int_entry(rc, "focused_color", 0x000000);
config->urgent_color = xfce_rc_read_int_entry(rc, "urgent_color", 0xff0000);
config->mode_color = xfce_rc_read_int_entry(rc, "mode_color", 0xff0000);
config->visible_color = xfce_rc_read_int_entry(rc, "visible_color", 0x000000);
gdk_rgba_parse(&config->normal_color,
xfce_rc_read_entry(rc, "normal_color", "#000000"));
gdk_rgba_parse(&config->focused_color,
xfce_rc_read_entry(rc, "focused_color", "#000000"));
gdk_rgba_parse(&config->urgent_color,
xfce_rc_read_entry(rc, "urgent_color", "#ff0000"));
gdk_rgba_parse(&config->mode_color,
xfce_rc_read_entry(rc, "mode_color", "#ff0000"));
gdk_rgba_parse(&config->visible_color,
xfce_rc_read_entry(rc, "visible_color", "#000000"));

const gchar default_css[] =
".workspace { }\n"
".workspace.visible { }\n"
".workspace.focused { font-weight: bold; }\n"
".workspace.urgent { color: red; }\n"
".binding-mode { }\n";

config->css = g_strdup(xfce_rc_read_entry(rc, "css", default_css));
config->use_css = xfce_rc_read_bool_entry(rc, "use_css", FALSE);

config->strip_workspace_numbers = xfce_rc_read_bool_entry(rc,
"strip_workspace_numbers", FALSE);
config->auto_detect_outputs = xfce_rc_read_bool_entry(rc,
Expand All @@ -143,11 +119,13 @@ i3_workspaces_config_save(i3WorkspacesConfig *config, XfcePanelPlugin *plugin)
XfceRc *rc = xfce_rc_simple_open(file, FALSE);
g_free(file);

xfce_rc_write_int_entry(rc, "normal_color", config->normal_color);
xfce_rc_write_int_entry(rc, "focused_color", config->focused_color);
xfce_rc_write_int_entry(rc, "urgent_color", config->urgent_color);
xfce_rc_write_int_entry(rc, "mode_color", config->mode_color);
xfce_rc_write_int_entry(rc, "visible_color", config->visible_color);
xfce_rc_write_bool_entry(rc, "use_css", config->use_css);
xfce_rc_write_entry(rc, "normal_color", gdk_rgba_to_string(&config->normal_color));
xfce_rc_write_entry(rc, "focused_color", gdk_rgba_to_string(&config->focused_color));
xfce_rc_write_entry(rc, "urgent_color", gdk_rgba_to_string(&config->urgent_color));
xfce_rc_write_entry(rc, "mode_color", gdk_rgba_to_string(&config->mode_color));
xfce_rc_write_entry(rc, "visible_color", gdk_rgba_to_string(&config->visible_color));
xfce_rc_write_entry(rc, "css", config->css);
xfce_rc_write_bool_entry(rc, "strip_workspace_numbers",
config->strip_workspace_numbers);
xfce_rc_write_bool_entry(rc, "auto_detect_outputs",
Expand All @@ -159,27 +137,30 @@ i3_workspaces_config_save(i3WorkspacesConfig *config, XfcePanelPlugin *plugin)
return TRUE;
}

void add_color_picker(i3WorkspacesConfig *config, GtkWidget *dialog_vbox, char *text, guint32 color, gpointer callback) {
void
add_color_picker(i3WorkspacesConfig *config, GtkWidget *vbox, char *text, GdkRGBA *color_setting) {
GtkWidget *hbox, *button, *label;

/* focused color */
hbox = gtk_box_new(FALSE, 3);
gtk_container_add(GTK_CONTAINER(dialog_vbox), hbox);
gtk_container_add(GTK_CONTAINER(vbox), hbox);
gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);

label = gtk_label_new(_(text));
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);

button = gtk_color_button_new_with_rgba(unserialize_gdkrgba(color));
button = gtk_color_button_new_with_rgba(color_setting);
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
g_signal_connect(G_OBJECT(button), "color-set", G_CALLBACK(callback), config);

g_signal_connect(G_OBJECT(button), "color-set", G_CALLBACK(color_changed), color_setting);
}

void
i3_workspaces_config_show(i3WorkspacesConfig *config, XfcePanelPlugin *plugin,
ConfigChangedCallback cb, gpointer cb_data)
{
GtkWidget *dialog, *dialog_content, *hbox, *button, *label;
GtkWidget *dialog, *dialog_content, *hbox, *vbox, *view, *button, *label, *stack, *stack_switcher;
GtkTextBuffer *buffer;

xfce_panel_plugin_block_menu(plugin);

Expand All @@ -197,11 +178,38 @@ i3_workspaces_config_show(i3WorkspacesConfig *config, XfcePanelPlugin *plugin,

dialog_content = gtk_dialog_get_content_area(GTK_DIALOG(dialog));

add_color_picker(config, dialog_content, "Normal Workspace Color:", config->normal_color, normal_color_changed);
add_color_picker(config, dialog_content, "Focused Workspace Color:", config->focused_color, focused_color_changed);
add_color_picker(config, dialog_content, "Urgent Workspace Color:", config->urgent_color, urgent_color_changed);
add_color_picker(config, dialog_content, "Unfocused Visible Workspace Color:", config->visible_color, visible_color_changed);
add_color_picker(config, dialog_content, "Binding Mode Color:", config->mode_color, mode_color_changed);
/* color buttons or CSS */
stack = gtk_stack_new();

/* color buttons */
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 3);
add_color_picker(config, vbox, "Normal Workspace Color:", &config->normal_color);
add_color_picker(config, vbox, "Focused Workspace Color:", &config->focused_color);
add_color_picker(config, vbox, "Urgent Workspace Color:", &config->urgent_color);
add_color_picker(config, vbox, "Unfocused Visible Workspace Color:", &config->visible_color);
add_color_picker(config, vbox, "Binding Mode Color:", &config->mode_color);
gtk_stack_add_titled(GTK_STACK(stack), vbox, "buttons", "Color Pickers");
gtk_widget_set_visible(vbox, TRUE);

/* CSS */
hbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 3);
gtk_container_set_border_width(GTK_CONTAINER(hbox), 3);

view = gtk_text_view_new();
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (view));
gtk_text_buffer_set_text(buffer, config->css, -1);
gtk_box_pack_start(GTK_BOX(hbox), view, FALSE, FALSE, 0);
gtk_stack_add_titled(GTK_STACK(stack), hbox, "css", "Raw CSS");
g_signal_connect(G_OBJECT(buffer), "changed", G_CALLBACK(css_changed), config);

stack_switcher = gtk_stack_switcher_new();
gtk_stack_switcher_set_stack(GTK_STACK_SWITCHER(stack_switcher), GTK_STACK(stack));
gtk_box_pack_start(GTK_BOX(dialog_content), stack_switcher, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(dialog_content), stack, FALSE, FALSE, 0);
gtk_widget_set_visible(hbox, TRUE);
gtk_stack_set_visible_child_name(GTK_STACK(stack), config->use_css ? "css" : "buttons");
g_signal_connect(G_OBJECT(stack), "notify::visible-child", G_CALLBACK(use_css_changed), config);


/* strip workspace numbers */
hbox = gtk_box_new(FALSE, 3);
Expand Down Expand Up @@ -249,62 +257,41 @@ i3_workspaces_config_show(i3WorkspacesConfig *config, XfcePanelPlugin *plugin,
}

void
strip_workspace_numbers_changed(GtkWidget *button, i3WorkspacesConfig *config)
{
config->strip_workspace_numbers = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
use_css_changed(GtkStack *stack, GParamSpec *pspec, i3WorkspacesConfig *config) {
const gchar *visible_child = gtk_stack_get_visible_child_name(stack);
config->use_css = !g_strcmp0(visible_child, "css");
}

void
auto_detect_outputs_changed(GtkWidget *button, i3WorkspacesConfig *config)
css_changed(GtkTextBuffer *buffer, i3WorkspacesConfig *config)
{
config->auto_detect_outputs = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
GtkTextIter start, end;
gtk_text_buffer_get_bounds(buffer, &start, &end);
config->css = g_strdup(gtk_text_buffer_get_text(buffer, &start, &end, FALSE));
}

void
output_changed(GtkWidget *entry, i3WorkspacesConfig *config)
{
config->output = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
}

void
normal_color_changed(GtkWidget *button, i3WorkspacesConfig *config)
{
GdkRGBA color;
gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(button), &color);
config->normal_color = serialize_gdkrgba(&color);
}

void
focused_color_changed(GtkWidget *button, i3WorkspacesConfig *config)
strip_workspace_numbers_changed(GtkWidget *button, i3WorkspacesConfig *config)
{
GdkRGBA color;
gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(button), &color);
config->focused_color = serialize_gdkrgba(&color);
config->strip_workspace_numbers = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
}

void
urgent_color_changed(GtkWidget *button, i3WorkspacesConfig *config)
auto_detect_outputs_changed(GtkWidget *button, i3WorkspacesConfig *config)
{
GdkRGBA color;
gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(button), &color);
config->urgent_color = serialize_gdkrgba(&color);
config->auto_detect_outputs = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
}

void
visible_color_changed(GtkWidget *button, i3WorkspacesConfig *config)
output_changed(GtkWidget *entry, i3WorkspacesConfig *config)
{
GdkRGBA color;
gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(button), &color);
config->visible_color = serialize_gdkrgba(&color);
config->output = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
}

void

mode_color_changed(GtkWidget *button, i3WorkspacesConfig *config)
color_changed(GtkWidget *button, GdkRGBA *color_setting)
{
GdkRGBA color;
gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(button), &color);
config->mode_color = serialize_gdkrgba(&color);
gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(button), color_setting);
}

void
Expand Down
18 changes: 7 additions & 11 deletions panel-plugin/i3w-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@

typedef struct
{
guint32 normal_color;
guint32 focused_color;
guint32 visible_color;
guint32 urgent_color;
guint32 mode_color;
gboolean use_css;
GdkRGBA normal_color;
GdkRGBA focused_color;
GdkRGBA visible_color;
GdkRGBA urgent_color;
GdkRGBA mode_color;
gchar *css;
gboolean strip_workspace_numbers;
gboolean auto_detect_outputs;
gchar *output;
Expand All @@ -36,12 +38,6 @@ i3WorkspacesConfig;

typedef void (*ConfigChangedCallback) (gpointer cb_data);

/* utility functions */
guint32
serialize_gdkrgba(GdkRGBA *gdkrgba);
GdkRGBA *
unserialize_gdkrgba(guint32 color);

/* interface functions */
i3WorkspacesConfig *
i3_workspaces_config_new();
Expand Down
Loading

0 comments on commit a402de5

Please sign in to comment.