Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 2 additions & 33 deletions main/pages/home/addresses.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
91 changes: 91 additions & 0 deletions main/ui/address_label.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "address_label.h"
#include "theme.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 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;
}
22 changes: 22 additions & 0 deletions main/ui/address_label.h
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions simulator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading