From f9b7fc9555d4a18fb6ead89865d0e087c3f0031a Mon Sep 17 00:00:00 2001 From: Peter van Dijk Date: Sun, 20 Oct 2024 21:17:51 +0200 Subject: [PATCH] replace MDI_ defines with a map (that gets constructed into memory, sadly) --- meson.build | 11 ++++++ scripts/mdimap.py | 59 +++++++++++++++++++++++++++++++ src/front-lvgl.cpp | 5 +-- src/uicomponents/UIComponents.hpp | 5 --- src/uicomponents/mdimap.hpp | 12 +++++++ src/uicomponents/uirgblight.cpp | 3 +- 6 files changed, 87 insertions(+), 8 deletions(-) create mode 100755 scripts/mdimap.py create mode 100644 src/uicomponents/mdimap.hpp diff --git a/meson.build b/meson.build index e9040fe..080766e 100644 --- a/meson.build +++ b/meson.build @@ -82,6 +82,16 @@ font_mdi_c = xxd_generator.process( extra_args: 'mdi_ttf', ) +mdimap_prog = find_program('scripts/mdimap.py', required: true) +mdimap_generator = generator( + mdimap_prog, + output: '@BASENAME@.cpp', + arguments: ['@INPUT@', '@OUTPUT@'], +) +mdimap_cpp = mdimap_generator.process( + 'src/fonts/MaterialDesign-Webfont/scss/_variables.scss', +) + brew_prefix = '/opt/homebrew/include' brew = find_program('brew', required: false) if brew.found() @@ -241,6 +251,7 @@ if get_option('front-lvgl').enabled() font_mdi_c, 'src/generated/domains.hpp', version_file, + mdimap_cpp, ], install: true, dependencies: [ diff --git a/scripts/mdimap.py b/scripts/mdimap.py new file mode 100755 index 0000000..94af932 --- /dev/null +++ b/scripts/mdimap.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +import re +import sys + +infile = open(sys.argv[1]) +outfile = open(sys.argv[2], 'w') # FIXME tempfile+rename + +# skip until $mdi-icons +for line in infile: + if line.startswith("$mdi-icons"): + break + +icons = dict() + +# now handle all icons +for line in infile: + if not line.startswith(" "): + break + # "ab-testing": F01C9,\n + line = line.strip() + + # "ab-testing": F01C9, + if line.endswith(","): + line = line[:-1] + + # "ab-testing": F01C9 + k, v = line.split(":") + + # k = "ab-testing" (INCLUDING quotes) + # v = F01C9 (no quotes, leading space) + v = v.strip() + + # k = "ab-testing" (INCLUDING quotes) + # v = F01C9 (no quotes, no leading space) + v = repr(chr(int(v, 16)).encode('utf-8'))[2:-1] + + # k = "ab-testing" (INCLUDING quotes) + # v = \\xf3\\xb0\\x87\\x89 (no quotes) + + icons[k] = v + +outfile.write(f"""#include "uicomponents/mdimap.hpp" + +static std::map map_name2id = {{ +""") + + +for k, v in icons.items(): + outfile.write(f' {{{k}, "{v}"}},\n') + +outfile.write(""" +}; + +// making _name a ref is tempting but currently some code tries to pass simple static "foo" (char array) in +std::string voorkant::mdi::name2id(std::string _name) { + return map_name2id.at(_name); +} + +\n""") diff --git a/src/front-lvgl.cpp b/src/front-lvgl.cpp index 8ac8b51..f58dab5 100644 --- a/src/front-lvgl.cpp +++ b/src/front-lvgl.cpp @@ -5,6 +5,7 @@ #include "uicomponents/UIComponents.hpp" #include "uicomponents/UILogBox.hpp" #include "uicomponents/uirgblight.hpp" +#include "uicomponents/mdimap.hpp" #include #include #include @@ -206,7 +207,7 @@ void uithread(int _argc, char* _argv[]) lv_obj_t* left_btn = lv_button_create(bottom_row); lv_obj_t* left_btn_txt = lv_label_create(left_btn); - lv_label_set_text(left_btn_txt, MDI_ARROW_LEFT); + lv_label_set_text(left_btn_txt, voorkant::mdi::name2id("arrow-left").data()); lv_obj_add_event_cb(left_btn, btnLeftPress, LV_EVENT_CLICKED, NULL); lv_obj_add_style(left_btn, &voorkant::lvgl::mdistyle, 0); @@ -214,7 +215,7 @@ void uithread(int _argc, char* _argv[]) lv_obj_t* right_btn = lv_button_create(bottom_row); lv_obj_t* right_btn_txt = lv_label_create(right_btn); - lv_label_set_text(right_btn_txt, MDI_ARROW_RIGHT); + lv_label_set_text(right_btn_txt, voorkant::mdi::name2id("arrow-right").data()); lv_obj_add_style(right_btn_txt, &voorkant::lvgl::mdistyle, 0); lv_obj_add_event_cb(right_btn, btnRightPress, LV_EVENT_CLICKED, NULL); diff --git a/src/uicomponents/UIComponents.hpp b/src/uicomponents/UIComponents.hpp index 26ab9a2..e5e3a10 100644 --- a/src/uicomponents/UIComponents.hpp +++ b/src/uicomponents/UIComponents.hpp @@ -22,11 +22,6 @@ namespace lvgl } } -#define MDI_ARROW_LEFT "\xf3\xb0\x81\x8d" -#define MDI_ARROW_RIGHT "\xf3\xb0\x81\x94" -#define MDI_POWER_STANDBY "\xf3\xb0\xa4\x86" -#define MDI_WAZE "\xf3\xb0\xaf\x9e" - // FIXME: we never free() the lv_obj_t*'s in code class UIComponent : public IObserver { diff --git a/src/uicomponents/mdimap.hpp b/src/uicomponents/mdimap.hpp new file mode 100644 index 0000000..2954fe7 --- /dev/null +++ b/src/uicomponents/mdimap.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +namespace voorkant +{ +namespace mdi +{ + std::string name2id(std::string _name); +} +} diff --git a/src/uicomponents/uirgblight.cpp b/src/uicomponents/uirgblight.cpp index 8605cdd..5e7212b 100644 --- a/src/uicomponents/uirgblight.cpp +++ b/src/uicomponents/uirgblight.cpp @@ -1,6 +1,7 @@ #include "uirgblight.hpp" #include "logger.hpp" #include "uicomponents/UIComponents.hpp" +#include "uicomponents/mdimap.hpp" #include #include #include @@ -262,7 +263,7 @@ UIRGBLight::UIRGBLight(std::shared_ptr _entity, lv_obj_t* _parent) : lv_obj_set_flex_align(btns, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); lv_obj_remove_flag(btns, LV_OBJ_FLAG_SCROLLABLE); - btnOnOff = createImageButton(MDI_POWER_STANDBY, UIRGBLight::btnOnOffCB, LV_EVENT_VALUE_CHANGED, true); + btnOnOff = createImageButton(voorkant::mdi::name2id("power-standby").data(), UIRGBLight::btnOnOffCB, LV_EVENT_VALUE_CHANGED, true); if (showBrightness) { btnBrightness = createImageButton(&G_BRIGHTNESS24, UIRGBLight::btnBrightnessCB, LV_EVENT_CLICKED);