summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPeter Nelson <peter1138@openttd.org>2021-05-07 12:36:08 +0100
committerPeterN <peter@fuzzle.org>2021-05-08 09:52:54 +0100
commit52b16237ad863ce30746ac7efc46a9ece7e3bdd2 (patch)
tree4cd5c83fb53da9c89219c7d0eba636d04b5d3634 /src
parent8321ef0061fa898cfb215e57c67aebd7411475e1 (diff)
downloadopenttd-52b16237ad863ce30746ac7efc46a9ece7e3bdd2.tar.xz
Codechange: Don't update window contents if scrollbar position has not moved.
Diffstat (limited to 'src')
-rw-r--r--src/widget.cpp17
-rw-r--r--src/widget_type.h12
-rw-r--r--src/window.cpp9
3 files changed, 23 insertions, 15 deletions
diff --git a/src/widget.cpp b/src/widget.cpp
index 5c1c0ae8f..93a5afe95 100644
--- a/src/widget.cpp
+++ b/src/widget.cpp
@@ -108,6 +108,7 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in
int pos;
int button_size;
bool rtl = false;
+ bool changed = false;
if (sb->type == NWID_HSCROLLBAR) {
pos = x;
@@ -122,7 +123,7 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in
SetBit(sb->disp_flags, NDB_SCROLLBAR_UP);
if (_scroller_click_timeout <= 1) {
_scroller_click_timeout = 3;
- sb->UpdatePosition(rtl ? 1 : -1);
+ changed = sb->UpdatePosition(rtl ? 1 : -1);
}
w->mouse_capture_widget = sb->index;
} else if (pos >= ma - button_size) {
@@ -131,16 +132,16 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in
if (_scroller_click_timeout <= 1) {
_scroller_click_timeout = 3;
- sb->UpdatePosition(rtl ? -1 : 1);
+ changed = sb->UpdatePosition(rtl ? -1 : 1);
}
w->mouse_capture_widget = sb->index;
} else {
Point pt = HandleScrollbarHittest(sb, mi, ma, sb->type == NWID_HSCROLLBAR);
if (pos < pt.x) {
- sb->UpdatePosition(rtl ? 1 : -1, Scrollbar::SS_BIG);
+ changed = sb->UpdatePosition(rtl ? 1 : -1, Scrollbar::SS_BIG);
} else if (pos > pt.y) {
- sb->UpdatePosition(rtl ? -1 : 1, Scrollbar::SS_BIG);
+ changed = sb->UpdatePosition(rtl ? -1 : 1, Scrollbar::SS_BIG);
} else {
_scrollbar_start_pos = pt.x - mi - button_size;
_scrollbar_size = ma - mi - button_size * 2;
@@ -149,7 +150,13 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in
}
}
- w->SetDirty();
+ if (changed) {
+ /* Position changed so refresh the window */
+ w->SetDirty();
+ } else {
+ /* No change so only refresh this scrollbar */
+ sb->SetDirty(w);
+ }
}
/**
diff --git a/src/widget_type.h b/src/widget_type.h
index 200711356..c50a263d6 100644
--- a/src/widget_type.h
+++ b/src/widget_type.h
@@ -737,12 +737,15 @@ public:
/**
* Sets the position of the first visible element
* @param position the position of the element
+ * @return true iff the position has changed
*/
- void SetPosition(int position)
+ bool SetPosition(int position)
{
assert(position >= 0);
assert(this->count <= this->cap ? (position == 0) : (position + this->cap <= this->count));
+ uint16 old_pos = this->pos;
this->pos = position;
+ return this->pos != old_pos;
}
/**
@@ -750,16 +753,17 @@ public:
* If the position would be too low or high it will be clamped appropriately
* @param difference the amount of change requested
* @param unit The stepping unit of \a difference
+ * @return true iff the position has changed
*/
- void UpdatePosition(int difference, ScrollbarStepping unit = SS_SMALL)
+ bool UpdatePosition(int difference, ScrollbarStepping unit = SS_SMALL)
{
- if (difference == 0) return;
+ if (difference == 0) return false;
switch (unit) {
case SS_SMALL: difference *= this->stepsize; break;
case SS_BIG: difference *= this->cap; break;
default: break;
}
- this->SetPosition(Clamp(this->pos + difference, 0, std::max(this->count - this->cap, 0)));
+ return this->SetPosition(Clamp(this->pos + difference, 0, std::max(this->count - this->cap, 0)));
}
/**
diff --git a/src/window.cpp b/src/window.cpp
index 20c7ffbdd..d69a34144 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -849,8 +849,7 @@ static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
if (nwid->type == NWID_VSCROLLBAR) {
NWidgetScrollbar *sb = static_cast<NWidgetScrollbar *>(nwid);
if (sb->GetCount() > sb->GetCapacity()) {
- sb->UpdatePosition(wheel);
- w->SetDirty();
+ if (sb->UpdatePosition(wheel)) w->SetDirty();
}
return;
}
@@ -858,8 +857,7 @@ static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
/* Scroll the widget attached to the scrollbar. */
Scrollbar *sb = (nwid->scrollbar_index >= 0 ? w->GetScrollbar(nwid->scrollbar_index) : nullptr);
if (sb != nullptr && sb->GetCount() > sb->GetCapacity()) {
- sb->UpdatePosition(wheel);
- w->SetDirty();
+ if (sb->UpdatePosition(wheel)) w->SetDirty();
}
}
@@ -2413,8 +2411,7 @@ static void HandleScrollbarScrolling(Window *w)
if (sb->disp_flags & ND_SCROLLBAR_BTN) {
if (_scroller_click_timeout == 1) {
_scroller_click_timeout = 3;
- sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1);
- w->SetDirty();
+ if (sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1)) w->SetDirty();
}
return;
}