Files
ink-board/main/ui/ui_handler.h

123 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>
#include <vector>
/**
* @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. The descriptor must remain valid in the
* AppRegistry for the lifetime of the app.
*
* @param app_descriptor Pointer to the app descriptor (managed by AppRegistry)
* @return ESP_OK on success, error code otherwise
*/
esp_err_t switch_app(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
AppDescriptor* active_descriptor_ = nullptr; ///< Currently active app descriptor (managed by AppRegistry)
// Track allocated user data for cleanup
std::vector<void*> app_click_user_data_;
};