Refactor RootLayout and UIHandler for improved structure and functionality

- Updated RootLayout to manage layout initialization and deinitialization more effectively.
- Removed unnecessary dependencies and streamlined event handling for keyboard events.
- Enhanced UIHandler to utilize shared pointers for app descriptors, improving memory management.
- Added methods for showing and hiding navigation elements in RootLayout.
- Introduced textarea widget with instant response by disabling animations.
- Improved error handling and logging throughout the UI components.
This commit is contained in:
GW_MC
2026-02-01 13:03:56 +08:00
parent 237a3a96c5
commit 06e81301b2
22 changed files with 880 additions and 2198 deletions

View File

@@ -1,138 +1,126 @@
#pragma once
#include "lvgl.h"
#include "esp_err.h"
#include "esp_event.h"
#include "lvgl.h"
#include <string>
// Forward declaration
// Forward declaration to avoid circular dependency
class UIHandler;
/**
* @brief Root Layout Manager - manages the main screen layout
*
* The RootLayout class is responsible for:
* - Creating and managing the main screen structure (header, app container, nav bar)
* - Rendering app icons from the AppRegistry
* - Managing the back button
* - Updating header content
*/
class RootLayout {
public:
/**
* @brief Construct a new RootLayout object
*
* @param ui_handler Pointer to the UIHandler (for callbacks)
*/
RootLayout(UIHandler* ui_handler);
RootLayout() = default;
~RootLayout();
/**
* @brief Initialize the layout
* @brief Initialize the root layout within the given parent object
*
* Creates the main screen with header, app container, and navigation bar.
* Sets up the header, app container, and navigation bar.
*
* @param parent Parent LVGL object to attach layout to
* @param parent Parent LVGL object to contain the layout
* @return ESP_OK on success, error code otherwise
*/
esp_err_t init(lv_obj_t* parent);
esp_err_t init(lv_obj_t* parent, UIHandler* ui_handler);
/**
* @brief Deinitialize the layout
* @brief Deinitialize the root layout
*
* Cleans up all layout widgets.
* Cleans up references to layout components.
*
* @return ESP_OK on success, error code otherwise
*/
esp_err_t deinit(void);
/**
* @brief Render app icons in the navigation bar
* @brief Show the back button in the navigation bar
*/
void show_back_button(void) const;
/**
* @brief Hide the back button in the navigation bar
*/
void hide_back_button(void) const;
/**
* @brief Show the home button in the navigation bar
*/
void show_home_button(void) const;
/**
* @brief Hide the home button in the navigation bar
*/
void hide_home_button(void) const;
/**
* @brief Show navigation bar
*
* Queries the AppRegistry for all registered apps and
* renders their icons in the navigation bar. Also creates
* the back button.
*/
void show_nav_bar(void) const;
/**
* @brief Hide navigation bar
*
*/
void hide_nav_bar(void) const;
/**
* @brief Register a callback for back button presses
*
*
* @param callback
* @param user_data
* @param out_event_dsc
* @return ESP_OK on success, error code otherwise
*/
esp_err_t render_app_icons(void);
esp_err_t register_back_button_callback(lv_event_cb_t callback, void* user_data, lv_event_dsc_t** out_event_dsc) const;
/**
* @brief Update header with app name
* @brief Register a callback for home button presses
*
* @param app_name Name to display in header (nullptr for default)
* @param callback
* @param user_data
* @param out_event_dsc
* @return ESP_OK on success, error code otherwise
*/
void update_header(std::string app_name);
esp_err_t register_home_button_callback(lv_event_cb_t callback, void* user_data, lv_event_dsc_t** out_event_dsc) const;
/**
* @brief Show the back button
*/
void show_back_button(void);
/**
* @brief Hide the back button
*/
void hide_back_button(void);
/**
* @brief Get the header object
* @brief Update the header title text
*
* @return lv_obj_t* pointer to the header container
* @param title New title text
* @return ESP_OK on success, error code otherwise
*/
lv_obj_t* get_header(void) const {
return _header;
}
esp_err_t update_header(const std::string& title) const;
/**
* @brief Get the app container (where apps render)
* @brief Get the app container object, which holds the active app's UI
* Caller can add/remove app UI elements to/from this container.
* Caller must not delete this object directly or edit its layout properties.
*
* @return lv_obj_t* pointer to the app container
* @return lv_obj_t*
*/
lv_obj_t* get_app_container(void) const {
return _app_container;
}
/**
* @brief Get the navigation bar object
*
* @return lv_obj_t* pointer to the navigation bar container
*/
lv_obj_t* get_nav_bar(void) const {
return _nav_bar;
lv_obj_t* get_app_container() const {
return app_container_;
}
private:
UIHandler* _ui_handler = nullptr; ///< Reference to UIHandler for callbacks
lv_obj_t* _header = nullptr; ///< Header area (top)
lv_obj_t* _header_label = nullptr; ///< Header text label
lv_obj_t* _app_container = nullptr; ///< Container for app widgets (middle)
lv_obj_t* _nav_bar = nullptr; ///< Navigation bar (bottom)
lv_obj_t* _back_button = nullptr; ///< Back button in navigation bar
/**
* @brief Create the layout structure
*
* Sets up header, app container, and navigation bar with
* appropriate dimensions and positioning.
*
* @param parent Parent object to attach layout to
* @return ESP_OK on success, error code otherwise
*/
esp_err_t create_layout(lv_obj_t* parent);
// Event handler for keyboard show/hide events
void on_keyboard_event_(void* handler_args, esp_event_base_t base, int32_t id, void* event_data);
/**
* @brief Handle app icon click event
*
* Static callback for LVGL event handling.
*
* @param event LVGL event object
*/
static void on_app_icon_clicked(lv_event_t* event);
// layout objects
// header
lv_obj_t* header_obj_ = nullptr; ///< Header area object
lv_obj_t* header_label_ = nullptr; ///< Header title label
// app container
lv_obj_t* app_container_ = nullptr; ///< App container object
// navigation bar
lv_obj_t* nav_bar_obj_ = nullptr; ///< Navigation bar object
lv_obj_t* back_button_ = nullptr; ///< Back button object
lv_obj_t* home_button_ = nullptr; ///< Home button object
/**
* @brief Handle back button click event
*
* Static callback for LVGL event handling.
*
* @param event LVGL event object
*/
static void on_back_button_clicked(lv_event_t* event);
esp_event_handler_instance_t keyboard_event_handler_instance_ = nullptr; ///< Event handler instance for keyboard events
};