From 2749ce836389db9308c7eb588639887839282c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Mon, 28 Jun 2021 18:22:09 +0200 Subject: [PATCH] feat: add more columns to render --- file-browser/src/window_browser.c | 88 +++++++++++++++++++++++++++++-- lib-gui/src/gui_component_line.c | 2 +- lib-gui/src/gui_list_container.c | 2 +- 3 files changed, 85 insertions(+), 7 deletions(-) diff --git a/file-browser/src/window_browser.c b/file-browser/src/window_browser.c index cb252e2..79b7769 100644 --- a/file-browser/src/window_browser.c +++ b/file-browser/src/window_browser.c @@ -2,6 +2,7 @@ #include "display_utils.h" #include "file_access.h" #include "file_open.h" +#include "font.h" #include "gui.h" #include "gui_component_text.h" #include "gui_container_info.h" @@ -13,11 +14,17 @@ #include "path.h" #include "renderer.h" +#define COLUMNS_COUNT 4 +#define MAX_COLUMN_CHARS 200 + +char *column_names[] ={"NAME", "TYPE", "SIZE", "MODIFIED"}; + typedef struct { bool running; gui_t *gui; container_t *list_container; + component_t *line_component; window_t *browser_window; font_t *font; @@ -27,6 +34,8 @@ typedef struct { directory_t *current_directory; fileaccess_state_t state; + + uint16_t column_widths[COLUMNS_COUNT]; } browser_window_state_t; static bool browser_window_list_render_item(void *state, uint32_t index, @@ -46,6 +55,8 @@ static void *browser_window_construct(window_t *window, void *state); static bool browser_window_running(void *state); static void browser_window_job(void *state); +static char *browser_get_column_data(file_t *file, uint16_t column, char* out); + gui_container_info_t window_browser_containers[] = { {.type = CONT_TABLE, .payload.list = {.render_item_fn = browser_window_list_render_item, @@ -122,12 +133,13 @@ static void *browser_window_construct(window_t *window, void *state) { component_t line_component = gui_line_create(&WHITE_PIXEL, 0, path_text.height + path_text.y + 3, 1000, 1); gui_group_container_add_component(&window->containers[1], path_text); - gui_group_container_add_component(&window->containers[1], line_component); + bstate->line_component = + gui_group_container_add_component(&window->containers[1], line_component); // list init gui_container_info_init(bstate->list_container, bstate, bstate->current_directory->files_count, 5, - 5 + line_component.height + 5); + bstate->font->size / 2 + 3); bstate->list_container->width = bstate->gui->size.x - 20; bstate->list_container->height = bstate->gui->size.y - bstate->list_container->y - 20; @@ -188,8 +200,16 @@ static bool browser_window_list_render_item(void *state, uint32_t index, return false; } file_t file = bstate->current_directory->files[index]; - renderer_write_string(renderer, beg_x, beg_y, 0, bstate->font, file.name, - color); + + uint16_t offset = beg_x; + char tmp[MAX_COLUMN_CHARS]; + for (int i = 0; i < COLUMNS_COUNT; i++) { + char *data = browser_get_column_data(&file, i, tmp); + renderer_write_string(renderer, offset, beg_y, 0, bstate->font, data, + color); + offset += bstate->column_widths[i]; + } + return true; } @@ -198,7 +218,15 @@ static bool browser_window_list_render_header(void *state, uint32_t index, int16_t beg_x, int16_t beg_y, display_pixel_t color) { browser_window_state_t *bstate = (browser_window_state_t *)state; - renderer_write_string(renderer, beg_x, beg_y, 0, bstate->font, "This is header", color); + renderer_render_rectangle(renderer, beg_x - 3, beg_y + bstate->font->size, + 1000, 1, color); + + uint16_t offset = beg_x; + + for (int i = 0; i < COLUMNS_COUNT; i++) { + renderer_write_string(renderer, offset, beg_y, 0, bstate->font, column_names[i], color); + offset += bstate->column_widths[i]; + } return true; } @@ -209,8 +237,58 @@ static bool browser_window_running(void *state) { static void browser_window_job(void *state) { browser_window_state_t *bstate = (browser_window_state_t *)state; + + char tmp[MAX_COLUMN_CHARS]; + for (int i = 0; i < COLUMNS_COUNT; i++) { + uint16_t max_size = 0; + for (int j = 0; j < bstate->current_directory->files_count; j++) { + char *data = browser_get_column_data(bstate->current_directory->files, i, tmp); + if (data == NULL) { + continue; + } + uint16_t current_size = font_measure_text(bstate->font, data).x; + + if (current_size > max_size) { + max_size = current_size; + } + } + bstate->column_widths[i] = max_size + 50; + } + + bstate->line_component->y = bstate->font->size + 5; + bstate->list_container->y = bstate->line_component->y / 2; + gui_list_container_set_item_height(bstate->list_container, + bstate->font->size); + if (!bstate->running) { // cleanup fileaccess_directory_close(bstate->state, bstate->current_directory); } } + +static char *browser_get_column_data(file_t *file, uint16_t column, char *out) { + switch (column) { + case 0: + return file->name; + case 1: + switch (file->type) { + case FT_FILE: + return "FILE"; + case FT_FOLDER: + return "DIR"; + case FT_OTHER: + return "OTHER"; + case FT_UNKNOWN: + return "UNKNOWN"; + } + break; + case 2: + // get size + return ""; + case 3: + // date modified + return ""; + } + + return NULL; +} diff --git a/lib-gui/src/gui_component_line.c b/lib-gui/src/gui_component_line.c index 8a8d606..9cf738d 100644 --- a/lib-gui/src/gui_component_line.c +++ b/lib-gui/src/gui_component_line.c @@ -15,7 +15,7 @@ void gui_line_render(container_t *container, component_t *component, gui_t *gui) { if (gui_is_component_visible(gui, container, component)) { coords_t coords = gui_component_get_screen_position(container, component); - renderer_render_border(gui->renderer, coords.x, coords.y, component->width, + renderer_render_rectangle(gui->renderer, coords.x, coords.y, component->width, component->height, *(display_pixel_t *)component->state); } diff --git a/lib-gui/src/gui_list_container.c b/lib-gui/src/gui_list_container.c index 7821d4f..73127c3 100644 --- a/lib-gui/src/gui_list_container.c +++ b/lib-gui/src/gui_list_container.c @@ -95,7 +95,7 @@ void gui_list_container_render(gui_t *gui, container_t *container) { uint32_t selected_index = gui_list_get_selected_index(container); if (list.render_header_fn && - list.render_header_fn(list.state, 0, gui->renderer, 0 + list.item_padding, + list.render_header_fn(list.state, 0, gui->renderer, beg_x + list.item_padding, 0 + list.item_padding, WHITE_PIXEL)) { // if header was rendered, translate initial position renderer_translate(gui->renderer, 0, item_full_height); -- 2.48.1