Skip to content

Commit ca42d9c

Browse files
authored
Fix toggle redraw in panels
1 parent e69f2fe commit ca42d9c

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

include/Widget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,7 @@ class Toggle : public Widget {
12081208
const double animationSpeed = 0.1;
12091209
bool keepColor = false;
12101210
color_t baseColor = EGERGB(33, 150, 243);
1211+
bool m_animatingDirty = false;
12111212

12121213

12131214
std::function<void(bool)> onToggle;

src/Widget.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,6 +2873,11 @@ Toggle::Toggle(double cx, double cy, double w, double h)
28732873
void Toggle::setChecked(bool c) {
28742874
checked = c;
28752875
knobTarget = c ? 1.0 : 0.0;
2876+
if (this->parent != nullptr) {
2877+
if (Panel* p = dynamic_cast<Panel*>(this->parent)) {
2878+
p->setDirty();
2879+
}
2880+
}
28762881
}
28772882

28782883
void Toggle::toggle() {
@@ -2920,7 +2925,15 @@ bool Toggle::handleEvent(const mouse_msg& msg) {
29202925
int dx = msg.x - cx;
29212926
int dy = msg.y - cy;
29222927

2928+
bool hoverChanged = false;
2929+
bool prevHovered = hovered;
29232930
hovered = (std::abs(dx) <= w / 2 && std::abs(dy) <= h / 2);
2931+
hoverChanged = (prevHovered != hovered);
2932+
if (hoverChanged && this->parent != nullptr) {
2933+
if (Panel* p = dynamic_cast<Panel*>(this->parent)) {
2934+
p->setDirty();
2935+
}
2936+
}
29242937

29252938
if (disabled) return false;
29262939

@@ -2977,6 +2990,24 @@ void Toggle::draw(PIMAGE dst, double x, double y) {
29772990
else
29782991
knobOffset = knobTarget;
29792992

2993+
bool isAnimating = std::abs(knobOffset - knobTarget) > 1e-3;
2994+
if (this->parent != nullptr) {
2995+
if (Panel* p = dynamic_cast<Panel*>(this->parent)) {
2996+
if (isAnimating) {
2997+
if (!m_animatingDirty) {
2998+
p->setAlwaysDirty(true);
2999+
this->setDrawing(true);
3000+
m_animatingDirty = true;
3001+
}
3002+
p->setDirty();
3003+
} else if (m_animatingDirty) {
3004+
p->setAlwaysDirty(false);
3005+
this->setDrawing(false);
3006+
m_animatingDirty = false;
3007+
}
3008+
}
3009+
}
3010+
29803011
if (BackendFlag) {
29813012
return;
29823013
}

0 commit comments

Comments
 (0)