diff options
| author | Federico Angelilli <code@fedang.net> | 2023-11-19 13:10:54 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2023-11-19 13:10:54 +0100 |
| commit | e774c39c76379738e383e23938f3762b1a57e3b2 (patch) | |
| tree | d9b15c5a4695efc513f23057c8d326c02905811d /src | |
| parent | a8a1c47f4c22497c983856c04c099058a42e518b (diff) | |
Add per-button colors
Diffstat (limited to 'src')
| -rw-r--r-- | src/button.c | 7 | ||||
| -rw-r--r-- | src/button.h | 9 | ||||
| -rw-r--r-- | src/comet.c | 11 | ||||
| -rw-r--r-- | src/draw.c | 18 | ||||
| -rw-r--r-- | src/draw.h | 3 |
5 files changed, 38 insertions, 10 deletions
diff --git a/src/button.c b/src/button.c index 11ca2b7..b2155ed 100644 --- a/src/button.c +++ b/src/button.c @@ -10,6 +10,13 @@ Button *button_create(const char *text, PangoAlignment align) return btn; } +void button_set_colors(Button *btn, Color background, Color foreground, Color stroke) +{ + btn->background = background; + btn->foreground = foreground; + btn->stroke = stroke; +} + void button_set_action(Button *btn, ButtonAction action) { btn->action = action; diff --git a/src/button.h b/src/button.h index 1d736e6..7def0b3 100644 --- a/src/button.h +++ b/src/button.h @@ -10,14 +10,23 @@ typedef struct Button Button; typedef void (* ButtonAction)(Button *btn); +typedef struct { + double r, g, b, a; +} Color; + struct Button { ButtonAction action; char *text; PangoAlignment align; + Color background; + Color foreground; + Color stroke; }; Button *button_create(const char *text, PangoAlignment align); +void button_set_colors(Button *btn, Color background, Color foreground, Color stroke); + void button_set_action(Button *btn, ButtonAction action); void button_destroy(Button *btn); diff --git a/src/comet.c b/src/comet.c index a1bc605..53f4eff 100644 --- a/src/comet.c +++ b/src/comet.c @@ -63,26 +63,35 @@ int main(int argc, char **argv) log_debug("Calculated dimensions [height=%d, x_pad=%d, y_pad=%d]", height, x_padding, y_padding); - Drawable *draw = draw_create("Hack 13 Bold", height, x_padding, x_padding, y_padding, 1); + Drawable *draw = draw_create("Hack 13 Bold", height, x_padding, x_padding, y_padding, 1, 0); State *state = state_create(win, draw); + // purple + Color background = { 0.502, 0.168, 0.886, 1 }; + Color foreground = { 0.8, 0.8, 0.8, 1 }; + Color stroke = { 0.8, 0.8, 0.8, 1 }; + for (int i = 0; i < 9; ++i) { char text[] = { '1' + i, '\0' }; Button *btn = button_create(text, PANGO_ALIGN_LEFT); + button_set_colors(btn, background, foreground, stroke); button_set_action(btn, action); state_add_button(state, btn); } Button *long_btn = button_create("long button", PANGO_ALIGN_CENTER); + button_set_colors(long_btn, background, foreground, stroke); button_set_action(long_btn, action); state_add_button(state, long_btn); Button *date_btn = button_create("date = today", PANGO_ALIGN_RIGHT); + button_set_colors(date_btn, background, foreground, stroke); button_set_action(date_btn, action); state_add_button(state, date_btn); Button *u_btn = button_create("U", PANGO_ALIGN_RIGHT); + button_set_colors(u_btn, background, foreground, stroke); button_set_action(u_btn, action); state_add_button(state, u_btn); @@ -18,7 +18,7 @@ static void layout_destroy(Layout *layout) g_free(layout); } -Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha) +Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha, int line_w) { Drawable *draw = g_malloc(sizeof(Drawable)); g_assert_nonnull(draw); @@ -34,6 +34,7 @@ Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, draw->alpha = alpha; g_assert(alpha >= 0 && alpha <= 1); + draw->line_w = line_w; draw->layouts = NULL; log_debug("Draw context created [height=%d, left_pad=%d, right_pad=%d, top_pad=%d, alpha=%.2lf]", height, left_pad, right_pad, top_pad, alpha); @@ -86,7 +87,7 @@ void draw_paint(Drawable *draw, Window *win) cairo_paint(cr); - int line_w = 1 * scale; + int line_w = draw->line_w * scale; cairo_set_line_width(cr, line_w); for (GList *it = draw->layouts; it; it = it->next) { @@ -95,22 +96,24 @@ void draw_paint(Drawable *draw, Window *win) int text_x = layout->x + (layout->width / 2) - (layout->text_w / 2); int text_y = layout->y + (layout->height / 2) - (layout->text_h / 2); - // purple - cairo_set_source_rgba(cr, 0.502, 0.168, 0.886, 1); + Color color = layout->btn->background; + cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); cairo_new_sub_path(cr); cairo_arc(cr, layout->x + radius, layout->y + radius, radius, 90 * degree, 270 * degree); cairo_arc(cr, layout->x + layout->width - radius, layout->y + radius, radius, 270 * degree, 450 * degree); cairo_close_path(cr); cairo_fill(cr); - cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1); + color = layout->btn->stroke; + cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); cairo_new_sub_path(cr); cairo_arc(cr, layout->x + radius, layout->y + radius, radius - line_w , 90 * degree, 270 * degree); cairo_arc(cr, layout->x + layout->width - radius, layout->y + radius, radius - line_w, 270 * degree, 450 * degree); cairo_close_path(cr); cairo_stroke(cr); - cairo_set_source_rgb(cr, 0.8, 0.8, 0.8); + color = layout->btn->foreground; + cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); cairo_move_to(cr, text_x, text_y); pango_cairo_update_layout(cr, layout->pl); @@ -153,7 +156,7 @@ void draw_compute_layout(Drawable *draw, Window *win, GList *btns) int radius = height / 2; int sep = 10 * scale; - int line_w = 1 * scale; + int line_w = draw->line_w * scale; btns = g_list_sort(btns, align_compare); @@ -166,7 +169,6 @@ void draw_compute_layout(Drawable *draw, Window *win, GList *btns) for (GList *it = btns; it; it = it->next) { Button *btn = it->data; - Layout *layout = g_malloc(sizeof(Layout)); layout->btn = btn; @@ -16,6 +16,7 @@ struct Drawable { int right_pad; int top_pad; double alpha; + int line_w; GList *layouts; PangoFontDescription *desc; }; @@ -28,7 +29,7 @@ typedef struct { PangoLayout *pl; } Layout; -Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha); +Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha, int line_w); void draw_compute_layout(Drawable *draw, Window *win, GList *btns); |
