diff --git a/main/pages/home/addresses.c b/main/pages/home/addresses.c index cee309b2..d9673014 100644 --- a/main/pages/home/addresses.c +++ b/main/pages/home/addresses.c @@ -9,6 +9,7 @@ #include "../../qr/encoder.h" #include "../../qr/scanner.h" #include "../../qr/viewer.h" +#include "../../ui/address_label.h" #include "../../ui/assets/icons.h" #include "../../ui/dialog.h" #include "../../ui/input_helpers.h" @@ -70,30 +71,6 @@ static void refresh_address_list(void); static void scan_button_cb(lv_event_t *e); static void return_from_scan_cb(void); -// Format address as 4-char blocks with alternating main/highlight colors -static void format_address_colored_blocks(char *dest, size_t dest_size, - const char *address) { - lv_color32_t c1 = lv_color_to_32(primary_color(), LV_OPA_COVER); - lv_color32_t c2 = lv_color_to_32(highlight_color(), LV_OPA_COVER); - uint32_t hex1 = (c1.red << 16) | (c1.green << 8) | c1.blue; - uint32_t hex2 = (c2.red << 16) | (c2.green << 8) | c2.blue; - - size_t len = strlen(address); - size_t written = 0; - dest[0] = '\0'; - - for (size_t pos = 0; pos < len; pos += 4) { - uint32_t color = ((pos / 4) % 2 == 0) ? hex1 : hex2; - size_t chunk = (len - pos < 4) ? (len - pos) : 4; - int n = snprintf(dest + written, dest_size - written, "%s#%06X %.*s#", - (pos > 0) ? " " : "", (unsigned)color, (int)chunk, - address + pos); - if (n < 0 || (size_t)n >= dest_size - written) - break; - written += n; - } -} - static void show_address_detail(int index); static void back_button_cb(lv_event_t *e) { @@ -235,19 +212,11 @@ static void show_address_detail(int index) { qr_viewer_attach_fullscreen(qr_container, qr_payload, title); free(upper); - // Full address text with alternating colored 4-char blocks - char colored_addr[512]; - format_address_colored_blocks(colored_addr, sizeof(colored_addr), address); - lv_obj_t *addr_label = lv_label_create(content); - lv_label_set_recolor(addr_label, true); - lv_label_set_text(addr_label, colored_addr); + lv_obj_t *addr_label = address_label_create(content, address); if (landscape) lv_obj_set_width(addr_label, scr_w - 2 * pad - square_size - pad); else lv_obj_set_width(addr_label, LV_PCT(95)); - lv_label_set_long_mode(addr_label, LV_LABEL_LONG_WRAP); - lv_obj_set_style_text_align(addr_label, LV_TEXT_ALIGN_CENTER, 0); - lv_obj_set_style_text_font(addr_label, theme_font_medium(), 0); // Back button detail_back_button = ui_create_back_button(parent, detail_back_cb); diff --git a/main/ui/address_label.c b/main/ui/address_label.c new file mode 100644 index 00000000..1472e042 --- /dev/null +++ b/main/ui/address_label.c @@ -0,0 +1,91 @@ +#include "address_label.h" +#include "theme.h" +#include +#include +#include + +// Block sizes a tap cycles through, 0 shows the plain address +static const size_t BLOCK_SIZES[] = {4, 6, 0}; +#define BLOCK_SIZES_COUNT (sizeof(BLOCK_SIZES) / sizeof(BLOCK_SIZES[0])) + +// Recolor markup around each block: separator space, "#RRGGBB " and "#" +#define BLOCK_MARKUP_LEN 10 + +typedef struct { + size_t mode; + char address[]; +} address_label_t; + +static uint32_t color_hex(lv_color_t color) { + lv_color32_t c = lv_color_to_32(color, LV_OPA_COVER); + return (c.red << 16) | (c.green << 8) | c.blue; +} + +// Show `address` in `block_size`-char blocks with alternating primary and +// highlight colors, or plain when `block_size` is 0. Returns false when out of +// memory, the label then keeps its text. +static bool set_blocks_text(lv_obj_t *label, const char *address, + size_t block_size) { + if (block_size == 0) { + lv_label_set_text(label, address); + return true; + } + + size_t len = strlen(address); + size_t blocks = (len + block_size - 1) / block_size; + size_t size = len + blocks * BLOCK_MARKUP_LEN + 1; + char *text = malloc(size); + if (!text) + return false; + + uint32_t hex1 = color_hex(primary_color()); + uint32_t hex2 = color_hex(highlight_color()); + size_t written = 0; + text[0] = '\0'; + for (size_t pos = 0; pos < len; pos += block_size) { + uint32_t color = ((pos / block_size) % 2 == 0) ? hex1 : hex2; + size_t chunk = (len - pos < block_size) ? (len - pos) : block_size; + written += snprintf(text + written, size - written, "%s#%06X %.*s#", + (pos > 0) ? " " : "", (unsigned)color, (int)chunk, + address + pos); + } + + lv_label_set_text(label, text); + free(text); + return true; +} + +static void address_label_clicked_cb(lv_event_t *e) { + lv_obj_t *label = lv_event_get_target(e); + address_label_t *state = lv_event_get_user_data(e); + size_t next = (state->mode + 1) % BLOCK_SIZES_COUNT; + if (set_blocks_text(label, state->address, BLOCK_SIZES[next])) + state->mode = next; +} + +static void address_label_delete_cb(lv_event_t *e) { + free(lv_event_get_user_data(e)); +} + +lv_obj_t *address_label_create(lv_obj_t *parent, const char *address) { + lv_obj_t *label = lv_label_create(parent); + lv_label_set_recolor(label, true); + lv_label_set_long_mode(label, LV_LABEL_LONG_WRAP); + lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0); + lv_obj_set_style_text_font(label, theme_font_medium(), 0); + lv_obj_set_style_text_color(label, primary_color(), 0); + lv_label_set_text(label, address); + + size_t len = strlen(address); + address_label_t *state = malloc(sizeof(*state) + len + 1); + if (!state) + return label; + state->mode = 0; + memcpy(state->address, address, len + 1); + set_blocks_text(label, state->address, BLOCK_SIZES[state->mode]); + + lv_obj_add_flag(label, LV_OBJ_FLAG_CLICKABLE); + lv_obj_add_event_cb(label, address_label_clicked_cb, LV_EVENT_CLICKED, state); + lv_obj_add_event_cb(label, address_label_delete_cb, LV_EVENT_DELETE, state); + return label; +} diff --git a/main/ui/address_label.h b/main/ui/address_label.h new file mode 100644 index 00000000..7cc8c30f --- /dev/null +++ b/main/ui/address_label.h @@ -0,0 +1,22 @@ +/** + * Address label shown next to an address QR code. + */ + +#ifndef ADDRESS_LABEL_H +#define ADDRESS_LABEL_H + +#include "lvgl.h" + +/** + * Create a wrapped, centered label showing `address` in 4-char blocks with + * alternating primary/highlight colors. Tapping it cycles the display through + * 4-char blocks, 6-char blocks and the plain address. The label keeps its own + * copy of `address`. The caller sets the width. + * + * @param parent Parent LVGL object + * @param address Address to display + * @return The label object + */ +lv_obj_t *address_label_create(lv_obj_t *parent, const char *address); + +#endif // ADDRESS_LABEL_H diff --git a/simulator/CMakeLists.txt b/simulator/CMakeLists.txt index b0d36d45..234a90c8 100644 --- a/simulator/CMakeLists.txt +++ b/simulator/CMakeLists.txt @@ -199,6 +199,7 @@ set(APP_CORE_SOURCES # --- UI components --- set(APP_UI_SOURCES + ${APP_UI_DIR}/address_label.c ${APP_UI_DIR}/dialog.c ${APP_UI_DIR}/dropdown_page.c ${APP_UI_DIR}/input_helpers.c