Skip to content

Commit

Permalink
Merge pull request #130 from Habbie/mdi-names
Browse files Browse the repository at this point in the history
allow usage of Material Design mdi:foo names
  • Loading branch information
Habbie authored Nov 10, 2024
2 parents 0b7591c + f9b7fc9 commit ead0075
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 8 deletions.
11 changes: 11 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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: '@[email protected]',
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()
Expand Down Expand Up @@ -241,6 +251,7 @@ if get_option('front-lvgl').enabled()
font_mdi_c,
'src/generated/domains.hpp',
version_file,
mdimap_cpp,
],
install: true,
dependencies: [
Expand Down
59 changes: 59 additions & 0 deletions scripts/mdimap.py
Original file line number Diff line number Diff line change
@@ -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<std::string, std::string> 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""")
5 changes: 3 additions & 2 deletions src/front-lvgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "uicomponents/UIComponents.hpp"
#include "uicomponents/UILogBox.hpp"
#include "uicomponents/uirgblight.hpp"
#include "uicomponents/mdimap.hpp"
#include <memory>
#include <src/core/lv_obj.h>
#include <src/core/lv_obj_pos.h>
Expand Down Expand Up @@ -206,15 +207,15 @@ 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);

UILogBox logbox(bottom_row, &voorkant::lvgl::b612style);

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);
Expand Down
5 changes: 0 additions & 5 deletions src/uicomponents/UIComponents.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
12 changes: 12 additions & 0 deletions src/uicomponents/mdimap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <map>
#include <string>

namespace voorkant
{
namespace mdi
{
std::string name2id(std::string _name);
}
}
3 changes: 2 additions & 1 deletion src/uicomponents/uirgblight.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "uirgblight.hpp"
#include "logger.hpp"
#include "uicomponents/UIComponents.hpp"
#include "uicomponents/mdimap.hpp"
#include <src/core/lv_obj_pos.h>
#include <src/misc/lv_area.h>
#include <src/misc/lv_color.h>
Expand Down Expand Up @@ -262,7 +263,7 @@ UIRGBLight::UIRGBLight(std::shared_ptr<HAEntity> _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);
Expand Down

0 comments on commit ead0075

Please sign in to comment.