Skip to content

Commit

Permalink
Fixed caret drawing getting a stale pointer to this
Browse files Browse the repository at this point in the history
  • Loading branch information
djowel committed May 10, 2023
1 parent acda9b4 commit 1e7b9fa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/external/asio
Submodule asio updated 1577 files
4 changes: 4 additions & 0 deletions lib/include/elements/element/text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,17 @@ namespace cycfi { namespace elements

state_saver_f capture_state();

using this_handle = std::shared_ptr<basic_text_box*>;
using this_weak_handle = std::weak_ptr<basic_text_box*>;

int _select_start;
int _select_end;
float _current_x;
state_saver_f _typing_state;
bool _is_focus : 1;
bool _show_caret : 1;
bool _caret_started : 1;
this_handle _this_handle;
};

////////////////////////////////////////////////////////////////////////////
Expand Down
20 changes: 15 additions & 5 deletions lib/src/element/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ namespace cycfi { namespace elements
{}

basic_text_box::~basic_text_box()
{}
{
_this_handle.reset();
}

void basic_text_box::draw(context const& ctx)
{
Expand Down Expand Up @@ -539,6 +541,10 @@ namespace cycfi { namespace elements

void basic_text_box::draw_caret(context const& ctx)
{
// Make sure _this_handle is initialized to this
if (!_this_handle)
_this_handle = std::make_shared<basic_text_box*>(this);

if (_select_start == -1)
return;

Expand Down Expand Up @@ -598,12 +604,16 @@ namespace cycfi { namespace elements
caret_bounds = { tl.x, tl.y, br.x, br.y };

_caret_started = true;
this_weak_handle wp = _this_handle;
ctx.view.post(500ms,
[this, &_view = ctx.view, caret_bounds]()
[wp, &_view = ctx.view, caret_bounds]()
{
_show_caret = !_show_caret;
_view.refresh(caret_bounds);
_caret_started = false;
if (auto p = wp.lock())
{
(*p)->_show_caret = !(*p)->_show_caret;
_view.refresh(caret_bounds);
(*p)->_caret_started = false;
}
}
);
}
Expand Down

0 comments on commit 1e7b9fa

Please # to comment.