lcd_display.cc 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. #include "lcd_display.h"
  2. #include <vector>
  3. #include <font_awesome_symbols.h>
  4. #include <esp_log.h>
  5. #include <esp_err.h>
  6. #include <esp_lvgl_port.h>
  7. #include "assets/lang_config.h"
  8. #include <cstring>
  9. #include "settings.h"
  10. #include "command.h"
  11. #include "board.h"
  12. #include "esp_lcd_touch_gt911.h"
  13. #define TAG "LcdDisplay"
  14. // Color definitions for dark theme
  15. #define DARK_BACKGROUND_COLOR lv_color_hex(0x121212) // Dark background
  16. #define DARK_TEXT_COLOR lv_color_white() // White text
  17. #define DARK_CHAT_BACKGROUND_COLOR lv_color_hex(0x1E1E1E) // Slightly lighter than background
  18. #define DARK_USER_BUBBLE_COLOR lv_color_hex(0x1A6C37) // Dark green
  19. #define DARK_ASSISTANT_BUBBLE_COLOR lv_color_hex(0x333333) // Dark gray
  20. #define DARK_SYSTEM_BUBBLE_COLOR lv_color_hex(0x2A2A2A) // Medium gray
  21. #define DARK_SYSTEM_TEXT_COLOR lv_color_hex(0xAAAAAA) // Light gray text
  22. #define DARK_BORDER_COLOR lv_color_hex(0x333333) // Dark gray border
  23. #define DARK_LOW_BATTERY_COLOR lv_color_hex(0xFF0000) // Red for dark mode
  24. // Color definitions for light theme
  25. #define LIGHT_BACKGROUND_COLOR lv_color_white() // White background
  26. #define LIGHT_TEXT_COLOR lv_color_black() // Black text
  27. #define LIGHT_CHAT_BACKGROUND_COLOR lv_color_hex(0xE0E0E0) // Light gray background
  28. #define LIGHT_USER_BUBBLE_COLOR lv_color_hex(0x95EC69) // WeChat green
  29. #define LIGHT_ASSISTANT_BUBBLE_COLOR lv_color_white() // White
  30. #define LIGHT_SYSTEM_BUBBLE_COLOR lv_color_hex(0xE0E0E0) // Light gray
  31. #define LIGHT_SYSTEM_TEXT_COLOR lv_color_hex(0x666666) // Dark gray text
  32. #define LIGHT_BORDER_COLOR lv_color_hex(0xE0E0E0) // Light gray border
  33. #define LIGHT_LOW_BATTERY_COLOR lv_color_black() // Black for light mode
  34. // Theme color structure
  35. struct ThemeColors {
  36. lv_color_t background;
  37. lv_color_t text;
  38. lv_color_t chat_background;
  39. lv_color_t user_bubble;
  40. lv_color_t assistant_bubble;
  41. lv_color_t system_bubble;
  42. lv_color_t system_text;
  43. lv_color_t border;
  44. lv_color_t low_battery;
  45. };
  46. // Define dark theme colors
  47. static const ThemeColors DARK_THEME = {
  48. .background = DARK_BACKGROUND_COLOR,
  49. .text = DARK_TEXT_COLOR,
  50. .chat_background = DARK_CHAT_BACKGROUND_COLOR,
  51. .user_bubble = DARK_USER_BUBBLE_COLOR,
  52. .assistant_bubble = DARK_ASSISTANT_BUBBLE_COLOR,
  53. .system_bubble = DARK_SYSTEM_BUBBLE_COLOR,
  54. .system_text = DARK_SYSTEM_TEXT_COLOR,
  55. .border = DARK_BORDER_COLOR,
  56. .low_battery = DARK_LOW_BATTERY_COLOR
  57. };
  58. // Define light theme colors
  59. static const ThemeColors LIGHT_THEME = {
  60. .background = LIGHT_BACKGROUND_COLOR,
  61. .text = LIGHT_TEXT_COLOR,
  62. .chat_background = LIGHT_CHAT_BACKGROUND_COLOR,
  63. .user_bubble = LIGHT_USER_BUBBLE_COLOR,
  64. .assistant_bubble = LIGHT_ASSISTANT_BUBBLE_COLOR,
  65. .system_bubble = LIGHT_SYSTEM_BUBBLE_COLOR,
  66. .system_text = LIGHT_SYSTEM_TEXT_COLOR,
  67. .border = LIGHT_BORDER_COLOR,
  68. .low_battery = LIGHT_LOW_BATTERY_COLOR
  69. };
  70. // Current theme - initialize based on default config
  71. static ThemeColors current_theme = LIGHT_THEME;
  72. LV_FONT_DECLARE(font_awesome_30_4);
  73. SpiLcdDisplay::SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
  74. int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy,
  75. DisplayFonts fonts)
  76. : LcdDisplay(panel_io, panel, fonts) {
  77. width_ = width;
  78. height_ = height;
  79. // draw white
  80. std::vector<uint16_t> buffer(width_, 0xFFFF);
  81. for (int y = 0; y < height_; y++) {
  82. esp_lcd_panel_draw_bitmap(panel_, 0, y, width_, y + 1, buffer.data());
  83. }
  84. // Set the display to on
  85. ESP_LOGI(TAG, "Turning display on");
  86. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
  87. ESP_LOGI(TAG, "Initialize LVGL library");
  88. lv_init();
  89. ESP_LOGI(TAG, "Initialize LVGL port");
  90. lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
  91. port_cfg.task_priority = 1;
  92. lvgl_port_init(&port_cfg);
  93. ESP_LOGI(TAG, "Adding LCD screen");
  94. const lvgl_port_display_cfg_t display_cfg = {
  95. .io_handle = panel_io_,
  96. .panel_handle = panel_,
  97. .control_handle = nullptr,
  98. .buffer_size = static_cast<uint32_t>(width_ * 10),
  99. .double_buffer = false,
  100. .trans_size = 0,
  101. .hres = static_cast<uint32_t>(width_),
  102. .vres = static_cast<uint32_t>(height_),
  103. .monochrome = false,
  104. .rotation = {
  105. .swap_xy = swap_xy,
  106. .mirror_x = mirror_x,
  107. .mirror_y = mirror_y,
  108. },
  109. .color_format = LV_COLOR_FORMAT_RGB565,
  110. .flags = {
  111. .buff_dma = 1,
  112. .buff_spiram = 0,
  113. .sw_rotate = 0,
  114. .swap_bytes = 1,
  115. .full_refresh = 0,
  116. .direct_mode = 0,
  117. },
  118. };
  119. display_ = lvgl_port_add_disp(&display_cfg);
  120. if (display_ == nullptr) {
  121. ESP_LOGE(TAG, "Failed to add display");
  122. return;
  123. }
  124. if (offset_x != 0 || offset_y != 0) {
  125. lv_display_set_offset(display_, offset_x, offset_y);
  126. }
  127. // Update the theme
  128. if (current_theme_name_ == "dark") {
  129. current_theme = DARK_THEME;
  130. } else if (current_theme_name_ == "light") {
  131. current_theme = LIGHT_THEME;
  132. }
  133. SetupUI();
  134. }
  135. // RGB LCD实现
  136. RgbLcdDisplay::RgbLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
  137. int width, int height, int offset_x, int offset_y,
  138. bool mirror_x, bool mirror_y, bool swap_xy,
  139. DisplayFonts fonts)
  140. : LcdDisplay(panel_io, panel, fonts) {
  141. width_ = width;
  142. height_ = height;
  143. // draw white
  144. std::vector<uint16_t> buffer(width_, 0xFFFF);
  145. for (int y = 0; y < height_; y++) {
  146. esp_lcd_panel_draw_bitmap(panel_, 0, y, width_, y + 1, buffer.data());
  147. }
  148. ESP_LOGI(TAG, "Initialize LVGL library");
  149. lv_init();
  150. ESP_LOGI(TAG, "Initialize LVGL port");
  151. lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
  152. port_cfg.task_priority = 1;
  153. lvgl_port_init(&port_cfg);
  154. ESP_LOGI(TAG, "Adding LCD screen");
  155. const lvgl_port_display_cfg_t display_cfg = {
  156. .io_handle = panel_io_,
  157. .panel_handle = panel_,
  158. .buffer_size = static_cast<uint32_t>(width_ * 10),
  159. .double_buffer = true,
  160. .hres = static_cast<uint32_t>(width_),
  161. .vres = static_cast<uint32_t>(height_),
  162. .rotation = {
  163. .swap_xy = swap_xy,
  164. .mirror_x = mirror_x,
  165. .mirror_y = mirror_y,
  166. },
  167. .flags = {
  168. .buff_dma = 1,
  169. .swap_bytes = 0,
  170. .full_refresh = 1,
  171. .direct_mode = 1,
  172. },
  173. };
  174. const lvgl_port_display_rgb_cfg_t rgb_cfg = {
  175. .flags = {
  176. .bb_mode = true,
  177. .avoid_tearing = true,
  178. }
  179. };
  180. display_ = lvgl_port_add_disp_rgb(&display_cfg, &rgb_cfg);
  181. if (display_ == nullptr) {
  182. ESP_LOGE(TAG, "Failed to add RGB display");
  183. return;
  184. }
  185. if (offset_x != 0 || offset_y != 0) {
  186. lv_display_set_offset(display_, offset_x, offset_y);
  187. }
  188. // Update the theme
  189. if (current_theme_name_ == "dark") {
  190. current_theme = DARK_THEME;
  191. } else if (current_theme_name_ == "light") {
  192. current_theme = LIGHT_THEME;
  193. }
  194. SetupUI();
  195. }
  196. MipiLcdDisplay::MipiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,i2c_master_bus_handle_t i2c_handle,
  197. int width, int height, int offset_x, int offset_y,
  198. bool mirror_x, bool mirror_y, bool swap_xy,
  199. DisplayFonts fonts): LcdDisplay(panel_io, panel, fonts) {
  200. width_ = width;
  201. height_ = height;
  202. ESP_LOGI(TAG, "Initialize LVGL library");
  203. // printf("Initialize LVGL library for P4");
  204. //P4的LCD用的是这里,但是ESP_LOGI在这里不打印,不知道为啥。使用printf可以打印出来。
  205. lv_init();
  206. ESP_LOGI(TAG, "Initialize LVGL port");
  207. lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
  208. lvgl_port_init(&port_cfg);
  209. const lvgl_port_display_cfg_t disp_cfg = {
  210. .io_handle = panel_io_,
  211. .panel_handle = panel_,
  212. .control_handle = NULL,
  213. .buffer_size = static_cast<uint32_t>(width * 50),
  214. .double_buffer = false,
  215. .hres = static_cast<uint32_t>(width_),
  216. .vres = static_cast<uint32_t>(height_),
  217. .monochrome = false,
  218. .rotation = {
  219. .swap_xy = false,
  220. .mirror_x = true,
  221. .mirror_y = true,
  222. },
  223. .color_format = LV_COLOR_FORMAT_RGB565,
  224. .flags = {
  225. .buff_dma = true,
  226. .buff_spiram = true,
  227. .sw_rotate = true,
  228. .swap_bytes = 0,
  229. .full_refresh = false,
  230. .direct_mode = false,
  231. }
  232. };
  233. lvgl_port_display_dsi_cfg_t dpi_cfg = {
  234. .flags = {
  235. .avoid_tearing = false,
  236. }
  237. };
  238. display_ = lvgl_port_add_disp_dsi(&disp_cfg, &dpi_cfg);
  239. if (display_ == nullptr) {
  240. ESP_LOGE(TAG, "Failed to add display");
  241. return;
  242. }
  243. if (offset_x != 0 || offset_y != 0) {
  244. lv_display_set_offset(display_, offset_x, offset_y);
  245. }
  246. if (current_theme_name_ == "dark") {
  247. current_theme = DARK_THEME;
  248. } else if (current_theme_name_ == "light") {
  249. current_theme = LIGHT_THEME;
  250. }
  251. esp_lcd_touch_handle_t tp;
  252. esp_lcd_touch_config_t tp_cfg = {
  253. .x_max = 800,
  254. .y_max = 1280,
  255. .rst_gpio_num = GPIO_NUM_NC,
  256. .int_gpio_num = GPIO_NUM_NC,
  257. .levels = {.reset = 0, .interrupt = 0},
  258. .flags = {.swap_xy = 0, .mirror_x = 0, .mirror_y = 0},
  259. };
  260. esp_lcd_panel_io_handle_t tp_io_handle = NULL;
  261. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
  262. tp_io_config.scl_speed_hz = 100000;
  263. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(i2c_handle, &tp_io_config, &tp_io_handle));
  264. ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, &tp));
  265. const lvgl_port_touch_cfg_t touch_cfg = {
  266. .disp = lv_display_get_default(),
  267. .handle = tp,
  268. };
  269. lvgl_port_add_touch(&touch_cfg);
  270. lv_disp_t* disp = lv_display_get_default();
  271. if (disp) {
  272. // 先设置旋转(顺时针90度)
  273. lv_disp_set_rotation(disp, LV_DISP_ROTATION_90);
  274. }
  275. SetupUI();
  276. }
  277. LcdDisplay::~LcdDisplay() {
  278. // 然后再清理 LVGL 对象
  279. if (content_ != nullptr) {
  280. lv_obj_del(content_);
  281. }
  282. if (status_bar_ != nullptr) {
  283. lv_obj_del(status_bar_);
  284. }
  285. if (side_bar_ != nullptr) {
  286. lv_obj_del(side_bar_);
  287. }
  288. if (container_ != nullptr) {
  289. lv_obj_del(container_);
  290. }
  291. if (display_ != nullptr) {
  292. lv_display_delete(display_);
  293. }
  294. if (panel_ != nullptr) {
  295. esp_lcd_panel_del(panel_);
  296. }
  297. if (panel_io_ != nullptr) {
  298. esp_lcd_panel_io_del(panel_io_);
  299. }
  300. }
  301. bool LcdDisplay::Lock(int timeout_ms) {
  302. return lvgl_port_lock(timeout_ms);
  303. }
  304. void LcdDisplay::Unlock() {
  305. lvgl_port_unlock();
  306. }
  307. #if CONFIG_USE_WECHAT_MESSAGE_STYLE
  308. void LcdDisplay::SetupUI() {
  309. DisplayLockGuard lock(this);
  310. printf("SetupUI1\n");
  311. auto screen = lv_screen_active();
  312. lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
  313. lv_obj_set_style_text_color(screen, current_theme.text, 0);
  314. lv_obj_set_style_bg_color(screen, current_theme.background, 0);
  315. /* Container */
  316. container_ = lv_obj_create(screen);
  317. lv_obj_set_size(container_, LV_HOR_RES, LV_VER_RES);
  318. lv_obj_set_flex_flow(container_, LV_FLEX_FLOW_COLUMN);
  319. lv_obj_set_style_pad_all(container_, 0, 0);
  320. lv_obj_set_style_border_width(container_, 0, 0);
  321. lv_obj_set_style_pad_row(container_, 0, 0);
  322. lv_obj_set_style_bg_color(container_, current_theme.background, 0);
  323. lv_obj_set_style_border_color(container_, current_theme.border, 0);
  324. /* Status bar */
  325. status_bar_ = lv_obj_create(container_);
  326. lv_obj_set_size(status_bar_, LV_HOR_RES, fonts_.emoji_font->line_height);
  327. lv_obj_set_style_radius(status_bar_, 0, 0);
  328. lv_obj_set_style_bg_color(status_bar_, current_theme.background, 0);
  329. lv_obj_set_style_text_color(status_bar_, current_theme.text, 0);
  330. /* Content - Chat area */
  331. content_ = lv_obj_create(container_);
  332. lv_obj_set_style_radius(content_, 0, 0);
  333. lv_obj_set_width(content_, LV_HOR_RES);
  334. lv_obj_set_flex_grow(content_, 1);
  335. lv_obj_set_style_pad_all(content_, 5, 0);
  336. lv_obj_set_style_bg_color(content_, current_theme.chat_background, 0); // Background for chat area
  337. lv_obj_set_style_border_color(content_, current_theme.border, 0); // Border color for chat area
  338. // Enable scrolling for chat content
  339. lv_obj_set_scrollbar_mode(content_, LV_SCROLLBAR_MODE_OFF);
  340. lv_obj_set_scroll_dir(content_, LV_DIR_VER);
  341. // Create a flex container for chat messages
  342. lv_obj_set_flex_flow(content_, LV_FLEX_FLOW_COLUMN);
  343. lv_obj_set_flex_align(content_, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
  344. // 替代方案:使用旧版本兼容方法固定元素位置
  345. // 1. 创建一个固定在右下角的容器
  346. lv_obj_t* fixed_container = lv_obj_create(screen);
  347. lv_obj_set_size(fixed_container, 140, 140); // 根据图标大小调整
  348. lv_obj_align(fixed_container, LV_ALIGN_BOTTOM_RIGHT, -10, -10);
  349. lv_obj_set_style_bg_opa(fixed_container, LV_OPA_TRANSP, 0); // 透明背景
  350. lv_obj_set_style_border_width(fixed_container, 0, 0); // 无边框
  351. lv_obj_clear_flag(fixed_container, LV_OBJ_FLAG_SCROLLABLE); // 禁止滚动
  352. // 2. 将图标添加到这个容器中
  353. // emotion_label_1 = lv_label_create(fixed_container);
  354. // lv_obj_set_style_text_font(emotion_label_1, &font_awesome_30_4, 0);
  355. // lv_obj_set_style_text_color(emotion_label_1, current_theme.text, 0);
  356. // lv_label_set_text(emotion_label_1, FONT_AWESOME_AI_CHIP);
  357. // lv_obj_center(emotion_label_1); // 居中显示在容器中
  358. // lv_obj_set_style_pad_row(content_, 10, 0); // Space between messages
  359. // 添加图片到容器中
  360. lv_obj_t* image_obj = lv_img_create(fixed_container);
  361. // 设置图片源,这里假设使用内置图片资源或已注册的图片
  362. lv_img_set_src(image_obj, &command_img); // 示例:使用内置符号作为图片
  363. // 或者使用自定义图片:lv_img_set_src(image_obj, &image_data);
  364. lv_obj_align(image_obj, LV_ALIGN_BOTTOM_MID, 0, -5); // 居底部中间显示
  365. // lv_obj_set_style_img_recolor_opa(image_obj, LV_OPA_70, 0); // 设置图片透明度
  366. // lv_obj_set_style_img_recolor(image_obj, lv_color_hex(0xF8F8FF), 0); // 设置图片颜色
  367. // We'll create chat messages dynamically in SetChatMessage
  368. chat_message_label_ = nullptr;
  369. /* Status bar */
  370. lv_obj_set_flex_flow(status_bar_, LV_FLEX_FLOW_ROW);
  371. lv_obj_set_style_pad_all(status_bar_, 0, 0);
  372. lv_obj_set_style_border_width(status_bar_, 0, 0);
  373. lv_obj_set_style_pad_column(status_bar_, 0, 0);
  374. lv_obj_set_style_pad_left(status_bar_, 2, 0);
  375. lv_obj_set_style_pad_right(status_bar_, 2, 0);
  376. lv_obj_set_scrollbar_mode(status_bar_, LV_SCROLLBAR_MODE_OFF);
  377. // 设置状态栏的内容垂直居中
  378. lv_obj_set_flex_align(status_bar_, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
  379. // 创建emotion_label_在状态栏最左侧
  380. emotion_label_ = lv_label_create(status_bar_);
  381. lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
  382. lv_obj_set_style_text_color(emotion_label_, current_theme.text, 0);
  383. lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
  384. lv_obj_set_style_margin_right(emotion_label_, 5, 0); // 添加右边距,与后面的元素分隔
  385. notification_label_ = lv_label_create(status_bar_);
  386. lv_obj_set_flex_grow(notification_label_, 1);
  387. lv_obj_set_style_text_align(notification_label_, LV_TEXT_ALIGN_CENTER, 0);
  388. lv_obj_set_style_text_color(notification_label_, current_theme.text, 0);
  389. lv_label_set_text(notification_label_, "");
  390. lv_obj_add_flag(notification_label_, LV_OBJ_FLAG_HIDDEN);
  391. status_label_ = lv_label_create(status_bar_);
  392. lv_obj_set_flex_grow(status_label_, 1);
  393. lv_label_set_long_mode(status_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
  394. lv_obj_set_style_text_align(status_label_, LV_TEXT_ALIGN_CENTER, 0);
  395. lv_obj_set_style_text_color(status_label_, current_theme.text, 0);
  396. lv_label_set_text(status_label_, Lang::Strings::INITIALIZING);
  397. mute_label_ = lv_label_create(status_bar_);
  398. lv_label_set_text(mute_label_, "");
  399. lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
  400. lv_obj_set_style_text_color(mute_label_, current_theme.text, 0);
  401. network_label_ = lv_label_create(status_bar_);
  402. lv_label_set_text(network_label_, "");
  403. lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
  404. lv_obj_set_style_text_color(network_label_, current_theme.text, 0);
  405. lv_obj_set_style_margin_left(network_label_, 5, 0); // 添加左边距,与前面的元素分隔
  406. battery_label_ = lv_label_create(status_bar_);
  407. lv_label_set_text(battery_label_, "");
  408. lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
  409. lv_obj_set_style_text_color(battery_label_, current_theme.text, 0);
  410. lv_obj_set_style_margin_left(battery_label_, 5, 0); // 添加左边距,与前面的元素分隔
  411. low_battery_popup_ = lv_obj_create(screen);
  412. lv_obj_set_scrollbar_mode(low_battery_popup_, LV_SCROLLBAR_MODE_OFF);
  413. lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, fonts_.text_font->line_height * 2);
  414. lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
  415. lv_obj_set_style_bg_color(low_battery_popup_, current_theme.low_battery, 0);
  416. lv_obj_set_style_radius(low_battery_popup_, 10, 0);
  417. lv_obj_t* low_battery_label = lv_label_create(low_battery_popup_);
  418. lv_label_set_text(low_battery_label, Lang::Strings::BATTERY_NEED_CHARGE);
  419. lv_obj_set_style_text_color(low_battery_label, lv_color_white(), 0);
  420. lv_obj_center(low_battery_label);
  421. lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN);
  422. }
  423. #define MAX_MESSAGES 300
  424. void LcdDisplay::SetChatMessage(const char* role, const char* content) {
  425. DisplayLockGuard lock(this);
  426. if (content_ == nullptr) {
  427. return;
  428. }
  429. //避免出现空的消息框
  430. if(strlen(content) == 0) return;
  431. // Create a message bubble
  432. lv_obj_t* msg_bubble = lv_obj_create(content_);
  433. lv_obj_set_style_radius(msg_bubble, 8, 0);
  434. lv_obj_set_scrollbar_mode(msg_bubble, LV_SCROLLBAR_MODE_OFF);
  435. lv_obj_set_style_border_width(msg_bubble, 1, 0);
  436. lv_obj_set_style_border_color(msg_bubble, current_theme.border, 0);
  437. lv_obj_set_style_pad_all(msg_bubble, 8, 0);
  438. // Create the message text
  439. lv_obj_t* msg_text = lv_label_create(msg_bubble);
  440. lv_label_set_text(msg_text, content);
  441. // 计算文本实际宽度
  442. lv_coord_t text_width = lv_txt_get_width(content, strlen(content), fonts_.text_font, 0);
  443. // 计算气泡宽度
  444. lv_coord_t max_width = LV_HOR_RES * 85 / 100 - 16; // 屏幕宽度的85%
  445. lv_coord_t min_width = 20;
  446. lv_coord_t bubble_width;
  447. // 确保文本宽度不小于最小宽度
  448. if (text_width < min_width) {
  449. text_width = min_width;
  450. }
  451. // 如果文本宽度小于最大宽度,使用文本宽度
  452. if (text_width < max_width) {
  453. bubble_width = text_width;
  454. } else {
  455. bubble_width = max_width;
  456. }
  457. // 设置消息文本的宽度
  458. lv_obj_set_width(msg_text, bubble_width); // 减去padding
  459. lv_label_set_long_mode(msg_text, LV_LABEL_LONG_WRAP);
  460. lv_obj_set_style_text_font(msg_text, fonts_.text_font, 0);
  461. // 设置气泡宽度
  462. lv_obj_set_width(msg_bubble, bubble_width);
  463. lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
  464. // Set alignment and style based on message role
  465. if (strcmp(role, "user") == 0) {
  466. // User messages are right-aligned with green background
  467. lv_obj_set_style_bg_color(msg_bubble, current_theme.user_bubble, 0);
  468. // Set text color for contrast
  469. lv_obj_set_style_text_color(msg_text, current_theme.text, 0);
  470. // 设置自定义属性标记气泡类型
  471. lv_obj_set_user_data(msg_bubble, (void*)"user");
  472. // Set appropriate width for content
  473. lv_obj_set_width(msg_bubble, LV_SIZE_CONTENT);
  474. lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
  475. // Add some margin
  476. lv_obj_set_style_margin_right(msg_bubble, 10, 0);
  477. // Don't grow
  478. lv_obj_set_style_flex_grow(msg_bubble, 0, 0);
  479. } else if (strcmp(role, "assistant") == 0) {
  480. // Assistant messages are left-aligned with white background
  481. lv_obj_set_style_bg_color(msg_bubble, current_theme.assistant_bubble, 0);
  482. // Set text color for contrast
  483. lv_obj_set_style_text_color(msg_text, current_theme.text, 0);
  484. // 设置自定义属性标记气泡类型
  485. lv_obj_set_user_data(msg_bubble, (void*)"assistant");
  486. // Set appropriate width for content
  487. lv_obj_set_width(msg_bubble, LV_SIZE_CONTENT);
  488. lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
  489. // Add some margin
  490. lv_obj_set_style_margin_left(msg_bubble, -4, 0);
  491. // Don't grow
  492. lv_obj_set_style_flex_grow(msg_bubble, 0, 0);
  493. } else if (strcmp(role, "system") == 0) {
  494. // System messages are center-aligned with light gray background
  495. lv_obj_set_style_bg_color(msg_bubble, current_theme.system_bubble, 0);
  496. // Set text color for contrast
  497. lv_obj_set_style_text_color(msg_text, current_theme.system_text, 0);
  498. // 设置自定义属性标记气泡类型
  499. lv_obj_set_user_data(msg_bubble, (void*)"system");
  500. // Set appropriate width for content
  501. lv_obj_set_width(msg_bubble, LV_SIZE_CONTENT);
  502. lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
  503. // Don't grow
  504. lv_obj_set_style_flex_grow(msg_bubble, 0, 0);
  505. }
  506. // Create a full-width container for user messages to ensure right alignment
  507. if (strcmp(role, "user") == 0) {
  508. // Create a full-width container
  509. lv_obj_t* container = lv_obj_create(content_);
  510. lv_obj_set_width(container, LV_HOR_RES);
  511. lv_obj_set_height(container, LV_SIZE_CONTENT);
  512. // Make container transparent and borderless
  513. lv_obj_set_style_bg_opa(container, LV_OPA_TRANSP, 0);
  514. lv_obj_set_style_border_width(container, 0, 0);
  515. lv_obj_set_style_pad_all(container, 0, 0);
  516. // Move the message bubble into this container
  517. lv_obj_set_parent(msg_bubble, container);
  518. // Right align the bubble in the container
  519. lv_obj_align(msg_bubble, LV_ALIGN_RIGHT_MID, -10, 0);
  520. // Auto-scroll to this container
  521. lv_obj_scroll_to_view_recursive(container, LV_ANIM_ON);
  522. } else if (strcmp(role, "system") == 0) {
  523. // 为系统消息创建全宽容器以确保居中对齐
  524. lv_obj_t* container = lv_obj_create(content_);
  525. lv_obj_set_width(container, LV_HOR_RES);
  526. lv_obj_set_height(container, LV_SIZE_CONTENT);
  527. // 使容器透明且无边框
  528. lv_obj_set_style_bg_opa(container, LV_OPA_TRANSP, 0);
  529. lv_obj_set_style_border_width(container, 0, 0);
  530. lv_obj_set_style_pad_all(container, 0, 0);
  531. // 将消息气泡移入此容器
  532. lv_obj_set_parent(msg_bubble, container);
  533. // 将气泡居中对齐在容器中
  534. lv_obj_align(msg_bubble, LV_ALIGN_CENTER, 0, 0);
  535. // 自动滚动底部
  536. lv_obj_scroll_to_view_recursive(container, LV_ANIM_ON);
  537. } else {
  538. // For assistant messages
  539. // Left align assistant messages
  540. lv_obj_align(msg_bubble, LV_ALIGN_LEFT_MID, 0, 0);
  541. // Auto-scroll to the message bubble
  542. lv_obj_scroll_to_view_recursive(msg_bubble, LV_ANIM_ON);
  543. }
  544. // Store reference to the latest message label
  545. chat_message_label_ = msg_text;
  546. // 检查消息数量是否超过限制
  547. uint32_t msg_count = lv_obj_get_child_cnt(content_);
  548. while (msg_count >= MAX_MESSAGES) {
  549. // 删除最早的消息(第一个子节点)
  550. lv_obj_t* oldest_msg = lv_obj_get_child(content_, 0);
  551. if (oldest_msg != nullptr) {
  552. lv_obj_del(oldest_msg);
  553. msg_count--;
  554. // 删除最早的消息会导致所有气泡整体往上移
  555. // 所以需要重新滚动到当前消息气泡位置
  556. lv_obj_scroll_to_view_recursive(msg_bubble, LV_ANIM_ON);
  557. }else{
  558. break;
  559. }
  560. }
  561. }
  562. #else
  563. void LcdDisplay::SetupUI() {
  564. DisplayLockGuard lock(this);
  565. printf("SetupUI2\n");
  566. auto screen = lv_screen_active();
  567. lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
  568. lv_obj_set_style_text_color(screen, current_theme.text, 0);
  569. lv_obj_set_style_bg_color(screen, current_theme.background, 0);
  570. /* Container */
  571. container_ = lv_obj_create(screen);
  572. lv_obj_set_size(container_, LV_HOR_RES, LV_VER_RES);
  573. lv_obj_set_flex_flow(container_, LV_FLEX_FLOW_COLUMN);
  574. lv_obj_set_style_pad_all(container_, 0, 0);
  575. lv_obj_set_style_border_width(container_, 0, 0);
  576. lv_obj_set_style_pad_row(container_, 0, 0);
  577. lv_obj_set_style_bg_color(container_, current_theme.background, 0);
  578. lv_obj_set_style_border_color(container_, current_theme.border, 0);
  579. /* Status bar */
  580. status_bar_ = lv_obj_create(container_);
  581. lv_obj_set_size(status_bar_, LV_HOR_RES, fonts_.text_font->line_height);
  582. lv_obj_set_style_radius(status_bar_, 0, 0);
  583. lv_obj_set_style_bg_color(status_bar_, current_theme.background, 0);
  584. lv_obj_set_style_text_color(status_bar_, current_theme.text, 0);
  585. /* Content */
  586. content_ = lv_obj_create(container_);
  587. lv_obj_set_scrollbar_mode(content_, LV_SCROLLBAR_MODE_OFF);
  588. lv_obj_set_style_radius(content_, 0, 0);
  589. lv_obj_set_width(content_, LV_HOR_RES);
  590. lv_obj_set_flex_grow(content_, 1);
  591. lv_obj_set_style_pad_all(content_, 5, 0);
  592. lv_obj_set_style_bg_color(content_, current_theme.chat_background, 0);
  593. lv_obj_set_style_border_color(content_, current_theme.border, 0); // Border color for content
  594. lv_obj_set_flex_flow(content_, LV_FLEX_FLOW_COLUMN); // 垂直布局(从上到下)
  595. lv_obj_set_flex_align(content_, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_EVENLY); // 子对象居中对齐,等距分布
  596. emotion_label_ = lv_label_create(content_);
  597. lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
  598. lv_obj_set_style_text_color(emotion_label_, current_theme.text, 0);
  599. lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
  600. chat_message_label_ = lv_label_create(content_);
  601. lv_label_set_text(chat_message_label_, "");
  602. lv_obj_set_width(chat_message_label_, LV_HOR_RES * 0.9); // 限制宽度为屏幕宽度的 90%
  603. lv_label_set_long_mode(chat_message_label_, LV_LABEL_LONG_WRAP); // 设置为自动换行模式
  604. lv_obj_set_style_text_align(chat_message_label_, LV_TEXT_ALIGN_CENTER, 0); // 设置文本居中对齐
  605. lv_obj_set_style_text_color(chat_message_label_, current_theme.text, 0);
  606. /* Status bar */
  607. lv_obj_set_flex_flow(status_bar_, LV_FLEX_FLOW_ROW);
  608. lv_obj_set_style_pad_all(status_bar_, 0, 0);
  609. lv_obj_set_style_border_width(status_bar_, 0, 0);
  610. lv_obj_set_style_pad_column(status_bar_, 0, 0);
  611. lv_obj_set_style_pad_left(status_bar_, 2, 0);
  612. lv_obj_set_style_pad_right(status_bar_, 2, 0);
  613. network_label_ = lv_label_create(status_bar_);
  614. lv_label_set_text(network_label_, "");
  615. lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
  616. lv_obj_set_style_text_color(network_label_, current_theme.text, 0);
  617. notification_label_ = lv_label_create(status_bar_);
  618. lv_obj_set_flex_grow(notification_label_, 1);
  619. lv_obj_set_style_text_align(notification_label_, LV_TEXT_ALIGN_CENTER, 0);
  620. lv_obj_set_style_text_color(notification_label_, current_theme.text, 0);
  621. lv_label_set_text(notification_label_, "");
  622. lv_obj_add_flag(notification_label_, LV_OBJ_FLAG_HIDDEN);
  623. status_label_ = lv_label_create(status_bar_);
  624. lv_obj_set_flex_grow(status_label_, 1);
  625. lv_label_set_long_mode(status_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
  626. lv_obj_set_style_text_align(status_label_, LV_TEXT_ALIGN_CENTER, 0);
  627. lv_obj_set_style_text_color(status_label_, current_theme.text, 0);
  628. lv_label_set_text(status_label_, Lang::Strings::INITIALIZING);
  629. mute_label_ = lv_label_create(status_bar_);
  630. lv_label_set_text(mute_label_, "");
  631. lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
  632. lv_obj_set_style_text_color(mute_label_, current_theme.text, 0);
  633. battery_label_ = lv_label_create(status_bar_);
  634. lv_label_set_text(battery_label_, "");
  635. lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
  636. lv_obj_set_style_text_color(battery_label_, current_theme.text, 0);
  637. low_battery_popup_ = lv_obj_create(screen);
  638. lv_obj_set_scrollbar_mode(low_battery_popup_, LV_SCROLLBAR_MODE_OFF);
  639. lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, fonts_.text_font->line_height * 2);
  640. lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
  641. lv_obj_set_style_bg_color(low_battery_popup_, current_theme.low_battery, 0);
  642. lv_obj_set_style_radius(low_battery_popup_, 10, 0);
  643. lv_obj_t* low_battery_label = lv_label_create(low_battery_popup_);
  644. lv_label_set_text(low_battery_label, Lang::Strings::BATTERY_NEED_CHARGE);
  645. lv_obj_set_style_text_color(low_battery_label, lv_color_white(), 0);
  646. lv_obj_center(low_battery_label);
  647. lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN);
  648. }
  649. #endif
  650. void LcdDisplay::SetEmotion(const char* emotion) {
  651. struct Emotion {
  652. const char* icon;
  653. const char* text;
  654. };
  655. static const std::vector<Emotion> emotions = {
  656. {"😶", "neutral"},
  657. {"🙂", "happy"},
  658. {"😆", "laughing"},
  659. {"😂", "funny"},
  660. {"😔", "sad"},
  661. {"😠", "angry"},
  662. {"😭", "crying"},
  663. {"😍", "loving"},
  664. {"😳", "embarrassed"},
  665. {"😯", "surprised"},
  666. {"😱", "shocked"},
  667. {"🤔", "thinking"},
  668. {"😉", "winking"},
  669. {"😎", "cool"},
  670. {"😌", "relaxed"},
  671. {"🤤", "delicious"},
  672. {"😘", "kissy"},
  673. {"😏", "confident"},
  674. {"😴", "sleepy"},
  675. {"😜", "silly"},
  676. {"🙄", "confused"}
  677. };
  678. // 查找匹配的表情
  679. std::string_view emotion_view(emotion);
  680. auto it = std::find_if(emotions.begin(), emotions.end(),
  681. [&emotion_view](const Emotion& e) { return e.text == emotion_view; });
  682. DisplayLockGuard lock(this);
  683. if (emotion_label_ == nullptr) {
  684. return;
  685. }
  686. // 如果找到匹配的表情就显示对应图标,否则显示默认的neutral表情
  687. lv_obj_set_style_text_font(emotion_label_, fonts_.emoji_font, 0);
  688. if (it != emotions.end()) {
  689. lv_label_set_text(emotion_label_, it->icon);
  690. } else {
  691. lv_label_set_text(emotion_label_, "😶");
  692. }
  693. }
  694. void LcdDisplay::SetIcon(const char* icon) {
  695. DisplayLockGuard lock(this);
  696. if (emotion_label_ == nullptr) {
  697. return;
  698. }
  699. lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
  700. lv_label_set_text(emotion_label_, icon);
  701. }
  702. void LcdDisplay::SetTheme(const std::string& theme_name) {
  703. DisplayLockGuard lock(this);
  704. if (theme_name == "dark" || theme_name == "DARK") {
  705. current_theme = DARK_THEME;
  706. } else if (theme_name == "light" || theme_name == "LIGHT") {
  707. current_theme = LIGHT_THEME;
  708. } else {
  709. // Invalid theme name, return false
  710. ESP_LOGE(TAG, "Invalid theme name: %s", theme_name.c_str());
  711. return;
  712. }
  713. // Get the active screen
  714. lv_obj_t* screen = lv_screen_active();
  715. // Update the screen colors
  716. lv_obj_set_style_bg_color(screen, current_theme.background, 0);
  717. lv_obj_set_style_text_color(screen, current_theme.text, 0);
  718. // Update container colors
  719. if (container_ != nullptr) {
  720. lv_obj_set_style_bg_color(container_, current_theme.background, 0);
  721. lv_obj_set_style_border_color(container_, current_theme.border, 0);
  722. }
  723. // Update status bar colors
  724. if (status_bar_ != nullptr) {
  725. lv_obj_set_style_bg_color(status_bar_, current_theme.background, 0);
  726. lv_obj_set_style_text_color(status_bar_, current_theme.text, 0);
  727. // Update status bar elements
  728. if (network_label_ != nullptr) {
  729. lv_obj_set_style_text_color(network_label_, current_theme.text, 0);
  730. }
  731. if (status_label_ != nullptr) {
  732. lv_obj_set_style_text_color(status_label_, current_theme.text, 0);
  733. }
  734. if (notification_label_ != nullptr) {
  735. lv_obj_set_style_text_color(notification_label_, current_theme.text, 0);
  736. }
  737. if (mute_label_ != nullptr) {
  738. lv_obj_set_style_text_color(mute_label_, current_theme.text, 0);
  739. }
  740. if (battery_label_ != nullptr) {
  741. lv_obj_set_style_text_color(battery_label_, current_theme.text, 0);
  742. }
  743. if (emotion_label_ != nullptr) {
  744. lv_obj_set_style_text_color(emotion_label_, current_theme.text, 0);
  745. }
  746. }
  747. // Update content area colors
  748. if (content_ != nullptr) {
  749. lv_obj_set_style_bg_color(content_, current_theme.chat_background, 0);
  750. lv_obj_set_style_border_color(content_, current_theme.border, 0);
  751. // If we have the chat message style, update all message bubbles
  752. #if CONFIG_USE_WECHAT_MESSAGE_STYLE
  753. // Iterate through all children of content (message containers or bubbles)
  754. uint32_t child_count = lv_obj_get_child_cnt(content_);
  755. for (uint32_t i = 0; i < child_count; i++) {
  756. lv_obj_t* obj = lv_obj_get_child(content_, i);
  757. if (obj == nullptr) continue;
  758. lv_obj_t* bubble = nullptr;
  759. // 检查这个对象是容器还是气泡
  760. // 如果是容器(用户或系统消息),则获取其子对象作为气泡
  761. // 如果是气泡(助手消息),则直接使用
  762. if (lv_obj_get_child_cnt(obj) > 0) {
  763. // 可能是容器,检查它是否为用户或系统消息容器
  764. // 用户和系统消息容器是透明的
  765. lv_opa_t bg_opa = lv_obj_get_style_bg_opa(obj, 0);
  766. if (bg_opa == LV_OPA_TRANSP) {
  767. // 这是用户或系统消息的容器
  768. bubble = lv_obj_get_child(obj, 0);
  769. } else {
  770. // 这可能是助手消息的气泡自身
  771. bubble = obj;
  772. }
  773. } else {
  774. // 没有子元素,可能是其他UI元素,跳过
  775. continue;
  776. }
  777. if (bubble == nullptr) continue;
  778. // 使用保存的用户数据来识别气泡类型
  779. void* bubble_type_ptr = lv_obj_get_user_data(bubble);
  780. if (bubble_type_ptr != nullptr) {
  781. const char* bubble_type = static_cast<const char*>(bubble_type_ptr);
  782. // 根据气泡类型应用正确的颜色
  783. if (strcmp(bubble_type, "user") == 0) {
  784. lv_obj_set_style_bg_color(bubble, current_theme.user_bubble, 0);
  785. } else if (strcmp(bubble_type, "assistant") == 0) {
  786. lv_obj_set_style_bg_color(bubble, current_theme.assistant_bubble, 0);
  787. } else if (strcmp(bubble_type, "system") == 0) {
  788. lv_obj_set_style_bg_color(bubble, current_theme.system_bubble, 0);
  789. }
  790. // Update border color
  791. lv_obj_set_style_border_color(bubble, current_theme.border, 0);
  792. // Update text color for the message
  793. if (lv_obj_get_child_cnt(bubble) > 0) {
  794. lv_obj_t* text = lv_obj_get_child(bubble, 0);
  795. if (text != nullptr) {
  796. // 根据气泡类型设置文本颜色
  797. if (strcmp(bubble_type, "system") == 0) {
  798. lv_obj_set_style_text_color(text, current_theme.system_text, 0);
  799. } else {
  800. lv_obj_set_style_text_color(text, current_theme.text, 0);
  801. }
  802. }
  803. }
  804. } else {
  805. // 如果没有标记,回退到之前的逻辑(颜色比较)
  806. // ...保留原有的回退逻辑...
  807. lv_color_t bg_color = lv_obj_get_style_bg_color(bubble, 0);
  808. // 改进bubble类型检测逻辑,不仅使用颜色比较
  809. bool is_user_bubble = false;
  810. bool is_assistant_bubble = false;
  811. bool is_system_bubble = false;
  812. // 检查用户bubble
  813. if (lv_color_eq(bg_color, DARK_USER_BUBBLE_COLOR) ||
  814. lv_color_eq(bg_color, LIGHT_USER_BUBBLE_COLOR) ||
  815. lv_color_eq(bg_color, current_theme.user_bubble)) {
  816. is_user_bubble = true;
  817. }
  818. // 检查系统bubble
  819. else if (lv_color_eq(bg_color, DARK_SYSTEM_BUBBLE_COLOR) ||
  820. lv_color_eq(bg_color, LIGHT_SYSTEM_BUBBLE_COLOR) ||
  821. lv_color_eq(bg_color, current_theme.system_bubble)) {
  822. is_system_bubble = true;
  823. }
  824. // 剩余的都当作助手bubble处理
  825. else {
  826. is_assistant_bubble = true;
  827. }
  828. // 根据bubble类型应用正确的颜色
  829. if (is_user_bubble) {
  830. lv_obj_set_style_bg_color(bubble, current_theme.user_bubble, 0);
  831. } else if (is_assistant_bubble) {
  832. lv_obj_set_style_bg_color(bubble, current_theme.assistant_bubble, 0);
  833. } else if (is_system_bubble) {
  834. lv_obj_set_style_bg_color(bubble, current_theme.system_bubble, 0);
  835. }
  836. // Update border color
  837. lv_obj_set_style_border_color(bubble, current_theme.border, 0);
  838. // Update text color for the message
  839. if (lv_obj_get_child_cnt(bubble) > 0) {
  840. lv_obj_t* text = lv_obj_get_child(bubble, 0);
  841. if (text != nullptr) {
  842. // 回退到颜色检测逻辑
  843. if (lv_color_eq(bg_color, current_theme.system_bubble) ||
  844. lv_color_eq(bg_color, DARK_SYSTEM_BUBBLE_COLOR) ||
  845. lv_color_eq(bg_color, LIGHT_SYSTEM_BUBBLE_COLOR)) {
  846. lv_obj_set_style_text_color(text, current_theme.system_text, 0);
  847. } else {
  848. lv_obj_set_style_text_color(text, current_theme.text, 0);
  849. }
  850. }
  851. }
  852. }
  853. }
  854. #else
  855. // Simple UI mode - just update the main chat message
  856. if (chat_message_label_ != nullptr) {
  857. lv_obj_set_style_text_color(chat_message_label_, current_theme.text, 0);
  858. }
  859. if (emotion_label_ != nullptr) {
  860. lv_obj_set_style_text_color(emotion_label_, current_theme.text, 0);
  861. }
  862. #endif
  863. }
  864. // Update low battery popup
  865. if (low_battery_popup_ != nullptr) {
  866. lv_obj_set_style_bg_color(low_battery_popup_, current_theme.low_battery, 0);
  867. }
  868. // No errors occurred. Save theme to settings
  869. Display::SetTheme(theme_name);
  870. }