This commit is contained in:
103
main/ui_task.c
103
main/ui_task.c
@@ -1094,6 +1094,90 @@ static void view_storage(void)
|
||||
lvgl_port_unlock();
|
||||
}
|
||||
|
||||
static void truncate_text_to_fit(char *str, size_t max_buf_sz, lv_coord_t max_w, const lv_font_t *font)
|
||||
{
|
||||
if (lv_txt_get_width(str, strlen(str), font, 0, LV_TEXT_FLAG_NONE) <= max_w) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t len = strlen(str);
|
||||
while (len > 0) {
|
||||
while (len > 0 && (str[len - 1] & 0xC0) == 0x80) {
|
||||
len--;
|
||||
}
|
||||
if (len > 0) {
|
||||
len--;
|
||||
}
|
||||
str[len] = '\0';
|
||||
char candidate[96];
|
||||
snprintf(candidate, sizeof(candidate), "%s...", str);
|
||||
if (lv_txt_get_width(candidate, strlen(candidate), font, 0, LV_TEXT_FLAG_NONE) <= max_w) {
|
||||
snprintf(str, max_buf_sz, "%s", candidate);
|
||||
return;
|
||||
}
|
||||
}
|
||||
snprintf(str, max_buf_sz, "...");
|
||||
}
|
||||
|
||||
static void get_fitted_wifi_text(char *out, size_t out_sz, const char *ssid, int rssi, lv_coord_t max_w)
|
||||
{
|
||||
if (!wifi_manager_connected() || !ssid || strlen(ssid) == 0) {
|
||||
snprintf(out, out_sz, "Disconnected");
|
||||
return;
|
||||
}
|
||||
|
||||
char suffix[24] = "";
|
||||
if (rssi != 0) {
|
||||
snprintf(suffix, sizeof(suffix), " (%ddB)", rssi);
|
||||
}
|
||||
|
||||
/* Try full SSID + suffix */
|
||||
snprintf(out, out_sz, "%s%s", ssid, suffix);
|
||||
if (lv_txt_get_width(out, strlen(out), &lv_font_montserrat_14, 0, LV_TEXT_FLAG_NONE) <= max_w) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* If it doesn't fit, truncate SSID with "..." while preserving the suffix */
|
||||
char temp_ssid[WIFI_SSID_MAX_LEN + 1];
|
||||
strncpy(temp_ssid, ssid, sizeof(temp_ssid) - 1);
|
||||
temp_ssid[sizeof(temp_ssid) - 1] = '\0';
|
||||
size_t len = strlen(temp_ssid);
|
||||
|
||||
while (len > 0) {
|
||||
while (len > 0 && (temp_ssid[len - 1] & 0xC0) == 0x80) {
|
||||
len--;
|
||||
}
|
||||
if (len > 0) {
|
||||
len--;
|
||||
}
|
||||
temp_ssid[len] = '\0';
|
||||
snprintf(out, out_sz, "%s...%s", temp_ssid, suffix);
|
||||
if (lv_txt_get_width(out, strlen(out), &lv_font_montserrat_14, 0, LV_TEXT_FLAG_NONE) <= max_w) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* If even "..." + suffix is too wide, drop suffix and truncate SSID */
|
||||
strncpy(temp_ssid, ssid, sizeof(temp_ssid) - 1);
|
||||
temp_ssid[sizeof(temp_ssid) - 1] = '\0';
|
||||
len = strlen(temp_ssid);
|
||||
while (len > 0) {
|
||||
while (len > 0 && (temp_ssid[len - 1] & 0xC0) == 0x80) {
|
||||
len--;
|
||||
}
|
||||
if (len > 0) {
|
||||
len--;
|
||||
}
|
||||
temp_ssid[len] = '\0';
|
||||
snprintf(out, out_sz, "%s...", temp_ssid);
|
||||
if (lv_txt_get_width(out, strlen(out), &lv_font_montserrat_14, 0, LV_TEXT_FLAG_NONE) <= max_w) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
snprintf(out, out_sz, "...");
|
||||
}
|
||||
|
||||
/* 7. Device & Wi-Fi Info */
|
||||
static void view_device_info(void)
|
||||
{
|
||||
@@ -1110,6 +1194,8 @@ static void view_device_info(void)
|
||||
lv_obj_set_style_radius(box, 6, 0);
|
||||
lv_obj_set_style_pad_all(box, 8, 0);
|
||||
|
||||
const lv_coord_t inner_w = (DISP_W - 24) - 16; /* 200px usable width */
|
||||
|
||||
size_t total_b = 0, used_b = 0;
|
||||
storage_manager_get_info(&total_b, &used_b);
|
||||
int json_c = 0, jpg_c = 0;
|
||||
@@ -1118,12 +1204,11 @@ static void view_device_info(void)
|
||||
|
||||
const char *ssid = wifi_manager_get_connected_ssid();
|
||||
int rssi = wifi_manager_get_rssi();
|
||||
lv_coord_t wifi_kw = lv_txt_get_width("WI-FI:", 6, &lv_font_montserrat_14, 0, LV_TEXT_FLAG_NONE);
|
||||
lv_coord_t wifi_val_w = inner_w - wifi_kw - 6;
|
||||
|
||||
char wifi_line[64];
|
||||
if (wifi_manager_connected() && ssid && strlen(ssid) > 0) {
|
||||
snprintf(wifi_line, sizeof(wifi_line), "%s (%ddB)", ssid, rssi);
|
||||
} else {
|
||||
snprintf(wifi_line, sizeof(wifi_line), "Disconnected");
|
||||
}
|
||||
get_fitted_wifi_text(wifi_line, sizeof(wifi_line), ssid, rssi, wifi_val_w);
|
||||
|
||||
struct {
|
||||
const char *key;
|
||||
@@ -1169,10 +1254,14 @@ static void view_device_info(void)
|
||||
lv_obj_set_style_text_color(k, lv_color_hex(0x00E5FF), 0);
|
||||
lv_obj_align(k, LV_ALIGN_TOP_LEFT, 0, y_pos);
|
||||
|
||||
lv_coord_t k_w = lv_txt_get_width(rows[i].key, strlen(rows[i].key), &lv_font_montserrat_14, 0, LV_TEXT_FLAG_NONE);
|
||||
lv_coord_t max_val_w = inner_w - k_w - 6;
|
||||
truncate_text_to_fit(rows[i].val, sizeof(rows[i].val), max_val_w, &lv_font_montserrat_14);
|
||||
|
||||
lv_obj_t *v = lv_label_create(box);
|
||||
lv_label_set_text(v, rows[i].val);
|
||||
lv_obj_set_width(v, 120);
|
||||
lv_label_set_long_mode(v, LV_LABEL_LONG_DOT);
|
||||
lv_obj_set_width(v, max_val_w);
|
||||
lv_label_set_long_mode(v, LV_LABEL_LONG_CLIP);
|
||||
lv_obj_set_style_text_font(v, &lv_font_montserrat_14, 0);
|
||||
lv_obj_set_style_text_color(v, lv_color_hex(COLOR_AMBER), 0);
|
||||
lv_obj_set_style_text_align(v, LV_TEXT_ALIGN_RIGHT, 0);
|
||||
|
||||
Reference in New Issue
Block a user