Skip to content

Commit d4e71f8

Browse files
committed
view::refresh(element) optimization by caching the latest element bounds.
1 parent 832f182 commit d4e71f8

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

lib/include/elements/support/context.hpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,23 @@ namespace cycfi::elements
7575
context(context const& rhs, artist::rect bounds_)
7676
: basic_context(rhs.view, rhs.canvas), element(rhs.element)
7777
, parent(rhs.parent), bounds(bounds_)
78-
{}
78+
{
79+
update_bounds();
80+
}
7981

8082
context(context const& parent_, element* element_, artist::rect bounds_)
8183
: basic_context(parent_.view, parent_.canvas), element(element_)
8284
, parent(&parent_), bounds(bounds_)
83-
{}
85+
{
86+
update_bounds();
87+
}
8488

8589
context(class view& view_, class canvas& canvas_, element* element_, artist::rect bounds_)
8690
: basic_context(view_, canvas_), element(element_)
8791
, parent(nullptr), bounds(bounds_)
88-
{}
92+
{
93+
update_bounds();
94+
}
8995

9096
context(context const&) = default;
9197
context& operator=(context const&) = delete;
@@ -122,6 +128,8 @@ namespace cycfi::elements
122128

123129
private:
124130

131+
void update_bounds();
132+
125133
using listener_function =
126134
std::function<
127135
void(context const& ctx, elements::element*, string_view what)

lib/include/elements/view.hpp

+14
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ namespace cycfi { namespace elements
120120

121121
private:
122122

123+
friend class context;
124+
123125
scaled_content make_scaled_content() { return elements::scale(1.0, link(_content)); }
124126

125127
layer_composite _content;
@@ -144,6 +146,10 @@ namespace cycfi { namespace elements
144146
using tracking_map = std::map<element*, time_point>;
145147

146148
tracking_map _tracking;
149+
150+
using bounds_map = std::unordered_map<element*, rect>;
151+
152+
bounds_map _bounds_map;
147153
};
148154

149155
////////////////////////////////////////////////////////////////////////////
@@ -352,6 +358,14 @@ namespace cycfi { namespace elements
352358
{
353359
_io.post(f);
354360
}
361+
362+
// Declared in context.hpp. We define this here because view is forward declared in
363+
// the header file and thus does not have access to the actual view class.
364+
inline void context::update_bounds()
365+
{
366+
if (auto i = view._bounds_map.find(element); i != view._bounds_map.end())
367+
i->second = bounds;
368+
}
355369
}}
356370

357371
#endif

lib/src/view.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ namespace cycfi { namespace elements
181181

182182
void view::refresh(context const& ctx, int outward)
183183
{
184+
if (auto i = _bounds_map.find(ctx.element); i != _bounds_map.end())
185+
{
186+
refresh(i->second);
187+
return;
188+
}
189+
184190
context const* ctx_ptr = &ctx;
185191
while (outward > 0 && ctx_ptr)
186192
{
@@ -192,6 +198,7 @@ namespace cycfi { namespace elements
192198
auto tl = ctx.canvas.user_to_device(ctx_ptr->bounds.top_left());
193199
auto br = ctx.canvas.user_to_device(ctx_ptr->bounds.bottom_right());
194200
refresh({tl.x, tl.y, br.x, br.y});
201+
_bounds_map[ctx_ptr->element] = rect{tl, br};
195202
}
196203
}
197204

0 commit comments

Comments
 (0)