feat: change icon and display, add time

This commit is contained in:
0Xiao0
2026-06-16 13:34:05 +08:00
parent 7b5da81892
commit 4903fdd7a8
7 changed files with 194 additions and 13 deletions

View File

@ -22,6 +22,57 @@ LV_FONT_DECLARE(BUILTIN_TEXT_FONT);
LV_FONT_DECLARE(BUILTIN_ICON_FONT);
LV_FONT_DECLARE(font_awesome_30_4);
namespace {
constexpr int kEmojiMaxScale = 1024;
lv_coord_t ObjectHeight(lv_obj_t* obj) {
if (obj == nullptr) {
return 0;
}
lv_obj_update_layout(obj);
return lv_obj_get_height(obj);
}
void ApplyEmojiImageScale(lv_obj_t* image_obj, lv_obj_t* image_box, const lv_image_dsc_t* image_dsc,
lv_coord_t top_reserved, lv_coord_t bottom_reserved) {
if (image_obj == nullptr || image_dsc == nullptr) {
return;
}
lv_coord_t image_width = image_dsc->header.w;
lv_coord_t image_height = image_dsc->header.h;
if (image_width <= 0 || image_height <= 0) {
lv_image_set_scale(image_obj, 256);
return;
}
lv_coord_t max_width = LV_HOR_RES;
lv_coord_t max_height = LV_VER_RES - top_reserved - bottom_reserved;
max_height = std::max<lv_coord_t>(max_height, 1);
lv_coord_t scale_w = max_width * 256 / image_width;
lv_coord_t scale_h = max_height * 256 / image_height;
lv_coord_t scale = std::min(scale_w, scale_h);
scale = std::min<lv_coord_t>(scale, kEmojiMaxScale);
scale = std::max<lv_coord_t>(scale, 1);
lv_coord_t scaled_width = image_width * scale / 256;
lv_coord_t scaled_height = image_height * scale / 256;
lv_image_set_scale(image_obj, scale);
lv_obj_set_size(image_obj, scaled_width, scaled_height);
lv_obj_t* align_obj = image_box != nullptr ? image_box : image_obj;
lv_obj_set_size(align_obj, scaled_width, scaled_height);
lv_obj_align(align_obj, LV_ALIGN_TOP_MID, 0, top_reserved + (max_height - scaled_height) / 2);
if (image_box != nullptr) {
lv_obj_center(image_obj);
}
ESP_LOGI(TAG, "Emoji image scale=%ld reserved=%ld/%ld size=%ldx%ld -> %ldx%ld",
static_cast<long>(scale), static_cast<long>(top_reserved),
static_cast<long>(bottom_reserved), static_cast<long>(image_width),
static_cast<long>(image_height),
static_cast<long>(scaled_width), static_cast<long>(scaled_height));
}
} // namespace
void LcdDisplay::InitializeLcdThemes() {
auto text_font = std::make_shared<LvglBuiltInFont>(&BUILTIN_TEXT_FONT);
auto icon_font = std::make_shared<LvglBuiltInFont>(&BUILTIN_ICON_FONT);
@ -1136,6 +1187,9 @@ void LcdDisplay::SetEmotion(const char* emotion) {
auto emoji_collection = static_cast<LvglTheme*>(current_theme_)->emoji_collection();
auto image = emoji_collection != nullptr ? emoji_collection->GetEmojiImage(emotion) : nullptr;
if (image == nullptr && emoji_collection != nullptr && strcmp(emotion, "neutral") != 0) {
image = emoji_collection->GetEmojiImage("neutral");
}
if (image == nullptr) {
const char* utf8 = font_awesome_get_utf8(emotion);
if (utf8 != nullptr && emoji_label_ != nullptr) {
@ -1148,17 +1202,28 @@ void LcdDisplay::SetEmotion(const char* emotion) {
}
DisplayLockGuard lock(this);
lv_coord_t top_reserved = ObjectHeight(top_bar_);
lv_coord_t bottom_reserved = ObjectHeight(status_bar_) + ObjectHeight(bottom_bar_);
auto apply_emoji_layout = [this, top_reserved, bottom_reserved](const lv_image_dsc_t* image_dsc) {
ApplyEmojiImageScale(emoji_image_, emoji_box_, image_dsc, top_reserved, bottom_reserved);
};
if (image->IsGif()) {
// Create new GIF controller
gif_controller_ = std::make_unique<LvglGif>(image->image_dsc());
if (gif_controller_->IsLoaded()) {
// Set up frame update callback
gif_controller_->SetFrameCallback(
[this]() { lv_image_set_src(emoji_image_, gif_controller_->image_dsc()); });
gif_controller_->SetFrameCallback([this, apply_emoji_layout]() {
auto frame = gif_controller_->image_dsc();
lv_image_set_src(emoji_image_, frame);
apply_emoji_layout(frame);
});
// Set initial frame and start animation
lv_image_set_src(emoji_image_, gif_controller_->image_dsc());
auto frame = gif_controller_->image_dsc();
lv_image_set_src(emoji_image_, frame);
apply_emoji_layout(frame);
gif_controller_->Start();
// Show GIF, hide others
@ -1169,7 +1234,9 @@ void LcdDisplay::SetEmotion(const char* emotion) {
gif_controller_.reset();
}
} else {
lv_image_set_src(emoji_image_, image->image_dsc());
auto image_dsc = image->image_dsc();
lv_image_set_src(emoji_image_, image_dsc);
apply_emoji_layout(image_dsc);
lv_obj_add_flag(emoji_label_, LV_OBJ_FLAG_HIDDEN);
lv_obj_remove_flag(emoji_image_, LV_OBJ_FLAG_HIDDEN);
}