Files
ink-board/main/ui/ui_handler.h
GW_MC 06e81301b2 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.
2026-02-01 13:03:56 +08:00

119 lines
3.3 KiB
C++

#pragma once
#include "esp_err.h"
#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
*
* The UIHandler manages:
* - Creation and destruction of UI apps
* - Switching between apps
* - Main screen layout (header, app container, navigation bar)
* - System event routing to active app
* - Displaying special screens (shutdown, etc.)
*/
class UIHandler {
public:
UIHandler() = default;
~UIHandler();
/**
* @brief Initialize the UI system with default layout
*
* Creates the main screen with:
* - Header area (top)
* - 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);
/**
* @brief Deinitialize the UI system
*
* Cleans up the current app and destroys the main screen.
*
* @return ESP_OK on success, error code otherwise
*/
esp_err_t deinit(void);
/**
* @brief Switch to a new app by its descriptor
*
* Deinitializes the current app (if any), initializes the new app,
* and updates the display. Holds shared ownership of the descriptor
* to ensure the app remains valid while active.
*
* @param app_descriptor Shared pointer to the app descriptor
* @return ESP_OK on success, error code otherwise
*/
esp_err_t switch_app(std::shared_ptr<AppDescriptor> app_descriptor);
/**
* @brief Display shutdown screen
*
* Shows a shutdown screen with a message. Typically called
* before the system enters deep sleep or powers off.
*
* @param message Optional message to display (e.g., "Shutting down...")
* @return ESP_OK on success, error code otherwise
*/
esp_err_t show_shutdown_screen(const std::string& message = "");
/**
* @brief Get the main screen object
*
* @return lv_obj_t* pointer to the main screen
*/
lv_obj_t* get_main_screen(void) const {
return main_screen_;
}
esp_err_t update_header_title(const std::string& title);
/**
* @brief Return to main screen (deinit app and show app icons)
*
* Deinitializes the active app and displays the app icons
* in the navigation bar, returning to the home screen.
*
* @return ESP_OK on success, error code otherwise
*/
esp_err_t return_to_main_screen(void);
private:
// 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)
};