knxdisplay/main/widgets/ButtonWidget.cpp
2026-01-24 19:22:12 +01:00

65 lines
1.9 KiB
C++

#include "ButtonWidget.hpp"
#include "../WidgetManager.hpp"
#include "esp_log.h"
static const char* TAG = "ButtonWidget";
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_, clickCallback);
}
label_ = nullptr;
}
void ButtonWidget::clickCallback(lv_event_t* e) {
ESP_LOGI(TAG, "clickCallback called");
ButtonWidget* widget = static_cast<ButtonWidget*>(lv_event_get_user_data(e));
if (!widget) return;
lv_obj_t* target = static_cast<lv_obj_t*>(lv_event_get_target(e));
WidgetManager::instance().handleButtonAction(widget->getConfig(), target);
}
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_, clickCallback, LV_EVENT_CLICKED, this);
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;
}