diff options
| author | Federico Angelilli <code@fedang.net> | 2024-03-13 23:19:32 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-03-13 23:19:32 +0100 |
| commit | cbe94d7c42422856275ad41332cbc980054d6bee (patch) | |
| tree | 08564115751ff681615988bfab5750db95acc43c | |
| parent | 4df262ade044448f83092ab157a15b4a3f170eec (diff) | |
Start refactoring state and draw
| -rw-r--r-- | src/comet.c | 2 | ||||
| -rw-r--r-- | src/draw.c | 16 | ||||
| -rw-r--r-- | src/draw.h | 18 | ||||
| -rw-r--r-- | src/state.c | 33 | ||||
| -rw-r--r-- | src/state.h | 5 |
5 files changed, 36 insertions, 38 deletions
diff --git a/src/comet.c b/src/comet.c index aacf89c..7f8b2d1 100644 --- a/src/comet.c +++ b/src/comet.c @@ -280,7 +280,7 @@ int main(int argc, char **argv) log_debug("Calculated dimensions [height=%d, x_pad=%d, y_pad=%d]", height, x_padding, y_padding); Color background_all = { 0.3, 0.3, 0.3, 1 }; - Drawable *draw = draw_create("Hack 13 Bold", height, x_padding, x_padding, y_padding, 0); + Drawer *draw = draw_create("Hack 13 Bold", height, x_padding, x_padding, y_padding, 0); draw_set_background(draw, background_all); State *state = state_create(win, draw); @@ -8,17 +8,15 @@ #include "button.h" #include "log.h" -// TODO: Animations ?? - static void layout_destroy(Layout *layout) { if (layout->pl != NULL) g_object_unref(layout->pl); g_free(layout); } -Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, int line_w) +Drawer *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, int line_w) { - Drawable *draw = g_malloc(sizeof(Drawable)); + Drawer *draw = g_malloc(sizeof(Drawer)); g_assert_nonnull(draw); log_debug("Pango loading font description '%s'", font); @@ -38,7 +36,7 @@ Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, return draw; } -static void compute_width(Drawable *draw, Window *win, int *width, int *height) +static void compute_width(Drawer *draw, Window *win, int *width, int *height) { int screen_width = win->con->screen_size->width; int screen_height = win->con->screen_size->height; @@ -47,7 +45,7 @@ static void compute_width(Drawable *draw, Window *win, int *width, int *height) *height = round(draw->height); } -void draw_paint(Drawable *draw, Window *win) +void draw_paint(Drawer *draw, Window *win) { int width, height; compute_width(draw, win, &width, &height); @@ -135,7 +133,7 @@ static void layout_text(Layout *layout, PangoFontDescription *desc, int height, layout->height = height; } -void draw_compute_layout(Drawable *draw, Window *win, GList *btns) +void draw_compute_layout(Drawer *draw, Window *win, GList *btns) { g_list_free_full(draw->layouts, (GDestroyNotify)layout_destroy); draw->layouts = NULL; @@ -272,12 +270,12 @@ void draw_compute_layout(Drawable *draw, Window *win, GList *btns) log_debug("Updated layouts"); } -void draw_set_background(Drawable *draw, Color background) +void draw_set_background(Drawer *draw, Color background) { draw->background = background; } -void draw_destroy(Drawable *draw) +void draw_destroy(Drawer *draw) { g_list_free_full(draw->layouts, (GDestroyNotify)layout_destroy); pango_font_description_free(draw->desc); @@ -8,9 +8,10 @@ #include "window.h" #include "button.h" -typedef struct Drawable Drawable; +// The one who draws ('o')7 -struct Drawable { +typedef struct { + PangoFontDescription *desc; int height; int left_pad; int right_pad; @@ -18,8 +19,7 @@ struct Drawable { int line_w; Color background; GList *layouts; - PangoFontDescription *desc; -}; +} Drawer; typedef struct { Button *btn; @@ -29,15 +29,15 @@ typedef struct { PangoLayout *pl; } Layout; -Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, int line_w); +Drawer *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, int line_w); -void draw_compute_layout(Drawable *draw, Window *win, GList *btns); +void draw_compute_layout(Drawer *draw, Window *win, GList *btns); -void draw_paint(Drawable *draw, Window *win); +void draw_paint(Drawer *draw, Window *win); -void draw_set_background(Drawable *draw, Color background); +void draw_set_background(Drawer *draw, Color background); -void draw_destroy(Drawable *draw); +void draw_destroy(Drawer *draw); #endif diff --git a/src/state.c b/src/state.c index c5dc759..2051652 100644 --- a/src/state.c +++ b/src/state.c @@ -2,7 +2,7 @@ #include "state.h" -State *state_create(Window *win, Drawable *draw) +State *state_create(Window *win, Drawer *draw) { State *state = g_malloc(sizeof(State)); g_assert_nonnull(state); @@ -17,7 +17,6 @@ State *state_create(Window *win, Drawable *draw) void state_add_button(State *state, Button *btn) { - // insert sorted state->btns = g_list_append(state->btns, btn); } @@ -26,33 +25,33 @@ void state_remove_button(State *state, Button *btn) state->btns = g_list_remove(state->btns, btn); } -static gboolean redraw_and_layout(gpointer data) +static gboolean redraw_handler(gpointer data) { State *state = data; - draw_compute_layout(state->draw, state->win, state->btns); - draw_paint(state->draw, state->win); - state->id = 0; - return G_SOURCE_REMOVE; -} -static gboolean redraw(gpointer data) -{ - State *state = data; + if (state->relayout) { + draw_compute_layout(state->draw, state->win, state->btns); + state->relayout = false; + } + draw_paint(state->draw, state->win); + state->id = 0; return G_SOURCE_REMOVE; } -// Sometimes we schedule multiple redraws in a row without actually checking if there are -// others already scheduled... -// This could probably be fixed by implementing a redraw source... void state_redraw(State *state, bool changed_layout) { - // XXX: Override old redraw - if (state->id != 0) + if (state->id != 0) { + if (!changed_layout || state->relayout) + return; + + // If we should relayout override old redraw g_source_remove(state->id); + } - state->id = g_idle_add_full(G_PRIORITY_HIGH_IDLE, changed_layout ? redraw_and_layout : redraw, state, NULL); + state->relayout = changed_layout; + state->id = g_idle_add_full(G_PRIORITY_HIGH_IDLE, redraw_handler, state, NULL); } static gint align_compare(gconstpointer a, gconstpointer b) diff --git a/src/state.h b/src/state.h index 9ce4cc9..14f7d4d 100644 --- a/src/state.h +++ b/src/state.h @@ -11,12 +11,13 @@ typedef struct State State; struct State { Window *win; - Drawable *draw; + Drawer *draw; GList *btns; gint id; + bool relayout; }; -State *state_create(Window *win, Drawable *draw); +State *state_create(Window *win, Drawer *draw); void state_add_button(State *state, Button *btn); |
