forked from xiaozhi/xiaozhi-esp32
Add theme switch to all LCD boards
This commit is contained in:
@@ -15,6 +15,10 @@
|
||||
#define TAG "Display"
|
||||
|
||||
Display::Display() {
|
||||
// Load theme from settings
|
||||
Settings settings("display", false);
|
||||
current_theme_name_ = settings.GetString("theme", "light");
|
||||
|
||||
// Notification timer
|
||||
esp_timer_create_args_t notification_timer_args = {
|
||||
.callback = [](void *arg) {
|
||||
@@ -249,3 +253,9 @@ void Display::SetChatMessage(const char* role, const char* content) {
|
||||
}
|
||||
lv_label_set_text(chat_message_label_, content);
|
||||
}
|
||||
|
||||
void Display::SetTheme(const std::string& theme_name) {
|
||||
current_theme_name_ = theme_name;
|
||||
Settings settings("display", true);
|
||||
settings.SetString("theme", theme_name);
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@ public:
|
||||
virtual void SetEmotion(const char* emotion);
|
||||
virtual void SetChatMessage(const char* role, const char* content);
|
||||
virtual void SetIcon(const char* icon);
|
||||
virtual void SetTheme(const std::string& theme_name){}
|
||||
virtual std::string GetTheme() { return "light"; }
|
||||
virtual void SetTheme(const std::string& theme_name);
|
||||
virtual std::string GetTheme() { return current_theme_name_; }
|
||||
|
||||
inline int width() const { return width_; }
|
||||
inline int height() const { return height_; }
|
||||
@@ -50,6 +50,7 @@ protected:
|
||||
const char* battery_icon_ = nullptr;
|
||||
const char* network_icon_ = nullptr;
|
||||
bool muted_ = false;
|
||||
std::string current_theme_name_;
|
||||
|
||||
esp_timer_handle_t notification_timer_ = nullptr;
|
||||
esp_timer_handle_t update_timer_ = nullptr;
|
||||
|
||||
@@ -76,8 +76,6 @@ static const ThemeColors LIGHT_THEME = {
|
||||
|
||||
// Current theme - initialize based on default config
|
||||
static ThemeColors current_theme = LIGHT_THEME;
|
||||
// Add theme name variable to track current theme
|
||||
static std::string current_theme_name = "light";
|
||||
|
||||
|
||||
LV_FONT_DECLARE(font_awesome_30_4);
|
||||
@@ -144,17 +142,11 @@ SpiLcdDisplay::SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
|
||||
lv_display_set_offset(display_, offset_x, offset_y);
|
||||
}
|
||||
|
||||
// Load theme from settings
|
||||
Settings settings("display", false);
|
||||
std::string theme = settings.GetString("theme", "");
|
||||
if (!theme.empty()) {
|
||||
if (theme == "dark") {
|
||||
current_theme = DARK_THEME;
|
||||
current_theme_name = "dark";
|
||||
} else if (theme == "light") {
|
||||
current_theme = LIGHT_THEME;
|
||||
current_theme_name = "light";
|
||||
}
|
||||
// Update the theme
|
||||
if (current_theme_name_ == "dark") {
|
||||
current_theme = DARK_THEME;
|
||||
} else if (current_theme_name_ == "light") {
|
||||
current_theme = LIGHT_THEME;
|
||||
}
|
||||
|
||||
SetupUI();
|
||||
@@ -221,17 +213,11 @@ RgbLcdDisplay::RgbLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
|
||||
lv_display_set_offset(display_, offset_x, offset_y);
|
||||
}
|
||||
|
||||
// Load theme from settings
|
||||
Settings settings("display", false);
|
||||
std::string theme = settings.GetString("theme", "");
|
||||
if (!theme.empty()) {
|
||||
if (theme == "dark") {
|
||||
current_theme = DARK_THEME;
|
||||
current_theme_name = "dark";
|
||||
} else if (theme == "light") {
|
||||
current_theme = LIGHT_THEME;
|
||||
current_theme_name = "light";
|
||||
}
|
||||
// Update the theme
|
||||
if (current_theme_name_ == "dark") {
|
||||
current_theme = DARK_THEME;
|
||||
} else if (current_theme_name_ == "light") {
|
||||
current_theme = LIGHT_THEME;
|
||||
}
|
||||
|
||||
SetupUI();
|
||||
@@ -711,22 +697,16 @@ void LcdDisplay::SetIcon(const char* icon) {
|
||||
void LcdDisplay::SetTheme(const std::string& theme_name) {
|
||||
DisplayLockGuard lock(this);
|
||||
|
||||
// Update the current theme based on the theme name
|
||||
if (theme_name == "dark" || theme_name == "DARK") {
|
||||
current_theme = DARK_THEME;
|
||||
current_theme_name = "dark";
|
||||
} else if (theme_name == "light" || theme_name == "LIGHT") {
|
||||
current_theme = LIGHT_THEME;
|
||||
current_theme_name = "light";
|
||||
} else {
|
||||
// Invalid theme name, return false
|
||||
ESP_LOGE(TAG, "Invalid theme name: %s", theme_name.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
// Save theme to settings
|
||||
Settings settings("display", true);
|
||||
settings.SetString("theme", current_theme_name);
|
||||
|
||||
// Get the active screen
|
||||
lv_obj_t* screen = lv_screen_active();
|
||||
|
||||
@@ -902,17 +882,7 @@ void LcdDisplay::SetTheme(const std::string& theme_name) {
|
||||
if (low_battery_popup_ != nullptr) {
|
||||
lv_obj_set_style_bg_color(low_battery_popup_, current_theme.low_battery, 0);
|
||||
}
|
||||
}
|
||||
|
||||
std::string LcdDisplay::GetTheme() {
|
||||
// First try to read from settings
|
||||
Settings settings("display", false);
|
||||
std::string theme = settings.GetString("theme", "light");
|
||||
|
||||
// If theme is not set in settings, return the current theme
|
||||
if (theme.empty()) {
|
||||
return current_theme_name;
|
||||
}
|
||||
|
||||
return theme;
|
||||
// No errors occurred. Save theme to settings
|
||||
Display::SetTheme(theme_name);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,6 @@ public:
|
||||
|
||||
// Add theme switching function
|
||||
virtual void SetTheme(const std::string& theme_name) override;
|
||||
virtual std::string GetTheme() override;
|
||||
};
|
||||
|
||||
// RGB LCD显示器
|
||||
|
||||
Reference in New Issue
Block a user