66 lines
1.8 KiB
C++
66 lines
1.8 KiB
C++
#include "ButtonWidget.hpp"
|
|
#include "../WidgetManager.hpp"
|
|
#include "esp_log.h"
|
|
|
|
static const char* TAG = "ButtonWidget";
|
|
|
|
// Free function instead of static member
|
|
static void button_event_cb(lv_event_t* e) {
|
|
ESP_LOGI(TAG, "button_event_cb called");
|
|
}
|
|
|
|
ButtonWidget::ButtonWidget(const WidgetConfig& config)
|
|
: Widget(config)
|
|
, label_(nullptr)
|
|
{
|
|
}
|
|
|
|
ButtonWidget::~ButtonWidget() {
|
|
// Remove event callback BEFORE the base class destructor deletes the object
|
|
if (obj_) {
|
|
lv_obj_remove_event_cb(obj_, button_event_cb);
|
|
}
|
|
label_ = nullptr;
|
|
}
|
|
|
|
void ButtonWidget::clickCallback(lv_event_t* e) {
|
|
// Not used currently
|
|
(void)e;
|
|
}
|
|
|
|
lv_obj_t* ButtonWidget::create(lv_obj_t* parent) {
|
|
obj_ = lv_btn_create(parent);
|
|
lv_obj_set_pos(obj_, config_.x, config_.y);
|
|
lv_obj_set_size(obj_, config_.width > 0 ? config_.width : 100,
|
|
config_.height > 0 ? config_.height : 50);
|
|
|
|
// Test: free function callback
|
|
lv_obj_add_event_cb(obj_, button_event_cb, LV_EVENT_CLICKED, NULL);
|
|
|
|
label_ = lv_label_create(obj_);
|
|
lv_label_set_text(label_, config_.text);
|
|
lv_obj_center(label_);
|
|
|
|
ESP_LOGI(TAG, "Created button '%s' at %d,%d", config_.text, config_.x, config_.y);
|
|
return obj_;
|
|
}
|
|
|
|
void ButtonWidget::applyStyle() {
|
|
if (obj_ == nullptr) return;
|
|
|
|
// Apply common style to button
|
|
applyCommonStyle();
|
|
|
|
// Apply text style to label
|
|
if (label_ != nullptr) {
|
|
lv_obj_set_style_text_color(label_, lv_color_make(
|
|
config_.textColor.r, config_.textColor.g, config_.textColor.b), 0);
|
|
lv_obj_set_style_text_font(label_, getFontBySize(config_.fontSize), 0);
|
|
}
|
|
}
|
|
|
|
bool ButtonWidget::isChecked() const {
|
|
if (obj_ == nullptr) return false;
|
|
return (lv_obj_get_state(obj_) & LV_STATE_CHECKED) != 0;
|
|
}
|