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,12 +1,12 @@
#pragma once
#include "ui_app.h"
#include "app_registry.h"
#include "root_layout.h"
#include "esp_err.h"
// Forward declaration
class RootLayout;
#include "ui/apps/app.h"
#include "ui/events.h"
#include "ui/root_layout.h"
#include "ui/interaction_handler.h"
#include "lvgl.h"
#include <memory>
/**
* @brief UI Handler - manages app lifecycle and rendering
@@ -20,6 +20,10 @@ class RootLayout;
*/
class UIHandler {
public:
UIHandler() = default;
~UIHandler();
/**
* @brief Initialize the UI system with default layout
*
@@ -28,6 +32,10 @@ public:
* - App container (middle)
* - Navigation bar (bottom)
*
* And display the main screen.
*
* And initializes the InteractionHandler, callbacks, etc.
*
* @return ESP_OK on success, error code otherwise
*/
esp_err_t init(void);
@@ -42,45 +50,16 @@ public:
esp_err_t deinit(void);
/**
* @brief Switch to a new app
* @brief Switch to a new app by its descriptor
*
* Deinitializes the current app (if any), initializes the new app,
* and updates the display.
* and updates the display. Holds shared ownership of the descriptor
* to ensure the app remains valid while active.
*
* @param app Pointer to the new app to switch to
* @param app_descriptor Shared pointer to the app descriptor
* @return ESP_OK on success, error code otherwise
*/
esp_err_t switch_app(UIApp* app);
/**
* @brief Switch to an app by its descriptor
*
* Convenience method that extracts the UIApp from the descriptor
* and calls switch_app().
*
* @param app_descriptor Pointer to the app descriptor
* @return ESP_OK on success, error code otherwise
*/
esp_err_t switch_app(AppDescriptor* app_descriptor);
/**
* @brief Get the currently active app
*
* @return Pointer to the active UIApp, or nullptr if none
*/
UIApp* get_active_app(void) const {
return _active_app;
}
/**
* @brief Route a system event to the active app
*
* If an app is active, this forwards the event to it.
*
* @param event_type Type/ID of the event
* @param event_data Optional event data payload
*/
void route_event(uint32_t event_type, void* event_data = nullptr);
esp_err_t switch_app(std::shared_ptr<AppDescriptor> app_descriptor);
/**
* @brief Display shutdown screen
@@ -91,7 +70,7 @@ public:
* @param message Optional message to display (e.g., "Shutting down...")
* @return ESP_OK on success, error code otherwise
*/
esp_err_t show_shutdown_screen(std::string message = "");
esp_err_t show_shutdown_screen(const std::string& message = "");
/**
* @brief Get the main screen object
@@ -99,35 +78,10 @@ public:
* @return lv_obj_t* pointer to the main screen
*/
lv_obj_t* get_main_screen(void) const {
return _main_screen;
return main_screen_;
}
/**
* @brief Get the app container (where apps render)
*
* @return lv_obj_t* pointer to the app container
*/
lv_obj_t* get_app_container(void) const {
return _root_layout ? _root_layout->get_app_container() : nullptr;
}
/**
* @brief Get the header object
*
* @return lv_obj_t* pointer to the header container
*/
lv_obj_t* get_header(void) const {
return _root_layout ? _root_layout->get_header() : nullptr;
}
/**
* @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 _root_layout ? _root_layout->get_nav_bar() : nullptr;
}
esp_err_t update_header_title(const std::string& title);
/**
* @brief Return to main screen (deinit app and show app icons)
@@ -140,8 +94,25 @@ public:
esp_err_t return_to_main_screen(void);
private:
lv_obj_t* _main_screen = nullptr; ///< Root screen
RootLayout* _root_layout = nullptr; ///< Root layout manager
UIApp* _active_app = nullptr; ///< Currently active app
UIApp* _shutdown_app = nullptr; ///< Cached shutdown app
// Handle back button press, route to active app if any
void on_back_button_pressed_(void);
// Helper to create the main screen layout
esp_err_t create_main_screen_(lv_obj_t* parent);
// Helper to destroy the main screen layout
esp_err_t destroy_main_screen_(void);
// delete copy constructor and assignment operator
// to prevent copying of the UIHandler instance
UIHandler(const UIHandler&) = delete;
UIHandler& operator=(const UIHandler&) = delete;
InteractionHandler interaction_handler_; ///< Manages user interactions
lv_obj_t* main_screen_ = nullptr; ///< Root screen
RootLayout root_layout_; ///< Main screen layout manager
std::shared_ptr<AppDescriptor> active_descriptor_ = nullptr; ///< Currently active app descriptor (shared ownership)
};