From 6e6334e603564e1b961c010bce2688839f64cca2 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Sat, 21 Aug 2021 17:42:48 +0200 Subject: [PATCH] Simplify adding pages in one place --- Action.c | 8 ++------ CategoriesPanel.c | 44 ++++++++++++++++++++++---------------------- CategoriesPanel.h | 2 -- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/Action.c b/Action.c index 802d2fc5..1d373efb 100644 --- a/Action.c +++ b/Action.c @@ -84,12 +84,8 @@ Object* Action_pickFromVector(State* st, Panel* list, int x, bool followProcess) static void Action_runSetup(State* st) { ScreenManager* scr = ScreenManager_new(st->header, st->settings, st, true); - CategoriesPanel* panelCategories = CategoriesPanel_new(scr, st->settings, st->header, st->pl); - ScreenManager_add(scr, (Panel*) panelCategories, 16); - CategoriesPanel_makeMetersPage(panelCategories); - Panel* panelFocus; - int ch; - ScreenManager_run(scr, &panelFocus, &ch); + CategoriesPanel_new(scr, st->settings, st->header, st->pl); + ScreenManager_run(scr, NULL, NULL); ScreenManager_delete(scr); if (st->settings->changed) { Header_writeBackToSettings(st->header); diff --git a/CategoriesPanel.c b/CategoriesPanel.c index 14f74c0e..1fefe47f 100644 --- a/CategoriesPanel.c +++ b/CategoriesPanel.c @@ -35,7 +35,7 @@ static void CategoriesPanel_delete(Object* object) { free(this); } -void CategoriesPanel_makeMetersPage(CategoriesPanel* this) { +static void CategoriesPanel_makeMetersPage(CategoriesPanel* this) { size_t columns = HeaderLayout_getColumns(this->scr->header->headerLayout); MetersPanel** meterPanels = xMallocArray(columns, sizeof(MetersPanel)); @@ -78,6 +78,20 @@ static void CategoriesPanel_makeHeaderOptionsPage(CategoriesPanel* this) { ScreenManager_add(this->scr, colors, -1); } +typedef void (* CategoriesPanel_makePageFunc)(CategoriesPanel* ref); +typedef struct CategoriesPanelPage_ { + const char* name; + CategoriesPanel_makePageFunc ctor; +} CategoriesPanelPage; + +static const CategoriesPanelPage categoriesPanelPages[] = { + { .name = "Display options", .ctor = CategoriesPanel_makeDisplayOptionsPage }, + { .name = "Header layout", .ctor = CategoriesPanel_makeHeaderOptionsPage }, + { .name = "Meters", .ctor = CategoriesPanel_makeMetersPage }, + { .name = "Columns", .ctor = CategoriesPanel_makeColumnsPage }, + { .name = "Colors", .ctor = CategoriesPanel_makeColorsPage }, +}; + static HandlerResult CategoriesPanel_eventHandler(Panel* super, int ch) { CategoriesPanel* this = (CategoriesPanel*) super; @@ -115,22 +129,8 @@ static HandlerResult CategoriesPanel_eventHandler(Panel* super, int ch) { for (int i = 1; i < size; i++) ScreenManager_remove(this->scr, 1); - switch (selected) { - case 0: - CategoriesPanel_makeMetersPage(this); - break; - case 1: - CategoriesPanel_makeDisplayOptionsPage(this); - break; - case 2: - CategoriesPanel_makeColorsPage(this); - break; - case 3: - CategoriesPanel_makeColumnsPage(this); - break; - case 4: - CategoriesPanel_makeHeaderOptionsPage(this); - break; + if (selected >= 0 && (size_t)selected < ARRAYSIZE(categoriesPanelPages)) { + categoriesPanelPages[selected].ctor(this); } } return result; @@ -155,10 +155,10 @@ CategoriesPanel* CategoriesPanel_new(ScreenManager* scr, Settings* settings, Hea this->header = header; this->pl = pl; Panel_setHeader(super, "Setup"); - Panel_add(super, (Object*) ListItem_new("Meters", 0)); - Panel_add(super, (Object*) ListItem_new("Display options", 0)); - Panel_add(super, (Object*) ListItem_new("Colors", 0)); - Panel_add(super, (Object*) ListItem_new("Columns", 0)); - Panel_add(super, (Object*) ListItem_new("Header layout", 0)); + for (size_t i = 0; i < ARRAYSIZE(categoriesPanelPages); i++) + Panel_add(super, (Object*) ListItem_new(categoriesPanelPages[i].name, 0)); + + ScreenManager_add(scr, super, 16); + categoriesPanelPages[0].ctor(this); return this; } diff --git a/CategoriesPanel.h b/CategoriesPanel.h index c438e45f..451a483d 100644 --- a/CategoriesPanel.h +++ b/CategoriesPanel.h @@ -23,8 +23,6 @@ typedef struct CategoriesPanel_ { ProcessList* pl; } CategoriesPanel; -void CategoriesPanel_makeMetersPage(CategoriesPanel* this); - extern const PanelClass CategoriesPanel_class; CategoriesPanel* CategoriesPanel_new(ScreenManager* scr, Settings* settings, Header* header, ProcessList* pl);