Skip to content

Commit b0d4bce

Browse files
committed
- Avoid view.refresh(ctx.bounds). It does not respect scaling and other canvas transformations. It's just view.refresh(ctx).
- Add a new view.refresh(ctx, bounds) that DO respect scaling and other canvas transformations. - Deal linux refesh(rect). GTK uses int coordinates. Make sure area is not empty when converting from float to int.
1 parent 1f66ae1 commit b0d4bce

File tree

9 files changed

+37
-25
lines changed

9 files changed

+37
-25
lines changed

examples/custom_control/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void my_custom_control::keep_tracking(context const& ctx, tracker_info& track_in
164164
clamp(_radius, 50, 150);
165165
if (on_change)
166166
on_change(_radius);
167-
ctx.view.refresh(ctx.bounds);
167+
ctx.view.refresh(ctx);
168168
}
169169

170170
///////////////////////////////////////////////////////////////////////////////
@@ -177,7 +177,7 @@ bool my_custom_control::cursor(context const& ctx, point p, cursor_tracking stat
177177
case cursor_tracking::hovering:
178178
case cursor_tracking::entering:
179179
_mouse_over = true;
180-
ctx.view.refresh(ctx.bounds);
180+
ctx.view.refresh(ctx);
181181
break;
182182
case cursor_tracking::leaving:
183183
_mouse_over = false;

lib/host/gtk3/base_view.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,13 @@ namespace cycfi { namespace elements
751751
GtkAllocation alloc;
752752
gtk_widget_get_allocation(_view->_widget, &alloc);
753753

754+
// Note: GTK uses int coordinates. Make sure area is not empty when converting
755+
// from float to int.
754756
gtk_widget_queue_draw_area(_view->_widget,
755-
area.left + alloc.x,
756-
area.top + alloc.y,
757-
area.width(),
758-
area.height()
757+
std::floor(area.left + alloc.x),
758+
std::floor(area.top + alloc.y),
759+
std::max<float>(area.width(), 1),
760+
std::max<float>(area.height(), 1)
759761
);
760762
}
761763

lib/include/elements/element/gallery/thumbwheel.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace cycfi { namespace elements
4141

4242
float quantize() const { return _quantize; }
4343
void make_aligner(context const& ctx);
44-
void do_align(view& view_, rect const& bounds, double val);
44+
void do_align(view& view_, double val);
4545

4646
private:
4747

lib/include/elements/view.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ namespace cycfi { namespace elements
5555

5656
void refresh() override;
5757
void refresh(rect area) override;
58+
void refresh(context const& ctx, rect area);
5859
void refresh(element& element, int outward = 0);
5960
void refresh(context const& ctx, int outward = 0);
6061
rect dirty() const;

lib/src/element/drag_and_drop.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace cycfi { namespace elements
5252
if (new_is_tracking != _is_tracking)
5353
{
5454
_is_tracking = new_is_tracking;
55-
ctx.view.refresh(ctx.bounds);
55+
ctx.view.refresh(ctx);
5656
}
5757
}
5858

@@ -84,7 +84,7 @@ namespace cycfi { namespace elements
8484
{
8585
base_type::drop(ctx, info);
8686
bool r = on_drop(info);
87-
ctx.view.refresh(ctx.bounds);
87+
ctx.view.refresh(ctx);
8888
return r;
8989
}
9090

@@ -143,7 +143,7 @@ namespace cycfi { namespace elements
143143
static constexpr auto offset = 20;
144144
rect r = {info.where.x-offset, info.where.y-offset, info.where.x+offset, info.where.y+offset};
145145
scrollable::find(ctx).scroll_into_view(r);
146-
ctx.view.refresh(ctx.bounds);
146+
ctx.view.refresh(ctx);
147147
}
148148
}
149149

@@ -153,7 +153,7 @@ namespace cycfi { namespace elements
153153
if (_insertion_pos >= 0)
154154
{
155155
bool r = on_drop(info, _insertion_pos);
156-
ctx.view.refresh(ctx.bounds);
156+
ctx.view.refresh(ctx);
157157
_insertion_pos = -1;
158158
return r;
159159
}

lib/src/element/gallery/thumbwheel.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,32 @@ namespace cycfi { namespace elements
2424
if (_quantize > 0)
2525
{
2626
_aligner =
27-
[this, &view = ctx.view, bounds = ctx.bounds](double val)
27+
[this, &view = ctx.view](double val)
2828
{
2929
view.post(
30-
[this, &view, val, bounds]()
30+
[this, &view, val]()
3131
{
32-
do_align(view, bounds, val);
32+
do_align(view, val);
3333
}
3434
);
3535
};
3636
}
3737
}
3838

39-
void basic_thumbwheel_element::do_align(view& view_, rect const& bounds, double val)
39+
void basic_thumbwheel_element::do_align(view& view_, double val)
4040
{
4141
auto curr = align();
4242
auto diff = val - curr;
4343
if (std::abs(diff) < (_quantize / 10))
4444
{
4545
align(val);
4646
if (diff > 0)
47-
view_.refresh(bounds);
47+
view_.refresh(*dynamic_cast<element*>(this));
4848
}
4949
else
5050
{
5151
align(curr + diff/10);
52-
view_.refresh(bounds);
52+
view_.refresh(*dynamic_cast<element*>(this));
5353
}
5454
}
5555

lib/src/element/selection.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ namespace cycfi::elements
279279
}
280280
if (r && _select_start >= 0)
281281
{
282-
ctx.view.refresh(ctx.bounds);
282+
ctx.view.refresh(ctx);
283283
on_select(_select_start, _select_end);
284284
}
285285
}
@@ -301,7 +301,7 @@ namespace cycfi::elements
301301
if (auto c = find_subject<composite_base*>(this))
302302
{
303303
detail::select_all(*c);
304-
ctx.view.refresh(ctx.bounds);
304+
ctx.view.refresh(ctx);
305305
return true;
306306
}
307307
}
@@ -326,7 +326,7 @@ namespace cycfi::elements
326326
[&](context const& cctx)
327327
{
328328
scrollable::find(ctx).scroll_into_view(c->bounds_of(cctx, _select_end));
329-
ctx.view.refresh(ctx.bounds);
329+
ctx.view.refresh(ctx);
330330
}
331331
);
332332
r = true;
@@ -354,7 +354,7 @@ namespace cycfi::elements
354354
[&](context const& cctx)
355355
{
356356
scrollable::find(ctx).scroll_into_view(c->bounds_of(cctx, _select_end));
357-
ctx.view.refresh(ctx.bounds);
357+
ctx.view.refresh(ctx);
358358
}
359359
);
360360
r = true;

lib/src/element/text.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ namespace cycfi { namespace elements
5757
if (_current_size.x != new_x || _current_size.y != new_y)
5858
{
5959
if (_current_size.x != -1 && _current_size.y != -1)
60-
ctx.view.refresh(union_(ctx.bounds, rect(ctx.bounds.top_left(), extent{_current_size})));
60+
ctx.view.refresh(ctx, union_(ctx.bounds, rect(ctx.bounds.top_left(), extent{_current_size})));
6161
else
62-
ctx.view.refresh(ctx.bounds);
62+
ctx.view.refresh(ctx);
6363
}
6464

6565
_current_size.x = new_x;
@@ -564,7 +564,7 @@ namespace cycfi { namespace elements
564564
auto size = current_size();
565565
bounds.width(size.x);
566566
bounds.height(size.y);
567-
ctx.view.refresh(bounds);
567+
ctx.view.refresh(ctx, bounds);
568568
}
569569

570570
if (handled)
@@ -631,9 +631,11 @@ namespace cycfi { namespace elements
631631

632632
if (_is_focus && has_caret && !_caret_started)
633633
{
634+
// We convert the caret bounds to device coordinates and expand it by 2 pixels
635+
// on all sides for good measure.
634636
auto tl = ctx.canvas.user_to_device(caret_bounds.top_left());
635637
auto br = ctx.canvas.user_to_device(caret_bounds.bottom_right());
636-
caret_bounds = {tl.x, tl.y, br.x, br.y};
638+
caret_bounds = {tl.x-2, tl.y-2, br.x+2, br.y+2};
637639

638640
_caret_started = true;
639641
ctx.view.post(500ms,

lib/src/view.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ namespace cycfi { namespace elements
160160
);
161161
}
162162

163+
void view::refresh(context const& ctx, rect area)
164+
{
165+
auto tl = ctx.canvas.user_to_device(area.top_left());
166+
auto br = ctx.canvas.user_to_device(area.bottom_right());
167+
refresh({tl.x, tl.y, br.x, br.y});
168+
}
169+
163170
void view::refresh(element& element, int outward)
164171
{
165172
if (_current_bounds.is_empty())

0 commit comments

Comments
 (0)