Support on_text_changed callback in ui::edit
This commit is contained in:
parent
e4a1ede2d2
commit
9a56228231
2 changed files with 20 additions and 0 deletions
|
|
@ -53,6 +53,7 @@ namespace psemek::ui
|
|||
virtual void on_text_entered(callback_type callback, bool signal = true);
|
||||
|
||||
virtual void on_start_input(std::function<void()> callback);
|
||||
virtual void on_text_changed(callback_type callback);
|
||||
|
||||
bool on_event(mouse_move const & e) override;
|
||||
bool on_event(mouse_click const & e) override;
|
||||
|
|
@ -100,6 +101,7 @@ namespace psemek::ui
|
|||
|
||||
callback_type on_text_entered_;
|
||||
std::function<void()> on_start_input_;
|
||||
callback_type on_text_changed_;
|
||||
|
||||
struct cached_state
|
||||
{
|
||||
|
|
@ -114,6 +116,7 @@ namespace psemek::ui
|
|||
void reset_caret();
|
||||
void post_text_entered() const;
|
||||
void post_start_input() const;
|
||||
void post_text_changed() const;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ namespace psemek::ui
|
|||
text_ = std::move(text);
|
||||
caret_ = std::min(caret_, text_.size());
|
||||
on_state_changed();
|
||||
post_text_changed();
|
||||
if (signal)
|
||||
post_text_entered();
|
||||
|
||||
|
|
@ -93,6 +94,11 @@ namespace psemek::ui
|
|||
on_start_input_ = std::move(callback);
|
||||
}
|
||||
|
||||
void edit::on_text_changed(callback_type callback)
|
||||
{
|
||||
on_text_changed_ = std::move(callback);
|
||||
}
|
||||
|
||||
bool edit::on_event(mouse_move const & e)
|
||||
{
|
||||
auto m = geom::cast<float>(e.position);
|
||||
|
|
@ -438,4 +444,15 @@ namespace psemek::ui
|
|||
post(on_start_input_);
|
||||
}
|
||||
|
||||
void edit::post_text_changed() const
|
||||
{
|
||||
if (on_text_changed_)
|
||||
{
|
||||
post([weak_self = weak_from_this(), cb = on_text_changed_]{
|
||||
if (auto self = weak_self.lock())
|
||||
cb(static_cast<edit const *>(self.get())->text_);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue