From f1fe3ce9e29a1dcb4000bedce6eb9bdeebd08f59 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sun, 26 Nov 2023 20:23:47 +0100 Subject: Refactor button creation out of main (mostly) --- src/comet.c | 72 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/src/comet.c b/src/comet.c index 7664ade..8fd5095 100644 --- a/src/comet.c +++ b/src/comet.c @@ -200,36 +200,10 @@ static void quit_action(Button *btn) g_main_loop_quit(CAST(btn, ButtonSimple)->action_data); } -int main(int argc, char **argv) +static void register_buttons(State *state, Color color, Color line_color, Color text_color) { - setlocale(LC_CTYPE, ""); - g_log_set_default_handler(log_handler, (gpointer)G_LOG_LEVEL_DEBUG); - - GMainLoop *mainloop = g_main_loop_new(NULL, FALSE); - - Connection *con = connect_create(); - - Window *win = window_create(con); - - int screen_width = con->screen_size->width; - int screen_height = con->screen_size->height; - - int height = EVEN(round(screen_height * 0.021)); - int x_padding = EVEN(round(screen_width * 0.005)); - int y_padding = EVEN(round(screen_height * 0.004)); - - 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); - draw_set_background(draw, background_all); - - State *state = state_create(win, draw); - - Color color = { 0.4, 0.4, 0.4, 1 }; - Color line_color = { 0.8, 0.8, 0.8, 1 }; - Color text_color = { 0.8, 0.8, 0.8, 1 }; - + // Tags button + // TODO: They don't do anything now... for (int i = 0; i < 9; ++i) { char text[] = { '1' + i, '\0' }; Button *btn = button_simple_create(PANGO_ALIGN_LEFT, color, line_color); @@ -238,6 +212,7 @@ int main(int argc, char **argv) state_add_button(state, btn); } + // Cpu usage button Button *cpu_btn = button_simple_create(PANGO_ALIGN_RIGHT, color, line_color); button_simple_set_text(cpu_btn, "cpu", text_color); button_simple_set_action(cpu_btn, show_action, state); @@ -246,6 +221,7 @@ int main(int argc, char **argv) cpu_update(cpu_btn); g_timeout_add(1000, cpu_update, cpu_btn); + // Temperature button Button *temp_btn = button_simple_create(PANGO_ALIGN_RIGHT, color, line_color); button_simple_set_text(temp_btn, "temp", text_color); button_simple_set_action(temp_btn, show_action, state); @@ -254,6 +230,7 @@ int main(int argc, char **argv) temp_update(temp_btn); g_timeout_add(20 * 1000, temp_update, temp_btn); + // Ram usage button Button *ram_btn = button_simple_create(PANGO_ALIGN_RIGHT, color, line_color); button_simple_set_text(ram_btn, "ram", text_color); button_simple_set_action(ram_btn, show_action, state); @@ -262,6 +239,7 @@ int main(int argc, char **argv) ram_update(ram_btn); g_timeout_add(10 * 1000, ram_update, ram_btn); + // Disk usage button Button *disk_btn = button_simple_create(PANGO_ALIGN_RIGHT, color, line_color); button_simple_set_text(disk_btn, "disk", text_color); button_simple_set_action(disk_btn, show_action, state); @@ -269,12 +247,47 @@ int main(int argc, char **argv) disk_update(disk_btn); g_timeout_add(60 * 1000, disk_update, disk_btn); +} + +int main(int argc, char **argv) +{ + setlocale(LC_CTYPE, ""); + g_log_set_default_handler(log_handler, (gpointer)G_LOG_LEVEL_DEBUG); + + GMainLoop *mainloop = g_main_loop_new(NULL, FALSE); + + Connection *con = connect_create(); + + Window *win = window_create(con); + + int screen_width = con->screen_size->width; + int screen_height = con->screen_size->height; + + int height = EVEN(round(screen_height * 0.021)); + int x_padding = EVEN(round(screen_width * 0.005)); + int y_padding = EVEN(round(screen_height * 0.004)); + + 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); + draw_set_background(draw, background_all); + + State *state = state_create(win, draw); + + Color color = { 0.4, 0.4, 0.4, 1 }; + Color line_color = { 0.8, 0.8, 0.8, 1 }; + Color text_color = { 0.8, 0.8, 0.8, 1 }; + register_buttons(state, color, line_color, text_color); + + // Buttons with special handling struct { State *state; timer_t timer; } date_ctx = { state, 0 }; + // Date & time button Button *date_btn = button_simple_create(PANGO_ALIGN_CENTER, color, line_color); button_simple_set_text(date_btn, "date", text_color); button_simple_set_action(date_btn, show_action, &date_ctx); @@ -287,6 +300,7 @@ int main(int argc, char **argv) g_assert(timer_create(CLOCK_REALTIME, &sev, &date_ctx.timer) == 0); date_update(date_btn); + // Quit button Color purple = { 0.502, 0.168, 0.886, 1 }; Button *q_btn = button_simple_create(PANGO_ALIGN_RIGHT, purple, line_color); button_simple_set_text(q_btn, "", text_color); -- cgit v1.2.3