lcd_display.cc 44 KB

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