knxdisplay/main/widgets/ButtonWidget.cpp
2026-01-24 10:42:15 +01:00

57 lines
1.6 KiB
C++

#include "ButtonWidget.hpp"
#include "../WidgetManager.hpp"
ButtonWidget::ButtonWidget(const WidgetConfig& config)
: Widget(config)
, label_(nullptr)
{
}
void ButtonWidget::clickCallback(lv_event_t* e) {
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);
if (config_.isToggle) {
lv_obj_add_flag(obj_, LV_OBJ_FLAG_CHECKABLE);
}
lv_obj_add_event_cb(obj_, clickCallback, LV_EVENT_CLICKED, this);
// Create label inside button
label_ = lv_label_create(obj_);
lv_label_set_text(label_, config_.text);
lv_obj_center(label_);
lv_obj_set_pos(obj_, config_.x, config_.y);
if (config_.width > 0 && config_.height > 0) {
lv_obj_set_size(obj_, config_.width, config_.height);
}
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;
}